]> git.lizzy.rs Git - bspwm.git/blob - src/rule.c
bspwm: port rounded corners patch to latest version
[bspwm.git] / src / rule.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <sys/types.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include "bspwm.h"
32 #include "ewmh.h"
33 #include "window.h"
34 #include "query.h"
35 #include "parse.h"
36 #include "settings.h"
37 #include "rule.h"
38
39 rule_t *make_rule(void)
40 {
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;
44         r->one_shot = false;
45         return r;
46 }
47
48 void add_rule(rule_t *r)
49  {
50         if (rule_head == NULL) {
51                 rule_head = rule_tail = r;
52         } else {
53                 rule_tail->next = r;
54                 r->prev = rule_tail;
55                 rule_tail = r;
56         }
57 }
58
59 void remove_rule(rule_t *r)
60 {
61         if (r == NULL) {
62                 return;
63         }
64         rule_t *prev = r->prev;
65         rule_t *next = r->next;
66         if (prev != NULL) {
67                 prev->next = next;
68         }
69         if (next != NULL) {
70                 next->prev = prev;
71         }
72         if (r == rule_head) {
73                 rule_head = next;
74         }
75         if (r == rule_tail) {
76                 rule_tail = prev;
77         }
78         free(r);
79 }
80
81 void remove_rule_by_cause(char *cause)
82 {
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);
87         while (r != NULL) {
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))) {
92                         remove_rule(r);
93                 }
94                 r = next;
95         }
96 }
97
98 bool remove_rule_by_index(int idx)
99 {
100         for (rule_t *r = rule_head; r != NULL; r = r->next, idx--) {
101                 if (idx == 0) {
102                         remove_rule(r);
103                         return true;
104                 }
105         }
106         return false;
107 }
108
109 rule_consequence_t *make_rule_consequence(void)
110 {
111         rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
112         rc->manage = rc->focus = rc->border = true;
113         rc->layer = NULL;
114         rc->state = NULL;
115         rc->rect = NULL;
116         return rc;
117 }
118
119 pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *csq)
120 {
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;
124         pr->fd = fd;
125         pr->win = win;
126         pr->csq = csq;
127         return pr;
128 }
129
130 void add_pending_rule(pending_rule_t *pr)
131 {
132         if (pr == NULL) {
133                 return;
134         }
135         if (pending_rule_head == NULL) {
136                 pending_rule_head = pending_rule_tail = pr;
137         } else {
138                 pending_rule_tail->next = pr;
139                 pr->prev = pending_rule_tail;
140                 pending_rule_tail = pr;
141         }
142 }
143
144 void remove_pending_rule(pending_rule_t *pr)
145 {
146         if (pr == NULL) {
147                 return;
148         }
149         pending_rule_t *a = pr->prev;
150         pending_rule_t *b = pr->next;
151         if (a != NULL) {
152                 a->next = b;
153         }
154         if (b != NULL) {
155                 b->prev = a;
156         }
157         if (pr == pending_rule_head) {
158                 pending_rule_head = b;
159         }
160         if (pr == pending_rule_tail) {
161                 pending_rule_tail = a;
162         }
163         close(pr->fd);
164         free(pr->csq);
165         event_queue_t *eq = pr->event_head;
166         while (eq != NULL) {
167                 event_queue_t *next = eq->next;
168                 free(eq);
169                 eq = next;
170         }
171         free(pr);
172 }
173
174 void postpone_event(pending_rule_t *pr, xcb_generic_event_t *evt)
175 {
176         event_queue_t *eq = make_event_queue(evt);
177         if (pr->event_tail == NULL) {
178                 pr->event_head = pr->event_tail = eq;
179         } else {
180                 pr->event_tail->next = eq;
181                 eq->prev = pr->event_tail;
182                 pr->event_tail = eq;
183         }
184 }
185
186 event_queue_t *make_event_queue(xcb_generic_event_t *evt)
187 {
188         event_queue_t *eq = calloc(1, sizeof(event_queue_t));
189         eq->prev = eq->next = NULL;
190         eq->event = *evt;
191         return eq;
192 }
193
194
195 #define SET_CSQ_SPLIT_DIR(val) \
196         do { \
197                 if (csq->split_dir == NULL) { \
198                         csq->split_dir = calloc(1, sizeof(direction_t)); \
199                 } \
200                 *(csq->split_dir) = (val); \
201         } while (0)
202
203 #define SET_CSQ_STATE(val) \
204         do { \
205                 if (csq->state == NULL) { \
206                         csq->state = calloc(1, sizeof(client_state_t)); \
207                 } \
208                 *(csq->state) = (val); \
209         } while (0)
210
211 #define SET_CSQ_LAYER(val) \
212         do { \
213                 if (csq->layer == NULL) { \
214                         csq->layer = calloc(1, sizeof(stack_layer_t)); \
215                 } \
216                 *(csq->layer) = (val); \
217         } while (0)
218
219 void _apply_window_type(xcb_window_t win, rule_consequence_t *csq)
220 {
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) {
227                                 csq->focus = false;
228                         } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
229                                 SET_CSQ_STATE(STATE_FLOATING);
230                                 csq->center = true;
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) {
234                                 csq->manage = false;
235                                 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP) {
236                                         window_lower(win);
237                                 }
238                         }
239                 }
240                 xcb_ewmh_get_atoms_reply_wipe(&win_type);
241         }
242 }
243
244 void _apply_window_state(xcb_window_t win, rule_consequence_t *csq)
245 {
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) {
257                                 csq->sticky = true;
258                         }
259                 }
260                 xcb_ewmh_get_atoms_reply_wipe(&win_state);
261         }
262 }
263
264 void _apply_transient(xcb_window_t win, rule_consequence_t *csq)
265 {
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);
270         }
271 }
272
273 void _apply_hints(xcb_window_t win, rule_consequence_t *csq)
274 {
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);
280                 }
281         }
282 }
283
284 void _apply_class(xcb_window_t win, rule_consequence_t *csq)
285 {
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);
291         }
292 }
293
294 void _apply_name(xcb_window_t win, rule_consequence_t *csq)
295 {
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);
300         }
301 }
302
303 void parse_keys_values(char *buf, rule_consequence_t *csq)
304 {
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);
311         }
312 }
313
314 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
315 {
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);
322
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))) {
329                         char effect[MAXLEN];
330                         snprintf(effect, sizeof(effect), "%s", rule->effect);
331                         parse_keys_values(effect, csq);
332                         if (rule->one_shot) {
333                                 remove_rule(rule);
334                                 break;
335                         }
336                 }
337                 rule = next;
338         }
339 }
340
341 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
342 {
343         if (external_rules_command[0] == '\0') {
344                 return false;
345         }
346         resolve_rule_consequence(csq);
347         int fds[2];
348         if (pipe(fds) == -1) {
349                 return false;
350         }
351         pid_t pid = fork();
352         if (pid == 0) {
353                 if (dpy != NULL) {
354                         close(xcb_get_file_descriptor(dpy));
355                 }
356                 dup2(fds[1], 1);
357                 close(fds[0]);
358                 char wid[SMALEN];
359                 char *csq_buf;
360                 print_rule_consequence(&csq_buf, csq);
361                 snprintf(wid, sizeof(wid), "%i", win);
362                 setsid();
363                 execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, csq_buf, NULL);
364                 free(csq_buf);
365                 err("Couldn't spawn rule command.\n");
366         } else if (pid > 0) {
367                 close(fds[1]);
368                 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
369                 add_pending_rule(pr);
370         }
371         return (pid != -1);
372 }
373
374 void parse_rule_consequence(int fd, rule_consequence_t *csq)
375 {
376         if (fd == -1) {
377                 return;
378         }
379         char data[BUFSIZ];
380         int nb;
381         while ((nb = read(fd, data, sizeof(data))) > 0) {
382                 int end = MIN(nb, (int) sizeof(data) - 1);
383                 data[end] = '\0';
384                 parse_keys_values(data, csq);
385         }
386 }
387
388 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
389 {
390         bool v;
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)) {
398                 direction_t dir;
399                 if (parse_direction(value, &dir)) {
400                         SET_CSQ_SPLIT_DIR(dir);
401                 }
402         } else if (streq("state", key)) {
403                 client_state_t cst;
404                 if (parse_client_state(value, &cst)) {
405                         SET_CSQ_STATE(cst);
406                 }
407         } else if (streq("layer", key)) {
408                 stack_layer_t lyr;
409                 if (parse_stack_layer(value, &lyr)) {
410                         SET_CSQ_LAYER(lyr);
411                 }
412         } else if (streq("split_ratio", key)) {
413                 double rat;
414                 if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
415                         csq->split_ratio = rat;
416                 }
417         } else if (streq("rectangle", key)) {
418                 if (csq->rect == NULL) {
419                         csq->rect = calloc(1, sizeof(xcb_rectangle_t));
420                 }
421                 if (!parse_rectangle(value, csq->rect)) {
422                         free(csq->rect);
423                         csq->rect = NULL;
424                 }
425         } else if (parse_bool(value, &v)) {
426                 if (streq("hidden", key)) {
427                         csq->hidden = v;
428                 }
429 #define SETCSQ(name) \
430                 else if (streq(#name, key)) { \
431                         csq->name = v; \
432                 }
433                 SETCSQ(sticky)
434                 SETCSQ(private)
435                 SETCSQ(locked)
436                 SETCSQ(marked)
437                 SETCSQ(center)
438                 SETCSQ(follow)
439                 SETCSQ(manage)
440                 SETCSQ(focus)
441                 SETCSQ(border)
442 #undef SETCSQ
443         }
444 }
445
446 #undef SET_CSQ_LAYER
447 #undef SET_CSQ_STATE
448
449 void list_rules(FILE *rsp)
450 {
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);
453         }
454 }