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_tree(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\":");
65 void query_monitor(monitor_t *m, FILE *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);
79 fprintf(rsp, "\"rectangle\":");
80 query_rectangle(m->rectangle, rsp);
82 fprintf(rsp, "\"desktops\":");
84 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
85 query_desktop(d, rsp);
86 if (d->next != NULL) {
94 void query_desktop(desktop_t *d, FILE *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);
106 fprintf(rsp, "\"root\":");
107 query_node(d->root, rsp);
111 void query_node(node_t *n, FILE *rsp)
114 fprintf(rsp, "null");
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);
129 fprintf(rsp, "\"rectangle\":");
130 query_rectangle(n->rectangle, rsp);
132 fprintf(rsp, "\"constraints\":");
133 query_constraints(n->constraints, rsp);
135 fprintf(rsp, "\"firstChild\":");
136 query_node(n->first_child, rsp);
138 fprintf(rsp, "\"secondChild\":");
139 query_node(n->second_child, rsp);
141 fprintf(rsp, "\"client\":");
142 query_client(n->client, rsp);
147 void query_presel(presel_t *p, FILE *rsp)
150 fprintf(rsp, "null");
152 fprintf(rsp, "{\"splitDir\":\"%s\",\"splitRatio\":%lf}", SPLIT_DIR_STR(p->split_dir), p->split_ratio);
156 void query_client(client_t *c, FILE *rsp)
159 fprintf(rsp, "null");
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);
174 fprintf(rsp, "\"floatingRectangle\":");
175 query_rectangle(c->floating_rectangle, rsp);
180 void query_rectangle(xcb_rectangle_t r, FILE *rsp)
182 fprintf(rsp, "{\"x\":%i,\"y\":%i,\"width\":%u,\"height\":%u}", r.x, r.y, r.width, r.height);
185 void query_constraints(constraints_t c, FILE *rsp)
187 fprintf(rsp, "{\"min_width\":%u,\"min_height\":%u}", c.min_width, c.min_height);
190 void query_padding(padding_t p, FILE *rsp)
192 fprintf(rsp, "{\"top\":%i,\"right\":%i,\"bottom\":%i,\"left\":%i}", p.top, p.right, p.bottom, p.left);
195 void query_history(FILE *rsp)
198 for (history_t *h = history_head; h != NULL; h = h->next) {
199 query_coordinates(&h->loc, rsp);
200 if (h->next != NULL) {
207 void query_coordinates(coordinates_t *loc, FILE *rsp)
209 fprintf(rsp, "{\"monitorId\":%u,\"desktopId\":%u,\"nodeId\":%u}", loc->monitor->id, loc->desktop->id, loc->node!=NULL?loc->node->id:0);
212 void query_stack(FILE *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) {
224 int query_node_ids(coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *rsp)
227 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
228 if (trg->monitor != NULL && m != trg->monitor) {
231 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
232 if (trg->desktop != NULL && d != trg->desktop) {
235 count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
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)
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);
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);
259 int query_desktop_ids(coordinates_t *ref, coordinates_t *trg, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
262 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
263 if (trg->monitor != NULL && m != trg->monitor) {
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))) {
279 int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *sel, monitor_printer_t printer, FILE *rsp)
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))) {
294 void fprint_monitor_id(monitor_t *m, FILE *rsp)
296 fprintf(rsp, "0x%08X\n", m->id);
299 void fprint_monitor_name(monitor_t *m, FILE *rsp)
301 fprintf(rsp, "%s\n", m->name);
304 void fprint_desktop_id(desktop_t *d, FILE *rsp)
306 fprintf(rsp, "0x%08X\n", d->id);
309 void fprint_desktop_name(desktop_t *d, FILE *rsp)
311 fprintf(rsp, "%s\n", d->name);
314 void print_ignore_request(state_transition_t st, FILE *rsp)
317 fprintf(rsp, "none");
319 unsigned int cnt = 0;
320 if (st & STATE_TRANSITION_ENTER) {
321 fprintf(rsp, "enter");
324 if (st & STATE_TRANSITION_EXIT) {
325 fprintf(rsp, "%sexit", cnt > 0 ? "," : "");
330 void print_modifier_mask(uint16_t m, FILE *rsp)
333 case XCB_MOD_MASK_SHIFT:
334 fprintf(rsp, "shift");
336 case XCB_MOD_MASK_CONTROL:
337 fprintf(rsp, "control");
339 case XCB_MOD_MASK_LOCK:
340 fprintf(rsp, "lock");
343 fprintf(rsp, "mod1");
346 fprintf(rsp, "mod2");
349 fprintf(rsp, "mod3");
352 fprintf(rsp, "mod4");
355 fprintf(rsp, "mod5");
360 void print_button_index(int8_t b, FILE *rsp)
363 case XCB_BUTTON_INDEX_ANY:
366 case XCB_BUTTON_INDEX_1:
367 fprintf(rsp, "button1");
369 case XCB_BUTTON_INDEX_2:
370 fprintf(rsp, "button2");
372 case XCB_BUTTON_INDEX_3:
373 fprintf(rsp, "button3");
376 fprintf(rsp, "none");
381 void print_pointer_action(pointer_action_t a, FILE *rsp)
385 fprintf(rsp, "move");
387 case ACTION_RESIZE_SIDE:
388 fprintf(rsp, "resize_side");
390 case ACTION_RESIZE_CORNER:
391 fprintf(rsp, "resize_corner");
394 fprintf(rsp, "focus");
397 fprintf(rsp, "none");
402 void print_rule_consequence(char **buf, rule_consequence_t *csq)
404 char *rect_buf = NULL;
405 print_rectangle(&rect_buf, csq->rect);
406 if (rect_buf == NULL) {
407 rect_buf = malloc(1);
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);
421 void print_rectangle(char **buf, xcb_rectangle_t *rect)
424 asprintf(buf, "%hux%hu+%hi+%hi", rect->width, rect->height, rect->x, rect->y);
428 node_select_t make_node_select(void)
430 node_select_t sel = {
431 .automatic = OPTION_NONE,
432 .focused = OPTION_NONE,
433 .local = OPTION_NONE,
434 .active = 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,
457 desktop_select_t make_desktop_select(void)
459 desktop_select_t sel = {
460 .occupied = OPTION_NONE,
461 .focused = OPTION_NONE,
462 .urgent = OPTION_NONE,
468 monitor_select_t make_monitor_select(void)
470 monitor_select_t sel = {
471 .occupied = OPTION_NONE,
472 .focused = OPTION_NONE
477 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
479 coordinates_t ref_copy = *ref;
481 char *desc_copy = copy_string(desc, strlen(desc));
484 char *hash = strrchr(desc, '#');
485 char *path = strrchr(desc, '@');
486 char *colon = strrchr(desc, ':');
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) == '#') {
501 coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
502 if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
510 node_select_t sel = make_node_select();
512 if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
514 return SELECTOR_BAD_MODIFIERS;
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)) {
544 return SELECTOR_INVALID;
546 } else if (streq("focused", desc)) {
547 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
548 if (node_matches(&loc, ref, &sel)) {
551 } else if (*desc == '@') {
557 if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
558 dst->node = dst->desktop->focus;
566 dst->node = dst->desktop->root;
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);
580 if (parse_direction(move, &dir)) {
581 dst->node = find_fence(dst->node, dir);
584 return SELECTOR_BAD_DESCRIPTOR;
587 move = strtok(NULL, PTH_TOK);
590 if (dst->node != NULL) {
591 if (node_matches(dst, ref, &sel)) {
594 return SELECTOR_INVALID;
596 } else if (dst->desktop->root != NULL) {
597 return SELECTOR_INVALID;
602 if (parse_id(desc, &id)) {
604 if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
607 return SELECTOR_INVALID;
611 return SELECTOR_BAD_DESCRIPTOR;
617 if (dst->node == NULL) {
618 return SELECTOR_INVALID;
624 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
627 locate_desktop(desc + 1, dst);
631 coordinates_t ref_copy = *ref;
633 char *desc_copy = copy_string(desc, strlen(desc));
636 char *hash = strrchr(desc, '#');
641 coordinates_t tmp = {mon, mon->desk, NULL};
642 if ((ret = desktop_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
650 desktop_select_t sel = make_desktop_select();
651 char *colon = strrchr(desc, ':');
653 if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
655 return SELECTOR_BAD_MODIFIERS;
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)) {
679 } else if (colon != NULL) {
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)) {
688 } else if (parse_index(colon + 1, &idx)) {
690 if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, &sel)) {
693 return SELECTOR_INVALID;
697 return SELECTOR_BAD_DESCRIPTOR;
703 } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
705 if (desktop_matches(dst, ref, &sel)) {
708 return SELECTOR_INVALID;
710 } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
712 if (desktop_matches(dst, ref, &sel)) {
715 return SELECTOR_INVALID;
719 if (desktop_from_name(desc, ref, dst, &sel, &hits)) {
725 return SELECTOR_INVALID;
727 return SELECTOR_BAD_DESCRIPTOR;
735 if (dst->desktop == NULL) {
736 return SELECTOR_INVALID;
742 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
745 locate_monitor(desc + 1, dst);
749 coordinates_t ref_copy = *ref;
751 char *desc_copy = copy_string(desc, strlen(desc));
754 char *hash = strrchr(desc, '#');
759 coordinates_t tmp = {mon, NULL, NULL};
760 if ((ret = monitor_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
768 monitor_select_t sel = make_monitor_select();
770 if (!parse_monitor_modifiers(desc, &sel)) {
772 return SELECTOR_BAD_MODIFIERS;
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;
801 } else if (streq("focused", desc)) {
802 coordinates_t loc = {mon, NULL, NULL};
803 if (monitor_matches(&loc, ref, &sel)) {
806 } else if (streq("pointed", desc)) {
808 query_pointer(NULL, &pointer);
809 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
810 if (is_inside(pointer, m->rectangle)) {
815 } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
817 if (monitor_matches(dst, ref, &sel)) {
820 return SELECTOR_INVALID;
822 } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
824 if (monitor_matches(dst, ref, &sel)) {
827 return SELECTOR_INVALID;
830 if (locate_monitor(desc, dst)) {
832 if (monitor_matches(dst, ref, &sel)) {
835 return SELECTOR_INVALID;
839 return SELECTOR_BAD_DESCRIPTOR;
846 if (dst->monitor == NULL) {
847 return SELECTOR_INVALID;
853 bool locate_window(xcb_window_t win, coordinates_t *loc)
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) {
873 bool locate_desktop(char *name, coordinates_t *loc)
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)) {
887 bool locate_monitor(char *name, coordinates_t *loc)
889 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
890 if (streq(m->name, name)) {
898 bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
900 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
901 if (mm != NULL && m != mm) {
904 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
916 bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel, int *hits)
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)) {
924 coordinates_t loc = {m, d, NULL};
925 if (desktop_matches(&loc, ref, sel)) {
936 bool desktop_from_index(uint16_t idx, coordinates_t *loc, monitor_t *mm)
938 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
939 if (mm != NULL && m != mm) {
942 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, idx--) {
954 bool monitor_from_id(uint32_t id, coordinates_t *loc)
956 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
967 bool monitor_from_index(int idx, coordinates_t *loc)
969 for (monitor_t *m = mon_head; m != NULL; m = m->next, idx--) {
980 bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
982 if (loc->node == NULL) {
986 if (sel->focused != OPTION_NONE &&
987 loc->node != loc->desktop->focus
988 ? sel->focused == OPTION_TRUE
989 : sel->focused == OPTION_FALSE) {
993 if (sel->automatic != OPTION_NONE &&
994 loc->node->presel != NULL
995 ? sel->automatic == OPTION_TRUE
996 : sel->automatic == OPTION_FALSE) {
1000 if (sel->local != OPTION_NONE &&
1001 loc->desktop != ref->desktop
1002 ? sel->local == OPTION_TRUE
1003 : sel->local == OPTION_FALSE) {
1007 if (sel->active != OPTION_NONE &&
1008 loc->desktop != loc->monitor->desk
1009 ? sel->active == OPTION_TRUE
1010 : sel->active == OPTION_FALSE) {
1014 if (sel->leaf != OPTION_NONE &&
1016 ? sel->leaf == OPTION_TRUE
1017 : sel->leaf == OPTION_FALSE) {
1021 if (sel->window != OPTION_NONE &&
1022 loc->node->client == NULL
1023 ? sel->window == OPTION_TRUE
1024 : sel->window == OPTION_FALSE) {
1029 if (sel->p != OPTION_NONE && \
1031 ? sel->p == OPTION_TRUE \
1032 : sel->p == OPTION_FALSE) { \
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)) {
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) {
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) {
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) {
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) { \
1084 WSTATE(tiled, STATE_TILED)
1085 WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
1086 WSTATE(floating, STATE_FLOATING)
1087 WSTATE(fullscreen, STATE_FULLSCREEN)
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) { \
1097 WLAYER(below, LAYER_BELOW)
1098 WLAYER(normal, LAYER_NORMAL)
1099 WLAYER(above, LAYER_ABOVE)
1103 if (sel->p != OPTION_NONE && \
1104 !loc->node->client->p \
1105 ? sel->p == OPTION_TRUE \
1106 : sel->p == OPTION_FALSE) { \
1115 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *sel)
1117 if (sel->occupied != OPTION_NONE &&
1118 loc->desktop->root == NULL
1119 ? sel->occupied == OPTION_TRUE
1120 : sel->occupied == OPTION_FALSE) {
1124 if (sel->focused != OPTION_NONE &&
1125 loc->desktop != loc->monitor->desk
1126 ? sel->focused == OPTION_TRUE
1127 : sel->focused == OPTION_FALSE) {
1131 if (sel->urgent != OPTION_NONE &&
1132 !is_urgent(loc->desktop)
1133 ? sel->urgent == OPTION_TRUE
1134 : sel->urgent == OPTION_FALSE) {
1138 if (sel->local != OPTION_NONE &&
1139 ref->monitor != loc->monitor
1140 ? sel->local == OPTION_TRUE
1141 : sel->local == OPTION_FALSE) {
1148 bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t *sel)
1150 if (sel->occupied != OPTION_NONE &&
1151 loc->monitor->desk->root == NULL
1152 ? sel->occupied == OPTION_TRUE
1153 : sel->occupied == OPTION_FALSE) {
1157 if (sel->focused != OPTION_NONE &&
1159 ? sel->focused == OPTION_TRUE
1160 : sel->focused == OPTION_FALSE) {