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 *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)
248 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
249 coordinates_t loc = {m, NULL, NULL};
250 if ((trg->monitor != NULL && m != trg->monitor) ||
251 (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
254 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
255 coordinates_t loc = {m, d, NULL};
256 if ((trg->desktop != NULL && d != trg->desktop) ||
257 (desk_sel != NULL && !desktop_matches(&loc, desk_ref, desk_sel))) {
260 count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
266 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)
272 coordinates_t loc = {m, d, n};
273 if ((trg->node == NULL || n == trg->node) &&
274 (sel == NULL || node_matches(&loc, ref, sel))) {
275 fprintf(rsp, "0x%08X\n", n->id);
278 count += query_node_ids_in(n->first_child, d, m, ref, trg, sel, rsp);
279 count += query_node_ids_in(n->second_child, d, m, ref, trg, sel, rsp);
284 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)
287 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
288 coordinates_t loc = {m, NULL, NULL};
289 if ((trg->monitor != NULL && m != trg->monitor) ||
290 (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
293 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
294 coordinates_t loc = {m, d, NULL};
295 if ((trg->desktop != NULL && d != trg->desktop) ||
296 (sel != NULL && !desktop_matches(&loc, ref, sel))) {
306 int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *sel, monitor_printer_t printer, FILE *rsp)
309 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
310 coordinates_t loc = {m, NULL, NULL};
311 if ((trg->monitor != NULL && m != trg->monitor) ||
312 (sel != NULL && !monitor_matches(&loc, ref, sel))) {
321 void fprint_monitor_id(monitor_t *m, FILE *rsp)
323 fprintf(rsp, "0x%08X\n", m->id);
326 void fprint_monitor_name(monitor_t *m, FILE *rsp)
328 fprintf(rsp, "%s\n", m->name);
331 void fprint_desktop_id(desktop_t *d, FILE *rsp)
333 fprintf(rsp, "0x%08X\n", d->id);
336 void fprint_desktop_name(desktop_t *d, FILE *rsp)
338 fprintf(rsp, "%s\n", d->name);
341 void print_ignore_request(state_transition_t st, FILE *rsp)
344 fprintf(rsp, "none");
346 unsigned int cnt = 0;
347 if (st & STATE_TRANSITION_ENTER) {
348 fprintf(rsp, "enter");
351 if (st & STATE_TRANSITION_EXIT) {
352 fprintf(rsp, "%sexit", cnt > 0 ? "," : "");
357 void print_modifier_mask(uint16_t m, FILE *rsp)
360 case XCB_MOD_MASK_SHIFT:
361 fprintf(rsp, "shift");
363 case XCB_MOD_MASK_CONTROL:
364 fprintf(rsp, "control");
366 case XCB_MOD_MASK_LOCK:
367 fprintf(rsp, "lock");
370 fprintf(rsp, "mod1");
373 fprintf(rsp, "mod2");
376 fprintf(rsp, "mod3");
379 fprintf(rsp, "mod4");
382 fprintf(rsp, "mod5");
387 void print_button_index(int8_t b, FILE *rsp)
390 case XCB_BUTTON_INDEX_ANY:
393 case XCB_BUTTON_INDEX_1:
394 fprintf(rsp, "button1");
396 case XCB_BUTTON_INDEX_2:
397 fprintf(rsp, "button2");
399 case XCB_BUTTON_INDEX_3:
400 fprintf(rsp, "button3");
403 fprintf(rsp, "none");
408 void print_pointer_action(pointer_action_t a, FILE *rsp)
412 fprintf(rsp, "move");
414 case ACTION_RESIZE_SIDE:
415 fprintf(rsp, "resize_side");
417 case ACTION_RESIZE_CORNER:
418 fprintf(rsp, "resize_corner");
421 fprintf(rsp, "focus");
424 fprintf(rsp, "none");
429 void resolve_rule_consequence(rule_consequence_t *csq)
431 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
432 coordinates_t dst = {NULL, NULL, NULL};
433 monitor_t *monitor = monitor_from_desc(csq->monitor_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.monitor;
434 desktop_t *desktop = desktop_from_desc(csq->desktop_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.desktop;
435 node_t *node = node_from_desc(csq->node_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.node;
437 #define PRINT_OBJECT_ID(name) \
438 if (name == NULL) { \
439 csq->name##_desc[0] = '\0'; \
441 snprintf(csq->name##_desc, 11, "0x%08X", name->id); \
443 PRINT_OBJECT_ID(monitor)
444 PRINT_OBJECT_ID(desktop)
445 PRINT_OBJECT_ID(node)
446 #undef PRINT_OBJECT_ID
449 void print_rule_consequence(char **buf, rule_consequence_t *csq)
451 char *rect_buf = NULL;
452 print_rectangle(&rect_buf, csq->rect);
453 if (rect_buf == NULL) {
454 rect_buf = malloc(1);
458 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",
459 csq->monitor_desc, csq->desktop_desc, csq->node_desc,
460 csq->state == NULL ? "" : STATE_STR(*csq->state),
461 csq->layer == NULL ? "" : LAYER_STR(*csq->layer),
462 csq->split_dir == NULL ? "" : SPLIT_DIR_STR(*csq->split_dir), csq->split_ratio,
463 ON_OFF_STR(csq->hidden), ON_OFF_STR(csq->sticky), ON_OFF_STR(csq->private),
464 ON_OFF_STR(csq->locked), ON_OFF_STR(csq->marked), ON_OFF_STR(csq->center), ON_OFF_STR(csq->follow),
465 ON_OFF_STR(csq->manage), ON_OFF_STR(csq->focus), ON_OFF_STR(csq->border), rect_buf);
469 void print_rectangle(char **buf, xcb_rectangle_t *rect)
472 asprintf(buf, "%hux%hu+%hi+%hi", rect->width, rect->height, rect->x, rect->y);
476 node_select_t make_node_select(void)
478 node_select_t sel = {
479 .automatic = OPTION_NONE,
480 .focused = OPTION_NONE,
481 .active = OPTION_NONE,
482 .local = OPTION_NONE,
484 .window = OPTION_NONE,
485 .tiled = OPTION_NONE,
486 .pseudo_tiled = OPTION_NONE,
487 .floating = OPTION_NONE,
488 .fullscreen = OPTION_NONE,
489 .hidden = OPTION_NONE,
490 .sticky = OPTION_NONE,
491 .private = OPTION_NONE,
492 .locked = OPTION_NONE,
493 .marked = OPTION_NONE,
494 .urgent = OPTION_NONE,
495 .same_class = OPTION_NONE,
496 .descendant_of = OPTION_NONE,
497 .ancestor_of = OPTION_NONE,
498 .below = OPTION_NONE,
499 .normal = OPTION_NONE,
500 .above = OPTION_NONE,
501 .horizontal = OPTION_NONE,
502 .vertical = OPTION_NONE
507 desktop_select_t make_desktop_select(void)
509 desktop_select_t sel = {
510 .occupied = OPTION_NONE,
511 .focused = OPTION_NONE,
512 .active = OPTION_NONE,
513 .urgent = OPTION_NONE,
514 .local = OPTION_NONE,
515 .tiled = OPTION_NONE,
516 .monocle = OPTION_NONE,
517 .user_tiled = OPTION_NONE,
518 .user_monocle = OPTION_NONE
523 monitor_select_t make_monitor_select(void)
525 monitor_select_t sel = {
526 .occupied = OPTION_NONE,
527 .focused = OPTION_NONE
532 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
536 coordinates_t ref_copy = *ref;
538 char *desc_copy = copy_string(desc, strlen(desc));
541 char *hash = strrchr(desc, '#');
542 char *path = strrchr(desc, '@');
543 char *colon = strrchr(desc, ':');
545 /* Adjust or discard hashes inside a DESKTOP_SEL, e.g. `newest#@prev#older:/1/2` */
546 if (hash != NULL && colon != NULL && path != NULL &&
547 path < hash && hash < colon) {
548 if (path > desc && *(path - 1) == '#') {
558 coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
559 if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
567 /* Discard colons within references, e.g. `@next.occupied:/#any.descendant_of.window` */
568 if (colon != NULL && hash != NULL && colon < hash) {
572 node_select_t sel = make_node_select();
574 if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
576 return SELECTOR_BAD_MODIFIERS;
582 if (parse_direction(desc, &dir)) {
583 find_nearest_neighbor(ref, dst, dir, &sel);
584 } else if (parse_cycle_direction(desc, &cyc)) {
585 find_closest_node(ref, dst, cyc, &sel);
586 } else if (parse_history_direction(desc, &hdi)) {
587 history_find_node(hdi, ref, dst, &sel);
588 } else if (streq("any", desc)) {
589 find_any_node(ref, dst, &sel);
590 } else if (streq("first_ancestor", desc)) {
591 find_first_ancestor(ref, dst, &sel);
592 } else if (streq("last", desc)) {
593 history_find_node(HISTORY_OLDER, ref, dst, &sel);
594 } else if (streq("newest", desc)) {
595 history_find_newest_node(ref, dst, &sel);
596 } else if (streq("biggest", desc)) {
597 find_by_area(AREA_BIGGEST, ref, dst, &sel);
598 } else if (streq("smallest", desc)) {
599 find_by_area(AREA_SMALLEST, ref, dst, &sel);
600 } else if (streq("pointed", desc)) {
601 xcb_window_t win = XCB_NONE;
602 query_pointer(&win, NULL);
603 if (locate_leaf(win, dst) && node_matches(dst, ref, &sel)) {
606 return SELECTOR_INVALID;
608 } else if (streq("focused", desc)) {
609 coordinates_t loc = {mon, mon->desk, mon->desk->focus};
610 if (node_matches(&loc, ref, &sel)) {
613 } else if (*desc == '@') {
619 if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
620 dst->node = dst->desktop->focus;
628 dst->node = dst->desktop->root;
630 char *move = strtok(desc, PTH_TOK);
631 while (move != NULL && dst->node != NULL) {
632 if (streq("first", move) || streq("1", move)) {
633 dst->node = dst->node->first_child;
634 } else if (streq("second", move) || streq("2", move)) {
635 dst->node = dst->node->second_child;
636 } else if (streq("parent", move)) {
637 dst->node = dst->node->parent;
638 } else if (streq("brother", move)) {
639 dst->node = brother_tree(dst->node);
642 if (parse_direction(move, &dir)) {
643 dst->node = find_fence(dst->node, dir);
646 return SELECTOR_BAD_DESCRIPTOR;
649 move = strtok(NULL, PTH_TOK);
652 if (dst->node != NULL) {
653 if (node_matches(dst, ref, &sel)) {
656 return SELECTOR_INVALID;
658 } else if (dst->desktop->root != NULL) {
659 return SELECTOR_INVALID;
664 if (parse_id(desc, &id)) {
666 if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
669 return SELECTOR_INVALID;
673 return SELECTOR_BAD_DESCRIPTOR;
679 if (dst->node == NULL) {
680 return SELECTOR_INVALID;
686 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
691 locate_desktop(desc + 1, dst);
695 coordinates_t ref_copy = *ref;
697 char *desc_copy = copy_string(desc, strlen(desc));
700 char *hash = strrchr(desc, '#');
701 char *colon = strrchr(desc, ':');
703 /* Discard hashes inside a MONITOR_SEL, e.g. `primary#next:focused` */
704 if (hash != NULL && colon != NULL && hash < colon) {
711 coordinates_t tmp = {mon, mon->desk, NULL};
712 if ((ret = desktop_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
720 /* Discard colons within references, e.g. `DisplayPort-1:focused#next.local` */
721 if (colon != NULL && hash != NULL && colon < hash) {
725 desktop_select_t sel = make_desktop_select();
727 if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
729 return SELECTOR_BAD_MODIFIERS;
736 if (parse_cycle_direction(desc, &cyc)) {
737 find_closest_desktop(ref, dst, cyc, &sel);
738 } else if (parse_history_direction(desc, &hdi)) {
739 history_find_desktop(hdi, ref, dst, &sel);
740 } else if (streq("any", desc)) {
741 find_any_desktop(ref, dst, &sel);
742 } else if (streq("last", desc)) {
743 history_find_desktop(HISTORY_OLDER, ref, dst, &sel);
744 } else if (streq("newest", desc)) {
745 history_find_newest_desktop(ref, dst, &sel);
746 } else if (streq("focused", desc)) {
747 coordinates_t loc = {mon, mon->desk, NULL};
748 if (desktop_matches(&loc, ref, &sel)) {
751 } else if (colon != NULL) {
754 if ((ret = monitor_from_desc(desc, ref, dst)) == SELECTOR_OK) {
755 if (streq("focused", colon + 1)) {
756 coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
757 if (desktop_matches(&loc, ref, &sel)) {
760 } else if (parse_index(colon + 1, &idx)) {
762 if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, &sel)) {
765 return SELECTOR_INVALID;
769 return SELECTOR_BAD_DESCRIPTOR;
775 } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
777 if (desktop_matches(dst, ref, &sel)) {
780 return SELECTOR_INVALID;
782 } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
784 if (desktop_matches(dst, ref, &sel)) {
787 return SELECTOR_INVALID;
791 if (desktop_from_name(desc, ref, dst, &sel, &hits)) {
797 return SELECTOR_INVALID;
799 return SELECTOR_BAD_DESCRIPTOR;
807 if (dst->desktop == NULL) {
808 return SELECTOR_INVALID;
814 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
819 locate_monitor(desc + 1, dst);
823 coordinates_t ref_copy = *ref;
825 char *desc_copy = copy_string(desc, strlen(desc));
828 char *hash = strrchr(desc, '#');
833 coordinates_t tmp = {mon, NULL, NULL};
834 if ((ret = monitor_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
842 monitor_select_t sel = make_monitor_select();
844 if (!parse_monitor_modifiers(desc, &sel)) {
846 return SELECTOR_BAD_MODIFIERS;
854 if (parse_direction(desc, &dir)) {
855 dst->monitor = nearest_monitor(ref->monitor, dir, &sel);
856 } else if (parse_cycle_direction(desc, &cyc)) {
857 dst->monitor = closest_monitor(ref->monitor, cyc, &sel);
858 } else if (parse_history_direction(desc, &hdi)) {
859 history_find_monitor(hdi, ref, dst, &sel);
860 } else if (streq("any", desc)) {
861 find_any_monitor(ref, dst, &sel);
862 } else if (streq("last", desc)) {
863 history_find_monitor(HISTORY_OLDER, ref, dst, &sel);
864 } else if (streq("newest", desc)) {
865 history_find_newest_monitor(ref, dst, &sel);
866 } else if (streq("primary", desc)) {
867 if (pri_mon != NULL) {
868 coordinates_t loc = {pri_mon, NULL, NULL};
869 if (monitor_matches(&loc, ref, &sel)) {
870 dst->monitor = pri_mon;
873 } else if (streq("focused", desc)) {
874 coordinates_t loc = {mon, NULL, NULL};
875 if (monitor_matches(&loc, ref, &sel)) {
878 } else if (streq("pointed", desc)) {
880 query_pointer(NULL, &pointer);
881 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
882 if (is_inside(pointer, m->rectangle)) {
887 } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
889 if (monitor_matches(dst, ref, &sel)) {
892 return SELECTOR_INVALID;
894 } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
896 if (monitor_matches(dst, ref, &sel)) {
899 return SELECTOR_INVALID;
902 if (locate_monitor(desc, dst)) {
904 if (monitor_matches(dst, ref, &sel)) {
907 return SELECTOR_INVALID;
911 return SELECTOR_BAD_DESCRIPTOR;
918 if (dst->monitor == NULL) {
919 return SELECTOR_INVALID;
925 bool locate_leaf(xcb_window_t win, coordinates_t *loc)
927 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
928 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
929 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
942 bool locate_window(xcb_window_t win, coordinates_t *loc)
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 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
947 if (n->client == NULL) {
962 bool locate_desktop(char *name, coordinates_t *loc)
964 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
965 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
966 if (streq(d->name, name)) {
976 bool locate_monitor(char *name, coordinates_t *loc)
978 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
979 if (streq(m->name, name)) {
987 bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
989 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
990 if (mm != NULL && m != mm) {
993 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1005 bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel, int *hits)
1007 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1008 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1009 if (streq(d->name, name)) {
1013 coordinates_t loc = {m, d, NULL};
1014 if (desktop_matches(&loc, ref, sel)) {
1025 bool desktop_from_index(uint16_t idx, coordinates_t *loc, monitor_t *mm)
1027 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1028 if (mm != NULL && m != mm) {
1031 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, idx--) {
1043 bool monitor_from_id(uint32_t id, coordinates_t *loc)
1045 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1048 loc->desktop = NULL;
1056 bool monitor_from_index(int idx, coordinates_t *loc)
1058 for (monitor_t *m = mon_head; m != NULL; m = m->next, idx--) {
1061 loc->desktop = NULL;
1069 bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
1071 if (loc->node == NULL) {
1075 if (sel->focused != OPTION_NONE &&
1076 loc->node != mon->desk->focus
1077 ? sel->focused == OPTION_TRUE
1078 : sel->focused == OPTION_FALSE) {
1082 if (sel->active != OPTION_NONE &&
1083 loc->node != loc->desktop->focus
1084 ? sel->active == OPTION_TRUE
1085 : sel->active == OPTION_FALSE) {
1089 if (sel->automatic != OPTION_NONE &&
1090 loc->node->presel != NULL
1091 ? sel->automatic == OPTION_TRUE
1092 : sel->automatic == OPTION_FALSE) {
1096 if (sel->local != OPTION_NONE &&
1097 loc->desktop != ref->desktop
1098 ? sel->local == OPTION_TRUE
1099 : sel->local == OPTION_FALSE) {
1103 if (sel->active != OPTION_NONE &&
1104 loc->desktop != loc->monitor->desk
1105 ? sel->active == OPTION_TRUE
1106 : sel->active == OPTION_FALSE) {
1110 if (sel->leaf != OPTION_NONE &&
1112 ? sel->leaf == OPTION_TRUE
1113 : sel->leaf == OPTION_FALSE) {
1117 if (sel->window != OPTION_NONE &&
1118 loc->node->client == NULL
1119 ? sel->window == OPTION_TRUE
1120 : sel->window == OPTION_FALSE) {
1125 if (sel->p != OPTION_NONE && \
1127 ? sel->p == OPTION_TRUE \
1128 : sel->p == OPTION_FALSE) { \
1138 if (loc->node->client == NULL &&
1139 (sel->same_class != OPTION_NONE ||
1140 sel->tiled != OPTION_NONE ||
1141 sel->pseudo_tiled != OPTION_NONE ||
1142 sel->floating != OPTION_NONE ||
1143 sel->fullscreen != OPTION_NONE ||
1144 sel->below != OPTION_NONE ||
1145 sel->normal != OPTION_NONE ||
1146 sel->above != OPTION_NONE ||
1147 sel->urgent != OPTION_NONE)) {
1151 if (ref->node != NULL && ref->node->client != NULL &&
1152 sel->same_class != OPTION_NONE &&
1153 streq(loc->node->client->class_name, ref->node->client->class_name)
1154 ? sel->same_class == OPTION_FALSE
1155 : sel->same_class == OPTION_TRUE) {
1159 if (sel->descendant_of != OPTION_NONE &&
1160 !is_descendant(loc->node, ref->node)
1161 ? sel->descendant_of == OPTION_TRUE
1162 : sel->descendant_of == OPTION_FALSE) {
1166 if (sel->ancestor_of != OPTION_NONE &&
1167 !is_descendant(ref->node, loc->node)
1168 ? sel->ancestor_of == OPTION_TRUE
1169 : sel->ancestor_of == OPTION_FALSE) {
1173 #define WSTATE(p, e) \
1174 if (sel->p != OPTION_NONE && \
1175 loc->node->client->state != e \
1176 ? sel->p == OPTION_TRUE \
1177 : sel->p == OPTION_FALSE) { \
1180 WSTATE(tiled, STATE_TILED)
1181 WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
1182 WSTATE(floating, STATE_FLOATING)
1183 WSTATE(fullscreen, STATE_FULLSCREEN)
1186 #define WLAYER(p, e) \
1187 if (sel->p != OPTION_NONE && \
1188 loc->node->client->layer != e \
1189 ? sel->p == OPTION_TRUE \
1190 : sel->p == OPTION_FALSE) { \
1193 WLAYER(below, LAYER_BELOW)
1194 WLAYER(normal, LAYER_NORMAL)
1195 WLAYER(above, LAYER_ABOVE)
1199 if (sel->p != OPTION_NONE && \
1200 !loc->node->client->p \
1201 ? sel->p == OPTION_TRUE \
1202 : sel->p == OPTION_FALSE) { \
1208 if (sel->horizontal != OPTION_NONE &&
1209 loc->node->split_type != TYPE_HORIZONTAL
1210 ? sel->horizontal == OPTION_TRUE
1211 : sel->horizontal == OPTION_FALSE) {
1215 if (sel->vertical != OPTION_NONE &&
1216 loc->node->split_type != TYPE_VERTICAL
1217 ? sel->vertical == OPTION_TRUE
1218 : sel->vertical == OPTION_FALSE) {
1225 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *sel)
1227 if (sel->occupied != OPTION_NONE &&
1228 loc->desktop->root == NULL
1229 ? sel->occupied == OPTION_TRUE
1230 : sel->occupied == OPTION_FALSE) {
1234 if (sel->focused != OPTION_NONE &&
1235 loc->desktop != mon->desk
1236 ? sel->focused == OPTION_TRUE
1237 : sel->focused == OPTION_FALSE) {
1241 if (sel->active != OPTION_NONE &&
1242 loc->desktop != loc->monitor->desk
1243 ? sel->active == OPTION_TRUE
1244 : sel->active == OPTION_FALSE) {
1248 if (sel->urgent != OPTION_NONE &&
1249 !is_urgent(loc->desktop)
1250 ? sel->urgent == OPTION_TRUE
1251 : sel->urgent == OPTION_FALSE) {
1255 if (sel->local != OPTION_NONE &&
1256 ref->monitor != loc->monitor
1257 ? sel->local == OPTION_TRUE
1258 : sel->local == OPTION_FALSE) {
1262 #define DLAYOUT(p, e) \
1263 if (sel->p != OPTION_NONE && \
1264 loc->desktop->layout != e \
1265 ? sel->p == OPTION_TRUE \
1266 : sel->p == OPTION_FALSE) { \
1269 DLAYOUT(tiled, LAYOUT_TILED)
1270 DLAYOUT(monocle, LAYOUT_MONOCLE)
1273 #define DUSERLAYOUT(p, e) \
1274 if (sel->p != OPTION_NONE && \
1275 loc->desktop->user_layout != e \
1276 ? sel->p == OPTION_TRUE \
1277 : sel->p == OPTION_FALSE) { \
1280 DUSERLAYOUT(user_tiled, LAYOUT_TILED)
1281 DUSERLAYOUT(user_monocle, LAYOUT_MONOCLE)
1287 bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t *sel)
1289 if (sel->occupied != OPTION_NONE &&
1290 loc->monitor->desk->root == NULL
1291 ? sel->occupied == OPTION_TRUE
1292 : sel->occupied == OPTION_FALSE) {
1296 if (sel->focused != OPTION_NONE &&
1298 ? sel->focused == OPTION_TRUE
1299 : sel->focused == OPTION_FALSE) {