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, "\"focusedDesktopId\":%u,", m->desk->id);
80 fprintf(rsp, "\"padding\":");
81 query_padding(m->padding, rsp);
83 fprintf(rsp, "\"rectangle\":");
84 query_rectangle(m->rectangle, rsp);
86 fprintf(rsp, "\"desktops\":");
88 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
89 query_desktop(d, rsp);
90 if (d->next != NULL) {
98 void query_desktop(desktop_t *d, FILE *rsp)
101 fprintf(rsp, "\"name\":\"%s\",", d->name);
102 fprintf(rsp, "\"id\":%u,", d->id);
103 fprintf(rsp, "\"layout\":\"%s\",", LAYOUT_STR(d->layout));
104 fprintf(rsp, "\"userLayout\":\"%s\",", LAYOUT_STR(d->user_layout));
105 fprintf(rsp, "\"windowGap\":%i,", d->window_gap);
106 fprintf(rsp, "\"borderWidth\":%u,", d->border_width);
107 fprintf(rsp, "\"focusedNodeId\":%u,", d->focus != NULL ? d->focus->id : 0);
108 fprintf(rsp, "\"padding\":");
109 query_padding(d->padding, rsp);
111 fprintf(rsp, "\"root\":");
112 query_node(d->root, rsp);
116 void query_node(node_t *n, FILE *rsp)
119 fprintf(rsp, "null");
122 fprintf(rsp, "\"id\":%u,", n->id);
123 fprintf(rsp, "\"splitType\":\"%s\",", SPLIT_TYPE_STR(n->split_type));
124 fprintf(rsp, "\"splitRatio\":%lf,", n->split_ratio);
125 fprintf(rsp, "\"vacant\":%s,", BOOL_STR(n->vacant));
126 fprintf(rsp, "\"hidden\":%s,", BOOL_STR(n->hidden));
127 fprintf(rsp, "\"sticky\":%s,", BOOL_STR(n->sticky));
128 fprintf(rsp, "\"private\":%s,", BOOL_STR(n->private));
129 fprintf(rsp, "\"locked\":%s,", BOOL_STR(n->locked));
130 fprintf(rsp, "\"marked\":%s,", BOOL_STR(n->marked));
131 fprintf(rsp, "\"presel\":");
132 query_presel(n->presel, rsp);
134 fprintf(rsp, "\"rectangle\":");
135 query_rectangle(n->rectangle, rsp);
137 fprintf(rsp, "\"constraints\":");
138 query_constraints(n->constraints, rsp);
140 fprintf(rsp, "\"firstChild\":");
141 query_node(n->first_child, rsp);
143 fprintf(rsp, "\"secondChild\":");
144 query_node(n->second_child, rsp);
146 fprintf(rsp, "\"client\":");
147 query_client(n->client, rsp);
152 void query_presel(presel_t *p, FILE *rsp)
155 fprintf(rsp, "null");
157 fprintf(rsp, "{\"splitDir\":\"%s\",\"splitRatio\":%lf}", SPLIT_DIR_STR(p->split_dir), p->split_ratio);
161 void query_client(client_t *c, FILE *rsp)
164 fprintf(rsp, "null");
167 fprintf(rsp, "\"className\":\"%s\",", c->class_name);
168 fprintf(rsp, "\"instanceName\":\"%s\",", c->instance_name);
169 fprintf(rsp, "\"borderWidth\":%u,", c->border_width);
170 fprintf(rsp, "\"state\":\"%s\",", STATE_STR(c->state));
171 fprintf(rsp, "\"lastState\":\"%s\",", STATE_STR(c->last_state));
172 fprintf(rsp, "\"layer\":\"%s\",", LAYER_STR(c->layer));
173 fprintf(rsp, "\"lastLayer\":\"%s\",", LAYER_STR(c->last_layer));
174 fprintf(rsp, "\"urgent\":%s,", BOOL_STR(c->urgent));
175 fprintf(rsp, "\"shown\":%s,", BOOL_STR(c->shown));
176 fprintf(rsp, "\"tiledRectangle\":");
177 query_rectangle(c->tiled_rectangle, rsp);
179 fprintf(rsp, "\"floatingRectangle\":");
180 query_rectangle(c->floating_rectangle, rsp);
185 void query_rectangle(xcb_rectangle_t r, FILE *rsp)
187 fprintf(rsp, "{\"x\":%i,\"y\":%i,\"width\":%u,\"height\":%u}", r.x, r.y, r.width, r.height);
190 void query_constraints(constraints_t c, FILE *rsp)
192 fprintf(rsp, "{\"min_width\":%u,\"min_height\":%u}", c.min_width, c.min_height);
195 void query_padding(padding_t p, FILE *rsp)
197 fprintf(rsp, "{\"top\":%i,\"right\":%i,\"bottom\":%i,\"left\":%i}", p.top, p.right, p.bottom, p.left);
200 void query_history(FILE *rsp)
203 for (history_t *h = history_head; h != NULL; h = h->next) {
204 query_coordinates(&h->loc, rsp);
205 if (h->next != NULL) {
212 void query_coordinates(coordinates_t *loc, FILE *rsp)
214 fprintf(rsp, "{\"monitorId\":%u,\"desktopId\":%u,\"nodeId\":%u}", loc->monitor->id, loc->desktop->id, loc->node!=NULL?loc->node->id:0);
217 void query_stack(FILE *rsp)
220 for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
221 fprintf(rsp, "%u", s->node->id);
222 if (s->next != NULL) {
229 void query_subscribers(FILE *rsp)
232 for (subscriber_list_t *s = subscribe_head; s != NULL; s = s->next) {
233 fprintf(rsp, "{\"fileDescriptor\": %i", fileno(s->stream));
234 if (s->fifo_path != NULL) {
235 fprintf(rsp, ",\"fifoPath\":\"%s\"", s->fifo_path);
237 fprintf(rsp, ",\"field\":%i,\"count\":%i}", s->field, s->count);
238 if (s->next != NULL) {
245 int query_node_ids(coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *rsp)
248 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
249 if (trg->monitor != NULL && m != trg->monitor) {
252 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
253 if (trg->desktop != NULL && d != trg->desktop) {
256 count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
262 int query_node_ids_in(node_t *n, desktop_t *d, monitor_t *m, coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *rsp)
268 coordinates_t loc = {m, d, n};
269 if ((trg->node == NULL || n == trg->node) &&
270 (sel == NULL || node_matches(&loc, ref, sel))) {
271 fprintf(rsp, "0x%08X\n", n->id);
274 count += query_node_ids_in(n->first_child, d, m, ref, trg, sel, rsp);
275 count += query_node_ids_in(n->second_child, d, m, ref, trg, sel, rsp);
280 int query_desktop_ids(coordinates_t *ref, coordinates_t *trg, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
283 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
284 if (trg->monitor != NULL && m != trg->monitor) {
287 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
288 coordinates_t loc = {m, d, NULL};
289 if ((trg->desktop != NULL && d != trg->desktop) ||
290 (sel != NULL && !desktop_matches(&loc, ref, sel))) {
300 int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *sel, monitor_printer_t printer, FILE *rsp)
303 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
304 coordinates_t loc = {m, NULL, NULL};
305 if ((trg->monitor != NULL && m != trg->monitor) ||
306 (sel != NULL && !monitor_matches(&loc, ref, sel))) {
315 void fprint_monitor_id(monitor_t *m, FILE *rsp)
317 fprintf(rsp, "0x%08X\n", m->id);
320 void fprint_monitor_name(monitor_t *m, FILE *rsp)
322 fprintf(rsp, "%s\n", m->name);
325 void fprint_desktop_id(desktop_t *d, FILE *rsp)
327 fprintf(rsp, "0x%08X\n", d->id);
330 void fprint_desktop_name(desktop_t *d, FILE *rsp)
332 fprintf(rsp, "%s\n", d->name);
335 void print_ignore_request(state_transition_t st, FILE *rsp)
338 fprintf(rsp, "none");
340 unsigned int cnt = 0;
341 if (st & STATE_TRANSITION_ENTER) {
342 fprintf(rsp, "enter");
345 if (st & STATE_TRANSITION_EXIT) {
346 fprintf(rsp, "%sexit", cnt > 0 ? "," : "");
351 void print_modifier_mask(uint16_t m, FILE *rsp)
354 case XCB_MOD_MASK_SHIFT:
355 fprintf(rsp, "shift");
357 case XCB_MOD_MASK_CONTROL:
358 fprintf(rsp, "control");
360 case XCB_MOD_MASK_LOCK:
361 fprintf(rsp, "lock");
364 fprintf(rsp, "mod1");
367 fprintf(rsp, "mod2");
370 fprintf(rsp, "mod3");
373 fprintf(rsp, "mod4");
376 fprintf(rsp, "mod5");
381 void print_button_index(int8_t b, FILE *rsp)
384 case XCB_BUTTON_INDEX_ANY:
387 case XCB_BUTTON_INDEX_1:
388 fprintf(rsp, "button1");
390 case XCB_BUTTON_INDEX_2:
391 fprintf(rsp, "button2");
393 case XCB_BUTTON_INDEX_3:
394 fprintf(rsp, "button3");
397 fprintf(rsp, "none");
402 void print_pointer_action(pointer_action_t a, FILE *rsp)
406 fprintf(rsp, "move");
408 case ACTION_RESIZE_SIDE:
409 fprintf(rsp, "resize_side");
411 case ACTION_RESIZE_CORNER:
412 fprintf(rsp, "resize_corner");
415 fprintf(rsp, "focus");
418 fprintf(rsp, "none");
423 void print_rule_consequence(char **buf, rule_consequence_t *csq)
425 char *rect_buf = NULL;
426 print_rectangle(&rect_buf, csq->rect);
427 if (rect_buf == NULL) {
428 rect_buf = malloc(1);
431 asprintf(buf, "monitor=%s desktop=%s node=%s state=%s layer=%s split_dir=%s split_ratio=%lf hidden=%s sticky=%s private=%s locked=%s marked=%s center=%s follow=%s manage=%s focus=%s border=%s rectangle=%s",
432 csq->monitor_desc, csq->desktop_desc, csq->node_desc,
433 csq->state == NULL ? "" : STATE_STR(*csq->state),
434 csq->layer == NULL ? "" : LAYER_STR(*csq->layer),
435 csq->split_dir, csq->split_ratio,
436 ON_OFF_STR(csq->hidden), ON_OFF_STR(csq->sticky), ON_OFF_STR(csq->private),
437 ON_OFF_STR(csq->locked), ON_OFF_STR(csq->marked), ON_OFF_STR(csq->center), ON_OFF_STR(csq->follow),
438 ON_OFF_STR(csq->manage), ON_OFF_STR(csq->focus), ON_OFF_STR(csq->border), rect_buf);
442 void print_rectangle(char **buf, xcb_rectangle_t *rect)
445 asprintf(buf, "%hux%hu+%hi+%hi", rect->width, rect->height, rect->x, rect->y);
449 node_select_t make_node_select(void)
451 node_select_t sel = {
452 .automatic = OPTION_NONE,
453 .focused = OPTION_NONE,
454 .active = OPTION_NONE,
455 .local = OPTION_NONE,
457 .window = OPTION_NONE,
458 .tiled = OPTION_NONE,
459 .pseudo_tiled = OPTION_NONE,
460 .floating = OPTION_NONE,
461 .fullscreen = OPTION_NONE,
462 .hidden = OPTION_NONE,
463 .sticky = OPTION_NONE,
464 .private = OPTION_NONE,
465 .locked = OPTION_NONE,
466 .marked = OPTION_NONE,
467 .urgent = OPTION_NONE,
468 .same_class = OPTION_NONE,
469 .descendant_of = OPTION_NONE,
470 .ancestor_of = OPTION_NONE,
471 .below = OPTION_NONE,
472 .normal = OPTION_NONE,
473 .above = OPTION_NONE,
474 .horizontal = OPTION_NONE,
475 .vertical = OPTION_NONE
480 desktop_select_t make_desktop_select(void)
482 desktop_select_t sel = {
483 .occupied = OPTION_NONE,
484 .focused = OPTION_NONE,
485 .active = OPTION_NONE,
486 .urgent = OPTION_NONE,
492 monitor_select_t make_monitor_select(void)
494 monitor_select_t sel = {
495 .occupied = OPTION_NONE,
496 .focused = OPTION_NONE
501 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
503 coordinates_t ref_copy = *ref;
505 char *desc_copy = copy_string(desc, strlen(desc));
508 char *hash = strrchr(desc, '#');
509 char *path = strrchr(desc, '@');
510 char *colon = strrchr(desc, ':');
512 /* Discard hashes inside a DESKTOP_SEL */
513 if (hash != NULL && colon != NULL && path != NULL &&
514 path < hash && hash < colon) {
515 if (path > desc && *(path - 1) == '#') {
525 coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
526 if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
534 node_select_t sel = make_node_select();
536 if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
538 return SELECTOR_BAD_MODIFIERS;
546 if (parse_direction(desc, &dir)) {
547 find_nearest_neighbor(ref, dst, dir, &sel);
548 } else if (parse_cycle_direction(desc, &cyc)) {
549 find_closest_node(ref, dst, cyc, &sel);
550 } else if (parse_history_direction(desc, &hdi)) {
551 history_find_node(hdi, ref, dst, &sel);
552 } else if (streq("any", desc)) {
553 find_any_node(ref, dst, &sel);
554 } else if (streq("first_ancestor", desc)) {
555 find_first_ancestor(ref, dst, &sel);
556 } else if (streq("last", desc)) {
557 history_find_node(HISTORY_OLDER, ref, dst, &sel);
558 } else if (streq("newest", desc)) {
559 history_find_newest_node(ref, dst, &sel);
560 } else if (streq("biggest", desc)) {
561 find_by_area(AREA_BIGGEST, ref, dst, &sel);
562 } else if (streq("smallest", desc)) {
563 find_by_area(AREA_SMALLEST, ref, dst, &sel);
564 } else if (streq("pointed", desc)) {
565 xcb_window_t win = XCB_NONE;
566 query_pointer(&win, NULL);
567 if (locate_window(win, dst) && node_matches(dst, ref, &sel)) {
570 return SELECTOR_INVALID;
572 } else if (streq("focused", desc)) {
573 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
574 if (node_matches(&loc, ref, &sel)) {
577 } else if (*desc == '@') {
583 if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
584 dst->node = dst->desktop->focus;
592 dst->node = dst->desktop->root;
594 char *move = strtok(desc, PTH_TOK);
595 while (move != NULL && dst->node != NULL) {
596 if (streq("first", move) || streq("1", move)) {
597 dst->node = dst->node->first_child;
598 } else if (streq("second", move) || streq("2", move)) {
599 dst->node = dst->node->second_child;
600 } else if (streq("parent", move)) {
601 dst->node = dst->node->parent;
602 } else if (streq("brother", move)) {
603 dst->node = brother_tree(dst->node);
606 if (parse_direction(move, &dir)) {
607 dst->node = find_fence(dst->node, dir);
610 return SELECTOR_BAD_DESCRIPTOR;
613 move = strtok(NULL, PTH_TOK);
616 if (dst->node != NULL) {
617 if (node_matches(dst, ref, &sel)) {
620 return SELECTOR_INVALID;
622 } else if (dst->desktop->root != NULL) {
623 return SELECTOR_INVALID;
628 if (parse_id(desc, &id)) {
630 if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
633 return SELECTOR_INVALID;
637 return SELECTOR_BAD_DESCRIPTOR;
643 if (dst->node == NULL) {
644 return SELECTOR_INVALID;
650 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
653 locate_desktop(desc + 1, dst);
657 coordinates_t ref_copy = *ref;
659 char *desc_copy = copy_string(desc, strlen(desc));
662 char *hash = strrchr(desc, '#');
667 coordinates_t tmp = {mon, mon->desk, NULL};
668 if ((ret = desktop_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
676 desktop_select_t sel = make_desktop_select();
677 char *colon = strrchr(desc, ':');
679 if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
681 return SELECTOR_BAD_MODIFIERS;
690 if (parse_cycle_direction(desc, &cyc)) {
691 find_closest_desktop(ref, dst, cyc, &sel);
692 } else if (parse_history_direction(desc, &hdi)) {
693 history_find_desktop(hdi, ref, dst, &sel);
694 } else if (streq("any", desc)) {
695 find_any_desktop(ref, dst, &sel);
696 } else if (streq("last", desc)) {
697 history_find_desktop(HISTORY_OLDER, ref, dst, &sel);
698 } else if (streq("newest", desc)) {
699 history_find_newest_desktop(ref, dst, &sel);
700 } else if (streq("focused", desc)) {
701 coordinates_t loc = {mon, mon->desk, NULL};
702 if (desktop_matches(&loc, ref, &sel)) {
705 } else if (colon != NULL) {
708 if ((ret = monitor_from_desc(desc, ref, dst)) == SELECTOR_OK) {
709 if (streq("focused", colon + 1)) {
710 coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
711 if (desktop_matches(&loc, ref, &sel)) {
714 } else if (parse_index(colon + 1, &idx)) {
716 if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, &sel)) {
719 return SELECTOR_INVALID;
723 return SELECTOR_BAD_DESCRIPTOR;
729 } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
731 if (desktop_matches(dst, ref, &sel)) {
734 return SELECTOR_INVALID;
736 } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
738 if (desktop_matches(dst, ref, &sel)) {
741 return SELECTOR_INVALID;
745 if (desktop_from_name(desc, ref, dst, &sel, &hits)) {
751 return SELECTOR_INVALID;
753 return SELECTOR_BAD_DESCRIPTOR;
761 if (dst->desktop == NULL) {
762 return SELECTOR_INVALID;
768 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
771 locate_monitor(desc + 1, dst);
775 coordinates_t ref_copy = *ref;
777 char *desc_copy = copy_string(desc, strlen(desc));
780 char *hash = strrchr(desc, '#');
785 coordinates_t tmp = {mon, NULL, NULL};
786 if ((ret = monitor_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
794 monitor_select_t sel = make_monitor_select();
796 if (!parse_monitor_modifiers(desc, &sel)) {
798 return SELECTOR_BAD_MODIFIERS;
808 if (parse_direction(desc, &dir)) {
809 dst->monitor = nearest_monitor(ref->monitor, dir, &sel);
810 } else if (parse_cycle_direction(desc, &cyc)) {
811 dst->monitor = closest_monitor(ref->monitor, cyc, &sel);
812 } else if (parse_history_direction(desc, &hdi)) {
813 history_find_monitor(hdi, ref, dst, &sel);
814 } else if (streq("any", desc)) {
815 find_any_monitor(ref, dst, &sel);
816 } else if (streq("last", desc)) {
817 history_find_monitor(HISTORY_OLDER, ref, dst, &sel);
818 } else if (streq("newest", desc)) {
819 history_find_newest_monitor(ref, dst, &sel);
820 } else if (streq("primary", desc)) {
821 if (pri_mon != NULL) {
822 coordinates_t loc = {pri_mon, NULL, NULL};
823 if (monitor_matches(&loc, ref, &sel)) {
824 dst->monitor = pri_mon;
827 } else if (streq("focused", desc)) {
828 coordinates_t loc = {mon, NULL, NULL};
829 if (monitor_matches(&loc, ref, &sel)) {
832 } else if (streq("pointed", desc)) {
834 query_pointer(NULL, &pointer);
835 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
836 if (is_inside(pointer, m->rectangle)) {
841 } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
843 if (monitor_matches(dst, ref, &sel)) {
846 return SELECTOR_INVALID;
848 } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
850 if (monitor_matches(dst, ref, &sel)) {
853 return SELECTOR_INVALID;
856 if (locate_monitor(desc, dst)) {
858 if (monitor_matches(dst, ref, &sel)) {
861 return SELECTOR_INVALID;
865 return SELECTOR_BAD_DESCRIPTOR;
872 if (dst->monitor == NULL) {
873 return SELECTOR_INVALID;
879 bool locate_window(xcb_window_t win, coordinates_t *loc)
881 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
882 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
883 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
884 if (n->client == NULL) {
899 bool locate_desktop(char *name, coordinates_t *loc)
901 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
902 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
903 if (streq(d->name, name)) {
913 bool locate_monitor(char *name, coordinates_t *loc)
915 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
916 if (streq(m->name, name)) {
924 bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
926 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
927 if (mm != NULL && m != mm) {
930 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
942 bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel, int *hits)
944 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
945 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
946 if (streq(d->name, name)) {
950 coordinates_t loc = {m, d, NULL};
951 if (desktop_matches(&loc, ref, sel)) {
962 bool desktop_from_index(uint16_t idx, coordinates_t *loc, monitor_t *mm)
964 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
965 if (mm != NULL && m != mm) {
968 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, idx--) {
980 bool monitor_from_id(uint32_t id, coordinates_t *loc)
982 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
993 bool monitor_from_index(int idx, coordinates_t *loc)
995 for (monitor_t *m = mon_head; m != NULL; m = m->next, idx--) {
1006 bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
1008 if (loc->node == NULL) {
1012 if (sel->focused != OPTION_NONE &&
1013 loc->node != mon->desk->focus
1014 ? sel->focused == OPTION_TRUE
1015 : sel->focused == OPTION_FALSE) {
1019 if (sel->active != OPTION_NONE &&
1020 loc->node != loc->desktop->focus
1021 ? sel->active == OPTION_TRUE
1022 : sel->active == OPTION_FALSE) {
1026 if (sel->automatic != OPTION_NONE &&
1027 loc->node->presel != NULL
1028 ? sel->automatic == OPTION_TRUE
1029 : sel->automatic == OPTION_FALSE) {
1033 if (sel->local != OPTION_NONE &&
1034 loc->desktop != ref->desktop
1035 ? sel->local == OPTION_TRUE
1036 : sel->local == OPTION_FALSE) {
1040 if (sel->active != OPTION_NONE &&
1041 loc->desktop != loc->monitor->desk
1042 ? sel->active == OPTION_TRUE
1043 : sel->active == OPTION_FALSE) {
1047 if (sel->leaf != OPTION_NONE &&
1049 ? sel->leaf == OPTION_TRUE
1050 : sel->leaf == OPTION_FALSE) {
1054 if (sel->window != OPTION_NONE &&
1055 loc->node->client == NULL
1056 ? sel->window == OPTION_TRUE
1057 : sel->window == OPTION_FALSE) {
1062 if (sel->p != OPTION_NONE && \
1064 ? sel->p == OPTION_TRUE \
1065 : sel->p == OPTION_FALSE) { \
1075 if (loc->node->client == NULL &&
1076 (sel->same_class != OPTION_NONE ||
1077 sel->tiled != OPTION_NONE ||
1078 sel->pseudo_tiled != OPTION_NONE ||
1079 sel->floating != OPTION_NONE ||
1080 sel->fullscreen != OPTION_NONE ||
1081 sel->below != OPTION_NONE ||
1082 sel->normal != OPTION_NONE ||
1083 sel->above != OPTION_NONE ||
1084 sel->urgent != OPTION_NONE)) {
1088 if (ref->node != NULL && ref->node->client != NULL &&
1089 sel->same_class != OPTION_NONE &&
1090 streq(loc->node->client->class_name, ref->node->client->class_name)
1091 ? sel->same_class == OPTION_FALSE
1092 : sel->same_class == OPTION_TRUE) {
1096 if (sel->descendant_of != OPTION_NONE &&
1097 !is_descendant(loc->node, ref->node)
1098 ? sel->descendant_of == OPTION_TRUE
1099 : sel->descendant_of == OPTION_FALSE) {
1103 if (sel->ancestor_of != OPTION_NONE &&
1104 !is_descendant(ref->node, loc->node)
1105 ? sel->ancestor_of == OPTION_TRUE
1106 : sel->ancestor_of == OPTION_FALSE) {
1110 #define WSTATE(p, e) \
1111 if (sel->p != OPTION_NONE && \
1112 loc->node->client->state != e \
1113 ? sel->p == OPTION_TRUE \
1114 : sel->p == OPTION_FALSE) { \
1117 WSTATE(tiled, STATE_TILED)
1118 WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
1119 WSTATE(floating, STATE_FLOATING)
1120 WSTATE(fullscreen, STATE_FULLSCREEN)
1123 #define WLAYER(p, e) \
1124 if (sel->p != OPTION_NONE && \
1125 loc->node->client->layer != e \
1126 ? sel->p == OPTION_TRUE \
1127 : sel->p == OPTION_FALSE) { \
1130 WLAYER(below, LAYER_BELOW)
1131 WLAYER(normal, LAYER_NORMAL)
1132 WLAYER(above, LAYER_ABOVE)
1136 if (sel->p != OPTION_NONE && \
1137 !loc->node->client->p \
1138 ? sel->p == OPTION_TRUE \
1139 : sel->p == OPTION_FALSE) { \
1145 if (sel->horizontal != OPTION_NONE &&
1146 loc->node->split_type != TYPE_HORIZONTAL
1147 ? sel->horizontal == OPTION_TRUE
1148 : sel->horizontal == OPTION_FALSE) {
1152 if (sel->vertical != OPTION_NONE &&
1153 loc->node->split_type != TYPE_VERTICAL
1154 ? sel->vertical == OPTION_TRUE
1155 : sel->vertical == OPTION_FALSE) {
1162 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *sel)
1164 if (sel->occupied != OPTION_NONE &&
1165 loc->desktop->root == NULL
1166 ? sel->occupied == OPTION_TRUE
1167 : sel->occupied == OPTION_FALSE) {
1171 if (sel->focused != OPTION_NONE &&
1172 loc->desktop != mon->desk
1173 ? sel->focused == OPTION_TRUE
1174 : sel->focused == OPTION_FALSE) {
1178 if (sel->active != OPTION_NONE &&
1179 loc->desktop != loc->monitor->desk
1180 ? sel->active == OPTION_TRUE
1181 : sel->active == OPTION_FALSE) {
1185 if (sel->urgent != OPTION_NONE &&
1186 !is_urgent(loc->desktop)
1187 ? sel->urgent == OPTION_TRUE
1188 : sel->urgent == OPTION_FALSE) {
1192 if (sel->local != OPTION_NONE &&
1193 ref->monitor != loc->monitor
1194 ? sel->local == OPTION_TRUE
1195 : sel->local == OPTION_FALSE) {
1202 bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t *sel)
1204 if (sel->occupied != OPTION_NONE &&
1205 loc->monitor->desk->root == NULL
1206 ? sel->occupied == OPTION_TRUE
1207 : sel->occupied == OPTION_FALSE) {
1211 if (sel->focused != OPTION_NONE &&
1213 ? sel->focused == OPTION_TRUE
1214 : sel->focused == OPTION_FALSE) {