]> git.lizzy.rs Git - bspwm.git/blob - src/rule.c
Add WM_NAME rule matching
[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_STATE(val) \
196         do { \
197                 if (csq->state == NULL) { \
198                         csq->state = calloc(1, sizeof(client_state_t)); \
199                 } \
200                 *(csq->state) = (val); \
201         } while (0)
202
203 #define SET_CSQ_LAYER(val) \
204         do { \
205                 if (csq->layer == NULL) { \
206                         csq->layer = calloc(1, sizeof(stack_layer_t)); \
207                 } \
208                 *(csq->layer) = (val); \
209         } while (0)
210
211 void _apply_window_type(xcb_window_t win, rule_consequence_t *csq)
212 {
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) {
219                                 csq->focus = false;
220                         } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
221                                 SET_CSQ_STATE(STATE_FLOATING);
222                                 csq->center = true;
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) {
226                                 csq->manage = false;
227                                 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP) {
228                                         window_lower(win);
229                                 }
230                         }
231                 }
232                 xcb_ewmh_get_atoms_reply_wipe(&win_type);
233         }
234 }
235
236 void _apply_window_state(xcb_window_t win, rule_consequence_t *csq)
237 {
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) {
249                                 csq->sticky = true;
250                         }
251                 }
252                 xcb_ewmh_get_atoms_reply_wipe(&win_state);
253         }
254 }
255
256 void _apply_transient(xcb_window_t win, rule_consequence_t *csq)
257 {
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);
262         }
263 }
264
265 void _apply_hints(xcb_window_t win, rule_consequence_t *csq)
266 {
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);
272                 }
273         }
274 }
275
276 void _apply_class(xcb_window_t win, rule_consequence_t *csq)
277 {
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);
283         }
284 }
285
286 void _apply_name(xcb_window_t win, rule_consequence_t *csq)
287 {
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);
292         }
293 }
294
295 void parse_keys_values(char *buf, rule_consequence_t *csq)
296 {
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);
303         }
304 }
305
306 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
307 {
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);
314
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))) {
321                         char effect[MAXLEN];
322                         snprintf(effect, sizeof(effect), "%s", rule->effect);
323                         parse_keys_values(effect, csq);
324                         if (rule->one_shot) {
325                                 remove_rule(rule);
326                                 break;
327                         }
328                 }
329                 rule = next;
330         }
331 }
332
333 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
334 {
335         if (external_rules_command[0] == '\0') {
336                 return false;
337         }
338         int fds[2];
339         if (pipe(fds) == -1) {
340                 return false;
341         }
342         pid_t pid = fork();
343         if (pid == 0) {
344                 if (dpy != NULL) {
345                         close(xcb_get_file_descriptor(dpy));
346                 }
347                 dup2(fds[1], 1);
348                 close(fds[0]);
349                 char wid[SMALEN];
350                 char *csq_buf;
351                 print_rule_consequence(&csq_buf, csq);
352                 snprintf(wid, sizeof(wid), "%i", win);
353                 setsid();
354                 execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, csq_buf, NULL);
355                 free(csq_buf);
356                 err("Couldn't spawn rule command.\n");
357         } else if (pid > 0) {
358                 close(fds[1]);
359                 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
360                 add_pending_rule(pr);
361         }
362         return (pid != -1);
363 }
364
365 void parse_rule_consequence(int fd, rule_consequence_t *csq)
366 {
367         if (fd == -1) {
368                 return;
369         }
370         char data[BUFSIZ];
371         int nb;
372         while ((nb = read(fd, data, sizeof(data))) > 0) {
373                 int end = MIN(nb, (int) sizeof(data) - 1);
374                 data[end] = '\0';
375                 parse_keys_values(data, csq);
376         }
377 }
378
379 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
380 {
381         bool v;
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)) {
391                 client_state_t cst;
392                 if (parse_client_state(value, &cst)) {
393                         SET_CSQ_STATE(cst);
394                 }
395         } else if (streq("layer", key)) {
396                 stack_layer_t lyr;
397                 if (parse_stack_layer(value, &lyr)) {
398                         SET_CSQ_LAYER(lyr);
399                 }
400         } else if (streq("split_ratio", key)) {
401                 double rat;
402                 if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
403                         csq->split_ratio = rat;
404                 }
405         } else if (streq("rectangle", key)) {
406                 if (csq->rect == NULL) {
407                         csq->rect = calloc(1, sizeof(xcb_rectangle_t));
408                 }
409                 if (!parse_rectangle(value, csq->rect)) {
410                         free(csq->rect);
411                         csq->rect = NULL;
412                 }
413         } else if (parse_bool(value, &v)) {
414                 if (streq("hidden", key)) {
415                         csq->hidden = v;
416                 }
417 #define SETCSQ(name) \
418                 else if (streq(#name, key)) { \
419                         csq->name = v; \
420                 }
421                 SETCSQ(sticky)
422                 SETCSQ(private)
423                 SETCSQ(locked)
424                 SETCSQ(marked)
425                 SETCSQ(center)
426                 SETCSQ(follow)
427                 SETCSQ(manage)
428                 SETCSQ(focus)
429                 SETCSQ(border)
430 #undef SETCSQ
431         }
432 }
433
434 #undef SET_CSQ_LAYER
435 #undef SET_CSQ_STATE
436
437 void list_rules(FILE *rsp)
438 {
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);
441         }
442 }