1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
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.
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.
35 rule_t *make_rule(void)
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;
47 r->effect.desc[0] = '\0';
48 r->effect.tags[0] = '\0';
54 void add_rule(rule_t *r)
56 if (rule_head == NULL) {
57 rule_head = rule_tail = r;
65 void remove_rule(rule_t *r)
69 rule_t *prev = r->prev;
70 rule_t *next = r->next;
82 void remove_rule_by_name(char *name)
84 rule_t *r = rule_head;
86 rule_t *next = r->next;
87 if (streq(r->cause.name, name))
93 bool remove_rule_by_index(int idx)
95 for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
103 bool is_match(rule_t *r, xcb_window_t win)
105 xcb_icccm_get_wm_class_reply_t reply;
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)))) {
112 xcb_icccm_get_wm_class_reply_wipe(&reply);
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)
120 xcb_ewmh_get_atoms_reply_t win_type;
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) {
130 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
132 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
136 xcb_ewmh_get_atoms_reply_wipe(&win_type);
139 xcb_size_hints_t size_hints;
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)
148 xcb_ewmh_get_atoms_reply_t win_state;
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)
155 else if (a == ewmh->_NET_WM_STATE_STICKY)
158 xcb_ewmh_get_atoms_reply_wipe(&win_state);
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);
167 rule_t *rule = rule_head;
169 while (rule != NULL) {
170 if (is_match(rule, win)) {
171 rule_effect_t efc = rule->effect;
188 if (efc.desc[0] != '\0') {
189 coordinates_t ref = {*m, *d, NULL};
191 if (desktop_from_desc(efc.desc, &ref, &loc)) {
196 if (efc.tags[0] != '\0') {
197 char *name = strtok(efc.tags, LST_SEP);
198 while (name != NULL) {
201 if (parse_index(name, &idx))
202 tag = get_tag_by_index(idx - 1);
206 *tags_field |= tag->mask;
207 name = strtok(NULL, LST_SEP);
211 rule_t *next = rule->next;
223 void list_rules(char *pattern, char *rsp)
227 for (rule_t *r = rule_head; r != NULL; r = r->next) {
228 if (pattern != NULL && !streq(pattern, r->cause.name))
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));
243 strncat(rsp, " --focus", REMLEN(rsp));
245 strncat(rsp, " --frame", REMLEN(rsp));
246 if (r->effect.unmanage)
247 strncat(rsp, " --unmanage", REMLEN(rsp));
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));
254 if (r->effect.tags[0] != '\0') {
255 snprintf(line, sizeof(line), " --tags %s", r->effect.tags);
256 strncat(rsp, line, REMLEN(rsp));
258 strncat(rsp, "\n", REMLEN(rsp));