1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
38 void query_state(FILE *rsp)
41 fprintf(rsp, "\"focusedMonitorId\":%u,", mon->id);
42 if (pri_mon != NULL) {
43 fprintf(rsp, "\"primaryMonitorId\":%u,", pri_mon->id);
45 fprintf(rsp, "\"clientsCount\":%i,", clients_count);
46 fprintf(rsp, "\"monitors\":");
48 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
49 query_monitor(m, rsp);
50 if (m->next != NULL) {
56 fprintf(rsp, "\"focusHistory\":");
59 fprintf(rsp, "\"stackingList\":");
63 fprintf(rsp, "\"eventSubscribers\":");
64 query_subscribers(rsp);
69 void query_monitor(monitor_t *m, FILE *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, "\"borderRadius\":%u,", m->border_radius);
80 fprintf(rsp, "\"focusedDesktopId\":%u,", m->desk->id);
81 fprintf(rsp, "\"padding\":");
82 query_padding(m->padding, rsp);
84 fprintf(rsp, "\"rectangle\":");
85 query_rectangle(m->rectangle, rsp);
87 fprintf(rsp, "\"desktops\":");
89 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
90 query_desktop(d, rsp);
91 if (d->next != NULL) {
99 void query_desktop(desktop_t *d, FILE *rsp)
102 fprintf(rsp, "\"name\":\"%s\",", d->name);
103 fprintf(rsp, "\"id\":%u,", d->id);
104 fprintf(rsp, "\"layout\":\"%s\",", LAYOUT_STR(d->layout));
105 fprintf(rsp, "\"userLayout\":\"%s\",", LAYOUT_STR(d->user_layout));
106 fprintf(rsp, "\"windowGap\":%i,", d->window_gap);
107 fprintf(rsp, "\"borderWidth\":%u,", d->border_width);
108 fprintf(rsp, "\"borderRadius\":%u,", d->border_radius);
109 fprintf(rsp, "\"focusedNodeId\":%u,", d->focus != NULL ? d->focus->id : 0);
110 fprintf(rsp, "\"padding\":");
111 query_padding(d->padding, rsp);
113 fprintf(rsp, "\"root\":");
114 query_node(d->root, rsp);
118 void query_node(node_t *n, FILE *rsp)
121 fprintf(rsp, "null");
124 fprintf(rsp, "\"id\":%u,", n->id);
125 fprintf(rsp, "\"splitType\":\"%s\",", SPLIT_TYPE_STR(n->split_type));
126 fprintf(rsp, "\"splitRatio\":%lf,", n->split_ratio);
127 fprintf(rsp, "\"vacant\":%s,", BOOL_STR(n->vacant));
128 fprintf(rsp, "\"hidden\":%s,", BOOL_STR(n->hidden));
129 fprintf(rsp, "\"sticky\":%s,", BOOL_STR(n->sticky));
130 fprintf(rsp, "\"private\":%s,", BOOL_STR(n->private));
131 fprintf(rsp, "\"locked\":%s,", BOOL_STR(n->locked));
132 fprintf(rsp, "\"marked\":%s,", BOOL_STR(n->marked));
133 fprintf(rsp, "\"presel\":");
134 query_presel(n->presel, rsp);
136 fprintf(rsp, "\"rectangle\":");
137 query_rectangle(n->rectangle, rsp);
139 fprintf(rsp, "\"constraints\":");
140 query_constraints(n->constraints, rsp);
142 fprintf(rsp, "\"firstChild\":");
143 query_node(n->first_child, rsp);
145 fprintf(rsp, "\"secondChild\":");
146 query_node(n->second_child, rsp);
148 fprintf(rsp, "\"client\":");
149 query_client(n->client, rsp);
154 void query_presel(presel_t *p, FILE *rsp)
157 fprintf(rsp, "null");
159 fprintf(rsp, "{\"splitDir\":\"%s\",\"splitRatio\":%lf}", SPLIT_DIR_STR(p->split_dir), p->split_ratio);
163 void query_client(client_t *c, FILE *rsp)
166 fprintf(rsp, "null");
169 fprintf(rsp, "\"className\":\"%s\",", c->class_name);
170 fprintf(rsp, "\"instanceName\":\"%s\",", c->instance_name);
171 fprintf(rsp, "\"borderWidth\":%u,", c->border_width);
172 fprintf(rsp, "\"borderRadius\":%u,", c->border_radius);
173 fprintf(rsp, "\"state\":\"%s\",", STATE_STR(c->state));
174 fprintf(rsp, "\"lastState\":\"%s\",", STATE_STR(c->last_state));
175 fprintf(rsp, "\"layer\":\"%s\",", LAYER_STR(c->layer));
176 fprintf(rsp, "\"lastLayer\":\"%s\",", LAYER_STR(c->last_layer));
177 fprintf(rsp, "\"urgent\":%s,", BOOL_STR(c->urgent));
178 fprintf(rsp, "\"shown\":%s,", BOOL_STR(c->shown));
179 fprintf(rsp, "\"tiledRectangle\":");
180 query_rectangle(c->tiled_rectangle, rsp);
182 fprintf(rsp, "\"floatingRectangle\":");
183 query_rectangle(c->floating_rectangle, rsp);
188 void query_rectangle(xcb_rectangle_t r, FILE *rsp)
190 fprintf(rsp, "{\"x\":%i,\"y\":%i,\"width\":%u,\"height\":%u}", r.x, r.y, r.width, r.height);
193 void query_constraints(constraints_t c, FILE *rsp)
195 fprintf(rsp, "{\"min_width\":%u,\"min_height\":%u}", c.min_width, c.min_height);
198 void query_padding(padding_t p, FILE *rsp)
200 fprintf(rsp, "{\"top\":%i,\"right\":%i,\"bottom\":%i,\"left\":%i}", p.top, p.right, p.bottom, p.left);
203 void query_history(FILE *rsp)
206 for (history_t *h = history_head; h != NULL; h = h->next) {
207 query_coordinates(&h->loc, rsp);
208 if (h->next != NULL) {
215 void query_coordinates(coordinates_t *loc, FILE *rsp)
217 fprintf(rsp, "{\"monitorId\":%u,\"desktopId\":%u,\"nodeId\":%u}", loc->monitor->id, loc->desktop->id, loc->node!=NULL?loc->node->id:0);
220 void query_stack(FILE *rsp)
223 for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
224 fprintf(rsp, "%u", s->node->id);
225 if (s->next != NULL) {
232 void query_subscribers(FILE *rsp)
235 for (subscriber_list_t *s = subscribe_head; s != NULL; s = s->next) {
236 fprintf(rsp, "{\"fileDescriptor\": %i", fileno(s->stream));
237 if (s->fifo_path != NULL) {
238 fprintf(rsp, ",\"fifoPath\":\"%s\"", s->fifo_path);
240 fprintf(rsp, ",\"field\":%i,\"count\":%i}", s->field, s->count);
241 if (s->next != NULL) {
248 int query_node_ids(coordinates_t *mon_ref, coordinates_t *desk_ref, coordinates_t* ref, coordinates_t *trg, monitor_select_t *mon_sel, desktop_select_t *desk_sel, node_select_t *sel, FILE *rsp)
251 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
252 coordinates_t loc = {m, NULL, NULL};
253 if ((trg->monitor != NULL && m != trg->monitor) ||
254 (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
257 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
258 coordinates_t loc = {m, d, NULL};
259 if ((trg->desktop != NULL && d != trg->desktop) ||
260 (desk_sel != NULL && !desktop_matches(&loc, desk_ref, desk_sel))) {
263 count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
269 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)
275 coordinates_t loc = {m, d, n};
276 if ((trg->node == NULL || n == trg->node) &&
277 (sel == NULL || node_matches(&loc, ref, sel))) {
278 fprintf(rsp, "0x%08X\n", n->id);
281 count += query_node_ids_in(n->first_child, d, m, ref, trg, sel, rsp);
282 count += query_node_ids_in(n->second_child, d, m, ref, trg, sel, rsp);
287 int query_desktop_ids(coordinates_t* mon_ref, coordinates_t *ref, coordinates_t *trg, monitor_select_t *mon_sel, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
290 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
291 coordinates_t loc = {m, NULL, NULL};
292 if ((trg->monitor != NULL && m != trg->monitor) ||
293 (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
296 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
297 coordinates_t loc = {m, d, NULL};
298 if ((trg->desktop != NULL && d != trg->desktop) ||
299 (sel != NULL && !desktop_matches(&loc, ref, sel))) {
309 int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *sel, monitor_printer_t printer, FILE *rsp)
312 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
313 coordinates_t loc = {m, NULL, NULL};
314 if ((trg->monitor != NULL && m != trg->monitor) ||
315 (sel != NULL && !monitor_matches(&loc, ref, sel))) {
324 void fprint_monitor_id(monitor_t *m, FILE *rsp)
326 fprintf(rsp, "0x%08X\n", m->id);
329 void fprint_monitor_name(monitor_t *m, FILE *rsp)
331 fprintf(rsp, "%s\n", m->name);
334 void fprint_desktop_id(desktop_t *d, FILE *rsp)
336 fprintf(rsp, "0x%08X\n", d->id);
339 void fprint_desktop_name(desktop_t *d, FILE *rsp)
341 fprintf(rsp, "%s\n", d->name);
344 void print_ignore_request(state_transition_t st, FILE *rsp)
347 fprintf(rsp, "none");
349 unsigned int cnt = 0;
350 if (st & STATE_TRANSITION_ENTER) {
351 fprintf(rsp, "enter");
354 if (st & STATE_TRANSITION_EXIT) {
355 fprintf(rsp, "%sexit", cnt > 0 ? "," : "");
360 void print_modifier_mask(uint16_t m, FILE *rsp)
363 case XCB_MOD_MASK_SHIFT:
364 fprintf(rsp, "shift");
366 case XCB_MOD_MASK_CONTROL:
367 fprintf(rsp, "control");
369 case XCB_MOD_MASK_LOCK:
370 fprintf(rsp, "lock");
373 fprintf(rsp, "mod1");
376 fprintf(rsp, "mod2");
379 fprintf(rsp, "mod3");
382 fprintf(rsp, "mod4");
385 fprintf(rsp, "mod5");
390 void print_button_index(int8_t b, FILE *rsp)
393 case XCB_BUTTON_INDEX_ANY:
396 case XCB_BUTTON_INDEX_1:
397 fprintf(rsp, "button1");
399 case XCB_BUTTON_INDEX_2:
400 fprintf(rsp, "button2");
402 case XCB_BUTTON_INDEX_3:
403 fprintf(rsp, "button3");
406 fprintf(rsp, "none");
411 void print_pointer_action(pointer_action_t a, FILE *rsp)
415 fprintf(rsp, "move");
417 case ACTION_RESIZE_SIDE:
418 fprintf(rsp, "resize_side");
420 case ACTION_RESIZE_CORNER:
421 fprintf(rsp, "resize_corner");
424 fprintf(rsp, "focus");
427 fprintf(rsp, "none");
432 void resolve_rule_consequence(rule_consequence_t *csq)
434 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
435 coordinates_t dst = {NULL, NULL, NULL};
436 monitor_t *monitor = monitor_from_desc(csq->monitor_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.monitor;
437 desktop_t *desktop = desktop_from_desc(csq->desktop_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.desktop;
438 node_t *node = node_from_desc(csq->node_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.node;
440 #define PRINT_OBJECT_ID(name) \
441 if (name == NULL) { \
442 csq->name##_desc[0] = '\0'; \
444 snprintf(csq->name##_desc, 11, "0x%08X", name->id); \
446 PRINT_OBJECT_ID(monitor)
447 PRINT_OBJECT_ID(desktop)
448 PRINT_OBJECT_ID(node)
449 #undef PRINT_OBJECT_ID
452 void print_rule_consequence(char **buf, rule_consequence_t *csq)
454 char *rect_buf = NULL;
455 print_rectangle(&rect_buf, csq->rect);
456 if (rect_buf == NULL) {
457 rect_buf = malloc(1);
461 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",
462 csq->monitor_desc, csq->desktop_desc, csq->node_desc,
463 csq->state == NULL ? "" : STATE_STR(*csq->state),
464 csq->layer == NULL ? "" : LAYER_STR(*csq->layer),
465 csq->split_dir == NULL ? "" : SPLIT_DIR_STR(*csq->split_dir), csq->split_ratio,
466 ON_OFF_STR(csq->hidden), ON_OFF_STR(csq->sticky), ON_OFF_STR(csq->private),
467 ON_OFF_STR(csq->locked), ON_OFF_STR(csq->marked), ON_OFF_STR(csq->center), ON_OFF_STR(csq->follow),
468 ON_OFF_STR(csq->manage), ON_OFF_STR(csq->focus), ON_OFF_STR(csq->border), rect_buf);
472 void print_rectangle(char **buf, xcb_rectangle_t *rect)
475 asprintf(buf, "%hux%hu+%hi+%hi", rect->width, rect->height, rect->x, rect->y);
479 node_select_t make_node_select(void)
481 node_select_t sel = {
482 .automatic = OPTION_NONE,
483 .focused = OPTION_NONE,
484 .active = OPTION_NONE,
485 .local = OPTION_NONE,
487 .window = OPTION_NONE,
488 .tiled = OPTION_NONE,
489 .pseudo_tiled = OPTION_NONE,
490 .floating = OPTION_NONE,
491 .fullscreen = OPTION_NONE,
492 .hidden = OPTION_NONE,
493 .sticky = OPTION_NONE,
494 .private = OPTION_NONE,
495 .locked = OPTION_NONE,
496 .marked = OPTION_NONE,
497 .urgent = OPTION_NONE,
498 .same_class = OPTION_NONE,
499 .descendant_of = OPTION_NONE,
500 .ancestor_of = OPTION_NONE,
501 .below = OPTION_NONE,
502 .normal = OPTION_NONE,
503 .above = OPTION_NONE,
504 .horizontal = OPTION_NONE,
505 .vertical = OPTION_NONE
510 desktop_select_t make_desktop_select(void)
512 desktop_select_t sel = {
513 .occupied = OPTION_NONE,
514 .focused = OPTION_NONE,
515 .active = OPTION_NONE,
516 .urgent = OPTION_NONE,
517 .local = OPTION_NONE,
518 .tiled = OPTION_NONE,
519 .monocle = OPTION_NONE,
520 .user_tiled = OPTION_NONE,
521 .user_monocle = OPTION_NONE
526 monitor_select_t make_monitor_select(void)
528 monitor_select_t sel = {
529 .occupied = OPTION_NONE,
530 .focused = OPTION_NONE
535 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
539 coordinates_t ref_copy = *ref;
541 char *desc_copy = copy_string(desc, strlen(desc));
544 char *hash = strrchr(desc, '#');
545 char *path = strrchr(desc, '@');
546 char *colon = strrchr(desc, ':');
548 /* Adjust or discard hashes inside a DESKTOP_SEL, e.g. `newest#@prev#older:/1/2` */
549 if (hash != NULL && colon != NULL && path != NULL &&
550 path < hash && hash < colon) {
551 if (path > desc && *(path - 1) == '#') {
561 coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
562 if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
570 /* Discard colons within references, e.g. `@next.occupied:/#any.descendant_of.window` */
571 if (colon != NULL && hash != NULL && colon < hash) {
575 node_select_t sel = make_node_select();
577 if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
579 return SELECTOR_BAD_MODIFIERS;
585 if (parse_direction(desc, &dir)) {
586 find_nearest_neighbor(ref, dst, dir, &sel);
587 } else if (parse_cycle_direction(desc, &cyc)) {
588 find_closest_node(ref, dst, cyc, &sel);
589 } else if (parse_history_direction(desc, &hdi)) {
590 history_find_node(hdi, ref, dst, &sel);
591 } else if (streq("any", desc)) {
592 find_any_node(ref, dst, &sel);
593 } else if (streq("first_ancestor", desc)) {
594 find_first_ancestor(ref, dst, &sel);
595 } else if (streq("last", desc)) {
596 history_find_node(HISTORY_OLDER, ref, dst, &sel);
597 } else if (streq("newest", desc)) {
598 history_find_newest_node(ref, dst, &sel);
599 } else if (streq("biggest", desc)) {
600 find_by_area(AREA_BIGGEST, ref, dst, &sel);
601 } else if (streq("smallest", desc)) {
602 find_by_area(AREA_SMALLEST, ref, dst, &sel);
603 } else if (streq("pointed", desc)) {
604 xcb_window_t win = XCB_NONE;
605 query_pointer(&win, NULL);
606 if (locate_leaf(win, dst) && node_matches(dst, ref, &sel)) {
609 return SELECTOR_INVALID;
611 } else if (streq("focused", desc)) {
612 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
613 if (node_matches(&loc, ref, &sel)) {
616 } else if (*desc == '@') {
622 if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
623 dst->node = dst->desktop->focus;
631 dst->node = dst->desktop->root;
633 char *move = strtok(desc, PTH_TOK);
634 while (move != NULL && dst->node != NULL) {
635 if (streq("first", move) || streq("1", move)) {
636 dst->node = dst->node->first_child;
637 } else if (streq("second", move) || streq("2", move)) {
638 dst->node = dst->node->second_child;
639 } else if (streq("parent", move)) {
640 dst->node = dst->node->parent;
641 } else if (streq("brother", move)) {
642 dst->node = brother_tree(dst->node);
645 if (parse_direction(move, &dir)) {
646 dst->node = find_fence(dst->node, dir);
649 return SELECTOR_BAD_DESCRIPTOR;
652 move = strtok(NULL, PTH_TOK);
655 if (dst->node != NULL) {
656 if (node_matches(dst, ref, &sel)) {
659 return SELECTOR_INVALID;
661 } else if (dst->desktop->root != NULL) {
662 return SELECTOR_INVALID;
667 if (parse_id(desc, &id)) {
669 if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
672 return SELECTOR_INVALID;
676 return SELECTOR_BAD_DESCRIPTOR;
682 if (dst->node == NULL) {
683 return SELECTOR_INVALID;
689 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
694 locate_desktop(desc + 1, dst);
698 coordinates_t ref_copy = *ref;
700 char *desc_copy = copy_string(desc, strlen(desc));
703 char *hash = strrchr(desc, '#');
704 char *colon = strrchr(desc, ':');
706 /* Discard hashes inside a MONITOR_SEL, e.g. `primary#next:focused` */
707 if (hash != NULL && colon != NULL && hash < colon) {
714 coordinates_t tmp = {mon, mon->desk, NULL};
715 if ((ret = desktop_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
723 /* Discard colons within references, e.g. `DisplayPort-1:focused#next.local` */
724 if (colon != NULL && hash != NULL && colon < hash) {
728 desktop_select_t sel = make_desktop_select();
730 if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
732 return SELECTOR_BAD_MODIFIERS;
739 if (parse_cycle_direction(desc, &cyc)) {
740 find_closest_desktop(ref, dst, cyc, &sel);
741 } else if (parse_history_direction(desc, &hdi)) {
742 history_find_desktop(hdi, ref, dst, &sel);
743 } else if (streq("any", desc)) {
744 find_any_desktop(ref, dst, &sel);
745 } else if (streq("last", desc)) {
746 history_find_desktop(HISTORY_OLDER, ref, dst, &sel);
747 } else if (streq("newest", desc)) {
748 history_find_newest_desktop(ref, dst, &sel);
749 } else if (streq("focused", desc)) {
750 coordinates_t loc = {mon, mon->desk, NULL};
751 if (desktop_matches(&loc, ref, &sel)) {
754 } else if (colon != NULL) {
757 if ((ret = monitor_from_desc(desc, ref, dst)) == SELECTOR_OK) {
758 if (streq("focused", colon + 1)) {
759 coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
760 if (desktop_matches(&loc, ref, &sel)) {
763 } else if (parse_index(colon + 1, &idx)) {
765 if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, &sel)) {
768 return SELECTOR_INVALID;
772 return SELECTOR_BAD_DESCRIPTOR;
778 } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
780 if (desktop_matches(dst, ref, &sel)) {
783 return SELECTOR_INVALID;
785 } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
787 if (desktop_matches(dst, ref, &sel)) {
790 return SELECTOR_INVALID;
794 if (desktop_from_name(desc, ref, dst, &sel, &hits)) {
800 return SELECTOR_INVALID;
802 return SELECTOR_BAD_DESCRIPTOR;
810 if (dst->desktop == NULL) {
811 return SELECTOR_INVALID;
817 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
822 locate_monitor(desc + 1, dst);
826 coordinates_t ref_copy = *ref;
828 char *desc_copy = copy_string(desc, strlen(desc));
831 char *hash = strrchr(desc, '#');
836 coordinates_t tmp = {mon, NULL, NULL};
837 if ((ret = monitor_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
845 monitor_select_t sel = make_monitor_select();
847 if (!parse_monitor_modifiers(desc, &sel)) {
849 return SELECTOR_BAD_MODIFIERS;
857 if (parse_direction(desc, &dir)) {
858 dst->monitor = nearest_monitor(ref->monitor, dir, &sel);
859 } else if (parse_cycle_direction(desc, &cyc)) {
860 dst->monitor = closest_monitor(ref->monitor, cyc, &sel);
861 } else if (parse_history_direction(desc, &hdi)) {
862 history_find_monitor(hdi, ref, dst, &sel);
863 } else if (streq("any", desc)) {
864 find_any_monitor(ref, dst, &sel);
865 } else if (streq("last", desc)) {
866 history_find_monitor(HISTORY_OLDER, ref, dst, &sel);
867 } else if (streq("newest", desc)) {
868 history_find_newest_monitor(ref, dst, &sel);
869 } else if (streq("primary", desc)) {
870 if (pri_mon != NULL) {
871 coordinates_t loc = {pri_mon, NULL, NULL};
872 if (monitor_matches(&loc, ref, &sel)) {
873 dst->monitor = pri_mon;
876 } else if (streq("focused", desc)) {
877 coordinates_t loc = {mon, NULL, NULL};
878 if (monitor_matches(&loc, ref, &sel)) {
881 } else if (streq("pointed", desc)) {
883 query_pointer(NULL, &pointer);
884 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
885 if (is_inside(pointer, m->rectangle)) {
890 } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
892 if (monitor_matches(dst, ref, &sel)) {
895 return SELECTOR_INVALID;
897 } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
899 if (monitor_matches(dst, ref, &sel)) {
902 return SELECTOR_INVALID;
905 if (locate_monitor(desc, dst)) {
907 if (monitor_matches(dst, ref, &sel)) {
910 return SELECTOR_INVALID;
914 return SELECTOR_BAD_DESCRIPTOR;
921 if (dst->monitor == NULL) {
922 return SELECTOR_INVALID;
928 bool locate_leaf(xcb_window_t win, coordinates_t *loc)
930 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
931 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
932 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
945 bool locate_window(xcb_window_t win, coordinates_t *loc)
947 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
948 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
949 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
950 if (n->client == NULL) {
965 bool locate_desktop(char *name, coordinates_t *loc)
967 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
968 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
969 if (streq(d->name, name)) {
979 bool locate_monitor(char *name, coordinates_t *loc)
981 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
982 if (streq(m->name, name)) {
990 bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
992 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
993 if (mm != NULL && m != mm) {
996 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1008 bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel, int *hits)
1010 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1011 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1012 if (streq(d->name, name)) {
1016 coordinates_t loc = {m, d, NULL};
1017 if (desktop_matches(&loc, ref, sel)) {
1028 bool desktop_from_index(uint16_t idx, coordinates_t *loc, monitor_t *mm)
1030 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1031 if (mm != NULL && m != mm) {
1034 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, idx--) {
1046 bool monitor_from_id(uint32_t id, coordinates_t *loc)
1048 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1051 loc->desktop = NULL;
1059 bool monitor_from_index(int idx, coordinates_t *loc)
1061 for (monitor_t *m = mon_head; m != NULL; m = m->next, idx--) {
1064 loc->desktop = NULL;
1072 bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
1074 if (loc->node == NULL) {
1078 if (sel->focused != OPTION_NONE &&
1079 loc->node != mon->desk->focus
1080 ? sel->focused == OPTION_TRUE
1081 : sel->focused == OPTION_FALSE) {
1085 if (sel->active != OPTION_NONE &&
1086 loc->node != loc->desktop->focus
1087 ? sel->active == OPTION_TRUE
1088 : sel->active == OPTION_FALSE) {
1092 if (sel->automatic != OPTION_NONE &&
1093 loc->node->presel != NULL
1094 ? sel->automatic == OPTION_TRUE
1095 : sel->automatic == OPTION_FALSE) {
1099 if (sel->local != OPTION_NONE &&
1100 loc->desktop != ref->desktop
1101 ? sel->local == OPTION_TRUE
1102 : sel->local == OPTION_FALSE) {
1106 if (sel->active != OPTION_NONE &&
1107 loc->desktop != loc->monitor->desk
1108 ? sel->active == OPTION_TRUE
1109 : sel->active == OPTION_FALSE) {
1113 if (sel->leaf != OPTION_NONE &&
1115 ? sel->leaf == OPTION_TRUE
1116 : sel->leaf == OPTION_FALSE) {
1120 if (sel->window != OPTION_NONE &&
1121 loc->node->client == NULL
1122 ? sel->window == OPTION_TRUE
1123 : sel->window == OPTION_FALSE) {
1128 if (sel->p != OPTION_NONE && \
1130 ? sel->p == OPTION_TRUE \
1131 : sel->p == OPTION_FALSE) { \
1141 #define NSPLIT(p, e) \
1142 if (sel->p != OPTION_NONE && \
1143 loc->node->split_type != e \
1144 ? sel->p == OPTION_TRUE \
1145 : sel->p == OPTION_FALSE) { \
1148 NSPLIT(horizontal, TYPE_HORIZONTAL)
1149 NSPLIT(vertical, TYPE_VERTICAL)
1152 if (sel->descendant_of != OPTION_NONE &&
1153 !is_descendant(loc->node, ref->node)
1154 ? sel->descendant_of == OPTION_TRUE
1155 : sel->descendant_of == OPTION_FALSE) {
1159 if (sel->ancestor_of != OPTION_NONE &&
1160 !is_descendant(ref->node, loc->node)
1161 ? sel->ancestor_of == OPTION_TRUE
1162 : sel->ancestor_of == OPTION_FALSE) {
1166 if (loc->node->client == NULL) {
1167 if (sel->same_class == OPTION_TRUE ||
1168 sel->tiled == OPTION_TRUE ||
1169 sel->pseudo_tiled == OPTION_TRUE ||
1170 sel->floating == OPTION_TRUE ||
1171 sel->fullscreen == OPTION_TRUE ||
1172 sel->below == OPTION_TRUE ||
1173 sel->normal == OPTION_TRUE ||
1174 sel->above == OPTION_TRUE ||
1175 sel->urgent == OPTION_TRUE) {
1181 if (ref->node != NULL && ref->node->client != NULL &&
1182 sel->same_class != OPTION_NONE &&
1183 streq(loc->node->client->class_name, ref->node->client->class_name)
1184 ? sel->same_class == OPTION_FALSE
1185 : sel->same_class == OPTION_TRUE) {
1189 #define WSTATE(p, e) \
1190 if (sel->p != OPTION_NONE && \
1191 loc->node->client->state != e \
1192 ? sel->p == OPTION_TRUE \
1193 : sel->p == OPTION_FALSE) { \
1196 WSTATE(tiled, STATE_TILED)
1197 WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
1198 WSTATE(floating, STATE_FLOATING)
1199 WSTATE(fullscreen, STATE_FULLSCREEN)
1202 #define WLAYER(p, e) \
1203 if (sel->p != OPTION_NONE && \
1204 loc->node->client->layer != e \
1205 ? sel->p == OPTION_TRUE \
1206 : sel->p == OPTION_FALSE) { \
1209 WLAYER(below, LAYER_BELOW)
1210 WLAYER(normal, LAYER_NORMAL)
1211 WLAYER(above, LAYER_ABOVE)
1215 if (sel->p != OPTION_NONE && \
1216 !loc->node->client->p \
1217 ? sel->p == OPTION_TRUE \
1218 : sel->p == OPTION_FALSE) { \
1227 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *sel)
1229 if (sel->occupied != OPTION_NONE &&
1230 loc->desktop->root == NULL
1231 ? sel->occupied == OPTION_TRUE
1232 : sel->occupied == OPTION_FALSE) {
1236 if (sel->focused != OPTION_NONE &&
1237 loc->desktop != mon->desk
1238 ? sel->focused == OPTION_TRUE
1239 : sel->focused == OPTION_FALSE) {
1243 if (sel->active != OPTION_NONE &&
1244 loc->desktop != loc->monitor->desk
1245 ? sel->active == OPTION_TRUE
1246 : sel->active == OPTION_FALSE) {
1250 if (sel->urgent != OPTION_NONE &&
1251 !is_urgent(loc->desktop)
1252 ? sel->urgent == OPTION_TRUE
1253 : sel->urgent == OPTION_FALSE) {
1257 if (sel->local != OPTION_NONE &&
1258 ref->monitor != loc->monitor
1259 ? sel->local == OPTION_TRUE
1260 : sel->local == OPTION_FALSE) {
1264 #define DLAYOUT(p, e) \
1265 if (sel->p != OPTION_NONE && \
1266 loc->desktop->layout != e \
1267 ? sel->p == OPTION_TRUE \
1268 : sel->p == OPTION_FALSE) { \
1271 DLAYOUT(tiled, LAYOUT_TILED)
1272 DLAYOUT(monocle, LAYOUT_MONOCLE)
1275 #define DUSERLAYOUT(p, e) \
1276 if (sel->p != OPTION_NONE && \
1277 loc->desktop->user_layout != e \
1278 ? sel->p == OPTION_TRUE \
1279 : sel->p == OPTION_FALSE) { \
1282 DUSERLAYOUT(user_tiled, LAYOUT_TILED)
1283 DUSERLAYOUT(user_monocle, LAYOUT_MONOCLE)
1289 bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t *sel)
1291 if (sel->occupied != OPTION_NONE &&
1292 loc->monitor->desk->root == NULL
1293 ? sel->occupied == OPTION_TRUE
1294 : sel->occupied == OPTION_FALSE) {
1298 if (sel->focused != OPTION_NONE &&
1300 ? sel->focused == OPTION_TRUE
1301 : sel->focused == OPTION_FALSE) {