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.
37 void query_monitors(coordinates_t loc, domain_t dom, FILE *rsp)
39 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
40 if (loc.monitor != NULL && m != loc.monitor)
42 if (dom != DOMAIN_DESKTOP) {
43 if (dom == DOMAIN_MONITOR) {
44 fprintf(rsp, "%s\n", m->name);
47 fprintf(rsp, "%s %ux%u%+i%+i %i,%i,%i,%i%s\n", m->name,
48 m->rectangle.width,m->rectangle.height, m->rectangle.x, m->rectangle.y,
49 m->top_padding, m->right_padding, m->bottom_padding, m->left_padding,
50 (m == mon ? " *" : ""));
53 query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
57 void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, FILE *rsp)
59 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
60 if (loc.desktop != NULL && d != loc.desktop)
62 for (unsigned int i = 0; i < depth; i++)
64 if (dom == DOMAIN_DESKTOP) {
65 fprintf(rsp, "%s\n", d->name);
68 fprintf(rsp, "%s %u %i %i,%i,%i,%i %c%s\n", d->name, d->border_width,
70 d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
71 (d->layout == LAYOUT_TILED ? 'T' : 'M'),
72 (d == m->desk ? " *" : ""));
74 query_tree(d, d->root, rsp, depth + 1);
78 void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
83 for (unsigned int i = 0; i < depth; i++)
87 client_t *c = n->client;
88 fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c%c %c%c %c%c%c%c%s\n",
89 (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')),
90 c->class_name, c->instance_name, c->window, c->border_width,
91 c->floating_rectangle.width, c->floating_rectangle.height,
92 c->floating_rectangle.x, c->floating_rectangle.y,
93 (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))),
94 (n->split_mode == MODE_AUTOMATIC ? '-' : 'p'),
95 (c->state == STATE_TILED ? '-' : (c->state == STATE_FLOATING ? 'f' : (c->state == STATE_FULLSCREEN ? 'F' : 'p'))),
96 (c->layer == LAYER_NORMAL ? '-' : (c->layer == LAYER_ABOVE ? 'a' : 'b')),
97 (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'), (c->private ? 'i' : '-'),
98 (n == d->focus ? " *" : ""));
100 fprintf(rsp, "%c %c %lf\n", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'),
101 (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
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, FILE *rsp)
110 for (history_t *h = history_head; h != NULL; h = h->next) {
111 if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
112 || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
114 xcb_window_t win = XCB_NONE;
115 if (h->loc.node != NULL)
116 win = h->loc.node->client->window;
117 fprintf(rsp, "%s %s 0x%X\n", h->loc.monitor->name, h->loc.desktop->name, win);
121 void query_stack(FILE *rsp)
123 for (stacking_list_t *s = stack_head; s != NULL; s = s->next)
124 fprintf(rsp, "0x%X\n", s->node->client->window);
127 void query_windows(coordinates_t loc, FILE *rsp)
129 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
130 if (loc.monitor != NULL && m != loc.monitor)
132 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
133 if (loc.desktop != NULL && d != loc.desktop)
135 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
136 if (loc.node != NULL && n != loc.node)
138 fprintf(rsp, "0x%X\n", n->client->window);
144 client_select_t make_client_select(void)
146 client_select_t sel = {
164 desktop_select_t make_desktop_select(void)
166 desktop_select_t sel = {
174 void cleanup_client_select(client_select_t *sel)
179 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
181 client_select_t sel = make_client_select();
183 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
186 if (streq("tiled", tok)) {
187 sel.tiled = OPTION_TRUE;
188 } else if (streq("!tiled", tok)) {
189 sel.tiled = OPTION_FALSE;
190 } else if (streq("pseudo_tiled", tok)) {
191 sel.pseudo_tiled = OPTION_TRUE;
192 } else if (streq("!pseudo_tiled", tok)) {
193 sel.pseudo_tiled = OPTION_FALSE;
194 } else if (streq("floating", tok)) {
195 sel.floating = OPTION_TRUE;
196 } else if (streq("!floating", tok)) {
197 sel.floating = OPTION_FALSE;
198 } else if (streq("same_class", tok)) {
199 sel.same_class = OPTION_TRUE;
200 } else if (streq("!same_class", tok)) {
201 sel.same_class = OPTION_FALSE;
202 } else if (streq("automatic", tok)) {
203 sel.automatic = OPTION_TRUE;
204 } else if (streq("!automatic", tok)) {
205 sel.automatic = OPTION_FALSE;
206 } else if (streq("fullscreen", tok)) {
207 sel.fullscreen = OPTION_TRUE;
208 } else if (streq("!fullscreen", tok)) {
209 sel.fullscreen = OPTION_FALSE;
210 } else if (streq("urgent", tok)) {
211 sel.urgent = OPTION_TRUE;
212 } else if (streq("!urgent", tok)) {
213 sel.urgent = OPTION_FALSE;
214 } else if (streq("local", tok)) {
215 sel.local = OPTION_TRUE;
216 } else if (streq("!local", tok)) {
217 sel.local = OPTION_FALSE;
218 } else if (streq("private", tok)) {
219 sel.private = OPTION_TRUE;
220 } else if (streq("!private", tok)) {
221 sel.private = OPTION_FALSE;
222 } else if (streq("sticky", tok)) {
223 sel.sticky = OPTION_TRUE;
224 } else if (streq("!sticky", tok)) {
225 sel.sticky = OPTION_FALSE;
226 } else if (streq("locked", tok)) {
227 sel.locked = OPTION_TRUE;
228 } else if (streq("!locked", tok)) {
229 sel.locked = OPTION_FALSE;
230 } else if (streq("focused", tok)) {
231 sel.focused = OPTION_TRUE;
232 } else if (streq("!focused", tok)) {
233 sel.focused = OPTION_FALSE;
234 } else if (streq("below", tok)) {
235 if (sel.layer == NULL) {
236 sel.layer = malloc(sizeof(stack_layer_t));
238 *(sel.layer) = LAYER_BELOW;
239 } else if (streq("normal", tok)) {
240 if (sel.layer == NULL) {
241 sel.layer = malloc(sizeof(stack_layer_t));
243 *(sel.layer) = LAYER_NORMAL;
244 } else if (streq("above", tok)) {
245 if (sel.layer == NULL) {
246 sel.layer = malloc(sizeof(stack_layer_t));
248 *(sel.layer) = LAYER_ABOVE;
252 dst->monitor = ref->monitor;
253 dst->desktop = ref->desktop;
259 if (parse_direction(desc, &dir)) {
260 dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
261 if (dst->node == NULL && num_monitors > 1) {
262 monitor_t *m = nearest_monitor(ref->monitor, dir, make_desktop_select());
264 coordinates_t loc = {m, m->desk, m->desk->focus};
265 if (node_matches(&loc, ref, sel)) {
267 dst->desktop = m->desk;
268 dst->node = m->desk->focus;
272 } else if (parse_cycle_direction(desc, &cyc)) {
273 dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
274 } else if (parse_history_direction(desc, &hdi)) {
275 history_find_node(hdi, ref, dst, sel);
276 } else if (streq("last", desc)) {
277 history_find_node(HISTORY_OLDER, ref, dst, sel);
278 } else if (streq("biggest", desc)) {
279 dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
280 } else if (streq("focused", desc)) {
281 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
282 if (node_matches(&loc, ref, sel)) {
284 dst->desktop = mon->desk;
285 dst->node = mon->desk->focus;
289 if (parse_window_id(desc, &wid))
290 locate_window(wid, dst);
293 cleanup_client_select(&sel);
295 return (dst->node != NULL);
298 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
300 desktop_select_t sel = make_desktop_select();
302 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
305 if (streq("occupied", tok)) {
306 sel.occupied = OPTION_FALSE;
307 } else if (streq("!occupied", tok)) {
308 sel.occupied = OPTION_TRUE;
309 } else if (streq("urgent", tok)) {
310 sel.urgent = OPTION_TRUE;
311 } else if (streq("!urgent", tok)) {
312 sel.urgent = OPTION_FALSE;
313 } else if (streq("local", tok)) {
314 sel.local = OPTION_TRUE;
315 } else if (streq("!local", tok)) {
316 sel.local = OPTION_FALSE;
326 if (parse_cycle_direction(desc, &cyc)) {
327 dst->monitor = ref->monitor;
328 dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
329 } else if (parse_history_direction(desc, &hdi)) {
330 history_find_desktop(hdi, ref, dst, sel);
331 } else if (streq("last", desc)) {
332 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
333 } else if (streq("focused", desc)) {
334 coordinates_t loc = {mon, mon->desk, NULL};
335 if (desktop_matches(&loc, ref, sel)) {
337 dst->desktop = mon->desk;
339 } else if ((colon = strchr(desc, ':')) != NULL) {
341 if (monitor_from_desc(desc, ref, dst)) {
342 if (streq("focused", colon + 1)) {
343 dst->desktop = dst->monitor->desk;
344 } else if (parse_index(colon + 1, &idx)) {
345 desktop_from_index(idx, dst, dst->monitor);
348 } else if (parse_index(desc, &idx)) {
349 desktop_from_index(idx, dst, NULL);
351 locate_desktop(desc, dst);
354 return (dst->desktop != NULL);
357 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
359 desktop_select_t sel = make_desktop_select();
361 while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
364 if (streq("occupied", tok)) {
365 sel.occupied = OPTION_FALSE;
366 } else if (streq("!occupied", tok)) {
367 sel.occupied = OPTION_TRUE;
377 if (parse_direction(desc, &dir)) {
378 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
379 } else if (parse_cycle_direction(desc, &cyc)) {
380 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
381 } else if (parse_history_direction(desc, &hdi)) {
382 history_find_monitor(hdi, ref, dst, sel);
383 } else if (streq("last", desc)) {
384 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
385 } else if (streq("primary", desc)) {
386 if (pri_mon != NULL) {
387 coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
388 if (desktop_matches(&loc, ref, sel))
389 dst->monitor = pri_mon;
391 } else if (streq("focused", desc)) {
392 coordinates_t loc = {mon, mon->desk, NULL};
393 if (desktop_matches(&loc, ref, sel))
395 } else if (parse_index(desc, &idx)) {
396 monitor_from_index(idx, dst);
398 locate_monitor(desc, dst);
401 return (dst->monitor != NULL);
404 bool locate_window(xcb_window_t win, coordinates_t *loc)
406 for (monitor_t *m = mon_head; m != NULL; m = m->next)
407 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
408 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
409 if (n->client->window == win) {
418 bool locate_desktop(char *name, coordinates_t *loc)
420 for (monitor_t *m = mon_head; m != NULL; m = m->next)
421 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
422 if (streq(d->name, name)) {
430 bool locate_monitor(char *name, coordinates_t *loc)
432 for (monitor_t *m = mon_head; m != NULL; m = m->next)
433 if (streq(m->name, name)) {
440 bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
442 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
443 if (mm != NULL && m != mm)
445 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
456 bool monitor_from_index(int i, coordinates_t *loc)
458 for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
468 bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
470 if (loc->node == NULL)
473 #define WSTATE(prop) \
474 if (sel.prop != OPTION_NONE && \
475 !loc->node->client->prop \
476 ? sel.prop == OPTION_TRUE \
477 : sel.prop == OPTION_FALSE) { \
486 if (sel.tiled != OPTION_NONE &&
487 loc->node->client->state != STATE_TILED
488 ? sel.tiled == OPTION_TRUE
489 : sel.tiled == OPTION_FALSE) {
493 if (sel.pseudo_tiled != OPTION_NONE &&
494 loc->node->client->state != STATE_PSEUDO_TILED
495 ? sel.pseudo_tiled == OPTION_TRUE
496 : sel.pseudo_tiled == OPTION_FALSE) {
500 if (sel.floating != OPTION_NONE &&
501 loc->node->client->state != STATE_FLOATING
502 ? sel.floating == OPTION_TRUE
503 : sel.floating == OPTION_FALSE) {
507 if (sel.fullscreen != OPTION_NONE &&
508 loc->node->client->state != STATE_FULLSCREEN
509 ? sel.fullscreen == OPTION_TRUE
510 : sel.fullscreen == OPTION_FALSE) {
514 if (sel.same_class != OPTION_NONE && ref->node != NULL &&
515 streq(loc->node->client->class_name, ref->node->client->class_name)
516 ? sel.same_class == OPTION_FALSE
517 : sel.same_class == OPTION_TRUE) {
521 if (sel.automatic != OPTION_NONE &&
522 loc->node->split_mode == MODE_MANUAL
523 ? sel.automatic == OPTION_TRUE
524 : sel.automatic == OPTION_FALSE) {
528 if (sel.local != OPTION_NONE &&
529 loc->desktop != ref->desktop
530 ? sel.local == OPTION_TRUE
531 : sel.local == OPTION_FALSE) {
535 if (sel.layer != NULL && loc->node->client->layer != *(sel.layer)) {
539 if (sel.focused != OPTION_NONE &&
540 loc->node != loc->desktop->focus
541 ? sel.focused == OPTION_TRUE
542 : sel.focused == OPTION_FALSE) {
549 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
551 if (sel.occupied != OPTION_NONE &&
552 loc->desktop->root == NULL
553 ? sel.occupied == OPTION_TRUE
554 : sel.occupied == OPTION_FALSE) {
558 if (sel.urgent != OPTION_NONE &&
559 !is_urgent(loc->desktop)
560 ? sel.urgent == OPTION_TRUE
561 : sel.urgent == OPTION_FALSE) {
565 if (sel.local != OPTION_NONE &&
566 ref->monitor != loc->monitor
567 ? sel.local == OPTION_TRUE
568 : sel.local == OPTION_FALSE) {