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 void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
38 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
39 if (loc.monitor != NULL && m != loc.monitor)
41 if (dom != DOMAIN_DESKTOP) {
42 if (dom == DOMAIN_MONITOR) {
43 snprintf(line, sizeof(line), "%s\n", m->name);
44 strncat(rsp, line, REMLEN(rsp));
47 snprintf(line, sizeof(line), "%s %ux%u%+i%+i %i,%i,%i,%i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y, m->top_padding, m->right_padding, m->bottom_padding, m->left_padding);
48 strncat(rsp, line, REMLEN(rsp));
50 strncat(rsp, " *", REMLEN(rsp));
51 strncat(rsp, "\n", REMLEN(rsp));
54 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
58 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, char *rsp)
61 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
62 if (loc.desktop != NULL && d != loc.desktop)
64 for (unsigned int i = 0; i < depth; i++)
65 strncat(rsp, " ", REMLEN(rsp));
66 if (dom == DOMAIN_DESKTOP) {
67 snprintf(line, sizeof(line), "%s\n", d->name);
68 strncat(rsp, line, REMLEN(rsp));
71 snprintf(line, sizeof(line), "%s %u %i %c %c", d->name, d->border_width, d->window_gap, (d->layout == LAYOUT_TILED ? 'T' : 'M'), (d->floating ? 'f' : '-'));
72 strncat(rsp, line, REMLEN(rsp));
74 strncat(rsp, " *", REMLEN(rsp));
75 strncat(rsp, "\n", REMLEN(rsp));
77 query_tree(d, d->root, rsp, depth + 1);
81 void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
88 for (unsigned int i = 0; i < depth; i++)
89 strncat(rsp, " ", REMLEN(rsp));
92 client_t *c = n->client;
93 snprintf(line, sizeof(line), "%c %s 0x%X %u %ux%u%+i%+i %c %c%c%c%c%c%c%c%c%c", (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), c->class_name, c->window, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))), (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'), (c->frame ? 'e' : '-'), (c->private ? 'i' : '-'), (n->split_mode ? 'p' : '-'));
95 snprintf(line, sizeof(line), "%c %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
98 strncat(rsp, line, REMLEN(rsp));
101 strncat(rsp, " *", REMLEN(rsp));
102 strncat(rsp, "\n", REMLEN(rsp));
104 query_tree(d, n->first_child, rsp, depth + 1);
105 query_tree(d, n->second_child, rsp, depth + 1);
108 void query_history(coordinates_t loc, char *rsp)
111 for (history_t *h = history_head; h != NULL; h = h->next) {
112 if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
113 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
115 xcb_window_t win = XCB_NONE;
116 if (h->loc.node != NULL)
117 win = h->loc.node->client->window;
118 snprintf(line, sizeof(line), "%s %s 0x%X", h->loc.monitor->name, h->loc.desktop->name, win);
119 strncat(rsp, line, REMLEN(rsp));
120 strncat(rsp, "\n", REMLEN(rsp));
124 void query_stack(char *rsp)
127 for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
128 snprintf(line, sizeof(line), "0x%X", s->node->client->window);
129 strncat(rsp, line, REMLEN(rsp));
130 strncat(rsp, "\n", REMLEN(rsp));
134 void query_windows(coordinates_t loc, char *rsp)
138 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
139 if (loc.monitor != NULL && m != loc.monitor)
141 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
142 if (loc.desktop != NULL && d != loc.desktop)
144 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
145 if (loc.node != NULL && n != loc.node)
147 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
148 strncat(rsp, line, REMLEN(rsp));
154 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
156 client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, false, false, false};
158 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
161 if (streq("tiled", tok)) {
162 sel.type = CLIENT_TYPE_TILED;
163 } else if (streq("floating", tok)) {
164 sel.type = CLIENT_TYPE_FLOATING;
165 } else if (streq("like", tok)) {
166 sel.class = CLIENT_CLASS_EQUAL;
167 } else if (streq("unlike", tok)) {
168 sel.class = CLIENT_CLASS_DIFFER;
169 } else if (streq("urgent", tok)) {
171 } else if (streq("manual", tok)) {
173 } else if (streq("local", tok)) {
178 dst->monitor = ref->monitor;
179 dst->desktop = ref->desktop;
185 if (parse_direction(desc, &dir)) {
186 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
187 } else if (parse_cycle_direction(desc, &cyc)) {
188 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
189 } else if (parse_history_direction(desc, &hdi)) {
190 history_find_node(hdi, ref, dst, sel);
191 } else if (streq("last", desc)) {
192 history_find_node(HISTORY_OLDER, ref, dst, sel);
193 } else if (streq("biggest", desc)) {
194 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
195 } else if (streq("focused", desc)) {
196 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
197 if (node_matches(&loc, ref, sel)) {
199 dst->desktop = mon->desk;
200 dst->node = mon->desk->focus;
204 if (parse_window_id(desc, &wid))
205 locate_window(wid, dst);
208 return (dst->node != NULL);
211 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
213 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
215 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
218 if (streq("free", tok)) {
219 sel.status = DESKTOP_STATUS_FREE;
220 } else if (streq("occupied", tok)) {
221 sel.status = DESKTOP_STATUS_OCCUPIED;
222 } else if (streq("urgent", tok)) {
224 } else if (streq("local", tok)) {
234 if (parse_cycle_direction(desc, &cyc)) {
235 dst->monitor = ref->monitor;
236 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
237 } else if (parse_history_direction(desc, &hdi)) {
238 history_find_desktop(hdi, ref, dst, sel);
239 } else if (streq("last", desc)) {
240 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
241 } else if (streq("focused", desc)) {
242 coordinates_t loc = {mon, mon->desk, NULL};
243 if (desktop_matches(&loc, ref, sel)) {
245 dst->desktop = mon->desk;
247 } else if (parse_index(desc, &idx)) {
248 desktop_from_index(idx, dst);
250 locate_desktop(desc, dst);
253 return (dst->desktop != NULL);
256 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
258 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
260 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
263 if (streq("free", tok)) {
264 sel.status = DESKTOP_STATUS_FREE;
265 } else if (streq("occupied", tok)) {
266 sel.status = DESKTOP_STATUS_OCCUPIED;
276 if (parse_direction(desc, &dir)) {
277 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
278 } else if (parse_cycle_direction(desc, &cyc)) {
279 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
280 } else if (parse_history_direction(desc, &hdi)) {
281 history_find_monitor(hdi, ref, dst, sel);
282 } else if (streq("last", desc)) {
283 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
284 } else if (streq("primary", desc)) {
285 if (pri_mon != NULL) {
286 coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
287 if (desktop_matches(&loc, ref, sel))
288 dst->monitor = pri_mon;
290 } else if (streq("focused", desc)) {
291 coordinates_t loc = {mon, mon->desk, NULL};
292 if (desktop_matches(&loc, ref, sel))
294 } else if (parse_index(desc, &idx)) {
295 monitor_from_index(idx, dst);
297 locate_monitor(desc, dst);
300 return (dst->monitor != NULL);
303 bool locate_window(xcb_window_t win, coordinates_t *loc)
305 for (monitor_t *m = mon_head; m != NULL; m = m->next)
306 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
307 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
308 if (n->client->window == win) {
317 bool locate_desktop(char *name, coordinates_t *loc)
319 for (monitor_t *m = mon_head; m != NULL; m = m->next)
320 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
321 if (streq(d->name, name)) {
329 bool locate_monitor(char *name, coordinates_t *loc)
331 for (monitor_t *m = mon_head; m != NULL; m = m->next)
332 if (streq(m->name, name)) {
339 bool desktop_from_index(int i, coordinates_t *loc)
341 for (monitor_t *m = mon_head; m != NULL; m = m->next)
342 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
352 bool monitor_from_index(int i, coordinates_t *loc)
354 for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
364 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
366 if (sel.type != CLIENT_TYPE_ALL &&
367 is_tiled(loc->node->client)
368 ? sel.type == CLIENT_TYPE_FLOATING
369 : sel.type == CLIENT_TYPE_TILED)
372 if (sel.class != CLIENT_CLASS_ALL &&
373 streq(loc->node->client->class_name, ref->node->client->class_name)
374 ? sel.class == CLIENT_CLASS_DIFFER
375 : sel.class == CLIENT_CLASS_EQUAL)
378 if (sel.manual && loc->node->split_mode != MODE_MANUAL)
381 if (sel.local && loc->desktop != ref->desktop)
384 if (sel.urgent && !loc->node->client->urgent)
390 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
392 if (sel.status != DESKTOP_STATUS_ALL &&
393 loc->desktop->root == NULL
394 ? sel.status == DESKTOP_STATUS_OCCUPIED
395 : sel.status == DESKTOP_STATUS_FREE)
398 if (sel.urgent && !is_urgent(loc->desktop))
401 if (sel.local && ref->monitor != loc->monitor)