]> git.lizzy.rs Git - bspwm.git/blob - rule.c
Don't try to set the split ratio of NULL
[bspwm.git] / 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 <string.h>
27 #include <unistd.h>
28 #include "bspwm.h"
29 #include "ewmh.h"
30 #include "window.h"
31 #include "messages.h"
32 #include "settings.h"
33 #include "rule.h"
34
35 rule_t *make_rule(void)
36 {
37         rule_t *r = malloc(sizeof(rule_t));
38         r->cause[0] = r->effect[0] = '\0';
39         r->next = r->prev = NULL;
40         r->one_shot = false;
41         return r;
42 }
43
44 void add_rule(rule_t *r)
45  {
46         if (rule_head == NULL) {
47                 rule_head = rule_tail = r;
48         } else {
49                 rule_tail->next = r;
50                 r->prev = rule_tail;
51                 rule_tail = r;
52         }
53 }
54
55 void remove_rule(rule_t *r)
56 {
57         if (r == NULL)
58                 return;
59         rule_t *prev = r->prev;
60         rule_t *next = r->next;
61         if (prev != NULL)
62                 prev->next = next;
63         if (next != NULL)
64                 next->prev = prev;
65         if (r == rule_head)
66                 rule_head = next;
67         if (r == rule_tail)
68                 rule_tail = prev;
69         free(r);
70 }
71
72 void remove_rule_by_cause(char *cause)
73 {
74         rule_t *r = rule_head;
75         while (r != NULL) {
76                 rule_t *next = r->next;
77                 if (streq(r->cause, cause))
78                         remove_rule(r);
79                 r = next;
80         }
81 }
82
83 bool remove_rule_by_index(int idx)
84 {
85         for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
86                 if (idx == 0) {
87                         remove_rule(r);
88                         return true;
89                 }
90         return false;
91 }
92
93 rule_consequence_t *make_rule_conquence(void)
94 {
95         rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
96         rc->manage = rc->focus = rc->border = true;
97         return rc;
98 }
99
100 pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *csq)
101 {
102         pending_rule_t *pr = malloc(sizeof(pending_rule_t));
103         pr->prev = pr->next = NULL;
104         pr->fd = fd;
105         pr->win = win;
106         pr->csq = csq;
107         return pr;
108 }
109
110 void add_pending_rule(pending_rule_t *pr)
111 {
112         if (pr == NULL)
113                 return;
114         PRINTF("add pending rule %i\n", pr->fd);
115         if (pending_rule_head == NULL) {
116                 pending_rule_head = pending_rule_tail = pr;
117         } else {
118                 pending_rule_tail->next = pr;
119                 pr->prev = pending_rule_tail;
120                 pending_rule_tail = pr;
121         }
122 }
123
124 void remove_pending_rule(pending_rule_t *pr)
125 {
126         if (pr == NULL)
127                 return;
128         PRINTF("remove pending rule %i\n", pr->fd);
129         pending_rule_t *a = pr->prev;
130         pending_rule_t *b = pr->next;
131         if (a != NULL)
132                 a->next = b;
133         if (b != NULL)
134                 b->prev = a;
135         if (pr == pending_rule_head)
136                 pending_rule_head = b;
137         if (pr == pending_rule_tail)
138                 pending_rule_tail = a;
139         close(pr->fd);
140         free(pr->csq);
141         free(pr);
142 }
143
144 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
145 {
146         xcb_ewmh_get_atoms_reply_t win_type;
147
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) {
153                                 csq->focus = false;
154                         } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
155                                 csq->floating = true;
156                                 csq->center = 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) {
160                                 csq->manage = false;
161                                 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
162                                         window_lower(win);
163                         }
164                 }
165                 xcb_ewmh_get_atoms_reply_wipe(&win_type);
166         }
167
168         xcb_ewmh_get_atoms_reply_t win_state;
169
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)
176                                 csq->sticky = true;
177                 }
178                 xcb_ewmh_get_atoms_reply_wipe(&win_state);
179         }
180
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;
191         }
192
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;
197
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);
203         }
204
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)) {
211                         char effect[MAXLEN];
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);
219                         }
220                         if (rule->one_shot)
221                                 remove_rule(rule);
222                 }
223                 rule = next;
224         }
225 }
226
227 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
228 {
229         if (external_rules_command[0] == '\0')
230                 return false;
231         int fds[2];
232         if (pipe(fds) == -1)
233                 return false;
234         pid_t pid = fork();
235         if (pid == 0) {
236                 if (dpy != NULL)
237                         close(xcb_get_file_descriptor(dpy));
238                 dup2(fds[1], 1);
239                 close(fds[0]);
240                 char wid[SMALEN];
241                 snprintf(wid, sizeof(wid), "%i", win);
242                 setsid();
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) {
246                 close(fds[1]);
247                 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
248                 add_pending_rule(pr);
249         }
250         return (pid != -1);
251 }
252
253 void parse_rule_consequence(int fd, rule_consequence_t *csq)
254 {
255         if (fd == -1)
256                 return;
257         char data[BUFSIZ];
258         int nb;
259         while ((nb = read(fd, data, sizeof(data))) > 0) {
260                 int end = MIN(nb, (int) sizeof(data) - 1);
261                 data[end] = '\0';
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);
268                 }
269         }
270 }
271
272 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
273 {
274         bool v;
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)) {
284                 double rat;
285                 if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
286                         csq->split_ratio = rat;
287                 }
288         } else if (parse_bool(value, &v)) {
289                 if (streq("floating", key))
290                         csq->floating = v;
291 #define SETCSQ(name) \
292                 else if (streq(#name, key)) \
293                         csq->name = v;
294                 SETCSQ(pseudo_tiled)
295                 SETCSQ(fullscreen)
296                 SETCSQ(locked)
297                 SETCSQ(sticky)
298                 SETCSQ(private)
299                 SETCSQ(center)
300                 SETCSQ(follow)
301                 SETCSQ(manage)
302                 SETCSQ(focus)
303                 SETCSQ(border)
304 #undef SETCSQ
305         }
306 }
307
308 void list_rules(char *pattern, FILE *rsp)
309 {
310         for (rule_t *r = rule_head; r != NULL; r = r->next) {
311                 if (pattern != NULL && !streq(pattern, r->cause))
312                         continue;
313                 fprintf(rsp, "%s => %s\n", r->cause, r->effect);
314         }
315 }