]> git.lizzy.rs Git - bspwm.git/blob - rule.c
060999a06e245765a7948ed24406575825dfc434
[bspwm.git] / rule.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "bspwm.h"
4 #include "ewmh.h"
5 #include "window.h"
6 #include "query.h"
7 #include "rule.h"
8
9 rule_t *make_rule(void)
10 {
11     rule_t *r = malloc(sizeof(rule_t));
12     r->uid = ++rule_uid;
13     r->effect.floating = false;
14     r->effect.fullscreen = false;
15     r->effect.locked = false;
16     r->effect.follow = false;
17     r->effect.focus = false;
18     r->effect.unmanage = false;
19     r->one_shot = false;
20     r->effect.desc[0] = '\0';
21     r->prev = NULL;
22     r->next = NULL;
23     return r;
24 }
25
26 void add_rule(rule_t *r)
27 {
28     if (rule_head == NULL) {
29         rule_head = rule_tail = r;
30     } else {
31         rule_tail->next = r;
32         r->prev = rule_tail;
33         rule_tail = r;
34     }
35 }
36
37 void remove_rule(rule_t *r)
38 {
39     if (r == NULL)
40         return;
41     rule_t *prev = r->prev;
42     rule_t *next = r->next;
43     if (prev != NULL)
44         prev->next = next;
45     if (next != NULL)
46         next->prev = prev;
47     if (r == rule_head)
48         rule_head = next;
49     if (r == rule_tail)
50         rule_tail = prev;
51     free(r);
52 }
53
54 void remove_rule_by_uid(unsigned int uid)
55 {
56     remove_rule(find_rule(uid));
57 }
58
59 rule_t *find_rule(unsigned int uid)
60 {
61     for (rule_t *r = rule_head; r != NULL; r = r->next)
62         if (r->uid == uid)
63             return r;
64     return NULL;
65 }
66
67 bool is_match(rule_t *r, xcb_window_t win)
68 {
69     xcb_icccm_get_wm_class_reply_t reply;
70     int8_t success = 0;
71     if (streq(r->cause.name, MATCH_ALL) ||
72             ((success = xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL)) == 1
73             && (streq(reply.class_name, r->cause.name)
74                 || streq(reply.instance_name, r->cause.name)))) {
75         if (success == 1)
76             xcb_icccm_get_wm_class_reply_wipe(&reply);
77         return true;
78     }
79     return false;
80 }
81
82 void handle_rules(xcb_window_t win, monitor_t **m, desktop_t **d, bool *floating, bool *fullscreen, bool *locked, bool *follow, bool *transient, bool *takes_focus, bool *manage)
83 {
84     xcb_ewmh_get_atoms_reply_t win_type;
85
86     if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
87         for (unsigned int i = 0; i < win_type.atoms_len; i++) {
88             xcb_atom_t a = win_type.atoms[i];
89             if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
90                     || a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
91                 *takes_focus = false;
92             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
93                 *floating = true;
94             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
95                 *manage = false;
96                 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
97                     window_lower(win);
98             }
99         }
100         xcb_ewmh_get_atoms_reply_wipe(&win_type);
101     }
102
103     xcb_size_hints_t size_hints;
104
105     if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
106         if (size_hints.min_width > 0 && size_hints.min_height > 0
107                 && size_hints.min_width == size_hints.max_width
108                 && size_hints.min_height == size_hints.max_height)
109             *floating = true;
110     }
111
112     xcb_ewmh_get_atoms_reply_t win_state;
113
114     if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
115         for (unsigned int i = 0; i < win_state.atoms_len; i++) {
116             xcb_atom_t a = win_state.atoms[i];
117             if (a == ewmh->_NET_WM_STATE_FULLSCREEN) {
118                 *fullscreen = true;
119             }
120         }
121         xcb_ewmh_get_atoms_reply_wipe(&win_state);
122     }
123
124     xcb_window_t transient_for = XCB_NONE;
125     xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
126     *transient = (transient_for == XCB_NONE ? false : true);
127     if (*transient)
128         *floating = true;
129
130     rule_t *rule = rule_head;
131
132     while (rule != NULL) {
133         if (is_match(rule, win)) {
134             rule_effect_t efc = rule->effect;
135             if (efc.floating)
136                 *floating = true;
137             if (efc.fullscreen)
138                 *fullscreen = true;
139             if (efc.locked)
140                 *locked = true;
141             if (efc.follow)
142                 *follow = true;
143             if (efc.focus)
144                 *takes_focus = true;
145             if (efc.unmanage)
146                 *manage = false;
147             if (efc.desc[0] != '\0') {
148                 coordinates_t ref = {*m, *d, NULL};
149                 coordinates_t loc;
150                 if (desktop_from_desc(efc.desc, &ref, &loc)) {
151                     *m = loc.monitor;
152                     *d = loc.desktop;
153                 }
154             }
155         }
156         rule_t *next = rule->next;
157         if (rule->one_shot)
158             remove_rule(rule);
159         rule = next;
160     }
161 }
162
163 void list_rules(char *pattern, char *rsp)
164 {
165     char line[MAXLEN];
166
167     for (rule_t *r = rule_head; r != NULL; r = r->next) {
168         if (pattern != NULL && !streq(pattern, r->cause.name))
169             continue;
170         snprintf(line, sizeof(line), "%2X %s", r->uid, r->cause.name);
171         strncat(rsp, line, REMLEN(rsp));
172         if (r->effect.floating)
173             strncat(rsp, " --floating", REMLEN(rsp));
174         if (r->effect.fullscreen)
175             strncat(rsp, " --fullscreen", REMLEN(rsp));
176         if (r->effect.locked)
177             strncat(rsp, " --locked", REMLEN(rsp));
178         if (r->effect.follow)
179             strncat(rsp, " --follow", REMLEN(rsp));
180         if (r->effect.focus)
181             strncat(rsp, " --focus", REMLEN(rsp));
182         if (r->effect.unmanage)
183             strncat(rsp, " --unmanage", REMLEN(rsp));
184         if (r->one_shot)
185             strncat(rsp, " --one-shot", REMLEN(rsp));
186         if (r->effect.desc[0] != '\0') {
187             snprintf(line, sizeof(line), " -d %s", r->effect.desc);
188             strncat(rsp, line, REMLEN(rsp));
189         }
190         strncat(rsp, "\n", REMLEN(rsp));
191     }
192 }