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