]> git.lizzy.rs Git - bspwm.git/blob - src/query.c
b11662adad394d06486ead9a4f6ba0133dec81ab
[bspwm.git] / src / query.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "bspwm.h"
29 #include "desktop.h"
30 #include "history.h"
31 #include "parse.h"
32 #include "monitor.h"
33 #include "window.h"
34 #include "tree.h"
35 #include "query.h"
36 #include "geometry.h"
37
38 void query_state(FILE *rsp)
39 {
40         fprintf(rsp, "{");
41         fprintf(rsp, "\"focusedMonitorId\":%u,", mon->id);
42         if (pri_mon != NULL) {
43                 fprintf(rsp, "\"primaryMonitorId\":%u,", pri_mon->id);
44         }
45         fprintf(rsp, "\"clientsCount\":%i,", clients_count);
46         fprintf(rsp, "\"monitors\":");
47         fprintf(rsp, "[");
48         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
49                 query_monitor(m, rsp);
50                 if (m->next != NULL) {
51                         fprintf(rsp, ",");
52                 }
53         }
54         fprintf(rsp, "]");
55         fprintf(rsp,",");
56         fprintf(rsp, "\"focusHistory\":");
57         query_history(rsp);
58         fprintf(rsp,",");
59         fprintf(rsp, "\"stackingList\":");
60         query_stack(rsp);
61         if (restart) {
62                 fprintf(rsp,",");
63                 fprintf(rsp, "\"eventSubscribers\":");
64                 query_subscribers(rsp);
65         }
66         fprintf(rsp, "}");
67 }
68
69 void query_monitor(monitor_t *m, FILE *rsp)
70 {
71         fprintf(rsp, "{");
72         fprintf(rsp, "\"name\":\"%s\",", m->name);
73         fprintf(rsp, "\"id\":%u,", m->id);
74         fprintf(rsp, "\"randrId\":%u,", m->randr_id);
75         fprintf(rsp, "\"wired\":%s,", BOOL_STR(m->wired));
76         fprintf(rsp, "\"stickyCount\":%i,", m->sticky_count);
77         fprintf(rsp, "\"windowGap\":%i,", m->window_gap);
78         fprintf(rsp, "\"borderWidth\":%u,", m->border_width);
79         fprintf(rsp, "\"focusedDesktopId\":%u,", m->desk->id);
80         fprintf(rsp, "\"padding\":");
81         query_padding(m->padding, rsp);
82         fprintf(rsp,",");
83         fprintf(rsp, "\"rectangle\":");
84         query_rectangle(m->rectangle, rsp);
85         fprintf(rsp,",");
86         fprintf(rsp, "\"desktops\":");
87         fprintf(rsp, "[");
88         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
89                 query_desktop(d, rsp);
90                 if (d->next != NULL) {
91                         fprintf(rsp,",");
92                 }
93         }
94         fprintf(rsp, "]");
95         fprintf(rsp, "}");
96 }
97
98 void query_desktop(desktop_t *d, FILE *rsp)
99 {
100         fprintf(rsp, "{");
101         fprintf(rsp, "\"name\":\"%s\",", d->name);
102         fprintf(rsp, "\"id\":%u,", d->id);
103         fprintf(rsp, "\"layout\":\"%s\",", LAYOUT_STR(d->layout));
104         fprintf(rsp, "\"userLayout\":\"%s\",", LAYOUT_STR(d->user_layout));
105         fprintf(rsp, "\"windowGap\":%i,", d->window_gap);
106         fprintf(rsp, "\"borderWidth\":%u,", d->border_width);
107         fprintf(rsp, "\"focusedNodeId\":%u,", d->focus != NULL ? d->focus->id : 0);
108         fprintf(rsp, "\"padding\":");
109         query_padding(d->padding, rsp);
110         fprintf(rsp,",");
111         fprintf(rsp, "\"root\":");
112         query_node(d->root, rsp);
113         fprintf(rsp, "}");
114 }
115
116 void query_node(node_t *n, FILE *rsp)
117 {
118         if (n == NULL) {
119                 fprintf(rsp, "null");
120         } else {
121                 fprintf(rsp, "{");
122                 fprintf(rsp, "\"id\":%u,", n->id);
123                 fprintf(rsp, "\"splitType\":\"%s\",", SPLIT_TYPE_STR(n->split_type));
124                 fprintf(rsp, "\"splitRatio\":%lf,", n->split_ratio);
125                 fprintf(rsp, "\"vacant\":%s,", BOOL_STR(n->vacant));
126                 fprintf(rsp, "\"hidden\":%s,", BOOL_STR(n->hidden));
127                 fprintf(rsp, "\"sticky\":%s,", BOOL_STR(n->sticky));
128                 fprintf(rsp, "\"private\":%s,", BOOL_STR(n->private));
129                 fprintf(rsp, "\"locked\":%s,", BOOL_STR(n->locked));
130                 fprintf(rsp, "\"marked\":%s,", BOOL_STR(n->marked));
131                 fprintf(rsp, "\"presel\":");
132                 query_presel(n->presel, rsp);
133                 fprintf(rsp,",");
134                 fprintf(rsp, "\"rectangle\":");
135                 query_rectangle(n->rectangle, rsp);
136                 fprintf(rsp,",");
137                 fprintf(rsp, "\"constraints\":");
138                 query_constraints(n->constraints, rsp);
139                 fprintf(rsp,",");
140                 fprintf(rsp, "\"firstChild\":");
141                 query_node(n->first_child, rsp);
142                 fprintf(rsp,",");
143                 fprintf(rsp, "\"secondChild\":");
144                 query_node(n->second_child, rsp);
145                 fprintf(rsp,",");
146                 fprintf(rsp, "\"client\":");
147                 query_client(n->client, rsp);
148                 fprintf(rsp, "}");
149         }
150 }
151
152 void query_presel(presel_t *p, FILE *rsp)
153 {
154         if (p == NULL) {
155                 fprintf(rsp, "null");
156         } else {
157                 fprintf(rsp, "{\"splitDir\":\"%s\",\"splitRatio\":%lf}", SPLIT_DIR_STR(p->split_dir), p->split_ratio);
158         }
159 }
160
161 void query_client(client_t *c, FILE *rsp)
162 {
163         if (c == NULL) {
164                 fprintf(rsp, "null");
165         } else {
166                 fprintf(rsp, "{");
167                 fprintf(rsp, "\"className\":\"%s\",", c->class_name);
168                 fprintf(rsp, "\"instanceName\":\"%s\",", c->instance_name);
169                 fprintf(rsp, "\"borderWidth\":%u,", c->border_width);
170                 fprintf(rsp, "\"state\":\"%s\",", STATE_STR(c->state));
171                 fprintf(rsp, "\"lastState\":\"%s\",", STATE_STR(c->last_state));
172                 fprintf(rsp, "\"layer\":\"%s\",", LAYER_STR(c->layer));
173                 fprintf(rsp, "\"lastLayer\":\"%s\",", LAYER_STR(c->last_layer));
174                 fprintf(rsp, "\"urgent\":%s,", BOOL_STR(c->urgent));
175                 fprintf(rsp, "\"shown\":%s,", BOOL_STR(c->shown));
176                 fprintf(rsp, "\"tiledRectangle\":");
177                 query_rectangle(c->tiled_rectangle, rsp);
178                 fprintf(rsp,",");
179                 fprintf(rsp, "\"floatingRectangle\":");
180                 query_rectangle(c->floating_rectangle, rsp);
181                 fprintf(rsp, "}");
182         }
183 }
184
185 void query_rectangle(xcb_rectangle_t r, FILE *rsp)
186 {
187         fprintf(rsp, "{\"x\":%i,\"y\":%i,\"width\":%u,\"height\":%u}", r.x, r.y, r.width, r.height);
188 }
189
190 void query_constraints(constraints_t c, FILE *rsp)
191 {
192         fprintf(rsp, "{\"min_width\":%u,\"min_height\":%u}", c.min_width, c.min_height);
193 }
194
195 void query_padding(padding_t p, FILE *rsp)
196 {
197         fprintf(rsp, "{\"top\":%i,\"right\":%i,\"bottom\":%i,\"left\":%i}", p.top, p.right, p.bottom, p.left);
198 }
199
200 void query_history(FILE *rsp)
201 {
202         fprintf(rsp, "[");
203         for (history_t *h = history_head; h != NULL; h = h->next) {
204                 query_coordinates(&h->loc, rsp);
205                 if (h->next != NULL) {
206                         fprintf(rsp, ",");
207                 }
208         }
209         fprintf(rsp, "]");
210 }
211
212 void query_coordinates(coordinates_t *loc, FILE *rsp)
213 {
214         fprintf(rsp, "{\"monitorId\":%u,\"desktopId\":%u,\"nodeId\":%u}", loc->monitor->id, loc->desktop->id, loc->node!=NULL?loc->node->id:0);
215 }
216
217 void query_stack(FILE *rsp)
218 {
219         fprintf(rsp, "[");
220         for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
221                 fprintf(rsp, "%u", s->node->id);
222                 if (s->next != NULL) {
223                         fprintf(rsp, ",");
224                 }
225         }
226         fprintf(rsp, "]");
227 }
228
229 void query_subscribers(FILE *rsp)
230 {
231         fprintf(rsp, "[");
232         for (subscriber_list_t *s = subscribe_head; s != NULL; s = s->next) {
233                 fprintf(rsp, "{\"fileDescriptor\": %i", fileno(s->stream));
234                 if (s->fifo_path != NULL) {
235                         fprintf(rsp, ",\"fifoPath\":\"%s\"", s->fifo_path);
236                 }
237                 fprintf(rsp, ",\"field\":%i,\"count\":%i}", s->field, s->count);
238                 if (s->next != NULL) {
239                         fprintf(rsp, ",");
240                 }
241         }
242         fprintf(rsp, "]");
243 }
244
245 int query_node_ids(coordinates_t *mon_ref, coordinates_t *desk_ref, coordinates_t* ref, coordinates_t *trg, monitor_select_t *mon_sel, desktop_select_t *desk_sel, node_select_t *sel, FILE *rsp)
246 {
247         int count = 0;
248         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
249                 coordinates_t loc = {m, NULL, NULL};
250                 if ((trg->monitor != NULL && m != trg->monitor) ||
251                     (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
252                         continue;
253                 }
254                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
255                         coordinates_t loc = {m, d, NULL};
256                         if ((trg->desktop != NULL && d != trg->desktop) ||
257                             (desk_sel != NULL && !desktop_matches(&loc, desk_ref, desk_sel))) {
258                                 continue;
259                         }
260                         count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
261                 }
262         }
263         return count;
264 }
265
266 int query_node_ids_in(node_t *n, desktop_t *d, monitor_t *m, coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *rsp)
267 {
268         int count = 0;
269         if (n == NULL) {
270                 return 0;
271         } else {
272                 coordinates_t loc = {m, d, n};
273                 if ((trg->node == NULL || n == trg->node) &&
274                     (sel == NULL || node_matches(&loc, ref, sel))) {
275                         fprintf(rsp, "0x%08X\n", n->id);
276                         count++;
277                 }
278                 count += query_node_ids_in(n->first_child, d, m, ref, trg, sel, rsp);
279                 count += query_node_ids_in(n->second_child, d, m, ref, trg, sel, rsp);
280         }
281         return count;
282 }
283
284 int query_desktop_ids(coordinates_t* mon_ref, coordinates_t *ref, coordinates_t *trg, monitor_select_t *mon_sel, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
285 {
286         int count = 0;
287         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
288                 coordinates_t loc = {m, NULL, NULL};
289                 if ((trg->monitor != NULL && m != trg->monitor) ||
290                     (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
291                         continue;
292                 }
293                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
294                         coordinates_t loc = {m, d, NULL};
295                         if ((trg->desktop != NULL && d != trg->desktop) ||
296                             (sel != NULL && !desktop_matches(&loc, ref, sel))) {
297                                 continue;
298                         }
299                         printer(d, rsp);
300                         count++;
301                 }
302         }
303         return count;
304 }
305
306 int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *sel, monitor_printer_t printer, FILE *rsp)
307 {
308         int count = 0;
309         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
310                 coordinates_t loc = {m, NULL, NULL};
311                 if ((trg->monitor != NULL && m != trg->monitor) ||
312                     (sel != NULL && !monitor_matches(&loc, ref, sel))) {
313                         continue;
314                 }
315                 printer(m, rsp);
316                 count++;
317         }
318         return count;
319 }
320
321 void fprint_monitor_id(monitor_t *m, FILE *rsp)
322 {
323         fprintf(rsp, "0x%08X\n", m->id);
324 }
325
326 void fprint_monitor_name(monitor_t *m, FILE *rsp)
327 {
328         fprintf(rsp, "%s\n", m->name);
329 }
330
331 void fprint_desktop_id(desktop_t *d, FILE *rsp)
332 {
333         fprintf(rsp, "0x%08X\n", d->id);
334 }
335
336 void fprint_desktop_name(desktop_t *d, FILE *rsp)
337 {
338         fprintf(rsp, "%s\n", d->name);
339 }
340
341 void print_ignore_request(state_transition_t st, FILE *rsp)
342 {
343         if (st == 0) {
344                 fprintf(rsp, "none");
345         } else {
346                 unsigned int cnt = 0;
347                 if (st & STATE_TRANSITION_ENTER) {
348                         fprintf(rsp, "enter");
349                         cnt++;
350                 }
351                 if (st & STATE_TRANSITION_EXIT) {
352                         fprintf(rsp, "%sexit", cnt > 0 ? "," : "");
353                 }
354         }
355 }
356
357 void print_modifier_mask(uint16_t m, FILE *rsp)
358 {
359         switch (m) {
360                 case XCB_MOD_MASK_SHIFT:
361                         fprintf(rsp, "shift");
362                         break;
363                 case XCB_MOD_MASK_CONTROL:
364                         fprintf(rsp, "control");
365                         break;
366                 case XCB_MOD_MASK_LOCK:
367                         fprintf(rsp, "lock");
368                         break;
369                 case XCB_MOD_MASK_1:
370                         fprintf(rsp, "mod1");
371                         break;
372                 case XCB_MOD_MASK_2:
373                         fprintf(rsp, "mod2");
374                         break;
375                 case XCB_MOD_MASK_3:
376                         fprintf(rsp, "mod3");
377                         break;
378                 case XCB_MOD_MASK_4:
379                         fprintf(rsp, "mod4");
380                         break;
381                 case XCB_MOD_MASK_5:
382                         fprintf(rsp, "mod5");
383                         break;
384         }
385 }
386
387 void print_button_index(int8_t b, FILE *rsp)
388 {
389         switch (b) {
390                 case XCB_BUTTON_INDEX_ANY:
391                         fprintf(rsp, "any");
392                         break;
393                 case XCB_BUTTON_INDEX_1:
394                         fprintf(rsp, "button1");
395                         break;
396                 case XCB_BUTTON_INDEX_2:
397                         fprintf(rsp, "button2");
398                         break;
399                 case XCB_BUTTON_INDEX_3:
400                         fprintf(rsp, "button3");
401                         break;
402                 case -1:
403                         fprintf(rsp, "none");
404                         break;
405         }
406 }
407
408 void print_pointer_action(pointer_action_t a, FILE *rsp)
409 {
410         switch (a) {
411                 case ACTION_MOVE:
412                         fprintf(rsp, "move");
413                         break;
414                 case ACTION_RESIZE_SIDE:
415                         fprintf(rsp, "resize_side");
416                         break;
417                 case ACTION_RESIZE_CORNER:
418                         fprintf(rsp, "resize_corner");
419                         break;
420                 case ACTION_FOCUS:
421                         fprintf(rsp, "focus");
422                         break;
423                 case ACTION_NONE:
424                         fprintf(rsp, "none");
425                         break;
426         }
427 }
428
429 void resolve_rule_consequence(rule_consequence_t *csq)
430 {
431         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
432         coordinates_t dst = {NULL, NULL, NULL};
433         monitor_t *monitor = monitor_from_desc(csq->monitor_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.monitor;
434         desktop_t *desktop = desktop_from_desc(csq->desktop_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.desktop;
435         node_t *node = node_from_desc(csq->node_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.node;
436
437 #define PRINT_OBJECT_ID(name) \
438         if (name == NULL) { \
439                 csq->name##_desc[0] = '\0'; \
440         } else { \
441                 snprintf(csq->name##_desc, 11, "0x%08X", name->id); \
442         }
443         PRINT_OBJECT_ID(monitor)
444         PRINT_OBJECT_ID(desktop)
445         PRINT_OBJECT_ID(node)
446 #undef PRINT_OBJECT_ID
447 }
448
449 void print_rule_consequence(char **buf, rule_consequence_t *csq)
450 {
451         char *rect_buf = NULL;
452         print_rectangle(&rect_buf, csq->rect);
453         if (rect_buf == NULL) {
454                 rect_buf = malloc(1);
455                 *rect_buf = '\0';
456         }
457
458         asprintf(buf, "monitor=%s desktop=%s node=%s state=%s layer=%s split_dir=%s split_ratio=%lf hidden=%s sticky=%s private=%s locked=%s marked=%s center=%s follow=%s manage=%s focus=%s border=%s rectangle=%s",
459                 csq->monitor_desc, csq->desktop_desc, csq->node_desc,
460                 csq->state == NULL ? "" : STATE_STR(*csq->state),
461                 csq->layer == NULL ? "" : LAYER_STR(*csq->layer),
462                 csq->split_dir == NULL ? "" : SPLIT_DIR_STR(*csq->split_dir), csq->split_ratio,
463                 ON_OFF_STR(csq->hidden), ON_OFF_STR(csq->sticky), ON_OFF_STR(csq->private),
464                 ON_OFF_STR(csq->locked), ON_OFF_STR(csq->marked), ON_OFF_STR(csq->center), ON_OFF_STR(csq->follow),
465                 ON_OFF_STR(csq->manage), ON_OFF_STR(csq->focus), ON_OFF_STR(csq->border), rect_buf);
466         free(rect_buf);
467 }
468
469 void print_rectangle(char **buf, xcb_rectangle_t *rect)
470 {
471         if (rect != NULL) {
472                 asprintf(buf, "%hux%hu+%hi+%hi", rect->width, rect->height, rect->x, rect->y);
473         }
474 }
475
476 node_select_t make_node_select(void)
477 {
478         node_select_t sel = {
479                 .automatic = OPTION_NONE,
480                 .focused = OPTION_NONE,
481                 .active = OPTION_NONE,
482                 .local = OPTION_NONE,
483                 .leaf = OPTION_NONE,
484                 .window = OPTION_NONE,
485                 .tiled = OPTION_NONE,
486                 .pseudo_tiled = OPTION_NONE,
487                 .floating = OPTION_NONE,
488                 .fullscreen = OPTION_NONE,
489                 .hidden = OPTION_NONE,
490                 .sticky = OPTION_NONE,
491                 .private = OPTION_NONE,
492                 .locked = OPTION_NONE,
493                 .marked = OPTION_NONE,
494                 .urgent = OPTION_NONE,
495                 .same_class = OPTION_NONE,
496                 .descendant_of = OPTION_NONE,
497                 .ancestor_of = OPTION_NONE,
498                 .below = OPTION_NONE,
499                 .normal = OPTION_NONE,
500                 .above = OPTION_NONE,
501                 .horizontal = OPTION_NONE,
502                 .vertical = OPTION_NONE
503         };
504         return sel;
505 }
506
507 desktop_select_t make_desktop_select(void)
508 {
509         desktop_select_t sel = {
510                 .occupied = OPTION_NONE,
511                 .focused = OPTION_NONE,
512                 .active = OPTION_NONE,
513                 .urgent = OPTION_NONE,
514                 .local = OPTION_NONE,
515                 .tiled = OPTION_NONE,
516                 .monocle = OPTION_NONE,
517                 .user_tiled = OPTION_NONE,
518                 .user_monocle = OPTION_NONE
519         };
520         return sel;
521 }
522
523 monitor_select_t make_monitor_select(void)
524 {
525         monitor_select_t sel = {
526                 .occupied = OPTION_NONE,
527                 .focused = OPTION_NONE
528         };
529         return sel;
530 }
531
532 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
533 {
534         dst->node = NULL;
535
536         coordinates_t ref_copy = *ref;
537         ref = &ref_copy;
538         char *desc_copy = copy_string(desc, strlen(desc));
539         desc = desc_copy;
540
541         char *hash = strrchr(desc, '#');
542         char *path = strrchr(desc, '@');
543         char *colon = strrchr(desc, ':');
544
545         /* Adjust or discard hashes inside a DESKTOP_SEL, e.g. `newest#@prev#older:/1/2` */
546         if (hash != NULL && colon != NULL && path != NULL &&
547             path < hash && hash < colon) {
548                 if (path > desc && *(path - 1) == '#') {
549                         hash = path - 1;
550                 } else {
551                         hash = NULL;
552                 }
553         }
554
555         if (hash != NULL) {
556                 *hash = '\0';
557                 int ret;
558                 coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
559                 if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
560                         desc = hash + 1;
561                 } else {
562                         free(desc_copy);
563                         return ret;
564                 }
565         }
566
567         /* Discard colons within references, e.g. `@next.occupied:/#any.descendant_of.window` */
568         if (colon != NULL && hash != NULL && colon < hash) {
569                 colon = NULL;
570         }
571
572         node_select_t sel = make_node_select();
573
574         if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
575                 free(desc_copy);
576                 return SELECTOR_BAD_MODIFIERS;
577         }
578
579         direction_t dir;
580         cycle_dir_t cyc;
581         history_dir_t hdi;
582         if (parse_direction(desc, &dir)) {
583                 find_nearest_neighbor(ref, dst, dir, &sel);
584         } else if (parse_cycle_direction(desc, &cyc)) {
585                 find_closest_node(ref, dst, cyc, &sel);
586         } else if (parse_history_direction(desc, &hdi)) {
587                 history_find_node(hdi, ref, dst, &sel);
588         } else if (streq("any", desc)) {
589                 find_any_node(ref, dst, &sel);
590         } else if (streq("first_ancestor", desc)) {
591                 find_first_ancestor(ref, dst, &sel);
592         } else if (streq("last", desc)) {
593                 history_find_node(HISTORY_OLDER, ref, dst, &sel);
594         } else if (streq("newest", desc)) {
595                 history_find_newest_node(ref, dst, &sel);
596         } else if (streq("biggest", desc)) {
597                 find_by_area(AREA_BIGGEST, ref, dst, &sel);
598         } else if (streq("smallest", desc)) {
599                 find_by_area(AREA_SMALLEST, ref, dst, &sel);
600         } else if (streq("pointed", desc)) {
601                 xcb_window_t win = XCB_NONE;
602                 query_pointer(&win, NULL);
603                 if (locate_leaf(win, dst) && node_matches(dst, ref, &sel)) {
604                         return SELECTOR_OK;
605                 } else {
606                         return SELECTOR_INVALID;
607                 }
608         } else if (streq("focused", desc)) {
609                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
610                 if (node_matches(&loc, ref, &sel)) {
611                         *dst = loc;
612                 }
613         } else if (*desc == '@') {
614                 desc++;
615                 *dst = *ref;
616                 if (colon != NULL) {
617                         *colon = '\0';
618                         int ret;
619                         if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
620                                 dst->node = dst->desktop->focus;
621                                 desc = colon + 1;
622                         } else {
623                                 free(desc_copy);
624                                 return ret;
625                         }
626                 }
627                 if (*desc == '/') {
628                         dst->node = dst->desktop->root;
629                 }
630                 char *move = strtok(desc, PTH_TOK);
631                 while (move != NULL && dst->node != NULL) {
632                         if (streq("first", move) || streq("1", move)) {
633                                 dst->node = dst->node->first_child;
634                         } else if (streq("second", move) || streq("2", move)) {
635                                 dst->node = dst->node->second_child;
636                         } else if (streq("parent", move)) {
637                                 dst->node = dst->node->parent;
638                         } else if (streq("brother", move)) {
639                                 dst->node = brother_tree(dst->node);
640                         } else {
641                                 direction_t dir;
642                                 if (parse_direction(move, &dir)) {
643                                         dst->node = find_fence(dst->node, dir);
644                                 } else {
645                                         free(desc_copy);
646                                         return SELECTOR_BAD_DESCRIPTOR;
647                                 }
648                         }
649                         move = strtok(NULL, PTH_TOK);
650                 }
651                 free(desc_copy);
652                 if (dst->node != NULL) {
653                         if (node_matches(dst, ref, &sel)) {
654                                 return SELECTOR_OK;
655                         } else {
656                                 return SELECTOR_INVALID;
657                         }
658                 } else if (dst->desktop->root != NULL) {
659                         return SELECTOR_INVALID;
660                 }
661                 return SELECTOR_OK;
662         } else {
663                 uint32_t id;
664                 if (parse_id(desc, &id)) {
665                         free(desc_copy);
666                         if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
667                                 return SELECTOR_OK;
668                         } else {
669                                 return SELECTOR_INVALID;
670                         }
671                 } else {
672                         free(desc_copy);
673                         return SELECTOR_BAD_DESCRIPTOR;
674                 }
675         }
676
677         free(desc_copy);
678
679         if (dst->node == NULL) {
680                 return SELECTOR_INVALID;
681         }
682
683         return SELECTOR_OK;
684 }
685
686 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
687 {
688         dst->desktop = NULL;
689
690         if (*desc == '%') {
691                 locate_desktop(desc + 1, dst);
692                 goto end;
693         }
694
695         coordinates_t ref_copy = *ref;
696         ref = &ref_copy;
697         char *desc_copy = copy_string(desc, strlen(desc));
698         desc = desc_copy;
699
700         char *hash = strrchr(desc, '#');
701         char *colon = strrchr(desc, ':');
702
703         /* Discard hashes inside a MONITOR_SEL, e.g. `primary#next:focused` */
704         if (hash != NULL && colon != NULL && hash < colon) {
705                 hash = NULL;
706         }
707
708         if (hash != NULL) {
709                 *hash = '\0';
710                 int ret;
711                 coordinates_t tmp = {mon, mon->desk, NULL};
712                 if ((ret = desktop_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
713                         desc = hash + 1;
714                 } else {
715                         free(desc_copy);
716                         return ret;
717                 }
718         }
719
720         desktop_select_t sel = make_desktop_select();
721
722         if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
723                 free(desc_copy);
724                 return SELECTOR_BAD_MODIFIERS;
725         }
726
727         cycle_dir_t cyc;
728         history_dir_t hdi;
729         uint16_t idx;
730         uint32_t id;
731         if (parse_cycle_direction(desc, &cyc)) {
732                 find_closest_desktop(ref, dst, cyc, &sel);
733         } else if (parse_history_direction(desc, &hdi)) {
734                 history_find_desktop(hdi, ref, dst, &sel);
735         } else if (streq("any", desc)) {
736                 find_any_desktop(ref, dst, &sel);
737         } else if (streq("last", desc)) {
738                 history_find_desktop(HISTORY_OLDER, ref, dst, &sel);
739         } else if (streq("newest", desc)) {
740                 history_find_newest_desktop(ref, dst, &sel);
741         } else if (streq("focused", desc)) {
742                 coordinates_t loc = {mon, mon->desk, NULL};
743                 if (desktop_matches(&loc, ref, &sel)) {
744                         *dst = loc;
745                 }
746         } else if (colon != NULL) {
747                 *colon = '\0';
748                 int ret;
749                 if ((ret = monitor_from_desc(desc, ref, dst)) == SELECTOR_OK) {
750                         if (streq("focused", colon + 1)) {
751                                 coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
752                                 if (desktop_matches(&loc, ref, &sel)) {
753                                         *dst = loc;
754                                 }
755                         } else if (parse_index(colon + 1, &idx)) {
756                                 free(desc_copy);
757                                 if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, &sel)) {
758                                         return SELECTOR_OK;
759                                 } else {
760                                         return SELECTOR_INVALID;
761                                 }
762                         } else {
763                                 free(desc_copy);
764                                 return SELECTOR_BAD_DESCRIPTOR;
765                         }
766                 } else {
767                         free(desc_copy);
768                         return ret;
769                 }
770         } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
771                 free(desc_copy);
772                 if (desktop_matches(dst, ref, &sel)) {
773                         return SELECTOR_OK;
774                 } else {
775                         return SELECTOR_INVALID;
776                 }
777         } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
778                 free(desc_copy);
779                 if (desktop_matches(dst, ref, &sel)) {
780                         return SELECTOR_OK;
781                 } else {
782                         return SELECTOR_INVALID;
783                 }
784         } else {
785                 int hits = 0;
786                 if (desktop_from_name(desc, ref, dst, &sel, &hits)) {
787                         free(desc_copy);
788                         return SELECTOR_OK;
789                 } else {
790                         free(desc_copy);
791                         if (hits > 0) {
792                                 return SELECTOR_INVALID;
793                         } else {
794                                 return SELECTOR_BAD_DESCRIPTOR;
795                         }
796                 }
797         }
798
799         free(desc_copy);
800
801 end:
802         if (dst->desktop == NULL) {
803                 return SELECTOR_INVALID;
804         }
805
806         return SELECTOR_OK;
807 }
808
809 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
810 {
811         dst->monitor = NULL;
812
813         if (*desc == '%') {
814                 locate_monitor(desc + 1, dst);
815                 goto end;
816         }
817
818         coordinates_t ref_copy = *ref;
819         ref = &ref_copy;
820         char *desc_copy = copy_string(desc, strlen(desc));
821         desc = desc_copy;
822
823         char *hash = strrchr(desc, '#');
824
825         if (hash != NULL) {
826                 *hash = '\0';
827                 int ret;
828                 coordinates_t tmp = {mon, NULL, NULL};
829                 if ((ret = monitor_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
830                         desc = hash + 1;
831                 } else {
832                         free(desc_copy);
833                         return ret;
834                 }
835         }
836
837         monitor_select_t sel = make_monitor_select();
838
839         if (!parse_monitor_modifiers(desc, &sel)) {
840                 free(desc_copy);
841                 return SELECTOR_BAD_MODIFIERS;
842         }
843
844         direction_t dir;
845         cycle_dir_t cyc;
846         history_dir_t hdi;
847         uint16_t idx;
848         uint32_t id;
849         if (parse_direction(desc, &dir)) {
850                 dst->monitor = nearest_monitor(ref->monitor, dir, &sel);
851         } else if (parse_cycle_direction(desc, &cyc)) {
852                 dst->monitor = closest_monitor(ref->monitor, cyc, &sel);
853         } else if (parse_history_direction(desc, &hdi)) {
854                 history_find_monitor(hdi, ref, dst, &sel);
855         } else if (streq("any", desc)) {
856                 find_any_monitor(ref, dst, &sel);
857         } else if (streq("last", desc)) {
858                 history_find_monitor(HISTORY_OLDER, ref, dst, &sel);
859         } else if (streq("newest", desc)) {
860                 history_find_newest_monitor(ref, dst, &sel);
861         } else if (streq("primary", desc)) {
862                 if (pri_mon != NULL) {
863                         coordinates_t loc = {pri_mon, NULL, NULL};
864                         if (monitor_matches(&loc, ref, &sel)) {
865                                 dst->monitor = pri_mon;
866                         }
867                 }
868         } else if (streq("focused", desc)) {
869                 coordinates_t loc = {mon, NULL, NULL};
870                 if (monitor_matches(&loc, ref, &sel)) {
871                         dst->monitor = mon;
872                 }
873         } else if (streq("pointed", desc)) {
874                 xcb_point_t pointer;
875                 query_pointer(NULL, &pointer);
876                 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
877                         if (is_inside(pointer, m->rectangle)) {
878                                 dst->monitor = m;
879                                 break;
880                         }
881                 }
882         } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
883                 free(desc_copy);
884                 if (monitor_matches(dst, ref, &sel)) {
885                         return SELECTOR_OK;
886                 } else {
887                         return SELECTOR_INVALID;
888                 }
889         } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
890                 free(desc_copy);
891                 if (monitor_matches(dst, ref, &sel)) {
892                         return SELECTOR_OK;
893                 } else {
894                         return SELECTOR_INVALID;
895                 }
896         } else {
897                 if (locate_monitor(desc, dst)) {
898                         free(desc_copy);
899                         if (monitor_matches(dst, ref, &sel)) {
900                                 return SELECTOR_OK;
901                         } else {
902                                 return SELECTOR_INVALID;
903                         }
904                 } else {
905                         free(desc_copy);
906                         return SELECTOR_BAD_DESCRIPTOR;
907                 }
908         }
909
910         free(desc_copy);
911
912 end:
913         if (dst->monitor == NULL) {
914                 return SELECTOR_INVALID;
915         }
916
917         return SELECTOR_OK;
918 }
919
920 bool locate_leaf(xcb_window_t win, coordinates_t *loc)
921 {
922         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
923                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
924                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
925                                 if (n->id == win) {
926                                         loc->monitor = m;
927                                         loc->desktop = d;
928                                         loc->node = n;
929                                         return true;
930                                 }
931                         }
932                 }
933         }
934         return false;
935 }
936
937 bool locate_window(xcb_window_t win, coordinates_t *loc)
938 {
939         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
940                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
941                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
942                                 if (n->client == NULL) {
943                                         continue;
944                                 }
945                                 if (n->id == win) {
946                                         loc->monitor = m;
947                                         loc->desktop = d;
948                                         loc->node = n;
949                                         return true;
950                                 }
951                         }
952                 }
953         }
954         return false;
955 }
956
957 bool locate_desktop(char *name, coordinates_t *loc)
958 {
959         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
960                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
961                         if (streq(d->name, name)) {
962                                 loc->monitor = m;
963                                 loc->desktop = d;
964                                 return true;
965                         }
966                 }
967         }
968         return false;
969 }
970
971 bool locate_monitor(char *name, coordinates_t *loc)
972 {
973         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
974                 if (streq(m->name, name)) {
975                         loc->monitor = m;
976                         return true;
977                 }
978         }
979         return false;
980 }
981
982 bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
983 {
984         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
985                 if (mm != NULL && m != mm) {
986                         continue;
987                 }
988                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
989                         if (d->id == id) {
990                                 loc->monitor = m;
991                                 loc->desktop = d;
992                                 loc->node = NULL;
993                                 return true;
994                         }
995                 }
996         }
997         return false;
998 }
999
1000 bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel, int *hits)
1001 {
1002         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1003                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1004                         if (streq(d->name, name)) {
1005                                 if (hits != NULL) {
1006                                         (*hits)++;
1007                                 }
1008                                 coordinates_t loc = {m, d, NULL};
1009                                 if (desktop_matches(&loc, ref, sel)) {
1010                                         dst->monitor = m;
1011                                         dst->desktop = d;
1012                                         return true;
1013                                 }
1014                         }
1015                 }
1016         }
1017         return false;
1018 }
1019
1020 bool desktop_from_index(uint16_t idx, coordinates_t *loc, monitor_t *mm)
1021 {
1022         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1023                 if (mm != NULL && m != mm) {
1024                         continue;
1025                 }
1026                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, idx--) {
1027                         if (idx == 1) {
1028                                 loc->monitor = m;
1029                                 loc->desktop = d;
1030                                 loc->node = NULL;
1031                                 return true;
1032                         }
1033                 }
1034         }
1035         return false;
1036 }
1037
1038 bool monitor_from_id(uint32_t id, coordinates_t *loc)
1039 {
1040         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1041                 if (m->id == id) {
1042                         loc->monitor = m;
1043                         loc->desktop = NULL;
1044                         loc->node = NULL;
1045                         return true;
1046                 }
1047         }
1048         return false;
1049 }
1050
1051 bool monitor_from_index(int idx, coordinates_t *loc)
1052 {
1053         for (monitor_t *m = mon_head; m != NULL; m = m->next, idx--) {
1054                 if (idx == 1) {
1055                         loc->monitor = m;
1056                         loc->desktop = NULL;
1057                         loc->node = NULL;
1058                         return true;
1059                 }
1060         }
1061         return false;
1062 }
1063
1064 bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
1065 {
1066         if (loc->node == NULL) {
1067                 return false;
1068         }
1069
1070         if (sel->focused != OPTION_NONE &&
1071             loc->node != mon->desk->focus
1072             ? sel->focused == OPTION_TRUE
1073             : sel->focused == OPTION_FALSE) {
1074                 return false;
1075         }
1076
1077         if (sel->active != OPTION_NONE &&
1078             loc->node != loc->desktop->focus
1079             ? sel->active == OPTION_TRUE
1080             : sel->active == OPTION_FALSE) {
1081                 return false;
1082         }
1083
1084         if (sel->automatic != OPTION_NONE &&
1085             loc->node->presel != NULL
1086             ? sel->automatic == OPTION_TRUE
1087             : sel->automatic == OPTION_FALSE) {
1088                 return false;
1089         }
1090
1091         if (sel->local != OPTION_NONE &&
1092             loc->desktop != ref->desktop
1093             ? sel->local == OPTION_TRUE
1094             : sel->local == OPTION_FALSE) {
1095                 return false;
1096         }
1097
1098         if (sel->active != OPTION_NONE &&
1099             loc->desktop != loc->monitor->desk
1100             ? sel->active == OPTION_TRUE
1101             : sel->active == OPTION_FALSE) {
1102                 return false;
1103         }
1104
1105         if (sel->leaf != OPTION_NONE &&
1106             !is_leaf(loc->node)
1107             ? sel->leaf == OPTION_TRUE
1108             : sel->leaf == OPTION_FALSE) {
1109                 return false;
1110         }
1111
1112         if (sel->window != OPTION_NONE &&
1113             loc->node->client == NULL
1114             ? sel->window == OPTION_TRUE
1115             : sel->window == OPTION_FALSE) {
1116                 return false;
1117         }
1118
1119 #define NFLAG(p) \
1120         if (sel->p != OPTION_NONE && \
1121             !loc->node->p \
1122             ? sel->p == OPTION_TRUE \
1123             : sel->p == OPTION_FALSE) { \
1124                 return false; \
1125         }
1126         NFLAG(hidden)
1127         NFLAG(sticky)
1128         NFLAG(private)
1129         NFLAG(locked)
1130         NFLAG(marked)
1131 #undef NFLAG
1132
1133         if (loc->node->client == NULL &&
1134                 (sel->same_class != OPTION_NONE ||
1135                  sel->tiled != OPTION_NONE ||
1136                  sel->pseudo_tiled != OPTION_NONE ||
1137                  sel->floating != OPTION_NONE ||
1138                  sel->fullscreen != OPTION_NONE ||
1139                  sel->below != OPTION_NONE ||
1140                  sel->normal != OPTION_NONE ||
1141                  sel->above != OPTION_NONE ||
1142                  sel->urgent != OPTION_NONE)) {
1143                 return false;
1144         }
1145
1146         if (ref->node != NULL && ref->node->client != NULL &&
1147             sel->same_class != OPTION_NONE &&
1148             streq(loc->node->client->class_name, ref->node->client->class_name)
1149             ? sel->same_class == OPTION_FALSE
1150             : sel->same_class == OPTION_TRUE) {
1151                 return false;
1152         }
1153
1154         if (sel->descendant_of != OPTION_NONE &&
1155             !is_descendant(loc->node, ref->node)
1156             ? sel->descendant_of == OPTION_TRUE
1157             : sel->descendant_of == OPTION_FALSE) {
1158                 return false;
1159         }
1160
1161         if (sel->ancestor_of != OPTION_NONE &&
1162             !is_descendant(ref->node, loc->node)
1163             ? sel->ancestor_of == OPTION_TRUE
1164             : sel->ancestor_of == OPTION_FALSE) {
1165                 return false;
1166         }
1167
1168 #define WSTATE(p, e) \
1169         if (sel->p != OPTION_NONE && \
1170             loc->node->client->state != e \
1171             ? sel->p == OPTION_TRUE \
1172             : sel->p == OPTION_FALSE) { \
1173                 return false; \
1174         }
1175         WSTATE(tiled, STATE_TILED)
1176         WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
1177         WSTATE(floating, STATE_FLOATING)
1178         WSTATE(fullscreen, STATE_FULLSCREEN)
1179 #undef WSTATE
1180
1181 #define WLAYER(p, e) \
1182         if (sel->p != OPTION_NONE && \
1183             loc->node->client->layer != e \
1184             ? sel->p == OPTION_TRUE \
1185             : sel->p == OPTION_FALSE) { \
1186                 return false; \
1187         }
1188         WLAYER(below, LAYER_BELOW)
1189         WLAYER(normal, LAYER_NORMAL)
1190         WLAYER(above, LAYER_ABOVE)
1191 #undef WLAYER
1192
1193 #define WFLAG(p) \
1194         if (sel->p != OPTION_NONE && \
1195             !loc->node->client->p \
1196             ? sel->p == OPTION_TRUE \
1197             : sel->p == OPTION_FALSE) { \
1198                 return false; \
1199         }
1200         WFLAG(urgent)
1201 #undef WFLAG
1202
1203         if (sel->horizontal != OPTION_NONE &&
1204             loc->node->split_type != TYPE_HORIZONTAL
1205             ? sel->horizontal == OPTION_TRUE
1206             : sel->horizontal == OPTION_FALSE) {
1207                 return false;
1208         }
1209
1210         if (sel->vertical != OPTION_NONE &&
1211             loc->node->split_type != TYPE_VERTICAL
1212             ? sel->vertical == OPTION_TRUE
1213             : sel->vertical == OPTION_FALSE) {
1214                 return false;
1215         }
1216
1217         return true;
1218 }
1219
1220 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *sel)
1221 {
1222         if (sel->occupied != OPTION_NONE &&
1223             loc->desktop->root == NULL
1224             ? sel->occupied == OPTION_TRUE
1225             : sel->occupied == OPTION_FALSE) {
1226                 return false;
1227         }
1228
1229         if (sel->focused != OPTION_NONE &&
1230             loc->desktop != mon->desk
1231             ? sel->focused == OPTION_TRUE
1232             : sel->focused == OPTION_FALSE) {
1233                 return false;
1234         }
1235
1236         if (sel->active != OPTION_NONE &&
1237             loc->desktop != loc->monitor->desk
1238             ? sel->active == OPTION_TRUE
1239             : sel->active == OPTION_FALSE) {
1240                 return false;
1241         }
1242
1243         if (sel->urgent != OPTION_NONE &&
1244             !is_urgent(loc->desktop)
1245             ? sel->urgent == OPTION_TRUE
1246             : sel->urgent == OPTION_FALSE) {
1247                 return false;
1248         }
1249
1250         if (sel->local != OPTION_NONE &&
1251             ref->monitor != loc->monitor
1252             ? sel->local == OPTION_TRUE
1253             : sel->local == OPTION_FALSE) {
1254                 return false;
1255         }
1256
1257 #define DLAYOUT(p, e) \
1258         if (sel->p != OPTION_NONE && \
1259             loc->desktop->layout != e \
1260             ? sel->p == OPTION_TRUE \
1261             : sel->p == OPTION_FALSE) { \
1262                 return false; \
1263         }
1264         DLAYOUT(tiled, LAYOUT_TILED)
1265         DLAYOUT(monocle, LAYOUT_MONOCLE)
1266 #undef DLAYOUT
1267
1268 #define DUSERLAYOUT(p, e) \
1269         if (sel->p != OPTION_NONE && \
1270             loc->desktop->user_layout != e \
1271             ? sel->p == OPTION_TRUE \
1272             : sel->p == OPTION_FALSE) { \
1273                 return false; \
1274         }
1275         DUSERLAYOUT(user_tiled, LAYOUT_TILED)
1276         DUSERLAYOUT(user_monocle, LAYOUT_MONOCLE)
1277 #undef DUSERLAYOUT
1278
1279         return true;
1280 }
1281
1282 bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t *sel)
1283 {
1284         if (sel->occupied != OPTION_NONE &&
1285             loc->monitor->desk->root == NULL
1286             ? sel->occupied == OPTION_TRUE
1287             : sel->occupied == OPTION_FALSE) {
1288                 return false;
1289         }
1290
1291         if (sel->focused != OPTION_NONE &&
1292             loc->monitor != mon
1293             ? sel->focused == OPTION_TRUE
1294             : sel->focused == OPTION_FALSE) {
1295                 return false;
1296         }
1297
1298         return true;
1299 }