]> git.lizzy.rs Git - bspwm.git/blob - rules.c
Relieve tree.c from non tree related functions
[bspwm.git] / rules.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <xcb/xcb_icccm.h>
4 #include <xcb/xcb_ewmh.h>
5 #include "types.h"
6 #include "bspwm.h"
7 #include "ewmh.h"
8 #include "rules.h"
9
10 void add_rule(rule_t *r)
11 {
12     if (rule_head == NULL) {
13         rule_head = rule_tail = r;
14     } else {
15         rule_tail->next = r;
16         r->prev = rule_tail;
17         rule_tail = r;
18     }
19 }
20
21 void remove_rule(rule_t *r)
22 {
23     if (r == NULL)
24         return;
25     rule_t *prev = r->prev;
26     rule_t *next = r->next;
27     if (prev != NULL)
28         prev->next = next;
29     if (next != NULL)
30         next->prev = prev;
31     if (r == rule_head)
32         rule_head = next;
33     if (r == rule_tail)
34         rule_tail = prev;
35     free(r);
36 }
37
38 void remove_rule_by_uid(unsigned int uid)
39 {
40     remove_rule(find_rule(uid));
41 }
42
43 rule_t *find_rule(unsigned int uid)
44 {
45     for (rule_t *r = rule_head; r != NULL; r = r->next)
46         if (r->uid == uid)
47             return r;
48     return NULL;
49 }
50
51 bool is_match(rule_t *r, xcb_window_t win)
52 {
53     xcb_icccm_get_wm_class_reply_t reply; 
54     if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1
55             && (strcmp(reply.class_name, r->cause.name) == 0
56                 || strcmp(reply.instance_name, r->cause.name) == 0)) {
57         xcb_icccm_get_wm_class_reply_wipe(&reply);
58         return true;
59     }
60     return false;
61 }
62
63 void handle_rules(xcb_window_t win, monitor_t **m, desktop_t **d, bool *floating, bool *transient, bool *fullscreen, bool *takes_focus, bool *manage)
64 {
65     xcb_ewmh_get_atoms_reply_t win_type;
66
67     if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
68         for (unsigned int i = 0; i < win_type.atoms_len; i++) {
69             xcb_atom_t a = win_type.atoms[i];
70             if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
71                     || a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
72                 *takes_focus = false;
73             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
74                 *floating = true;
75             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
76                 *manage = false;
77             }
78         }
79         xcb_ewmh_get_atoms_reply_wipe(&win_type);
80     }
81
82     xcb_size_hints_t size_hints;
83
84     if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
85         if (size_hints.min_width > 0 && size_hints.min_height > 0
86                 && size_hints.min_width == size_hints.max_width
87                 && size_hints.min_height == size_hints.max_height)
88             *floating = true;
89     }
90
91     xcb_ewmh_get_atoms_reply_t win_state;
92
93     if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
94         for (unsigned int i = 0; i < win_state.atoms_len; i++) {
95             xcb_atom_t a = win_state.atoms[i];
96             if (a == ewmh->_NET_WM_STATE_FULLSCREEN) {
97                 *fullscreen = true;
98             }
99         }
100         xcb_ewmh_get_atoms_reply_wipe(&win_state);
101     }
102
103     xcb_window_t transient_for = XCB_NONE;
104     xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
105     *transient = (transient_for == XCB_NONE ? false : true);
106     if (*transient)
107         *floating = true;
108
109     rule_t *rule = rule_head;
110
111     while (rule != NULL) {
112         if (is_match(rule, win)) {
113             rule_effect_t efc = rule->effect;
114             if (efc.floating)
115                 *floating = true;
116             if (efc.monitor != NULL && efc.desktop != NULL) {
117                 *m = efc.monitor;
118                 *d = efc.desktop;
119             }
120         }
121         rule = rule->next;
122     }
123 }
124
125 void list_rules(char *rsp)
126 {
127     char line[MAXLEN];
128
129     for (rule_t *r = rule_head; r != NULL; r = r->next) {
130         snprintf(line, sizeof(line), "%02X %s %s %s\n", r->uid, r->cause.name, (r->effect.desktop != NULL ? r->effect.desktop->name : "\b"), (r->effect.floating ? "floating" : "\b"));
131         strncat(rsp, line, REMLEN(rsp));
132     }
133 }