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