]> git.lizzy.rs Git - bspwm.git/blob - rule.c
Reinstate the *rule* command
[bspwm.git] / rule.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation and/or
11  * other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 ON
20  * 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 "query.h"
34 #include "rule.h"
35
36 rule_t *make_rule(void)
37 {
38     rule_t *r = malloc(sizeof(rule_t));
39     r->cause[0] = r->effect[0] = '\0';
40     r->next = r->prev = NULL;
41     r->one_shot = false;
42     return r;
43 }
44
45 void add_rule(rule_t *r)
46  {
47     if (rule_head == NULL) {
48         rule_head = rule_tail = r;
49     } else {
50         rule_tail->next = r;
51         r->prev = rule_tail;
52         rule_tail = r;
53     }
54 }
55
56 void remove_rule(rule_t *r)
57 {
58     if (r == NULL)
59         return;
60     rule_t *prev = r->prev;
61     rule_t *next = r->next;
62     if (prev != NULL)
63         prev->next = next;
64     if (next != NULL)
65         next->prev = prev;
66     if (r == rule_head)
67         rule_head = next;
68     if (r == rule_tail)
69         rule_tail = prev;
70     free(r);
71 }
72
73 void remove_rule_by_cause(char *cause)
74 {
75     rule_t *r = rule_head;
76     while (r != NULL) {
77         rule_t *next = r->next;
78         if (streq(r->cause, cause))
79             remove_rule(r);
80         r = next;
81     }
82 }
83
84 bool remove_rule_by_index(int idx)
85 {
86     for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
87         if (idx == 0) {
88             remove_rule(r);
89             return true;
90         }
91     return false;
92 }
93
94 rule_consequence_t *make_rule_conquence(void)
95 {
96     rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
97     rc->manage = rc->focus = true;
98     return rc;
99 }
100
101 pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *csq)
102 {
103     pending_rule_t *pr = malloc(sizeof(pending_rule_t));
104     pr->prev = pr->next = NULL;
105     pr->fd = fd;
106     pr->win = win;
107     pr->csq = csq;
108     return pr;
109 }
110
111 void add_pending_rule(pending_rule_t *pr)
112 {
113     if (pr == NULL)
114         return;
115     PRINTF("add pending rule %i\n", pr->fd);
116     if (pending_rule_head == NULL) {
117         pending_rule_head = pending_rule_tail = pr;
118     } else {
119         pending_rule_tail->next = pr;
120         pr->prev = pending_rule_tail;
121         pending_rule_tail = pr;
122     }
123 }
124
125 void remove_pending_rule(pending_rule_t *pr)
126 {
127     if (pr == NULL)
128         return;
129     PRINTF("remove pending rule %i\n", pr->fd);
130     pending_rule_t *a = pr->prev;
131     pending_rule_t *b = pr->next;
132     if (a != NULL)
133         a->next = b;
134     if (b != NULL)
135         b->prev = a;
136     if (pr == pending_rule_head)
137         pending_rule_head = b;
138     if (pr == pending_rule_tail)
139         pending_rule_tail = a;
140     close(pr->fd);
141     free(pr->csq);
142     free(pr);
143 }
144
145 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
146 {
147     xcb_ewmh_get_atoms_reply_t win_type;
148
149     if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
150         for (unsigned int i = 0; i < win_type.atoms_len; i++) {
151             xcb_atom_t a = win_type.atoms[i];
152             if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
153                     || a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
154                 csq->focus = false;
155             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
156                 csq->floating = true;
157             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
158                 csq->manage = false;
159                 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
160                     window_lower(win);
161             }
162         }
163         xcb_ewmh_get_atoms_reply_wipe(&win_type);
164     }
165
166     xcb_ewmh_get_atoms_reply_t win_state;
167
168     if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
169         for (unsigned int i = 0; i < win_state.atoms_len; i++) {
170             xcb_atom_t a = win_state.atoms[i];
171             if (a == ewmh->_NET_WM_STATE_FULLSCREEN)
172                 csq->fullscreen = true;
173             else if (a == ewmh->_NET_WM_STATE_STICKY)
174                 csq->sticky = true;
175         }
176         xcb_ewmh_get_atoms_reply_wipe(&win_state);
177     }
178
179     xcb_size_hints_t size_hints;
180     if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
181         if (size_hints.min_width > 0 && size_hints.min_height > 0
182                 && size_hints.min_width == size_hints.max_width
183                 && size_hints.min_height == size_hints.max_height)
184             csq->floating = true;
185     }
186
187     xcb_window_t transient_for = XCB_NONE;
188     xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
189     if (transient_for != XCB_NONE)
190         csq->transient = csq->floating = true;
191
192     xcb_icccm_get_wm_class_reply_t reply;
193     if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
194         snprintf(csq->class_name, sizeof(csq->class_name), "%s", reply.class_name);
195         snprintf(csq->instance_name, sizeof(csq->instance_name), "%s", reply.instance_name);
196         xcb_icccm_get_wm_class_reply_wipe(&reply);
197     }
198
199     rule_t *rule = rule_head;
200     while (rule != NULL) {
201         rule_t *next = rule->next;
202         if (streq(rule->cause, MATCH_ANY)
203                 || streq(rule->cause, csq->class_name)
204                 || streq(rule->cause, csq->instance_name)) {
205             char effect[MAXLEN];
206             snprintf(effect, sizeof(effect), "%s", rule->effect);
207             char *key = strtok(effect, CSQ_BLK);
208             char *value = strtok(NULL, CSQ_BLK);
209             while (key != NULL && value != NULL) {
210                 parse_key_value(key, value, csq);
211                 key = strtok(NULL, CSQ_BLK);
212                 value = strtok(NULL, CSQ_BLK);
213             }
214             if (rule->one_shot)
215                 remove_rule(rule);
216         }
217         rule = next;
218     }
219 }
220
221 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
222 {
223     if (external_rules_command[0] == '\0')
224         return false;
225     int fds[2];
226     if (pipe(fds) == -1)
227         return false;
228     pid_t pid = fork();
229     if (pid == 0) {
230         if (dpy != NULL)
231             close(xcb_get_file_descriptor(dpy));
232         dup2(fds[1], 1);
233         close(fds[0]);
234         char wid[SMALEN];
235         snprintf(wid, sizeof(wid), "%i", win);
236         setsid();
237         execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, NULL);
238         err("Couldn't spawn rule command.\n");
239     } else if (pid > 0) {
240         close(fds[1]);
241         pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
242         add_pending_rule(pr);
243     }
244     return (pid != -1);
245 }
246
247 void parse_rule_consequence(int fd, rule_consequence_t *csq)
248 {
249     if (fd == -1)
250         return;
251     char data[BUFSIZ];
252     int nb;
253     while ((nb = read(fd, data, sizeof(data))) > 0) {
254         int end = MIN(nb, (int) sizeof(data) - 1);
255         data[end] = '\0';
256         char *key = strtok(data, CSQ_BLK);
257         char *value = strtok(NULL, CSQ_BLK);
258         while (key != NULL && value != NULL) {
259             parse_key_value(key, value, csq);
260             key = strtok(NULL, CSQ_BLK);
261             value = strtok(NULL, CSQ_BLK);
262         }
263     }
264 }
265
266 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
267 {
268     bool v;
269     if (streq("desktop", key)) {
270         snprintf(csq->desktop_desc, sizeof(csq->desktop_desc), "%s", value);
271     } else if (streq("monitor", key)) {
272         snprintf(csq->monitor_desc, sizeof(csq->monitor_desc), "%s", value);
273     } else if (parse_bool(value, &v)) {
274         if (streq("floating", key))
275             csq->floating = v;
276 #define SETCSQ(name) \
277         else if (streq(#name, key)) \
278             csq->name = v;
279         SETCSQ(fullscreen)
280         SETCSQ(locked)
281         SETCSQ(sticky)
282         SETCSQ(private)
283         SETCSQ(frame)
284         SETCSQ(center)
285         SETCSQ(lower)
286         SETCSQ(follow)
287         SETCSQ(manage)
288         SETCSQ(focus)
289 #undef SETCSQ
290     }
291 }
292
293 void list_rules(char *pattern, char *rsp)
294 {
295     char line[MAXLEN];
296     for (rule_t *r = rule_head; r != NULL; r = r->next) {
297         if (pattern != NULL && !streq(pattern, r->cause))
298             continue;
299         snprintf(line, sizeof(line), "%s => %s\n", r->cause, r->effect);
300         strncat(rsp, line, REMLEN(rsp));
301     }
302 }