]> git.lizzy.rs Git - bspwm.git/blob - rules.c
3b5e22fc219fdc5be092b4ac7395565ac4d14cbc
[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 bool is_match(rule_t *r, xcb_window_t win)
22 {
23     xcb_icccm_get_wm_class_reply_t reply; 
24     if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1
25             && (strcmp(reply.class_name, r->cause.name) == 0
26                 || strcmp(reply.instance_name, r->cause.name) == 0)) {
27         xcb_icccm_get_wm_class_reply_wipe(&reply);
28         return true;
29     }
30     return false;
31 }
32
33 void handle_rules(xcb_window_t win, monitor_t **m, desktop_t **d, bool *floating, bool *transient, bool *fullscreen, bool *takes_focus, bool *manage)
34 {
35     xcb_ewmh_get_atoms_reply_t win_type;
36
37     if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
38         for (unsigned int i = 0; i < win_type.atoms_len; i++) {
39             xcb_atom_t a = win_type.atoms[i];
40             if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
41                     || a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
42                 *takes_focus = false;
43             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
44                 *floating = true;
45             } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
46                 *manage = false;
47             }
48         }
49         xcb_ewmh_get_atoms_reply_wipe(&win_type);
50     }
51
52     xcb_size_hints_t size_hints;
53
54     if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
55         if (size_hints.min_width > 0 && size_hints.min_height > 0
56                 && size_hints.min_width == size_hints.max_width
57                 && size_hints.min_height == size_hints.max_height)
58             *floating = true;
59     }
60
61     xcb_ewmh_get_atoms_reply_t win_state;
62
63     if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
64         for (unsigned int i = 0; i < win_state.atoms_len; i++) {
65             xcb_atom_t a = win_state.atoms[i];
66             if (a == ewmh->_NET_WM_STATE_FULLSCREEN) {
67                 *fullscreen = true;
68             }
69         }
70         xcb_ewmh_get_atoms_reply_wipe(&win_state);
71     }
72
73     xcb_window_t transient_for = XCB_NONE;
74     xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
75     *transient = (transient_for == XCB_NONE ? false : true);
76     if (*transient)
77         *floating = true;
78
79     rule_t *rule = rule_head;
80
81     while (rule != NULL) {
82         if (is_match(rule, win)) {
83             rule_effect_t efc = rule->effect;
84             if (efc.floating)
85                 *floating = true;
86             if (efc.monitor != NULL && efc.desktop != NULL) {
87                 *m = efc.monitor;
88                 *d = efc.desktop;
89             }
90         }
91         rule = rule->next;
92     }
93 }
94
95 void list_rules(char *rsp)
96 {
97     char line[MAXLEN];
98
99     for (rule_t *r = rule_head; r != NULL; r = r->next) {
100         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"));
101         strncat(rsp, line, REMLEN(rsp));
102     }
103 }