2 #define I3__FILE__ "commands.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * commands.c: all command functions (see commands_parser.c)
16 #ifdef I3_ASAN_ENABLED
17 #include <sanitizer/lsan_interface.h>
23 // Macros to make the YAJL API a bit easier to use.
24 #define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
25 #define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
26 #define ysuccess(success) \
28 if (cmd_output->json_gen != NULL) { \
35 #define yerror(format, ...) \
37 if (cmd_output->json_gen != NULL) { \
39 sasprintf(&message, format, ##__VA_ARGS__); \
50 /** If an error occurred during parsing of the criteria, we want to exit instead
51 * of relying on fallback behavior. See #2091. */
52 #define HANDLE_INVALID_MATCH \
54 if (current_match->error != NULL) { \
55 yerror("Invalid match: %s", current_match->error); \
60 /** When the command did not include match criteria (!), we use the currently
61 * focused container. Do not confuse this case with a command which included
62 * criteria but which did not match any windows. This macro has to be called in
65 #define HANDLE_EMPTY_MATCH \
67 HANDLE_INVALID_MATCH; \
69 if (match_is_empty(current_match)) { \
70 while (!TAILQ_EMPTY(&owindows)) { \
71 owindow *ow = TAILQ_FIRST(&owindows); \
72 TAILQ_REMOVE(&owindows, ow, owindows); \
75 owindow *ow = smalloc(sizeof(owindow)); \
77 TAILQ_INIT(&owindows); \
78 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
83 * Returns true if a is definitely greater than b (using the given epsilon)
86 static bool definitelyGreaterThan(float a, float b, float epsilon) {
87 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
91 * Returns the output containing the given container.
93 static Output *get_output_of_con(Con *con) {
94 Con *output_con = con_get_output(con);
95 Output *output = get_output_by_name(output_con->name);
96 assert(output != NULL);
102 * Checks whether we switched to a new workspace and returns false in that case,
103 * signaling that further workspace switching should be done by the calling function
104 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
105 * and return true, signaling that no further workspace switching should occur in the calling function.
108 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
109 Con *ws = con_get_workspace(focused);
111 /* If we switched to a different workspace, do nothing */
112 if (strcmp(ws->name, name) != 0)
115 DLOG("This workspace is already focused.\n");
116 if (config.workspace_auto_back_and_forth) {
117 workspace_back_and_forth();
118 cmd_output->needs_tree_render = true;
124 * Return the passed workspace unless it is the current one and auto back and
125 * forth is enabled, in which case the back_and_forth workspace is returned.
127 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
130 if (!config.workspace_auto_back_and_forth)
133 current = con_get_workspace(focused);
135 if (current == workspace) {
136 baf = workspace_back_and_forth_get();
138 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
146 /*******************************************************************************
147 * Criteria functions.
148 ******************************************************************************/
151 * Helper data structure for an operation window (window on which the operation
152 * will be performed). Used to build the TAILQ owindows.
155 typedef struct owindow {
157 TAILQ_ENTRY(owindow) owindows;
160 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
162 static owindows_head owindows;
165 * Initializes the specified 'Match' data structure and the initial state of
166 * commands.c for matching target windows of a command.
169 void cmd_criteria_init(I3_CMD) {
173 DLOG("Initializing criteria, current_match = %p\n", current_match);
174 match_free(current_match);
175 match_init(current_match);
176 while (!TAILQ_EMPTY(&owindows)) {
177 ow = TAILQ_FIRST(&owindows);
178 TAILQ_REMOVE(&owindows, ow, owindows);
181 TAILQ_INIT(&owindows);
183 TAILQ_FOREACH(con, &all_cons, all_cons) {
184 ow = smalloc(sizeof(owindow));
186 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
191 * A match specification just finished (the closing square bracket was found),
192 * so we filter the list of owindows.
195 void cmd_criteria_match_windows(I3_CMD) {
196 owindow *next, *current;
198 DLOG("match specification finished, matching...\n");
199 /* copy the old list head to iterate through it and start with a fresh
200 * list which will contain only matching windows */
201 struct owindows_head old = owindows;
202 TAILQ_INIT(&owindows);
203 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
204 /* make a copy of the next pointer and advance the pointer to the
205 * next element as we are going to invalidate the element’s
206 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
208 next = TAILQ_NEXT(next, owindows);
210 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
212 /* We use this flag to prevent matching on window-less containers if
213 * only window-specific criteria were specified. */
214 bool accept_match = false;
216 if (current_match->con_id != NULL) {
219 if (current_match->con_id == current->con) {
220 DLOG("con_id matched.\n");
222 DLOG("con_id does not match.\n");
228 if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
230 bool matched_by_mark = false;
233 TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
234 if (!regex_matches(current_match->mark, mark->name))
237 DLOG("match by mark\n");
238 matched_by_mark = true;
242 if (!matched_by_mark) {
243 DLOG("mark does not match.\n");
249 if (current->con->window != NULL) {
250 if (match_matches_window(current_match, current->con->window)) {
251 DLOG("matches window!\n");
254 DLOG("doesn't match\n");
261 TAILQ_INSERT_TAIL(&owindows, current, owindows);
268 TAILQ_FOREACH(current, &owindows, owindows) {
269 DLOG("matching: %p / %s\n", current->con, current->con->name);
274 * Interprets a ctype=cvalue pair and adds it to the current match
278 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
279 match_parse_property(current_match, ctype, cvalue);
283 * Implementation of 'move [window|container] [to] workspace
284 * next|prev|next_on_output|prev_on_output|current'.
287 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
290 DLOG("which=%s\n", which);
292 /* We have nothing to move:
293 * when criteria was specified but didn't match any window or
294 * when criteria wasn't specified and we don't have any window focused. */
295 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
296 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
297 !con_has_children(focused))) {
304 /* get the workspace */
306 if (strcmp(which, "next") == 0)
307 ws = workspace_next();
308 else if (strcmp(which, "prev") == 0)
309 ws = workspace_prev();
310 else if (strcmp(which, "next_on_output") == 0)
311 ws = workspace_next_on_output();
312 else if (strcmp(which, "prev_on_output") == 0)
313 ws = workspace_prev_on_output();
314 else if (strcmp(which, "current") == 0)
315 ws = con_get_workspace(focused);
317 ELOG("BUG: called with which=%s\n", which);
322 TAILQ_FOREACH(current, &owindows, owindows) {
323 DLOG("matching: %p / %s\n", current->con, current->con->name);
324 con_move_to_workspace(current->con, ws, true, false, false);
327 cmd_output->needs_tree_render = true;
328 // XXX: default reply for now, make this a better reply
333 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
336 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
340 ws = workspace_back_and_forth_get();
343 yerror("No workspace was previously active.");
349 TAILQ_FOREACH(current, &owindows, owindows) {
350 DLOG("matching: %p / %s\n", current->con, current->con->name);
351 con_move_to_workspace(current->con, ws, true, false, false);
354 cmd_output->needs_tree_render = true;
355 // XXX: default reply for now, make this a better reply
360 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
363 void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
364 if (strncasecmp(name, "__", strlen("__")) == 0) {
365 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
370 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
373 /* We have nothing to move:
374 * when criteria was specified but didn't match any window or
375 * when criteria wasn't specified and we don't have any window focused. */
376 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
377 ELOG("No windows match your criteria, cannot move.\n");
380 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
381 !con_has_children(focused)) {
386 LOG("should move window to workspace %s\n", name);
387 /* get the workspace */
388 Con *ws = workspace_get(name, NULL);
390 if (!no_auto_back_and_forth)
391 ws = maybe_auto_back_and_forth_workspace(ws);
395 TAILQ_FOREACH(current, &owindows, owindows) {
396 DLOG("matching: %p / %s\n", current->con, current->con->name);
397 con_move_to_workspace(current->con, ws, true, false, false);
400 cmd_output->needs_tree_render = true;
401 // XXX: default reply for now, make this a better reply
406 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
409 void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
410 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
413 /* We have nothing to move:
414 * when criteria was specified but didn't match any window or
415 * when criteria wasn't specified and we don't have any window focused. */
416 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
417 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
418 !con_has_children(focused))) {
423 LOG("should move window to workspace %s\n", which);
424 /* get the workspace */
425 Con *output, *workspace = NULL;
427 long parsed_num = ws_name_to_number(which);
429 if (parsed_num == -1) {
430 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
431 yerror("Could not parse number \"%s\"", which);
435 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
436 GREP_FIRST(workspace, output_get_content(output),
437 child->num == parsed_num);
440 workspace = workspace_get(which, NULL);
443 if (!no_auto_back_and_forth)
444 workspace = maybe_auto_back_and_forth_workspace(workspace);
448 TAILQ_FOREACH(current, &owindows, owindows) {
449 DLOG("matching: %p / %s\n", current->con, current->con->name);
450 con_move_to_workspace(current->con, workspace, true, false, false);
453 cmd_output->needs_tree_render = true;
454 // XXX: default reply for now, make this a better reply
458 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) {
459 LOG("floating resize\n");
460 Rect old_rect = floating_con->rect;
461 Con *focused_con = con_descend_focused(floating_con);
463 /* ensure that resize will take place even if pixel increment is smaller than
464 * height increment or width increment.
466 const i3Window *window = focused_con->window;
467 if (window != NULL) {
468 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
469 strcmp(direction, "height") == 0) {
471 px = (-px < window->height_increment) ? -window->height_increment : px;
473 px = (px < window->height_increment) ? window->height_increment : px;
474 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
476 px = (-px < window->width_increment) ? -window->width_increment : px;
478 px = (px < window->width_increment) ? window->width_increment : px;
482 if (strcmp(direction, "up") == 0) {
483 floating_con->rect.height += px;
484 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
485 floating_con->rect.height += px;
486 } else if (strcmp(direction, "left") == 0) {
487 floating_con->rect.width += px;
489 floating_con->rect.width += px;
492 floating_check_size(floating_con);
494 /* Did we actually resize anything or did the size constraints prevent us?
495 * If we could not resize, exit now to not move the window. */
496 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
499 if (strcmp(direction, "up") == 0) {
500 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
501 } else if (strcmp(direction, "left") == 0) {
502 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
505 /* If this is a scratchpad window, don't auto center it from now on. */
506 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
507 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
510 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
511 LOG("tiling resize\n");
513 Con *first = current;
514 direction_t search_direction;
515 if (!strcmp(direction, "left"))
516 search_direction = D_LEFT;
517 else if (!strcmp(direction, "right"))
518 search_direction = D_RIGHT;
519 else if (!strcmp(direction, "up"))
520 search_direction = D_UP;
522 search_direction = D_DOWN;
524 bool res = resize_find_tiling_participants(&first, &second, search_direction);
526 LOG("No second container in this direction found.\n");
531 /* get the default percentage */
532 int children = con_num_children(first->parent);
533 LOG("ins. %d children\n", children);
534 double percentage = 1.0 / children;
535 LOG("default percentage = %f\n", percentage);
538 LOG("second->percent = %f\n", second->percent);
539 LOG("first->percent before = %f\n", first->percent);
540 if (first->percent == 0.0)
541 first->percent = percentage;
542 if (second->percent == 0.0)
543 second->percent = percentage;
544 double new_first_percent = first->percent + ((double)ppt / 100.0);
545 double new_second_percent = second->percent - ((double)ppt / 100.0);
546 LOG("new_first_percent = %f\n", new_first_percent);
547 LOG("new_second_percent = %f\n", new_second_percent);
548 /* Ensure that the new percentages are positive and greater than
549 * 0.05 to have a reasonable minimum size. */
550 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
551 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
552 first->percent += ((double)ppt / 100.0);
553 second->percent -= ((double)ppt / 100.0);
554 LOG("first->percent after = %f\n", first->percent);
555 LOG("second->percent after = %f\n", second->percent);
557 LOG("Not resizing, already at minimum size\n");
563 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
564 LOG("width/height resize\n");
565 /* get the appropriate current container (skip stacked/tabbed cons) */
566 while (current->parent->layout == L_STACKED ||
567 current->parent->layout == L_TABBED)
568 current = current->parent;
570 /* Then further go up until we find one with the matching orientation. */
571 orientation_t search_orientation =
572 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
574 while (current->type != CT_WORKSPACE &&
575 current->type != CT_FLOATING_CON &&
576 (con_orientation(current->parent) != search_orientation || con_num_children(current->parent) == 1))
577 current = current->parent;
579 /* get the default percentage */
580 int children = con_num_children(current->parent);
581 LOG("ins. %d children\n", children);
582 double percentage = 1.0 / children;
583 LOG("default percentage = %f\n", percentage);
585 orientation_t orientation = con_orientation(current->parent);
587 if ((orientation == HORIZ &&
588 strcmp(direction, "height") == 0) ||
589 (orientation == VERT &&
590 strcmp(direction, "width") == 0)) {
591 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
592 (orientation == HORIZ ? "horizontal" : "vertical"));
598 LOG("This is the only container, cannot resize.\n");
603 /* Ensure all the other children have a percentage set. */
605 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
606 LOG("child->percent = %f (child %p)\n", child->percent, child);
607 if (child->percent == 0.0)
608 child->percent = percentage;
611 double new_current_percent = current->percent + ((double)ppt / 100.0);
612 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
613 LOG("new_current_percent = %f\n", new_current_percent);
614 LOG("subtract_percent = %f\n", subtract_percent);
615 /* Ensure that the new percentages are positive and greater than
616 * 0.05 to have a reasonable minimum size. */
617 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
618 if (child == current)
620 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
621 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
626 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
627 LOG("Not resizing, already at minimum size\n");
632 current->percent += ((double)ppt / 100.0);
633 LOG("current->percent after = %f\n", current->percent);
635 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
636 if (child == current)
638 child->percent -= subtract_percent;
639 LOG("child->percent after (%p) = %f\n", child, child->percent);
646 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
649 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
650 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
651 if (strcmp(way, "shrink") == 0) {
659 TAILQ_FOREACH(current, &owindows, owindows) {
660 /* Don't handle dock windows (issue #1201) */
661 if (current->con->window && current->con->window->dock) {
662 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
667 if ((floating_con = con_inside_floating(current->con))) {
668 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
670 if (strcmp(direction, "width") == 0 ||
671 strcmp(direction, "height") == 0) {
672 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
673 current->con, way, direction, resize_ppt))
676 if (!cmd_resize_tiling_direction(current_match, cmd_output,
677 current->con, way, direction, resize_ppt))
683 cmd_output->needs_tree_render = true;
684 // XXX: default reply for now, make this a better reply
689 * Implementation of 'resize set <px> [px] <px> [px]'.
692 void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
693 DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
694 if (cwidth <= 0 || cheight <= 0) {
695 ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
702 TAILQ_FOREACH(current, &owindows, owindows) {
704 if ((floating_con = con_inside_floating(current->con))) {
705 floating_resize(floating_con, cwidth, cheight);
707 ELOG("Resize failed: %p not a floating container\n", current->con);
711 cmd_output->needs_tree_render = true;
712 // XXX: default reply for now, make this a better reply
717 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
720 void cmd_border(I3_CMD, const char *border_style_str, long border_width) {
721 DLOG("border style should be changed to %s with border width %ld\n", border_style_str, border_width);
726 TAILQ_FOREACH(current, &owindows, owindows) {
727 DLOG("matching: %p / %s\n", current->con, current->con->name);
728 int border_style = current->con->border_style;
729 int con_border_width = border_width;
731 if (strcmp(border_style_str, "toggle") == 0) {
734 if (border_style == BS_NORMAL)
735 con_border_width = 2;
736 else if (border_style == BS_NONE)
737 con_border_width = 0;
738 else if (border_style == BS_PIXEL)
739 con_border_width = 1;
741 if (strcmp(border_style_str, "normal") == 0) {
742 border_style = BS_NORMAL;
743 } else if (strcmp(border_style_str, "pixel") == 0) {
744 border_style = BS_PIXEL;
745 } else if (strcmp(border_style_str, "1pixel") == 0) {
746 border_style = BS_PIXEL;
747 con_border_width = 1;
748 } else if (strcmp(border_style_str, "none") == 0) {
749 border_style = BS_NONE;
751 ELOG("BUG: called with border_style=%s\n", border_style_str);
757 con_set_border_style(current->con, border_style, logical_px(con_border_width));
760 cmd_output->needs_tree_render = true;
761 // XXX: default reply for now, make this a better reply
766 * Implementation of 'nop <comment>'.
769 void cmd_nop(I3_CMD, const char *comment) {
770 LOG("-------------------------------------------------\n");
771 LOG(" NOP: %s\n", comment);
772 LOG("-------------------------------------------------\n");
776 * Implementation of 'append_layout <path>'.
779 void cmd_append_layout(I3_CMD, const char *cpath) {
780 char *path = sstrdup(cpath);
781 LOG("Appending layout \"%s\"\n", path);
783 /* Make sure we allow paths like '~/.i3/layout.json' */
784 path = resolve_tilde(path);
786 json_content_t content = json_determine_content(path);
787 LOG("JSON content = %d\n", content);
788 if (content == JSON_CONTENT_UNKNOWN) {
789 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
790 yerror("Could not determine the contents of \"%s\".", path);
795 Con *parent = focused;
796 if (content == JSON_CONTENT_WORKSPACE) {
797 parent = output_get_content(con_get_output(parent));
799 /* We need to append the layout to a split container, since a leaf
800 * container must not have any children (by definition).
801 * Note that we explicitly check for workspaces, since they are okay for
802 * this purpose, but con_accepts_window() returns false for workspaces. */
803 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
804 parent = parent->parent;
806 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
807 char *errormsg = NULL;
808 tree_append_json(parent, path, &errormsg);
809 if (errormsg != NULL) {
812 /* Note that we continue executing since tree_append_json() has
813 * side-effects — user-provided layouts can be partly valid, partly
814 * invalid, leading to half of the placeholder containers being
820 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
821 // false); should be enough, but when sending 'workspace 4; append_layout
822 // /tmp/foo.json', the needs_tree_render == true of the workspace command
823 // is not executed yet and will be batched with append_layout’s
824 // needs_tree_render after the parser finished. We should check if that is
826 render_con(croot, false);
828 restore_open_placeholder_windows(parent);
830 if (content == JSON_CONTENT_WORKSPACE)
831 ipc_send_workspace_event("restored", parent, NULL);
834 cmd_output->needs_tree_render = true;
838 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
841 void cmd_workspace(I3_CMD, const char *which) {
844 DLOG("which=%s\n", which);
846 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
847 LOG("Cannot switch workspace while in global fullscreen\n");
852 if (strcmp(which, "next") == 0)
853 ws = workspace_next();
854 else if (strcmp(which, "prev") == 0)
855 ws = workspace_prev();
856 else if (strcmp(which, "next_on_output") == 0)
857 ws = workspace_next_on_output();
858 else if (strcmp(which, "prev_on_output") == 0)
859 ws = workspace_prev_on_output();
861 ELOG("BUG: called with which=%s\n", which);
868 cmd_output->needs_tree_render = true;
869 // XXX: default reply for now, make this a better reply
874 * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
877 void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
878 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
879 Con *output, *workspace = NULL;
881 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
882 LOG("Cannot switch workspace while in global fullscreen\n");
887 long parsed_num = ws_name_to_number(which);
889 if (parsed_num == -1) {
890 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
891 yerror("Could not parse number \"%s\"", which);
895 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
896 GREP_FIRST(workspace, output_get_content(output),
897 child->num == parsed_num);
900 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
902 workspace_show_by_name(which);
903 cmd_output->needs_tree_render = true;
906 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name))
908 workspace_show(workspace);
910 cmd_output->needs_tree_render = true;
911 // XXX: default reply for now, make this a better reply
916 * Implementation of 'workspace back_and_forth'.
919 void cmd_workspace_back_and_forth(I3_CMD) {
920 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
921 LOG("Cannot switch workspace while in global fullscreen\n");
926 workspace_back_and_forth();
928 cmd_output->needs_tree_render = true;
929 // XXX: default reply for now, make this a better reply
934 * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
937 void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
938 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
940 if (strncasecmp(name, "__", strlen("__")) == 0) {
941 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
946 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
947 LOG("Cannot switch workspace while in global fullscreen\n");
952 DLOG("should switch to workspace %s\n", name);
953 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name))
955 workspace_show_by_name(name);
957 cmd_output->needs_tree_render = true;
958 // XXX: default reply for now, make this a better reply
963 * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
966 void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
969 owindow *current = TAILQ_FIRST(&owindows);
970 if (current == NULL) {
975 /* Marks must be unique, i.e., no two windows must have the same mark. */
976 if (current != TAILQ_LAST(&owindows, owindows_head)) {
977 yerror("A mark must not be put onto more than one window");
981 DLOG("matching: %p / %s\n", current->con, current->con->name);
983 mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
984 if (toggle != NULL) {
985 con_mark_toggle(current->con, mark, mark_mode);
987 con_mark(current->con, mark, mark_mode);
990 cmd_output->needs_tree_render = true;
991 // XXX: default reply for now, make this a better reply
996 * Implementation of 'unmark [mark]'
999 void cmd_unmark(I3_CMD, const char *mark) {
1000 if (match_is_empty(current_match)) {
1001 con_unmark(NULL, mark);
1004 TAILQ_FOREACH(current, &owindows, owindows) {
1005 con_unmark(current->con, mark);
1009 cmd_output->needs_tree_render = true;
1010 // XXX: default reply for now, make this a better reply
1015 * Implementation of 'mode <string>'.
1018 void cmd_mode(I3_CMD, const char *mode) {
1019 DLOG("mode=%s\n", mode);
1022 // XXX: default reply for now, make this a better reply
1027 * Implementation of 'move [window|container] [to] output <str>'.
1030 void cmd_move_con_to_output(I3_CMD, const char *name) {
1031 DLOG("Should move window to output \"%s\".\n", name);
1035 bool had_error = false;
1036 TAILQ_FOREACH(current, &owindows, owindows) {
1037 DLOG("matching: %p / %s\n", current->con, current->con->name);
1039 Output *current_output = get_output_of_con(current->con);
1040 assert(current_output != NULL);
1042 Output *output = get_output_from_string(current_output, name);
1043 if (output == NULL) {
1044 ELOG("Could not find output \"%s\", skipping.\n", name);
1050 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1052 ELOG("Could not find a visible workspace on output %p.\n", output);
1057 con_move_to_workspace(current->con, ws, true, false, false);
1060 cmd_output->needs_tree_render = true;
1061 ysuccess(!had_error);
1065 * Implementation of 'move [container|window] [to] mark <str>'.
1068 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1069 DLOG("moving window to mark \"%s\"\n", mark);
1075 TAILQ_FOREACH(current, &owindows, owindows) {
1076 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1077 result &= con_move_to_mark(current->con, mark);
1080 cmd_output->needs_tree_render = true;
1085 * Implementation of 'floating enable|disable|toggle'
1088 void cmd_floating(I3_CMD, const char *floating_mode) {
1091 DLOG("floating_mode=%s\n", floating_mode);
1095 TAILQ_FOREACH(current, &owindows, owindows) {
1096 DLOG("matching: %p / %s\n", current->con, current->con->name);
1097 if (strcmp(floating_mode, "toggle") == 0) {
1098 DLOG("should toggle mode\n");
1099 toggle_floating_mode(current->con, false);
1101 DLOG("should switch mode to %s\n", floating_mode);
1102 if (strcmp(floating_mode, "enable") == 0) {
1103 floating_enable(current->con, false);
1105 floating_disable(current->con, false);
1110 cmd_output->needs_tree_render = true;
1111 // XXX: default reply for now, make this a better reply
1116 * Implementation of 'move workspace to [output] <str>'.
1119 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1120 DLOG("should move workspace to output %s\n", name);
1125 TAILQ_FOREACH(current, &owindows, owindows) {
1126 Con *ws = con_get_workspace(current->con);
1127 bool success = workspace_move_to_output(ws, name);
1129 ELOG("Failed to move workspace to output.\n");
1135 cmd_output->needs_tree_render = true;
1136 // XXX: default reply for now, make this a better reply
1141 * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1144 void cmd_split(I3_CMD, const char *direction) {
1148 LOG("splitting in direction %c\n", direction[0]);
1149 TAILQ_FOREACH(current, &owindows, owindows) {
1150 if (con_is_docked(current->con)) {
1151 ELOG("Cannot split a docked container, skipping.\n");
1155 DLOG("matching: %p / %s\n", current->con, current->con->name);
1156 if (direction[0] == 't') {
1157 layout_t current_layout;
1158 if (current->con->type == CT_WORKSPACE) {
1159 current_layout = current->con->layout;
1161 current_layout = current->con->parent->layout;
1163 /* toggling split orientation */
1164 if (current_layout == L_SPLITH) {
1165 tree_split(current->con, VERT);
1167 tree_split(current->con, HORIZ);
1170 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1174 cmd_output->needs_tree_render = true;
1175 // XXX: default reply for now, make this a better reply
1180 * Implementation of 'kill [window|client]'.
1183 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1184 if (kill_mode_str == NULL)
1185 kill_mode_str = "window";
1187 DLOG("kill_mode=%s\n", kill_mode_str);
1190 if (strcmp(kill_mode_str, "window") == 0)
1191 kill_mode = KILL_WINDOW;
1192 else if (strcmp(kill_mode_str, "client") == 0)
1193 kill_mode = KILL_CLIENT;
1195 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1203 TAILQ_FOREACH(current, &owindows, owindows) {
1204 con_close(current->con, kill_mode);
1207 cmd_output->needs_tree_render = true;
1208 // XXX: default reply for now, make this a better reply
1213 * Implementation of 'exec [--no-startup-id] <command>'.
1216 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1217 bool no_startup_id = (nosn != NULL);
1219 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1220 start_application(command, no_startup_id);
1222 // XXX: default reply for now, make this a better reply
1227 * Implementation of 'focus left|right|up|down'.
1230 void cmd_focus_direction(I3_CMD, const char *direction) {
1231 DLOG("direction = *%s*\n", direction);
1233 if (strcmp(direction, "left") == 0)
1234 tree_next('p', HORIZ);
1235 else if (strcmp(direction, "right") == 0)
1236 tree_next('n', HORIZ);
1237 else if (strcmp(direction, "up") == 0)
1238 tree_next('p', VERT);
1239 else if (strcmp(direction, "down") == 0)
1240 tree_next('n', VERT);
1242 ELOG("Invalid focus direction (%s)\n", direction);
1247 cmd_output->needs_tree_render = true;
1248 // XXX: default reply for now, make this a better reply
1253 * Implementation of 'focus tiling|floating|mode_toggle'.
1256 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1257 DLOG("window_mode = %s\n", window_mode);
1259 Con *ws = con_get_workspace(focused);
1261 if (strcmp(window_mode, "mode_toggle") == 0) {
1262 if (con_inside_floating(focused))
1263 window_mode = "tiling";
1265 window_mode = "floating";
1268 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1269 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1270 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1273 con_focus(con_descend_focused(current));
1278 cmd_output->needs_tree_render = true;
1279 // XXX: default reply for now, make this a better reply
1284 * Implementation of 'focus parent|child'.
1287 void cmd_focus_level(I3_CMD, const char *level) {
1288 DLOG("level = %s\n", level);
1289 bool success = false;
1291 /* Focusing the parent can only be allowed if the newly
1292 * focused container won't escape the fullscreen container. */
1293 if (strcmp(level, "parent") == 0) {
1294 if (focused && focused->parent) {
1295 if (con_fullscreen_permits_focusing(focused->parent))
1296 success = level_up();
1298 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1302 /* Focusing a child should always be allowed. */
1304 success = level_down();
1306 cmd_output->needs_tree_render = success;
1307 // XXX: default reply for now, make this a better reply
1312 * Implementation of 'focus'.
1315 void cmd_focus(I3_CMD) {
1316 DLOG("current_match = %p\n", current_match);
1318 if (match_is_empty(current_match)) {
1319 ELOG("You have to specify which window/container should be focused.\n");
1320 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1322 yerror("You have to specify which window/container should be focused");
1327 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1330 TAILQ_FOREACH(current, &owindows, owindows) {
1331 Con *ws = con_get_workspace(current->con);
1332 /* If no workspace could be found, this was a dock window.
1333 * Just skip it, you cannot focus dock windows. */
1337 /* Check the fullscreen focus constraints. */
1338 if (!con_fullscreen_permits_focusing(current->con)) {
1339 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1344 /* In case this is a scratchpad window, call scratchpad_show(). */
1345 if (ws == __i3_scratch) {
1346 scratchpad_show(current->con);
1348 /* While for the normal focus case we can change focus multiple
1349 * times and only a single window ends up focused, we could show
1350 * multiple scratchpad windows. So, rather break here. */
1354 /* If the container is not on the current workspace,
1355 * workspace_show() will switch to a different workspace and (if
1356 * enabled) trigger a mouse pointer warp to the currently focused
1357 * container (!) on the target workspace.
1359 * Therefore, before calling workspace_show(), we make sure that
1360 * 'current' will be focused on the workspace. However, we cannot
1361 * just con_focus(current) because then the pointer will not be
1362 * warped at all (the code thinks we are already there).
1364 * So we focus 'current' to make it the currently focused window of
1365 * the target workspace, then revert focus. */
1366 Con *currently_focused = focused;
1367 con_focus(current->con);
1368 con_focus(currently_focused);
1370 /* Now switch to the workspace, then focus */
1372 LOG("focusing %p / %s\n", current->con, current->con->name);
1373 con_focus(current->con);
1378 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1379 "while only exactly one container can be focused at a time.\n",
1382 cmd_output->needs_tree_render = true;
1383 ysuccess(count > 0);
1387 * Implementation of 'fullscreen enable|toggle [global]' and
1388 * 'fullscreen disable'
1391 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1392 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1393 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1398 TAILQ_FOREACH(current, &owindows, owindows) {
1399 DLOG("matching: %p / %s\n", current->con, current->con->name);
1400 if (strcmp(action, "toggle") == 0) {
1401 con_toggle_fullscreen(current->con, mode);
1402 } else if (strcmp(action, "enable") == 0) {
1403 con_enable_fullscreen(current->con, mode);
1404 } else if (strcmp(action, "disable") == 0) {
1405 con_disable_fullscreen(current->con);
1409 cmd_output->needs_tree_render = true;
1410 // XXX: default reply for now, make this a better reply
1415 * Implementation of 'sticky enable|disable|toggle'.
1418 void cmd_sticky(I3_CMD, const char *action) {
1419 DLOG("%s sticky on window\n", action);
1423 TAILQ_FOREACH(current, &owindows, owindows) {
1424 if (current->con->window == NULL) {
1425 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1428 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1430 bool sticky = false;
1431 if (strcmp(action, "enable") == 0)
1433 else if (strcmp(action, "disable") == 0)
1435 else if (strcmp(action, "toggle") == 0)
1436 sticky = !current->con->sticky;
1438 current->con->sticky = sticky;
1439 ewmh_update_sticky(current->con->window->id, sticky);
1442 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1443 * sure it gets pushed to the front now. */
1444 output_push_sticky_windows(focused);
1446 ewmh_update_wm_desktop();
1448 cmd_output->needs_tree_render = true;
1453 * Implementation of 'move <direction> [<pixels> [px]]'.
1456 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1460 Con *initially_focused = focused;
1462 TAILQ_FOREACH(current, &owindows, owindows) {
1463 DLOG("moving in direction %s, px %ld\n", direction, move_px);
1464 if (con_is_floating(current->con)) {
1465 DLOG("floating move with %ld pixels\n", move_px);
1466 Rect newrect = current->con->parent->rect;
1467 if (strcmp(direction, "left") == 0) {
1468 newrect.x -= move_px;
1469 } else if (strcmp(direction, "right") == 0) {
1470 newrect.x += move_px;
1471 } else if (strcmp(direction, "up") == 0) {
1472 newrect.y -= move_px;
1473 } else if (strcmp(direction, "down") == 0) {
1474 newrect.y += move_px;
1476 floating_reposition(current->con->parent, newrect);
1478 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1479 cmd_output->needs_tree_render = true;
1483 /* the move command should not disturb focus */
1484 if (focused != initially_focused)
1485 con_focus(initially_focused);
1487 // XXX: default reply for now, make this a better reply
1492 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1495 void cmd_layout(I3_CMD, const char *layout_str) {
1498 if (strcmp(layout_str, "stacking") == 0)
1499 layout_str = "stacked";
1501 /* default is a special case which will be handled in con_set_layout(). */
1502 if (strcmp(layout_str, "default") == 0)
1504 else if (strcmp(layout_str, "stacked") == 0)
1506 else if (strcmp(layout_str, "tabbed") == 0)
1508 else if (strcmp(layout_str, "splitv") == 0)
1510 else if (strcmp(layout_str, "splith") == 0)
1513 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1517 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1520 TAILQ_FOREACH(current, &owindows, owindows) {
1521 if (con_is_docked(current->con)) {
1522 ELOG("cannot change layout of a docked container, skipping it.\n");
1526 DLOG("matching: %p / %s\n", current->con, current->con->name);
1527 con_set_layout(current->con, layout);
1530 cmd_output->needs_tree_render = true;
1531 // XXX: default reply for now, make this a better reply
1536 * Implementation of 'layout toggle [all|split]'.
1539 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1542 if (toggle_mode == NULL)
1543 toggle_mode = "default";
1545 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1547 /* check if the match is empty, not if the result is empty */
1548 if (match_is_empty(current_match))
1549 con_toggle_layout(focused, toggle_mode);
1551 TAILQ_FOREACH(current, &owindows, owindows) {
1552 DLOG("matching: %p / %s\n", current->con, current->con->name);
1553 con_toggle_layout(current->con, toggle_mode);
1557 cmd_output->needs_tree_render = true;
1558 // XXX: default reply for now, make this a better reply
1563 * Implementation of 'exit'.
1566 void cmd_exit(I3_CMD) {
1567 LOG("Exiting due to user command.\n");
1568 #ifdef I3_ASAN_ENABLED
1569 __lsan_do_leak_check();
1572 unlink(config.ipc_socket_path);
1573 xcb_disconnect(conn);
1580 * Implementation of 'reload'.
1583 void cmd_reload(I3_CMD) {
1585 kill_nagbar(&config_error_nagbar_pid, false);
1586 kill_nagbar(&command_error_nagbar_pid, false);
1587 load_configuration(conn, NULL, true);
1589 /* Send an IPC event just in case the ws names have changed */
1590 ipc_send_workspace_event("reload", NULL, NULL);
1591 /* Send an update event for the barconfig just in case it has changed */
1594 // XXX: default reply for now, make this a better reply
1599 * Implementation of 'restart'.
1602 void cmd_restart(I3_CMD) {
1603 LOG("restarting i3\n");
1605 unlink(config.ipc_socket_path);
1606 /* We need to call this manually since atexit handlers don’t get called
1608 purge_zerobyte_logfile();
1611 // XXX: default reply for now, make this a better reply
1616 * Implementation of 'open'.
1619 void cmd_open(I3_CMD) {
1620 LOG("opening new container\n");
1621 Con *con = tree_open_con(NULL, NULL);
1622 con->layout = L_SPLITH;
1629 y(integer, (uintptr_t)con);
1632 cmd_output->needs_tree_render = true;
1636 * Implementation of 'focus output <output>'.
1639 void cmd_focus_output(I3_CMD, const char *name) {
1642 DLOG("name = %s\n", name);
1646 /* get the output */
1647 Output *current_output = NULL;
1650 TAILQ_FOREACH(current, &owindows, owindows)
1651 current_output = get_output_of_con(current->con);
1652 assert(current_output != NULL);
1654 output = get_output_from_string(current_output, name);
1657 LOG("No such output found.\n");
1662 /* get visible workspace on output */
1664 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1672 cmd_output->needs_tree_render = true;
1673 // XXX: default reply for now, make this a better reply
1678 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1681 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1682 bool has_error = false;
1687 TAILQ_FOREACH(current, &owindows, owindows) {
1688 if (!con_is_floating(current->con)) {
1689 ELOG("Cannot change position. The window/container is not floating\n");
1692 yerror("Cannot change position of a window/container because it is not floating.");
1699 if (strcmp(method, "absolute") == 0) {
1700 current->con->parent->rect.x = x;
1701 current->con->parent->rect.y = y;
1703 DLOG("moving to absolute position %ld %ld\n", x, y);
1704 floating_maybe_reassign_ws(current->con->parent);
1705 cmd_output->needs_tree_render = true;
1708 if (strcmp(method, "position") == 0) {
1709 Rect newrect = current->con->parent->rect;
1711 DLOG("moving to position %ld %ld\n", x, y);
1715 floating_reposition(current->con->parent, newrect);
1719 // XXX: default reply for now, make this a better reply
1725 * Implementation of 'move [window|container] [to] [absolute] position center
1728 void cmd_move_window_to_center(I3_CMD, const char *method) {
1729 bool has_error = false;
1733 TAILQ_FOREACH(current, &owindows, owindows) {
1734 Con *floating_con = con_inside_floating(current->con);
1735 if (floating_con == NULL) {
1736 ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1737 current->con, current->con->name);
1740 yerror("Cannot change position of a window/container because it is not floating.");
1747 if (strcmp(method, "absolute") == 0) {
1748 DLOG("moving to absolute center\n");
1749 floating_center(floating_con, croot->rect);
1751 floating_maybe_reassign_ws(floating_con);
1752 cmd_output->needs_tree_render = true;
1755 if (strcmp(method, "position") == 0) {
1756 DLOG("moving to center\n");
1757 floating_center(floating_con, con_get_workspace(floating_con)->rect);
1759 cmd_output->needs_tree_render = true;
1763 // XXX: default reply for now, make this a better reply
1769 * Implementation of 'move [window|container] [to] position mouse'
1772 void cmd_move_window_to_mouse(I3_CMD) {
1776 TAILQ_FOREACH(current, &owindows, owindows) {
1777 Con *floating_con = con_inside_floating(current->con);
1778 if (floating_con == NULL) {
1779 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1780 current->con, current->con->name);
1784 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1785 floating_move_to_pointer(floating_con);
1788 cmd_output->needs_tree_render = true;
1793 * Implementation of 'move scratchpad'.
1796 void cmd_move_scratchpad(I3_CMD) {
1797 DLOG("should move window to scratchpad\n");
1802 TAILQ_FOREACH(current, &owindows, owindows) {
1803 DLOG("matching: %p / %s\n", current->con, current->con->name);
1804 scratchpad_move(current->con);
1807 cmd_output->needs_tree_render = true;
1808 // XXX: default reply for now, make this a better reply
1813 * Implementation of 'scratchpad show'.
1816 void cmd_scratchpad_show(I3_CMD) {
1817 DLOG("should show scratchpad window\n");
1820 if (match_is_empty(current_match)) {
1821 scratchpad_show(NULL);
1823 TAILQ_FOREACH(current, &owindows, owindows) {
1824 DLOG("matching: %p / %s\n", current->con, current->con->name);
1825 scratchpad_show(current->con);
1829 cmd_output->needs_tree_render = true;
1830 // XXX: default reply for now, make this a better reply
1835 * Implementation of 'title_format <format>'
1838 void cmd_title_format(I3_CMD, const char *format) {
1839 DLOG("setting title_format to \"%s\"\n", format);
1843 TAILQ_FOREACH(current, &owindows, owindows) {
1844 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1845 FREE(current->con->title_format);
1847 /* If we only display the title without anything else, we can skip the parsing step,
1848 * so we remove the title format altogether. */
1849 if (strcasecmp(format, "%title") != 0) {
1850 current->con->title_format = sstrdup(format);
1852 if (current->con->window != NULL) {
1853 i3String *formatted_title = con_parse_title_format(current->con);
1854 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1855 I3STRING_FREE(formatted_title);
1858 if (current->con->window != NULL) {
1859 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1860 ewmh_update_visible_name(current->con->window->id, NULL);
1864 if (current->con->window != NULL) {
1865 /* Make sure the window title is redrawn immediately. */
1866 current->con->window->name_x_changed = true;
1868 /* For windowless containers we also need to force the redrawing. */
1869 FREE(current->con->deco_render_params);
1873 cmd_output->needs_tree_render = true;
1878 * Implementation of 'rename workspace [<name>] to <name>'
1881 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1882 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1883 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1888 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1890 LOG("Renaming current workspace to \"%s\"\n", new_name);
1893 Con *output, *workspace = NULL;
1895 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1896 GREP_FIRST(workspace, output_get_content(output),
1897 !strcasecmp(child->name, old_name));
1899 workspace = con_get_workspace(focused);
1900 old_name = workspace->name;
1904 yerror("Old workspace \"%s\" not found", old_name);
1908 Con *check_dest = NULL;
1909 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1910 GREP_FIRST(check_dest, output_get_content(output),
1911 !strcasecmp(child->name, new_name));
1913 /* If check_dest == workspace, the user might be changing the case of the
1914 * workspace, or it might just be a no-op. */
1915 if (check_dest != NULL && check_dest != workspace) {
1916 yerror("New workspace \"%s\" already exists", new_name);
1920 /* Change the name and try to parse it as a number. */
1921 /* old_name might refer to workspace->name, so copy it before free()ing */
1922 char *old_name_copy = sstrdup(old_name);
1923 FREE(workspace->name);
1924 workspace->name = sstrdup(new_name);
1926 workspace->num = ws_name_to_number(new_name);
1927 LOG("num = %d\n", workspace->num);
1929 /* By re-attaching, the sort order will be correct afterwards. */
1930 Con *previously_focused = focused;
1931 Con *parent = workspace->parent;
1932 con_detach(workspace);
1933 con_attach(workspace, parent, false);
1935 /* Move the workspace to the correct output if it has an assignment */
1936 struct Workspace_Assignment *assignment = NULL;
1937 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1938 if (assignment->output == NULL)
1940 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1944 workspace_move_to_output(workspace, assignment->output);
1946 if (previously_focused)
1947 workspace_show(con_get_workspace(previously_focused));
1952 /* Restore the previous focus since con_attach messes with the focus. */
1953 con_focus(previously_focused);
1955 cmd_output->needs_tree_render = true;
1958 ipc_send_workspace_event("rename", workspace, NULL);
1959 ewmh_update_desktop_names();
1960 ewmh_update_desktop_viewport();
1961 ewmh_update_current_desktop();
1963 startup_sequence_rename_workspace(old_name_copy, new_name);
1964 free(old_name_copy);
1968 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1971 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
1973 bool toggle = false;
1974 if (strcmp(bar_mode, "dock") == 0)
1976 else if (strcmp(bar_mode, "hide") == 0)
1978 else if (strcmp(bar_mode, "invisible") == 0)
1980 else if (strcmp(bar_mode, "toggle") == 0)
1983 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1987 bool changed_sth = false;
1988 Barconfig *current = NULL;
1989 TAILQ_FOREACH(current, &barconfigs, configs) {
1990 if (bar_id && strcmp(current->id, bar_id) != 0)
1994 mode = (current->mode + 1) % 2;
1996 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1997 current->mode = mode;
2004 if (bar_id && !changed_sth) {
2005 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2013 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2016 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2017 int hidden_state = S_SHOW;
2018 bool toggle = false;
2019 if (strcmp(bar_hidden_state, "hide") == 0)
2020 hidden_state = S_HIDE;
2021 else if (strcmp(bar_hidden_state, "show") == 0)
2022 hidden_state = S_SHOW;
2023 else if (strcmp(bar_hidden_state, "toggle") == 0)
2026 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2030 bool changed_sth = false;
2031 Barconfig *current = NULL;
2032 TAILQ_FOREACH(current, &barconfigs, configs) {
2033 if (bar_id && strcmp(current->id, bar_id) != 0)
2037 hidden_state = (current->hidden_state + 1) % 2;
2039 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2040 current->hidden_state = hidden_state;
2047 if (bar_id && !changed_sth) {
2048 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2056 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2059 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2061 if (strcmp(bar_type, "mode") == 0)
2062 ret = cmd_bar_mode(bar_value, bar_id);
2063 else if (strcmp(bar_type, "hidden_state") == 0)
2064 ret = cmd_bar_hidden_state(bar_value, bar_id);
2066 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2078 * Implementation of 'shmlog <size>|toggle|on|off'
2081 void cmd_shmlog(I3_CMD, const char *argument) {
2082 if (!strcmp(argument, "toggle"))
2083 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2084 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2085 else if (!strcmp(argument, "on"))
2086 shmlog_size = default_shmlog_size;
2087 else if (!strcmp(argument, "off"))
2090 /* If shm logging now, restart logging with the new size. */
2091 if (shmlog_size > 0) {
2093 LOG("Restarting shm logging...\n");
2096 shmlog_size = atoi(argument);
2097 /* Make a weakly attempt at ensuring the argument is valid. */
2098 if (shmlog_size <= 0)
2099 shmlog_size = default_shmlog_size;
2101 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2103 update_shmlog_atom();
2104 // XXX: default reply for now, make this a better reply
2109 * Implementation of 'debuglog toggle|on|off'
2112 void cmd_debuglog(I3_CMD, const char *argument) {
2113 bool logging = get_debug_logging();
2114 if (!strcmp(argument, "toggle")) {
2115 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2116 set_debug_logging(!logging);
2117 } else if (!strcmp(argument, "on") && !logging) {
2118 LOG("Enabling debug logging\n");
2119 set_debug_logging(true);
2120 } else if (!strcmp(argument, "off") && logging) {
2121 LOG("Disabling debug logging\n");
2122 set_debug_logging(false);
2124 // XXX: default reply for now, make this a better reply