]> git.lizzy.rs Git - bspwm.git/blob - src/query.c
Fix non-matching undefs
[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
37 void query_tree(FILE *rsp)
38 {
39         fprintf(rsp, "{");
40         fprintf(rsp, "\"focusedMonitorId\":%u,", mon->id);
41         if (pri_mon != NULL) {
42                 fprintf(rsp, "\"primaryMonitorId\":%u,", pri_mon->id);
43         }
44         fprintf(rsp, "\"clientsCount\":%i,", clients_count);
45         fprintf(rsp, "\"monitors\":");
46         fprintf(rsp, "[");
47         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
48                 query_monitor(m, rsp);
49                 if (m->next != NULL) {
50                         fprintf(rsp, ",");
51                 }
52         }
53         fprintf(rsp, "]");
54         fprintf(rsp,",");
55         fprintf(rsp, "\"focusHistory\":");
56         query_history(rsp);
57         fprintf(rsp,",");
58         fprintf(rsp, "\"stackingList\":");
59         query_stack(rsp);
60         fprintf(rsp, "}");
61
62 }
63
64 void query_monitor(monitor_t *m, FILE *rsp)
65 {
66         fprintf(rsp, "{");
67         fprintf(rsp, "\"name\":\"%s\",", m->name);
68         fprintf(rsp, "\"id\":%u,", m->id);
69         fprintf(rsp, "\"randrId\":%u,", m->randr_id);
70         fprintf(rsp, "\"wired\":%s,", BOOL_STR(m->wired));
71         fprintf(rsp, "\"stickyCount\":%i,", m->sticky_count);
72         fprintf(rsp, "\"windowGap\":%i,", m->window_gap);
73         fprintf(rsp, "\"borderWidth\":%u,", m->border_width);
74         fprintf(rsp, "\"focusedDesktopId\":%u,", m->desk->id);
75         fprintf(rsp, "\"padding\":");
76         query_padding(m->padding, rsp);
77         fprintf(rsp,",");
78         fprintf(rsp, "\"rectangle\":");
79         query_rectangle(m->rectangle, rsp);
80         fprintf(rsp,",");
81         fprintf(rsp, "\"desktops\":");
82         fprintf(rsp, "[");
83         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
84                 query_desktop(d, rsp);
85                 if (d->next != NULL) {
86                         fprintf(rsp,",");
87                 }
88         }
89         fprintf(rsp, "]");
90         fprintf(rsp, "}");
91 }
92
93 void query_desktop(desktop_t *d, FILE *rsp)
94 {
95         fprintf(rsp, "{");
96         fprintf(rsp, "\"name\":\"%s\",", d->name);
97         fprintf(rsp, "\"id\":%u,", d->id);
98         fprintf(rsp, "\"layout\":\"%s\",", LAYOUT_STR(d->layout));
99         fprintf(rsp, "\"windowGap\":%i,", d->window_gap);
100         fprintf(rsp, "\"borderWidth\":%u,", d->border_width);
101         fprintf(rsp, "\"focusedNodeId\":%u,", d->focus != NULL ? d->focus->id : 0);
102         fprintf(rsp, "\"padding\":");
103         query_padding(d->padding, rsp);
104         fprintf(rsp,",");
105         fprintf(rsp, "\"root\":");
106         query_node(d->root, rsp);
107         fprintf(rsp, "}");
108 }
109
110 void query_node(node_t *n, FILE *rsp)
111 {
112         if (n == NULL) {
113                 fprintf(rsp, "null");
114         } else {
115                 fprintf(rsp, "{");
116                 fprintf(rsp, "\"id\":%u,", n->id);
117                 fprintf(rsp, "\"splitType\":\"%s\",", SPLIT_TYPE_STR(n->split_type));
118                 fprintf(rsp, "\"splitRatio\":%lf,", n->split_ratio);
119                 fprintf(rsp, "\"birthRotation\":%i,", n->birth_rotation);
120                 fprintf(rsp, "\"vacant\":%s,", BOOL_STR(n->vacant));
121                 fprintf(rsp, "\"hidden\":%s,", BOOL_STR(n->hidden));
122                 fprintf(rsp, "\"sticky\":%s,", BOOL_STR(n->sticky));
123                 fprintf(rsp, "\"private\":%s,", BOOL_STR(n->private));
124                 fprintf(rsp, "\"locked\":%s,", BOOL_STR(n->locked));
125                 fprintf(rsp, "\"presel\":");
126                 query_presel(n->presel, rsp);
127                 fprintf(rsp,",");
128                 fprintf(rsp, "\"rectangle\":");
129                 query_rectangle(n->rectangle, rsp);
130                 fprintf(rsp,",");
131                 fprintf(rsp, "\"firstChild\":");
132                 query_node(n->first_child, rsp);
133                 fprintf(rsp,",");
134                 fprintf(rsp, "\"secondChild\":");
135                 query_node(n->second_child, rsp);
136                 fprintf(rsp,",");
137                 fprintf(rsp, "\"client\":");
138                 query_client(n->client, rsp);
139                 fprintf(rsp, "}");
140         }
141 }
142
143 void query_presel(presel_t *p, FILE *rsp)
144 {
145         if (p == NULL) {
146                 fprintf(rsp, "null");
147         } else {
148                 fprintf(rsp, "{\"splitDir\":\"%s\",\"splitRatio\":%lf}", SPLIT_DIR_STR(p->split_dir), p->split_ratio);
149         }
150 }
151
152 void query_client(client_t *c, FILE *rsp)
153 {
154         if (c == NULL) {
155                 fprintf(rsp, "null");
156         } else {
157                 fprintf(rsp, "{");
158                 fprintf(rsp, "\"className\":\"%s\",", c->class_name);
159                 fprintf(rsp, "\"instanceName\":\"%s\",", c->instance_name);
160                 fprintf(rsp, "\"borderWidth\":%u,", c->border_width);
161                 fprintf(rsp, "\"state\":\"%s\",", STATE_STR(c->state));
162                 fprintf(rsp, "\"lastState\":\"%s\",", STATE_STR(c->last_state));
163                 fprintf(rsp, "\"layer\":\"%s\",", LAYER_STR(c->layer));
164                 fprintf(rsp, "\"lastLayer\":\"%s\",", LAYER_STR(c->last_layer));
165                 fprintf(rsp, "\"urgent\":%s,", BOOL_STR(c->urgent));
166                 fprintf(rsp, "\"shown\":%s,", BOOL_STR(c->shown));
167                 fprintf(rsp, "\"tiledRectangle\":");
168                 query_rectangle(c->tiled_rectangle, rsp);
169                 fprintf(rsp,",");
170                 fprintf(rsp, "\"floatingRectangle\":");
171                 query_rectangle(c->floating_rectangle, rsp);
172                 fprintf(rsp, "}");
173         }
174 }
175
176 void query_rectangle(xcb_rectangle_t r, FILE *rsp)
177 {
178         fprintf(rsp, "{\"x\":%i,\"y\":%i,\"width\":%u,\"height\":%u}", r.x, r.y, r.width, r.height);
179 }
180
181 void query_padding(padding_t p, FILE *rsp)
182 {
183         fprintf(rsp, "{\"top\":%i,\"right\":%i,\"bottom\":%i,\"left\":%i}", p.top, p.right, p.bottom, p.left);
184 }
185
186 void query_history(FILE *rsp)
187 {
188         fprintf(rsp, "[");
189         for (history_t *h = history_head; h != NULL; h = h->next) {
190                 query_coordinates(&h->loc, rsp);
191                 if (h->next != NULL) {
192                         fprintf(rsp, ",");
193                 }
194         }
195         fprintf(rsp, "]");
196 }
197
198 void query_coordinates(coordinates_t *loc, FILE *rsp)
199 {
200         fprintf(rsp, "{\"monitorId\":%u,\"desktopId\":%u,\"nodeId\":%u}", loc->monitor->id, loc->desktop->id, loc->node!=NULL?loc->node->id:0);
201 }
202
203 void query_stack(FILE *rsp)
204 {
205         fprintf(rsp, "[");
206         for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
207                 fprintf(rsp, "%u", s->node->id);
208                 if (s->next != NULL) {
209                         fprintf(rsp, ",");
210                 }
211         }
212         fprintf(rsp, "]");
213 }
214
215 int query_node_ids(coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *rsp)
216 {
217         int count = 0;
218         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
219                 if (trg->monitor != NULL && m != trg->monitor) {
220                         continue;
221                 }
222                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
223                         if (trg->desktop != NULL && d != trg->desktop) {
224                                 continue;
225                         }
226                         count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
227                 }
228         }
229         return count;
230 }
231
232 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)
233 {
234         int count = 0;
235         if (n == NULL) {
236                 return 0;
237         } else {
238                 coordinates_t loc = {m, d, n};
239                 if ((trg->node == NULL || n == trg->node) &&
240                     (sel == NULL || node_matches(&loc, ref, *sel))) {
241                         fprintf(rsp, "0x%08X\n", n->id);
242                         count++;
243                 }
244                 count += query_node_ids_in(n->first_child, d, m, ref, trg, sel, rsp);
245                 count += query_node_ids_in(n->second_child, d, m, ref, trg, sel, rsp);
246         }
247         return count;
248 }
249
250 int query_desktop_ids(coordinates_t *ref, coordinates_t *trg, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
251 {
252         int count = 0;
253         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
254                 if (trg->monitor != NULL && m != trg->monitor) {
255                         continue;
256                 }
257                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
258                         coordinates_t loc = {m, d, NULL};
259                         if ((trg->desktop != NULL && d != trg->desktop) ||
260                             (sel != NULL && !desktop_matches(&loc, ref, *sel))) {
261                                 continue;
262                         }
263                         printer(d, rsp);
264                         count++;
265                 }
266         }
267         return count;
268 }
269
270 int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *sel, monitor_printer_t printer, FILE *rsp)
271 {
272         int count = 0;
273         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
274                 coordinates_t loc = {m, NULL, NULL};
275                 if ((trg->monitor != NULL && m != trg->monitor) ||
276                         (sel != NULL && !monitor_matches(&loc, ref, *sel))) {
277                         continue;
278                 }
279                 printer(m, rsp);
280                 count++;
281         }
282         return count;
283 }
284
285 void fprint_monitor_id(monitor_t *m, FILE *rsp)
286 {
287         fprintf(rsp, "0x%08X\n", m->id);
288 }
289
290 void fprint_monitor_name(monitor_t *m, FILE *rsp)
291 {
292         fprintf(rsp, "%s\n", m->name);
293 }
294
295 void fprint_desktop_id(desktop_t *d, FILE *rsp)
296 {
297         fprintf(rsp, "0x%08X\n", d->id);
298 }
299
300 void fprint_desktop_name(desktop_t *d, FILE *rsp)
301 {
302         fprintf(rsp, "%s\n", d->name);
303 }
304
305 void print_modifier_mask(uint16_t m, FILE *rsp)
306 {
307         switch (m) {
308                 case XCB_MOD_MASK_SHIFT:
309                         fprintf(rsp, "shift");
310                         break;
311                 case XCB_MOD_MASK_CONTROL:
312                         fprintf(rsp, "control");
313                         break;
314                 case XCB_MOD_MASK_LOCK:
315                         fprintf(rsp, "lock");
316                         break;
317                 case XCB_MOD_MASK_1:
318                         fprintf(rsp, "mod1");
319                         break;
320                 case XCB_MOD_MASK_2:
321                         fprintf(rsp, "mod2");
322                         break;
323                 case XCB_MOD_MASK_3:
324                         fprintf(rsp, "mod3");
325                         break;
326                 case XCB_MOD_MASK_4:
327                         fprintf(rsp, "mod4");
328                         break;
329                 case XCB_MOD_MASK_5:
330                         fprintf(rsp, "mod5");
331                         break;
332         }
333 }
334
335 void print_pointer_action(pointer_action_t a, FILE *rsp)
336 {
337         switch (a) {
338                 case ACTION_MOVE:
339                         fprintf(rsp, "move");
340                         break;
341                 case ACTION_RESIZE_SIDE:
342                         fprintf(rsp, "resize_side");
343                         break;
344                 case ACTION_RESIZE_CORNER:
345                         fprintf(rsp, "resize_corner");
346                         break;
347                 case ACTION_FOCUS:
348                         fprintf(rsp, "focus");
349                         break;
350                 case ACTION_NONE:
351                         fprintf(rsp, "none");
352                         break;
353         }
354 }
355
356 node_select_t make_node_select(void)
357 {
358         node_select_t sel = {
359                 .automatic = OPTION_NONE,
360                 .focused = OPTION_NONE,
361                 .local = OPTION_NONE,
362                 .active = OPTION_NONE,
363                 .leaf = OPTION_NONE,
364                 .window = OPTION_NONE,
365                 .tiled = OPTION_NONE,
366                 .pseudo_tiled = OPTION_NONE,
367                 .floating = OPTION_NONE,
368                 .fullscreen = OPTION_NONE,
369                 .hidden = OPTION_NONE,
370                 .sticky = OPTION_NONE,
371                 .private = OPTION_NONE,
372                 .locked = OPTION_NONE,
373                 .urgent = OPTION_NONE,
374                 .same_class = OPTION_NONE,
375                 .descendant_of = OPTION_NONE,
376                 .ancestor_of = OPTION_NONE,
377                 .below = OPTION_NONE,
378                 .normal = OPTION_NONE,
379                 .above = OPTION_NONE
380         };
381         return sel;
382 }
383
384 desktop_select_t make_desktop_select(void)
385 {
386         desktop_select_t sel = {
387                 .occupied = OPTION_NONE,
388                 .focused = OPTION_NONE,
389                 .urgent = OPTION_NONE,
390                 .local = OPTION_NONE
391         };
392         return sel;
393 }
394
395 monitor_select_t make_monitor_select(void)
396 {
397         monitor_select_t sel = {
398                 .occupied = OPTION_NONE,
399                 .focused = OPTION_NONE
400         };
401         return sel;
402 }
403
404 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
405 {
406         coordinates_t ref_copy = *ref;
407         ref = &ref_copy;
408         char *desc_copy = copy_string(desc, strlen(desc));
409         desc = desc_copy;
410
411         char *hash = NULL;
412         char *path = strrchr(desc, '@');
413         if (path == NULL) {
414                 hash = strrchr(desc, '#');
415         } else {
416                 if (path != desc && *(path - 1) == '#') {
417                         hash = path - 1;
418                 }
419         }
420
421         if (hash != NULL) {
422                 *hash = '\0';
423                 int ret;
424                 coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
425                 if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
426                         desc = hash + 1;
427                 } else {
428                         free(desc_copy);
429                         return ret;
430                 }
431         }
432
433         node_select_t sel = make_node_select();
434         char *colon = strrchr(desc, ':');
435
436         if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
437                 free(desc_copy);
438                 return SELECTOR_BAD_MODIFIERS;
439         }
440
441         dst->node = NULL;
442
443         direction_t dir;
444         cycle_dir_t cyc;
445         history_dir_t hdi;
446         if (parse_direction(desc, &dir)) {
447                 find_nearest_neighbor(ref, dst, dir, sel);
448         } else if (parse_cycle_direction(desc, &cyc)) {
449                 find_closest_node(ref, dst, cyc, sel);
450         } else if (parse_history_direction(desc, &hdi)) {
451                 history_find_node(hdi, ref, dst, sel);
452         } else if (streq("last", desc)) {
453                 history_find_node(HISTORY_OLDER, ref, dst, sel);
454         } else if (streq("biggest", desc)) {
455                 find_biggest(ref, dst, sel);
456         } else if (streq("pointed", desc)) {
457                 xcb_window_t win = XCB_NONE;
458                 query_pointer(&win, NULL);
459                 if (locate_window(win, dst) && node_matches(dst, ref, sel)) {
460                         return SELECTOR_OK;
461                 } else {
462                         return SELECTOR_INVALID;
463                 }
464         } else if (streq("focused", desc)) {
465                 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
466                 if (node_matches(&loc, ref, sel)) {
467                         *dst = loc;
468                 }
469         } else if (*desc == '@') {
470                 desc++;
471                 *dst = *ref;
472                 if (colon != NULL) {
473                         *colon = '\0';
474                         int ret;
475                         if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
476                                 dst->node = dst->desktop->focus;
477                                 desc = colon + 1;
478                         } else {
479                                 free(desc_copy);
480                                 return ret;
481                         }
482                 }
483                 if (*desc == '/') {
484                         dst->node = dst->desktop->root;
485                 }
486                 char *move = strtok(desc, PTH_TOK);
487                 while (move != NULL && dst->node != NULL) {
488                         if (streq("first", move) || streq("1", move)) {
489                                 dst->node = dst->node->first_child;
490                         } else if (streq("second", move) || streq("2", move)) {
491                                 dst->node = dst->node->second_child;
492                         } else if (streq("parent", move)) {
493                                 dst->node = dst->node->parent;
494                         } else if (streq("brother", move)) {
495                                 dst->node = brother_tree(dst->node);
496                         } else {
497                                 direction_t dir;
498                                 if (parse_direction(move, &dir)) {
499                                         dst->node = find_fence(dst->node, dir);
500                                 } else {
501                                         free(desc_copy);
502                                         return SELECTOR_BAD_DESCRIPTOR;
503                                 }
504                         }
505                         move = strtok(NULL, PTH_TOK);
506                 }
507                 free(desc_copy);
508                 if (dst->node != NULL) {
509                         if (node_matches(dst, ref, sel)) {
510                                 return SELECTOR_OK;
511                         } else {
512                                 return SELECTOR_INVALID;
513                         }
514                 } else if (dst->desktop->root != NULL) {
515                         return SELECTOR_INVALID;
516                 }
517                 return SELECTOR_OK;
518         } else {
519                 uint32_t id;
520                 if (parse_id(desc, &id)) {
521                         free(desc_copy);
522                         if (find_by_id(id, dst) && node_matches(dst, ref, sel)) {
523                                 return SELECTOR_OK;
524                         } else {
525                                 return SELECTOR_INVALID;
526                         }
527                 } else {
528                         free(desc_copy);
529                         return SELECTOR_BAD_DESCRIPTOR;
530                 }
531         }
532
533         free(desc_copy);
534
535         if (dst->node == NULL) {
536                 return SELECTOR_INVALID;
537         }
538
539         return SELECTOR_OK;
540 }
541
542 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
543 {
544         coordinates_t ref_copy = *ref;
545         ref = &ref_copy;
546         char *desc_copy = copy_string(desc, strlen(desc));
547         desc = desc_copy;
548
549         char *hash = strrchr(desc, '#');
550
551         if (hash != NULL) {
552                 *hash = '\0';
553                 int ret;
554                 coordinates_t tmp = {mon, mon->desk, NULL};
555                 if ((ret = desktop_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
556                         desc = hash + 1;
557                 } else {
558                         free(desc_copy);
559                         return ret;
560                 }
561         }
562
563         desktop_select_t sel = make_desktop_select();
564         char *colon = strrchr(desc, ':');
565
566         if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
567                 free(desc_copy);
568                 return SELECTOR_BAD_MODIFIERS;
569         }
570
571         dst->desktop = NULL;
572
573         cycle_dir_t cyc;
574         history_dir_t hdi;
575         uint16_t idx;
576         uint32_t id;
577         if (parse_cycle_direction(desc, &cyc)) {
578                 find_closest_desktop(ref, dst, cyc, sel);
579         } else if (parse_history_direction(desc, &hdi)) {
580                 history_find_desktop(hdi, ref, dst, sel);
581         } else if (streq("last", desc)) {
582                 history_find_desktop(HISTORY_OLDER, ref, dst, sel);
583         } else if (streq("focused", desc)) {
584                 coordinates_t loc = {mon, mon->desk, NULL};
585                 if (desktop_matches(&loc, ref, sel)) {
586                         *dst = loc;
587                 }
588         } else if (colon != NULL) {
589                 *colon = '\0';
590                 int ret;
591                 if ((ret = monitor_from_desc(desc, ref, dst)) == SELECTOR_OK) {
592                         if (streq("focused", colon + 1)) {
593                                 coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
594                                 if (desktop_matches(&loc, ref, sel)) {
595                                         *dst = loc;
596                                 }
597                         } else if (parse_index(colon + 1, &idx)) {
598                                 free(desc_copy);
599                                 if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, sel)) {
600                                         return SELECTOR_OK;
601                                 } else {
602                                         return SELECTOR_INVALID;
603                                 }
604                         } else {
605                                 free(desc_copy);
606                                 return SELECTOR_BAD_DESCRIPTOR;
607                         }
608                 } else {
609                         free(desc_copy);
610                         return ret;
611                 }
612         } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
613                 free(desc_copy);
614                 if (desktop_matches(dst, ref, sel)) {
615                         return SELECTOR_OK;
616                 } else {
617                         return SELECTOR_INVALID;
618                 }
619         } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
620                 free(desc_copy);
621                 if (desktop_matches(dst, ref, sel)) {
622                         return SELECTOR_OK;
623                 } else {
624                         return SELECTOR_INVALID;
625                 }
626         } else {
627                 if (locate_desktop(desc, dst)) {
628                         free(desc_copy);
629                         if (desktop_matches(dst, ref, sel)) {
630                                 return SELECTOR_OK;
631                         } else {
632                                 return SELECTOR_INVALID;
633                         }
634                 } else {
635                         free(desc_copy);
636                         return SELECTOR_BAD_DESCRIPTOR;
637                 }
638         }
639
640         free(desc_copy);
641
642         if (dst->desktop == NULL) {
643                 return SELECTOR_INVALID;
644         }
645
646         return SELECTOR_OK;
647 }
648
649 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
650 {
651         coordinates_t ref_copy = *ref;
652         ref = &ref_copy;
653         char *desc_copy = copy_string(desc, strlen(desc));
654         desc = desc_copy;
655
656         char *hash = strrchr(desc, '#');
657
658         if (hash != NULL) {
659                 *hash = '\0';
660                 int ret;
661                 coordinates_t tmp = {mon, NULL, NULL};
662                 if ((ret = monitor_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
663                         desc = hash + 1;
664                 } else {
665                         free(desc_copy);
666                         return ret;
667                 }
668         }
669
670         monitor_select_t sel = make_monitor_select();
671
672         if (!parse_monitor_modifiers(desc, &sel)) {
673                 free(desc_copy);
674                 return SELECTOR_BAD_MODIFIERS;
675         }
676
677         dst->monitor = NULL;
678
679         direction_t dir;
680         cycle_dir_t cyc;
681         history_dir_t hdi;
682         uint16_t idx;
683         uint32_t id;
684         if (parse_direction(desc, &dir)) {
685                 dst->monitor = nearest_monitor(ref->monitor, dir, sel);
686         } else if (parse_cycle_direction(desc, &cyc)) {
687                 dst->monitor = closest_monitor(ref->monitor, cyc, sel);
688         } else if (parse_history_direction(desc, &hdi)) {
689                 history_find_monitor(hdi, ref, dst, sel);
690         } else if (streq("last", desc)) {
691                 history_find_monitor(HISTORY_OLDER, ref, dst, sel);
692         } else if (streq("primary", desc)) {
693                 if (pri_mon != NULL) {
694                         coordinates_t loc = {pri_mon, NULL, NULL};
695                         if (monitor_matches(&loc, ref, sel)) {
696                                 dst->monitor = pri_mon;
697                         }
698                 }
699         } else if (streq("focused", desc)) {
700                 coordinates_t loc = {mon, NULL, NULL};
701                 if (monitor_matches(&loc, ref, sel)) {
702                         dst->monitor = mon;
703                 }
704         } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
705                 free(desc_copy);
706                 if (monitor_matches(dst, ref, sel)) {
707                         return SELECTOR_OK;
708                 } else {
709                         return SELECTOR_INVALID;
710                 }
711         } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
712                 free(desc_copy);
713                 if (monitor_matches(dst, ref, sel)) {
714                         return SELECTOR_OK;
715                 } else {
716                         return SELECTOR_INVALID;
717                 }
718         } else {
719                 if (locate_monitor(desc, dst)) {
720                         free(desc_copy);
721                         if (monitor_matches(dst, ref, sel)) {
722                                 return SELECTOR_OK;
723                         } else {
724                                 return SELECTOR_INVALID;
725                         }
726                 } else {
727                         free(desc_copy);
728                         return SELECTOR_BAD_DESCRIPTOR;
729                 }
730         }
731
732         free(desc_copy);
733
734         if (dst->monitor == NULL) {
735                 return SELECTOR_INVALID;
736         }
737
738         return SELECTOR_OK;
739 }
740
741 bool locate_window(xcb_window_t win, coordinates_t *loc)
742 {
743         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
744                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
745                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
746                                 if (n->client == NULL) {
747                                         continue;
748                                 }
749                                 if (n->id == win) {
750                                         loc->monitor = m;
751                                         loc->desktop = d;
752                                         loc->node = n;
753                                         return true;
754                                 }
755                         }
756                 }
757         }
758         return false;
759 }
760
761 bool locate_desktop(char *name, coordinates_t *loc)
762 {
763         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
764                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
765                         if (streq(d->name, name)) {
766                                 loc->monitor = m;
767                                 loc->desktop = d;
768                                 return true;
769                         }
770                 }
771         }
772         return false;
773 }
774
775 bool locate_monitor(char *name, coordinates_t *loc)
776 {
777         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
778                 if (streq(m->name, name)) {
779                         loc->monitor = m;
780                         return true;
781                 }
782         }
783         return false;
784 }
785
786 bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
787 {
788         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
789                 if (mm != NULL && m != mm) {
790                         continue;
791                 }
792                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
793                         if (d->id == id) {
794                                 loc->monitor = m;
795                                 loc->desktop = d;
796                                 loc->node = NULL;
797                                 return true;
798                         }
799                 }
800         }
801         return false;
802 }
803
804 bool desktop_from_index(uint16_t idx, coordinates_t *loc, monitor_t *mm)
805 {
806         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
807                 if (mm != NULL && m != mm) {
808                         continue;
809                 }
810                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, idx--) {
811                         if (idx == 1) {
812                                 loc->monitor = m;
813                                 loc->desktop = d;
814                                 loc->node = NULL;
815                                 return true;
816                         }
817                 }
818         }
819         return false;
820 }
821
822 bool monitor_from_id(uint32_t id, coordinates_t *loc)
823 {
824         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
825                 if (m->id == id) {
826                         loc->monitor = m;
827                         loc->desktop = NULL;
828                         loc->node = NULL;
829                         return true;
830                 }
831         }
832         return false;
833 }
834
835 bool monitor_from_index(int idx, coordinates_t *loc)
836 {
837         for (monitor_t *m = mon_head; m != NULL; m = m->next, idx--) {
838                 if (idx == 1) {
839                         loc->monitor = m;
840                         loc->desktop = NULL;
841                         loc->node = NULL;
842                         return true;
843                 }
844         }
845         return false;
846 }
847
848 bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t sel)
849 {
850         if (loc->node == NULL) {
851                 return false;
852         }
853
854         if (sel.focused != OPTION_NONE &&
855             loc->node != loc->desktop->focus
856             ? sel.focused == OPTION_TRUE
857             : sel.focused == OPTION_FALSE) {
858                 return false;
859         }
860
861         if (sel.automatic != OPTION_NONE &&
862             loc->node->presel != NULL
863             ? sel.automatic == OPTION_TRUE
864             : sel.automatic == OPTION_FALSE) {
865                 return false;
866         }
867
868         if (sel.local != OPTION_NONE &&
869             loc->desktop != ref->desktop
870             ? sel.local == OPTION_TRUE
871             : sel.local == OPTION_FALSE) {
872                 return false;
873         }
874
875         if (sel.active != OPTION_NONE &&
876             loc->desktop != loc->monitor->desk
877             ? sel.active == OPTION_TRUE
878             : sel.active == OPTION_FALSE) {
879                 return false;
880         }
881
882         if (sel.leaf != OPTION_NONE &&
883             !is_leaf(loc->node)
884             ? sel.leaf == OPTION_TRUE
885             : sel.leaf == OPTION_FALSE) {
886                 return false;
887         }
888
889         if (sel.window != OPTION_NONE &&
890             loc->node->client == NULL
891             ? sel.window == OPTION_TRUE
892             : sel.window == OPTION_FALSE) {
893                 return false;
894         }
895
896 #define NFLAG(p) \
897         if (sel.p != OPTION_NONE && \
898             !loc->node->p \
899             ? sel.p == OPTION_TRUE \
900             : sel.p == OPTION_FALSE) { \
901                 return false; \
902         }
903         NFLAG(hidden)
904         NFLAG(sticky)
905         NFLAG(private)
906         NFLAG(locked)
907 #undef NFLAG
908
909         if (loc->node->client == NULL &&
910                 (sel.same_class != OPTION_NONE ||
911                  sel.tiled != OPTION_NONE ||
912                  sel.pseudo_tiled != OPTION_NONE ||
913                  sel.floating != OPTION_NONE ||
914                  sel.fullscreen != OPTION_NONE ||
915                  sel.below != OPTION_NONE ||
916                  sel.normal != OPTION_NONE ||
917                  sel.above != OPTION_NONE ||
918                  sel.urgent != OPTION_NONE)) {
919                 return false;
920         }
921
922         if (ref->node != NULL && ref->node->client != NULL &&
923             sel.same_class != OPTION_NONE &&
924             streq(loc->node->client->class_name, ref->node->client->class_name)
925             ? sel.same_class == OPTION_FALSE
926             : sel.same_class == OPTION_TRUE) {
927                 return false;
928         }
929
930         if (sel.descendant_of != OPTION_NONE &&
931             !is_descendant(loc->node, ref->node)
932             ? sel.descendant_of == OPTION_TRUE
933             : sel.descendant_of == OPTION_FALSE) {
934                 return false;
935         }
936
937         if (sel.ancestor_of != OPTION_NONE &&
938             !is_descendant(ref->node, loc->node)
939             ? sel.ancestor_of == OPTION_TRUE
940             : sel.ancestor_of == OPTION_FALSE) {
941                 return false;
942         }
943
944 #define WSTATE(p, e) \
945         if (sel.p != OPTION_NONE && \
946             loc->node->client->state != e \
947             ? sel.p == OPTION_TRUE \
948             : sel.p == OPTION_FALSE) { \
949                 return false; \
950         }
951         WSTATE(tiled, STATE_TILED)
952         WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
953         WSTATE(floating, STATE_FLOATING)
954         WSTATE(fullscreen, STATE_FULLSCREEN)
955 #undef WSTATE
956
957 #define WLAYER(p, e) \
958         if (sel.p != OPTION_NONE && \
959             loc->node->client->layer != e \
960             ? sel.p == OPTION_TRUE \
961             : sel.p == OPTION_FALSE) { \
962                 return false; \
963         }
964         WLAYER(below, LAYER_BELOW)
965         WLAYER(normal, LAYER_NORMAL)
966         WLAYER(above, LAYER_ABOVE)
967 #undef WLAYER
968
969 #define WFLAG(p) \
970         if (sel.p != OPTION_NONE && \
971             !loc->node->client->p \
972             ? sel.p == OPTION_TRUE \
973             : sel.p == OPTION_FALSE) { \
974                 return false; \
975         }
976         WFLAG(urgent)
977 #undef WFLAG
978
979         return true;
980 }
981
982 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
983 {
984         if (sel.occupied != OPTION_NONE &&
985             loc->desktop->root == NULL
986             ? sel.occupied == OPTION_TRUE
987             : sel.occupied == OPTION_FALSE) {
988                 return false;
989         }
990
991         if (sel.focused != OPTION_NONE &&
992             loc->desktop != loc->monitor->desk
993             ? sel.focused == OPTION_TRUE
994             : sel.focused == OPTION_FALSE) {
995                 return false;
996         }
997
998         if (sel.urgent != OPTION_NONE &&
999             !is_urgent(loc->desktop)
1000             ? sel.urgent == OPTION_TRUE
1001             : sel.urgent == OPTION_FALSE) {
1002                 return false;
1003         }
1004
1005         if (sel.local != OPTION_NONE &&
1006             ref->monitor != loc->monitor
1007             ? sel.local == OPTION_TRUE
1008             : sel.local == OPTION_FALSE) {
1009                 return false;
1010         }
1011
1012         return true;
1013 }
1014
1015 bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t sel)
1016 {
1017         if (sel.occupied != OPTION_NONE &&
1018             loc->monitor->desk->root == NULL
1019             ? sel.occupied == OPTION_TRUE
1020             : sel.occupied == OPTION_FALSE) {
1021                 return false;
1022         }
1023
1024         if (sel.focused != OPTION_NONE &&
1025             mon != loc->monitor
1026             ? sel.focused == OPTION_TRUE
1027             : sel.focused == OPTION_FALSE) {
1028                 return false;
1029         }
1030
1031         return true;
1032 }