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_STATE(val) \
197 if (csq->state == NULL) { \
198 csq->state = calloc(1, sizeof(client_state_t)); \
200 *(csq->state) = (val); \
203 #define SET_CSQ_LAYER(val) \
205 if (csq->layer == NULL) { \
206 csq->layer = calloc(1, sizeof(stack_layer_t)); \
208 *(csq->layer) = (val); \
211 void _apply_window_type(xcb_window_t win, rule_consequence_t *csq)
213 xcb_ewmh_get_atoms_reply_t win_type;
214 if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
215 for (unsigned int i = 0; i < win_type.atoms_len; i++) {
216 xcb_atom_t a = win_type.atoms[i];
217 if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR ||
218 a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
220 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
221 SET_CSQ_STATE(STATE_FLOATING);
223 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK ||
224 a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP ||
225 a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
227 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP) {
232 xcb_ewmh_get_atoms_reply_wipe(&win_type);
236 void _apply_window_state(xcb_window_t win, rule_consequence_t *csq)
238 xcb_ewmh_get_atoms_reply_t win_state;
239 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
240 for (unsigned int i = 0; i < win_state.atoms_len; i++) {
241 xcb_atom_t a = win_state.atoms[i];
242 if (a == ewmh->_NET_WM_STATE_FULLSCREEN) {
243 SET_CSQ_STATE(STATE_FULLSCREEN);
244 } else if (a == ewmh->_NET_WM_STATE_BELOW) {
245 SET_CSQ_LAYER(LAYER_BELOW);
246 } else if (a == ewmh->_NET_WM_STATE_ABOVE) {
247 SET_CSQ_LAYER(LAYER_ABOVE);
248 } else if (a == ewmh->_NET_WM_STATE_STICKY) {
252 xcb_ewmh_get_atoms_reply_wipe(&win_state);
256 void _apply_transient(xcb_window_t win, rule_consequence_t *csq)
258 xcb_window_t transient_for = XCB_NONE;
259 xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
260 if (transient_for != XCB_NONE) {
261 SET_CSQ_STATE(STATE_FLOATING);
265 void _apply_hints(xcb_window_t win, rule_consequence_t *csq)
267 xcb_size_hints_t size_hints;
268 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
269 if ((size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)) &&
270 size_hints.min_width == size_hints.max_width && size_hints.min_height == size_hints.max_height) {
271 SET_CSQ_STATE(STATE_FLOATING);
276 void _apply_class(xcb_window_t win, rule_consequence_t *csq)
278 xcb_icccm_get_wm_class_reply_t reply;
279 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
280 snprintf(csq->class_name, sizeof(csq->class_name), "%s", reply.class_name);
281 snprintf(csq->instance_name, sizeof(csq->instance_name), "%s", reply.instance_name);
282 xcb_icccm_get_wm_class_reply_wipe(&reply);
286 void _apply_name(xcb_window_t win, rule_consequence_t *csq)
288 xcb_icccm_get_text_property_reply_t reply;
289 if (xcb_icccm_get_wm_name_reply(dpy, xcb_icccm_get_wm_name(dpy, win), &reply, NULL) == 1) {
290 snprintf(csq->name, sizeof(csq->name), "%s", reply.name);
291 xcb_icccm_get_text_property_reply_wipe(&reply);
295 void parse_keys_values(char *buf, rule_consequence_t *csq)
297 char *key = strtok(buf, CSQ_BLK);
298 char *value = strtok(NULL, CSQ_BLK);
299 while (key != NULL && value != NULL) {
300 parse_key_value(key, value, csq);
301 key = strtok(NULL, CSQ_BLK);
302 value = strtok(NULL, CSQ_BLK);
306 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
308 _apply_window_type(win, csq);
309 _apply_window_state(win, csq);
310 _apply_transient(win, csq);
311 _apply_hints(win, csq);
312 _apply_class(win, csq);
313 _apply_name(win, csq);
315 rule_t *rule = rule_head;
316 while (rule != NULL) {
317 rule_t *next = rule->next;
318 if ((streq(rule->class_name, MATCH_ANY) || streq(rule->class_name, csq->class_name)) &&
319 (streq(rule->instance_name, MATCH_ANY) || streq(rule->instance_name, csq->instance_name)) &&
320 (streq(rule->name, MATCH_ANY) || streq(rule->name, csq->name))) {
322 snprintf(effect, sizeof(effect), "%s", rule->effect);
323 parse_keys_values(effect, csq);
324 if (rule->one_shot) {
333 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
335 if (external_rules_command[0] == '\0') {
339 if (pipe(fds) == -1) {
345 close(xcb_get_file_descriptor(dpy));
351 print_rule_consequence(&csq_buf, csq);
352 snprintf(wid, sizeof(wid), "%i", win);
354 execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, csq_buf, NULL);
356 err("Couldn't spawn rule command.\n");
357 } else if (pid > 0) {
359 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
360 add_pending_rule(pr);
365 void parse_rule_consequence(int fd, rule_consequence_t *csq)
372 while ((nb = read(fd, data, sizeof(data))) > 0) {
373 int end = MIN(nb, (int) sizeof(data) - 1);
375 parse_keys_values(data, csq);
379 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
382 if (streq("monitor", key)) {
383 snprintf(csq->monitor_desc, sizeof(csq->monitor_desc), "%s", value);
384 } else if (streq("desktop", key)) {
385 snprintf(csq->desktop_desc, sizeof(csq->desktop_desc), "%s", value);
386 } else if (streq("node", key)) {
387 snprintf(csq->node_desc, sizeof(csq->node_desc), "%s", value);
388 } else if (streq("split_dir", key)) {
389 snprintf(csq->split_dir, sizeof(csq->split_dir), "%s", value);
390 } else if (streq("state", key)) {
392 if (parse_client_state(value, &cst)) {
395 } else if (streq("layer", key)) {
397 if (parse_stack_layer(value, &lyr)) {
400 } else if (streq("split_ratio", key)) {
402 if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
403 csq->split_ratio = rat;
405 } else if (streq("rectangle", key)) {
406 if (csq->rect == NULL) {
407 csq->rect = calloc(1, sizeof(xcb_rectangle_t));
409 if (!parse_rectangle(value, csq->rect)) {
413 } else if (parse_bool(value, &v)) {
414 if (streq("hidden", key)) {
417 #define SETCSQ(name) \
418 else if (streq(#name, key)) { \
437 void list_rules(FILE *rsp)
439 for (rule_t *r = rule_head; r != NULL; r = r->next) {
440 fprintf(rsp, "%s:%s:%s %c> %s\n", r->class_name, r->instance_name, r->name, r->one_shot?'-':'=', r->effect);