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