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