]> git.lizzy.rs Git - bspwm.git/blob - rule.c
eef471d1061d6a79761959ad8f6901e466f9cc3f
[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 "bspwm.h"
28 #include "ewmh.h"
29 #include "window.h"
30 #include "messages.h"
31 #include "query.h"
32 #include "tag.h"
33 #include "rule.h"
34
35 rule_t *make_rule(void)
36 {
37     rule_t *r = malloc(sizeof(rule_t));
38     r->effect.floating = false;
39     r->effect.fullscreen = false;
40     r->effect.locked = false;
41     r->effect.sticky = false;
42     r->effect.follow = false;
43     r->effect.focus = false;
44     r->effect.frame = false;
45     r->effect.unmanage = false;
46     r->one_shot = false;
47     r->effect.desc[0] = '\0';
48     r->effect.tags[0] = '\0';
49     r->prev = NULL;
50     r->next = NULL;
51     return r;
52 }
53
54 void add_rule(rule_t *r)
55 {
56     if (rule_head == NULL) {
57         rule_head = rule_tail = r;
58     } else {
59         rule_tail->next = r;
60         r->prev = rule_tail;
61         rule_tail = r;
62     }
63 }
64
65 void remove_rule(rule_t *r)
66 {
67     if (r == NULL)
68         return;
69     rule_t *prev = r->prev;
70     rule_t *next = r->next;
71     if (prev != NULL)
72         prev->next = next;
73     if (next != NULL)
74         next->prev = prev;
75     if (r == rule_head)
76         rule_head = next;
77     if (r == rule_tail)
78         rule_tail = prev;
79     free(r);
80 }
81
82 void remove_rule_by_name(char *name)
83 {
84     rule_t *r = rule_head;
85     while (r != NULL) {
86         rule_t *next = r->next;
87         if (streq(r->cause.name, name))
88             remove_rule(r);
89         r = next;
90     }
91 }
92
93 bool remove_rule_by_index(int idx)
94 {
95     for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
96         if (idx == 0) {
97             remove_rule(r);
98             return true;
99         }
100     return false;
101 }
102
103 bool is_match(rule_t *r, xcb_window_t win)
104 {
105     xcb_icccm_get_wm_class_reply_t reply;
106     int8_t success = 0;
107     if (streq(r->cause.name, MATCH_ALL) ||
108             ((success = xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL)) == 1
109             && (streq(reply.class_name, r->cause.name)
110                 || streq(reply.instance_name, r->cause.name)))) {
111         if (success == 1)
112             xcb_icccm_get_wm_class_reply_wipe(&reply);
113         return true;
114     }
115     return false;
116 }
117
118 void handle_rules(xcb_window_t win, monitor_t **m, desktop_t **d, unsigned int *tags_field, bool *floating, bool *fullscreen, bool *locked, bool *sticky, bool *follow, bool *transient, bool *takes_focus, bool *frame, bool *manage)
119 {
120     xcb_ewmh_get_atoms_reply_t win_type;
121
122     if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
123         for (unsigned int i = 0; i < win_type.atoms_len; i++) {
124             xcb_atom_t a = win_type.atoms[i];
125             if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
126                     || a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
127                 *takes_focus = false;
128             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
129                 *floating = true;
130             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
131                 *manage = false;
132                 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
133                     window_lower(win);
134             }
135         }
136         xcb_ewmh_get_atoms_reply_wipe(&win_type);
137     }
138
139     xcb_size_hints_t size_hints;
140
141     if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
142         if (size_hints.min_width > 0 && size_hints.min_height > 0
143                 && size_hints.min_width == size_hints.max_width
144                 && size_hints.min_height == size_hints.max_height)
145             *floating = true;
146     }
147
148     xcb_ewmh_get_atoms_reply_t win_state;
149
150     if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
151         for (unsigned int i = 0; i < win_state.atoms_len; i++) {
152             xcb_atom_t a = win_state.atoms[i];
153             if (a == ewmh->_NET_WM_STATE_FULLSCREEN)
154                 *fullscreen = true;
155             else if (a == ewmh->_NET_WM_STATE_STICKY)
156                 *sticky = true;
157         }
158         xcb_ewmh_get_atoms_reply_wipe(&win_state);
159     }
160
161     xcb_window_t transient_for = XCB_NONE;
162     xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
163     *transient = (transient_for == XCB_NONE ? false : true);
164     if (*transient)
165         *floating = true;
166
167     rule_t *rule = rule_head;
168
169     while (rule != NULL) {
170         if (is_match(rule, win)) {
171             rule_effect_t efc = rule->effect;
172             if (efc.floating)
173                 *floating = true;
174             if (efc.fullscreen)
175                 *fullscreen = true;
176             if (efc.locked)
177                 *locked = true;
178             if (efc.sticky)
179                 *sticky = true;
180             if (efc.follow)
181                 *follow = true;
182             if (efc.focus)
183                 *takes_focus = true;
184             if (efc.frame)
185                 *frame = true;
186             if (efc.unmanage)
187                 *manage = false;
188             if (efc.desc[0] != '\0') {
189                 coordinates_t ref = {*m, *d, NULL};
190                 coordinates_t loc;
191                 if (desktop_from_desc(efc.desc, &ref, &loc)) {
192                     *m = loc.monitor;
193                     *d = loc.desktop;
194                 }
195             }
196             if (efc.tags[0] != '\0') {
197                 char *name = strtok(efc.tags, LST_SEP);
198                 while (name != NULL) {
199                     int idx;
200                     tag_t *tag = NULL;
201                     if (parse_index(name, &idx))
202                         tag = get_tag_by_index(idx - 1);
203                     else
204                         tag = get_tag(name);
205                     if (tag != NULL)
206                         *tags_field |= tag->mask;
207                     name = strtok(NULL, LST_SEP);
208                 }
209             }
210         }
211         rule_t *next = rule->next;
212         if (rule->one_shot)
213             remove_rule(rule);
214         rule = next;
215     }
216
217     if (*sticky) {
218         *m = mon;
219         *d = mon->desk;
220     }
221 }
222
223 void list_rules(char *pattern, char *rsp)
224 {
225     char line[MAXLEN];
226
227     for (rule_t *r = rule_head; r != NULL; r = r->next) {
228         if (pattern != NULL && !streq(pattern, r->cause.name))
229             continue;
230         snprintf(line, sizeof(line), "%s", r->cause.name);
231         strncat(rsp, line, REMLEN(rsp));
232         if (r->effect.floating)
233             strncat(rsp, " --floating", REMLEN(rsp));
234         if (r->effect.fullscreen)
235             strncat(rsp, " --fullscreen", REMLEN(rsp));
236         if (r->effect.locked)
237             strncat(rsp, " --locked", REMLEN(rsp));
238         if (r->effect.sticky)
239             strncat(rsp, " --sticky", REMLEN(rsp));
240         if (r->effect.follow)
241             strncat(rsp, " --follow", REMLEN(rsp));
242         if (r->effect.focus)
243             strncat(rsp, " --focus", REMLEN(rsp));
244         if (r->effect.frame)
245             strncat(rsp, " --frame", REMLEN(rsp));
246         if (r->effect.unmanage)
247             strncat(rsp, " --unmanage", REMLEN(rsp));
248         if (r->one_shot)
249             strncat(rsp, " --one-shot", REMLEN(rsp));
250         if (r->effect.desc[0] != '\0') {
251             snprintf(line, sizeof(line), " -d %s", r->effect.desc);
252             strncat(rsp, line, REMLEN(rsp));
253         }
254         if (r->effect.tags[0] != '\0') {
255             snprintf(line, sizeof(line), " --tags %s", r->effect.tags);
256             strncat(rsp, line, REMLEN(rsp));
257         }
258         strncat(rsp, "\n", REMLEN(rsp));
259     }
260 }