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.
36 rule_t *make_rule(void)
38 rule_t *r = malloc(sizeof(rule_t));
39 r->cause[0] = r->effect[0] = '\0';
40 r->next = r->prev = NULL;
45 void add_rule(rule_t *r)
47 if (rule_head == NULL) {
48 rule_head = rule_tail = r;
56 void remove_rule(rule_t *r)
60 rule_t *prev = r->prev;
61 rule_t *next = r->next;
73 void remove_rule_by_cause(char *cause)
75 rule_t *r = rule_head;
77 rule_t *next = r->next;
78 if (streq(r->cause, cause))
84 bool remove_rule_by_index(int idx)
86 for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
94 rule_consequence_t *make_rule_conquence(void)
96 rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
97 rc->manage = rc->focus = true;
101 pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *csq)
103 pending_rule_t *pr = malloc(sizeof(pending_rule_t));
104 pr->prev = pr->next = NULL;
111 void add_pending_rule(pending_rule_t *pr)
115 PRINTF("add pending rule %i\n", pr->fd);
116 if (pending_rule_head == NULL) {
117 pending_rule_head = pending_rule_tail = pr;
119 pending_rule_tail->next = pr;
120 pr->prev = pending_rule_tail;
121 pending_rule_tail = pr;
125 void remove_pending_rule(pending_rule_t *pr)
129 PRINTF("remove pending rule %i\n", pr->fd);
130 pending_rule_t *a = pr->prev;
131 pending_rule_t *b = pr->next;
136 if (pr == pending_rule_head)
137 pending_rule_head = b;
138 if (pr == pending_rule_tail)
139 pending_rule_tail = a;
145 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
147 xcb_ewmh_get_atoms_reply_t win_type;
149 if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
150 for (unsigned int i = 0; i < win_type.atoms_len; i++) {
151 xcb_atom_t a = win_type.atoms[i];
152 if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
153 || a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
155 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
156 csq->floating = true;
157 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
159 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
163 xcb_ewmh_get_atoms_reply_wipe(&win_type);
166 xcb_ewmh_get_atoms_reply_t win_state;
168 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
169 for (unsigned int i = 0; i < win_state.atoms_len; i++) {
170 xcb_atom_t a = win_state.atoms[i];
171 if (a == ewmh->_NET_WM_STATE_FULLSCREEN)
172 csq->fullscreen = true;
173 else if (a == ewmh->_NET_WM_STATE_STICKY)
176 xcb_ewmh_get_atoms_reply_wipe(&win_state);
179 xcb_size_hints_t size_hints;
180 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
181 if (size_hints.min_width > 0 && size_hints.min_height > 0
182 && size_hints.min_width == size_hints.max_width
183 && size_hints.min_height == size_hints.max_height)
184 csq->floating = true;
187 xcb_window_t transient_for = XCB_NONE;
188 xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
189 if (transient_for != XCB_NONE)
190 csq->transient = csq->floating = true;
192 xcb_icccm_get_wm_class_reply_t reply;
193 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
194 snprintf(csq->class_name, sizeof(csq->class_name), "%s", reply.class_name);
195 snprintf(csq->instance_name, sizeof(csq->instance_name), "%s", reply.instance_name);
196 xcb_icccm_get_wm_class_reply_wipe(&reply);
199 rule_t *rule = rule_head;
200 while (rule != NULL) {
201 rule_t *next = rule->next;
202 if (streq(rule->cause, MATCH_ANY)
203 || streq(rule->cause, csq->class_name)
204 || streq(rule->cause, csq->instance_name)) {
206 snprintf(effect, sizeof(effect), "%s", rule->effect);
207 char *key = strtok(effect, CSQ_BLK);
208 char *value = strtok(NULL, CSQ_BLK);
209 while (key != NULL && value != NULL) {
210 parse_key_value(key, value, csq);
211 key = strtok(NULL, CSQ_BLK);
212 value = strtok(NULL, CSQ_BLK);
221 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
223 if (external_rules_command[0] == '\0')
231 close(xcb_get_file_descriptor(dpy));
235 snprintf(wid, sizeof(wid), "%i", win);
237 execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, NULL);
238 err("Couldn't spawn rule command.\n");
239 } else if (pid > 0) {
241 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
242 add_pending_rule(pr);
247 void parse_rule_consequence(int fd, rule_consequence_t *csq)
253 while ((nb = read(fd, data, sizeof(data))) > 0) {
254 int end = MIN(nb, (int) sizeof(data) - 1);
256 char *key = strtok(data, CSQ_BLK);
257 char *value = strtok(NULL, CSQ_BLK);
258 while (key != NULL && value != NULL) {
259 parse_key_value(key, value, csq);
260 key = strtok(NULL, CSQ_BLK);
261 value = strtok(NULL, CSQ_BLK);
266 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
269 if (streq("desktop", key)) {
270 snprintf(csq->desktop_desc, sizeof(csq->desktop_desc), "%s", value);
271 } else if (streq("monitor", key)) {
272 snprintf(csq->monitor_desc, sizeof(csq->monitor_desc), "%s", value);
273 } else if (parse_bool(value, &v)) {
274 if (streq("floating", key))
276 #define SETCSQ(name) \
277 else if (streq(#name, key)) \
293 void list_rules(char *pattern, char *rsp)
296 for (rule_t *r = rule_head; r != NULL; r = r->next) {
297 if (pattern != NULL && !streq(pattern, r->cause))
299 snprintf(line, sizeof(line), "%s => %s\n", r->cause, r->effect);
300 strncat(rsp, line, REMLEN(rsp));