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