1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 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
20 * ON 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 void query_monitors(coordinates_t loc, domain_t dom, FILE *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 fprintf(rsp, "%s\n", m->name);
46 fprintf(rsp, "%s %ux%u%+i%+i %i,%i,%i,%i%s\n", m->name,
47 m->rectangle.width,m->rectangle.height, m->rectangle.x, m->rectangle.y,
48 m->top_padding, m->right_padding, m->bottom_padding, m->left_padding,
49 (m == mon ? " *" : ""));
52 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
56 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
58 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
59 if (loc.desktop != NULL && d != loc.desktop)
61 for (unsigned int i = 0; i < depth; i++)
63 if (dom == DOMAIN_DESKTOP) {
64 fprintf(rsp, "%s\n", d->name);
67 fprintf(rsp, "%s %u %i %i,%i,%i,%i %c %c%s\n", d->name, d->border_width,
69 d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
70 (d->layout == LAYOUT_TILED ? 'T' : 'M'), (d->floating ? 'f' : '-'),
71 (d == m->desk ? " *" : ""));
73 query_tree(d, d->root, rsp, depth + 1);
77 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
82 for (unsigned int i = 0; i < depth; i++)
86 client_t *c = n->client;
87 fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c %c%c%c%c%c%c%c%c%s\n",
88 (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')),
89 c->class_name, c->instance_name, c->window, c->border_width,
90 c->floating_rectangle.width, c->floating_rectangle.height,
91 c->floating_rectangle.x, c->floating_rectangle.y,
92 (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))),
93 (c->floating ? 'f' : '-'), (c->pseudo_tiled ? 'd' : '-'), (c->fullscreen ? 'F' : '-'),
94 (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'),
95 (c->private ? 'i' : '-'), (n->split_mode ? 'p' : '-'),
96 (n == d->focus ? " *" : ""));
98 fprintf(rsp, "%c %c %lf\n", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'),
99 (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
102 query_tree(d, n->first_child, rsp, depth + 1);
103 query_tree(d, n->second_child, rsp, depth + 1);
106 void query_history(coordinates_t loc, FILE *rsp)
108 for (history_t *h = history_head; h != NULL; h = h->next) {
109 if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
110 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
112 xcb_window_t win = XCB_NONE;
113 if (h->loc.node != NULL)
114 win = h->loc.node->client->window;
115 fprintf(rsp, "%s %s 0x%X\n", h->loc.monitor->name, h->loc.desktop->name, win);
119 void query_stack(FILE *rsp)
121 for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
122 fprintf(rsp, "0x%X\n", s->node->client->window);
125 void query_windows(coordinates_t loc, FILE *rsp)
127 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
128 if (loc.monitor != NULL && m != loc.monitor)
130 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
131 if (loc.desktop != NULL && d != loc.desktop)
133 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
134 if (loc.node != NULL && n != loc.node)
136 fprintf(rsp, "0x%X\n", n->client->window);
142 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
144 client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, CLIENT_MODE_ALL, false, false, false};
146 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
149 if (streq("tiled", tok)) {
150 sel.type = CLIENT_TYPE_TILED;
151 } else if (streq("floating", tok)) {
152 sel.type = CLIENT_TYPE_FLOATING;
153 } else if (streq("like", tok)) {
154 sel.class = CLIENT_CLASS_EQUAL;
155 } else if (streq("unlike", tok)) {
156 sel.class = CLIENT_CLASS_DIFFER;
157 } else if (streq("manual", tok)) {
158 sel.mode = CLIENT_MODE_MANUAL;
159 } else if (streq("automatic", tok)) {
160 sel.mode = CLIENT_MODE_AUTOMATIC;
161 } else if (streq("urgent", tok)) {
163 } else if (streq("local", tok)) {
165 } else if (streq("unfocused", tok)) {
166 sel.unfocused = true;
170 dst->monitor = ref->monitor;
171 dst->desktop = ref->desktop;
177 if (parse_direction(desc, &dir)) {
178 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
179 if (dst->node == NULL && num_monitors > 1) {
180 monitor_t *m = nearest_monitor(ref->monitor, dir, (desktop_select_t) {DESKTOP_STATUS_ALL, false, false});
182 coordinates_t loc = {m, m->desk, m->desk->focus};
183 if (node_matches(&loc, ref, sel)) {
185 dst->desktop = m->desk;
186 dst->node = m->desk->focus;
190 } else if (parse_cycle_direction(desc, &cyc)) {
191 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
192 } else if (parse_history_direction(desc, &hdi)) {
193 history_find_node(hdi, ref, dst, sel);
194 } else if (streq("last", desc)) {
195 history_find_node(HISTORY_OLDER, ref, dst, sel);
196 } else if (streq("biggest", desc)) {
197 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
198 } else if (streq("focused", desc)) {
199 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
200 if (node_matches(&loc, ref, sel)) {
202 dst->desktop = mon->desk;
203 dst->node = mon->desk->focus;
207 if (parse_window_id(desc, &wid))
208 locate_window(wid, dst);
211 return (dst->node != NULL);
214 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
216 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
218 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
221 if (streq("free", tok)) {
222 sel.status = DESKTOP_STATUS_FREE;
223 } else if (streq("occupied", tok)) {
224 sel.status = DESKTOP_STATUS_OCCUPIED;
225 } else if (streq("urgent", tok)) {
227 } else if (streq("local", tok)) {
238 if (parse_cycle_direction(desc, &cyc)) {
239 dst->monitor = ref->monitor;
240 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
241 } else if (parse_history_direction(desc, &hdi)) {
242 history_find_desktop(hdi, ref, dst, sel);
243 } else if (streq("last", desc)) {
244 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
245 } else if (streq("focused", desc)) {
246 coordinates_t loc = {mon, mon->desk, NULL};
247 if (desktop_matches(&loc, ref, sel)) {
249 dst->desktop = mon->desk;
251 } else if ((colon = strchr(desc, ':')) != NULL) {
253 if (monitor_from_desc(desc, ref, dst)) {
254 if (streq("focused", colon + 1)) {
255 dst->desktop = dst->monitor->desk;
256 } else if (parse_index(colon + 1, &idx)) {
257 desktop_from_index(idx, dst, dst->monitor);
260 } else if (parse_index(desc, &idx)) {
261 desktop_from_index(idx, dst, NULL);
263 locate_desktop(desc, dst);
266 return (dst->desktop != NULL);
269 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
271 desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
273 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
276 if (streq("free", tok)) {
277 sel.status = DESKTOP_STATUS_FREE;
278 } else if (streq("occupied", tok)) {
279 sel.status = DESKTOP_STATUS_OCCUPIED;
289 if (parse_direction(desc, &dir)) {
290 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
291 } else if (parse_cycle_direction(desc, &cyc)) {
292 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
293 } else if (parse_history_direction(desc, &hdi)) {
294 history_find_monitor(hdi, ref, dst, sel);
295 } else if (streq("last", desc)) {
296 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
297 } else if (streq("primary", desc)) {
298 if (pri_mon != NULL) {
299 coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
300 if (desktop_matches(&loc, ref, sel))
301 dst->monitor = pri_mon;
303 } else if (streq("focused", desc)) {
304 coordinates_t loc = {mon, mon->desk, NULL};
305 if (desktop_matches(&loc, ref, sel))
307 } else if (parse_index(desc, &idx)) {
308 monitor_from_index(idx, dst);
310 locate_monitor(desc, dst);
313 return (dst->monitor != NULL);
316 bool locate_window(xcb_window_t win, coordinates_t *loc)
318 for (monitor_t *m = mon_head; m != NULL; m = m->next)
319 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
320 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
321 if (n->client->window == win) {
330 bool locate_desktop(char *name, coordinates_t *loc)
332 for (monitor_t *m = mon_head; m != NULL; m = m->next)
333 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
334 if (streq(d->name, name)) {
342 bool locate_monitor(char *name, coordinates_t *loc)
344 for (monitor_t *m = mon_head; m != NULL; m = m->next)
345 if (streq(m->name, name)) {
352 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
354 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
355 if (mm != NULL && m != mm)
357 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
368 bool monitor_from_index(int i, coordinates_t *loc)
370 for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
380 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
382 if (loc->node == NULL)
385 if (sel.type != CLIENT_TYPE_ALL &&
386 is_tiled(loc->node->client)
387 ? sel.type == CLIENT_TYPE_FLOATING
388 : sel.type == CLIENT_TYPE_TILED)
391 if (sel.class != CLIENT_CLASS_ALL && ref->node != NULL &&
392 streq(loc->node->client->class_name, ref->node->client->class_name)
393 ? sel.class == CLIENT_CLASS_DIFFER
394 : sel.class == CLIENT_CLASS_EQUAL)
397 if (sel.mode != CLIENT_MODE_ALL &&
398 loc->node->split_mode == MODE_MANUAL
399 ? sel.mode == CLIENT_MODE_AUTOMATIC
400 : sel.mode == CLIENT_MODE_MANUAL)
403 if (sel.local && loc->desktop != ref->desktop)
406 if (sel.urgent && !loc->node->client->urgent)
409 if (sel.unfocused && loc->node == mon->desk->focus)
415 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
417 if (sel.status != DESKTOP_STATUS_ALL &&
418 loc->desktop->root == NULL
419 ? sel.status == DESKTOP_STATUS_OCCUPIED
420 : sel.status == DESKTOP_STATUS_FREE)
423 if (sel.urgent && !is_urgent(loc->desktop))
426 if (sel.local && ref->monitor != loc->monitor)