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