2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * commands.c: all command functions (see commands_parser.c)
18 // Macros to make the YAJL API a bit easier to use.
19 #define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
20 #define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
21 #define ysuccess(success) \
23 if (cmd_output->json_gen != NULL) { \
30 #define yerror(format, ...) \
32 if (cmd_output->json_gen != NULL) { \
34 sasprintf(&message, format, ##__VA_ARGS__); \
45 /** If an error occurred during parsing of the criteria, we want to exit instead
46 * of relying on fallback behavior. See #2091. */
47 #define HANDLE_INVALID_MATCH \
49 if (current_match->error != NULL) { \
50 yerror("Invalid match: %s", current_match->error); \
55 /** When the command did not include match criteria (!), we use the currently
56 * focused container. Do not confuse this case with a command which included
57 * criteria but which did not match any windows. This macro has to be called in
60 #define HANDLE_EMPTY_MATCH \
62 HANDLE_INVALID_MATCH; \
64 if (match_is_empty(current_match)) { \
65 while (!TAILQ_EMPTY(&owindows)) { \
66 owindow *ow = TAILQ_FIRST(&owindows); \
67 TAILQ_REMOVE(&owindows, ow, owindows); \
70 owindow *ow = smalloc(sizeof(owindow)); \
72 TAILQ_INIT(&owindows); \
73 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
78 * Checks whether we switched to a new workspace and returns false in that case,
79 * signaling that further workspace switching should be done by the calling function
80 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
81 * and return true, signaling that no further workspace switching should occur in the calling function.
84 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
85 Con *ws = con_get_workspace(focused);
87 /* If we switched to a different workspace, do nothing */
88 if (strcmp(ws->name, name) != 0)
91 DLOG("This workspace is already focused.\n");
92 if (config.workspace_auto_back_and_forth) {
93 workspace_back_and_forth();
94 cmd_output->needs_tree_render = true;
100 * Return the passed workspace unless it is the current one and auto back and
101 * forth is enabled, in which case the back_and_forth workspace is returned.
103 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
106 if (!config.workspace_auto_back_and_forth)
109 current = con_get_workspace(focused);
111 if (current == workspace) {
112 baf = workspace_back_and_forth_get();
114 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
122 /*******************************************************************************
123 * Criteria functions.
124 ******************************************************************************/
127 * Helper data structure for an operation window (window on which the operation
128 * will be performed). Used to build the TAILQ owindows.
131 typedef struct owindow {
138 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
140 static owindows_head owindows;
143 * Initializes the specified 'Match' data structure and the initial state of
144 * commands.c for matching target windows of a command.
147 void cmd_criteria_init(I3_CMD) {
151 DLOG("Initializing criteria, current_match = %p\n", current_match);
152 match_free(current_match);
153 match_init(current_match);
154 while (!TAILQ_EMPTY(&owindows)) {
155 ow = TAILQ_FIRST(&owindows);
156 TAILQ_REMOVE(&owindows, ow, owindows);
159 TAILQ_INIT(&owindows);
161 TAILQ_FOREACH(con, &all_cons, all_cons) {
162 ow = smalloc(sizeof(owindow));
164 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
169 * A match specification just finished (the closing square bracket was found),
170 * so we filter the list of owindows.
173 void cmd_criteria_match_windows(I3_CMD) {
174 owindow *next, *current;
176 DLOG("match specification finished, matching...\n");
177 /* copy the old list head to iterate through it and start with a fresh
178 * list which will contain only matching windows */
179 struct owindows_head old = owindows;
180 TAILQ_INIT(&owindows);
181 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
182 /* make a copy of the next pointer and advance the pointer to the
183 * next element as we are going to invalidate the element’s
184 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
186 next = TAILQ_NEXT(next, owindows);
188 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
190 /* We use this flag to prevent matching on window-less containers if
191 * only window-specific criteria were specified. */
192 bool accept_match = false;
194 if (current_match->con_id != NULL) {
197 if (current_match->con_id == current->con) {
198 DLOG("con_id matched.\n");
200 DLOG("con_id does not match.\n");
206 if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
208 bool matched_by_mark = false;
211 TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
212 if (!regex_matches(current_match->mark, mark->name))
215 DLOG("match by mark\n");
216 matched_by_mark = true;
220 if (!matched_by_mark) {
221 DLOG("mark does not match.\n");
227 if (current->con->window != NULL) {
228 if (match_matches_window(current_match, current->con->window)) {
229 DLOG("matches window!\n");
232 DLOG("doesn't match\n");
239 TAILQ_INSERT_TAIL(&owindows, current, owindows);
246 TAILQ_FOREACH(current, &owindows, owindows) {
247 DLOG("matching: %p / %s\n", current->con, current->con->name);
252 * Interprets a ctype=cvalue pair and adds it to the current match
256 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
257 match_parse_property(current_match, ctype, cvalue);
260 static void move_matches_to_workspace(Con *ws) {
262 TAILQ_FOREACH(current, &owindows, owindows) {
263 DLOG("matching: %p / %s\n", current->con, current->con->name);
264 con_move_to_workspace(current->con, ws, true, false, false);
268 #define CHECK_MOVE_CON_TO_WORKSPACE \
270 HANDLE_EMPTY_MATCH; \
271 if (TAILQ_EMPTY(&owindows)) { \
272 yerror("Nothing to move: specified criteria don't match any window"); \
275 bool found = false; \
276 owindow *current = TAILQ_FIRST(&owindows); \
278 owindow *next = TAILQ_NEXT(current, owindows); \
280 if (current->con->type == CT_WORKSPACE && !con_has_children(current->con)) { \
281 TAILQ_REMOVE(&owindows, current, owindows); \
289 yerror("Nothing to move: workspace empty"); \
296 * Implementation of 'move [window|container] [to] workspace
297 * next|prev|next_on_output|prev_on_output|current'.
300 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
301 DLOG("which=%s\n", which);
303 CHECK_MOVE_CON_TO_WORKSPACE;
305 /* get the workspace */
307 if (strcmp(which, "next") == 0)
308 ws = workspace_next();
309 else if (strcmp(which, "prev") == 0)
310 ws = workspace_prev();
311 else if (strcmp(which, "next_on_output") == 0)
312 ws = workspace_next_on_output();
313 else if (strcmp(which, "prev_on_output") == 0)
314 ws = workspace_prev_on_output();
315 else if (strcmp(which, "current") == 0)
316 ws = con_get_workspace(focused);
318 yerror("BUG: called with which=%s", which);
322 move_matches_to_workspace(ws);
324 cmd_output->needs_tree_render = true;
325 // XXX: default reply for now, make this a better reply
330 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
333 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
334 Con *ws = workspace_back_and_forth_get();
336 yerror("No workspace was previously active.");
342 move_matches_to_workspace(ws);
344 cmd_output->needs_tree_render = true;
345 // XXX: default reply for now, make this a better reply
350 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
353 void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *no_auto_back_and_forth) {
354 if (strncasecmp(name, "__", strlen("__")) == 0) {
355 yerror("You cannot move containers to i3-internal workspaces (\"%s\").", name);
359 CHECK_MOVE_CON_TO_WORKSPACE;
361 LOG("should move window to workspace %s\n", name);
362 /* get the workspace */
363 Con *ws = workspace_get(name, NULL);
365 if (no_auto_back_and_forth == NULL) {
366 ws = maybe_auto_back_and_forth_workspace(ws);
369 move_matches_to_workspace(ws);
371 cmd_output->needs_tree_render = true;
372 // XXX: default reply for now, make this a better reply
377 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
380 void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *no_auto_back_and_forth) {
381 CHECK_MOVE_CON_TO_WORKSPACE;
383 LOG("should move window to workspace %s\n", which);
385 long parsed_num = ws_name_to_number(which);
386 if (parsed_num == -1) {
387 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
388 yerror("Could not parse number \"%s\"", which);
392 Con *ws = get_existing_workspace_by_num(parsed_num);
394 ws = workspace_get(which, NULL);
397 if (no_auto_back_and_forth == NULL) {
398 ws = maybe_auto_back_and_forth_workspace(ws);
401 move_matches_to_workspace(ws);
403 cmd_output->needs_tree_render = true;
404 // XXX: default reply for now, make this a better reply
409 * Convert a string direction ("left", "right", etc.) to a direction_t. Assumes
410 * valid direction string.
412 static direction_t parse_direction(const char *str) {
413 if (strcmp(str, "left") == 0) {
415 } else if (strcmp(str, "right") == 0) {
417 } else if (strcmp(str, "up") == 0) {
419 } else if (strcmp(str, "down") == 0) {
422 ELOG("Invalid direction. This is a parser bug.\n");
427 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction_str, Con *floating_con, int px) {
428 Rect old_rect = floating_con->rect;
429 Con *focused_con = con_descend_focused(floating_con);
431 direction_t direction;
432 if (strcmp(direction_str, "height") == 0) {
434 } else if (strcmp(direction_str, "width") == 0) {
437 direction = parse_direction(direction_str);
439 orientation_t orientation = orientation_from_direction(direction);
441 /* ensure that resize will take place even if pixel increment is smaller than
442 * height increment or width increment.
444 const i3Window *window = focused_con->window;
445 if (window != NULL) {
446 if (orientation == VERT) {
448 px = (-px < window->height_increment) ? -window->height_increment : px;
450 px = (px < window->height_increment) ? window->height_increment : px;
454 px = (-px < window->width_increment) ? -window->width_increment : px;
456 px = (px < window->width_increment) ? window->width_increment : px;
461 if (orientation == VERT) {
462 floating_con->rect.height += px;
464 floating_con->rect.width += px;
466 floating_check_size(floating_con, orientation == VERT);
468 /* Did we actually resize anything or did the size constraints prevent us?
469 * If we could not resize, exit now to not move the window. */
470 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0) {
474 if (direction == D_UP) {
475 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
476 } else if (direction == D_LEFT) {
477 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
480 /* If this is a scratchpad window, don't auto center it from now on. */
481 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH) {
482 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
486 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *direction, int px, int ppt) {
488 Con *first = current;
489 direction_t search_direction = parse_direction(direction);
491 bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
493 yerror("No second container found in this direction.");
498 /* For backwards compatibility, 'X px or Y ppt' means that ppt is
502 return resize_neighboring_cons(first, second, px, ppt);
505 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *direction, int px, double ppt) {
506 LOG("width/height resize\n");
508 /* get the appropriate current container (skip stacked/tabbed cons) */
510 direction_t search_direction = (strcmp(direction, "width") == 0 ? D_LEFT : D_DOWN);
511 bool search_result = resize_find_tiling_participants(¤t, &dummy, search_direction, true);
512 if (search_result == false) {
513 yerror("Failed to find appropriate tiling containers for resize operation");
517 /* get the default percentage */
518 int children = con_num_children(current->parent);
519 LOG("ins. %d children\n", children);
520 double percentage = 1.0 / children;
521 LOG("default percentage = %f\n", percentage);
523 /* Ensure all the other children have a percentage set. */
525 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
526 LOG("child->percent = %f (child %p)\n", child->percent, child);
527 if (child->percent == 0.0)
528 child->percent = percentage;
531 double new_current_percent;
532 double subtract_percent;
534 new_current_percent = current->percent + ppt;
536 /* Convert px change to change in percentages */
537 ppt = (double)px / (double)con_rect_size_in_orientation(current->parent);
538 new_current_percent = current->percent + ppt;
540 subtract_percent = ppt / (children - 1);
541 if (ppt < 0.0 && new_current_percent < percent_for_1px(current)) {
542 yerror("Not resizing, container would end with less than 1px");
546 LOG("new_current_percent = %f\n", new_current_percent);
547 LOG("subtract_percent = %f\n", subtract_percent);
548 /* Ensure that the new percentages are positive. */
549 if (subtract_percent >= 0.0) {
550 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
551 if (child == current) {
554 if (child->percent - subtract_percent < percent_for_1px(child)) {
555 yerror("Not resizing, already at minimum size (child %p would end up with a size of %.f", child, child->percent - subtract_percent);
561 current->percent = new_current_percent;
562 LOG("current->percent after = %f\n", current->percent);
564 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
565 if (child == current)
567 child->percent -= subtract_percent;
568 LOG("child->percent after (%p) = %f\n", child, child->percent);
575 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
578 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
579 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
580 if (strcmp(way, "shrink") == 0) {
588 TAILQ_FOREACH(current, &owindows, owindows) {
589 /* Don't handle dock windows (issue #1201) */
590 if (current->con->window && current->con->window->dock) {
591 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
596 if ((floating_con = con_inside_floating(current->con))) {
597 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
599 if (strcmp(direction, "width") == 0 ||
600 strcmp(direction, "height") == 0) {
601 const double ppt = (double)resize_ppt / 100.0;
602 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
603 current->con, direction,
605 yerror("Cannot resize.");
609 if (!cmd_resize_tiling_direction(current_match, cmd_output,
610 current->con, direction,
611 resize_px, resize_ppt)) {
612 yerror("Cannot resize.");
619 cmd_output->needs_tree_render = true;
620 // XXX: default reply for now, make this a better reply
624 static bool resize_set_tiling(I3_CMD, Con *target, orientation_t resize_orientation, bool is_ppt, long target_size) {
625 direction_t search_direction;
627 if (resize_orientation == HORIZ) {
628 search_direction = D_LEFT;
631 search_direction = D_DOWN;
635 /* Get the appropriate current container (skip stacked/tabbed cons) */
637 resize_find_tiling_participants(&target, &dummy, search_direction, true);
639 /* Calculate new size for the target container */
643 ppt = (double)target_size / 100.0 - target->percent;
645 px = target_size - (resize_orientation == HORIZ ? target->rect.width : target->rect.height);
648 /* Perform resizing and report failure if not possible */
649 return cmd_resize_tiling_width_height(current_match, cmd_output,
650 target, mode, px, ppt);
654 * Implementation of 'resize set <width> [px | ppt] <height> [px | ppt]'.
657 void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, const char *mode_height) {
658 DLOG("resizing to %ld %s x %ld %s\n", cwidth, mode_width, cheight, mode_height);
659 if (cwidth < 0 || cheight < 0) {
660 yerror("Dimensions cannot be negative.");
668 TAILQ_FOREACH(current, &owindows, owindows) {
670 if ((floating_con = con_inside_floating(current->con))) {
671 Con *output = con_get_output(floating_con);
673 cwidth = floating_con->rect.width;
674 } else if (mode_width && strcmp(mode_width, "ppt") == 0) {
675 cwidth = output->rect.width * ((double)cwidth / 100.0);
678 cheight = floating_con->rect.height;
679 } else if (mode_height && strcmp(mode_height, "ppt") == 0) {
680 cheight = output->rect.height * ((double)cheight / 100.0);
682 floating_resize(floating_con, cwidth, cheight);
684 if (current->con->window && current->con->window->dock) {
685 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
690 bool is_ppt = mode_width && strcmp(mode_width, "ppt") == 0;
691 success &= resize_set_tiling(current_match, cmd_output, current->con,
692 HORIZ, is_ppt, cwidth);
695 bool is_ppt = mode_height && strcmp(mode_height, "ppt") == 0;
696 success &= resize_set_tiling(current_match, cmd_output, current->con,
697 VERT, is_ppt, cheight);
702 cmd_output->needs_tree_render = true;
706 static int border_width_from_style(border_style_t border_style, long border_width, Con *con) {
707 if (border_style == BS_NONE) {
710 if (border_width >= 0) {
711 return logical_px(border_width);
714 const bool is_floating = con_inside_floating(con) != NULL;
715 /* Load the configured defaults. */
716 if (is_floating && border_style == config.default_floating_border) {
717 return config.default_floating_border_width;
718 } else if (!is_floating && border_style == config.default_border) {
719 return config.default_border_width;
721 /* Use some hardcoded values. */
722 return logical_px(border_style == BS_NORMAL ? 2 : 1);
727 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
730 void cmd_border(I3_CMD, const char *border_style_str, long border_width) {
731 DLOG("border style should be changed to %s with border width %ld\n", border_style_str, border_width);
736 TAILQ_FOREACH(current, &owindows, owindows) {
737 DLOG("matching: %p / %s\n", current->con, current->con->name);
739 border_style_t border_style;
740 if (strcmp(border_style_str, "toggle") == 0) {
741 border_style = (current->con->border_style + 1) % 3;
742 } else if (strcmp(border_style_str, "normal") == 0) {
743 border_style = BS_NORMAL;
744 } else if (strcmp(border_style_str, "pixel") == 0) {
745 border_style = BS_PIXEL;
746 } else if (strcmp(border_style_str, "none") == 0) {
747 border_style = BS_NONE;
749 yerror("BUG: called with border_style=%s", border_style_str);
753 const int con_border_width = border_width_from_style(border_style, border_width, current->con);
754 con_set_border_style(current->con, border_style, con_border_width);
757 cmd_output->needs_tree_render = true;
762 * Implementation of 'nop <comment>'.
765 void cmd_nop(I3_CMD, const char *comment) {
766 LOG("-------------------------------------------------\n");
767 LOG(" NOP: %s\n", comment);
768 LOG("-------------------------------------------------\n");
773 * Implementation of 'append_layout <path>'.
776 void cmd_append_layout(I3_CMD, const char *cpath) {
777 LOG("Appending layout \"%s\"\n", cpath);
779 /* Make sure we allow paths like '~/.i3/layout.json' */
780 char *path = resolve_tilde(cpath);
784 if ((len = slurp(path, &buf)) < 0) {
785 yerror("Could not slurp \"%s\".", path);
786 /* slurp already logged an error. */
790 if (!json_validate(buf, len)) {
791 ELOG("Could not parse \"%s\" as JSON, not loading.\n", path);
792 yerror("Could not parse \"%s\" as JSON.", path);
796 json_content_t content = json_determine_content(buf, len);
797 LOG("JSON content = %d\n", content);
798 if (content == JSON_CONTENT_UNKNOWN) {
799 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
800 yerror("Could not determine the contents of \"%s\".", path);
804 Con *parent = focused;
805 if (content == JSON_CONTENT_WORKSPACE) {
806 parent = output_get_content(con_get_output(parent));
808 /* We need to append the layout to a split container, since a leaf
809 * container must not have any children (by definition).
810 * Note that we explicitly check for workspaces, since they are okay for
811 * this purpose, but con_accepts_window() returns false for workspaces. */
812 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
813 parent = parent->parent;
815 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
816 char *errormsg = NULL;
817 tree_append_json(parent, buf, len, &errormsg);
818 if (errormsg != NULL) {
821 /* Note that we continue executing since tree_append_json() has
822 * side-effects — user-provided layouts can be partly valid, partly
823 * invalid, leading to half of the placeholder containers being
829 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
830 // false); should be enough, but when sending 'workspace 4; append_layout
831 // /tmp/foo.json', the needs_tree_render == true of the workspace command
832 // is not executed yet and will be batched with append_layout’s
833 // needs_tree_render after the parser finished. We should check if that is
837 restore_open_placeholder_windows(parent);
839 if (content == JSON_CONTENT_WORKSPACE)
840 ipc_send_workspace_event("restored", parent, NULL);
842 cmd_output->needs_tree_render = true;
849 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
852 void cmd_workspace(I3_CMD, const char *which) {
855 DLOG("which=%s\n", which);
857 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
858 yerror("Cannot switch workspace while in global fullscreen");
862 if (strcmp(which, "next") == 0)
863 ws = workspace_next();
864 else if (strcmp(which, "prev") == 0)
865 ws = workspace_prev();
866 else if (strcmp(which, "next_on_output") == 0)
867 ws = workspace_next_on_output();
868 else if (strcmp(which, "prev_on_output") == 0)
869 ws = workspace_prev_on_output();
871 yerror("BUG: called with which=%s", which);
877 cmd_output->needs_tree_render = true;
878 // XXX: default reply for now, make this a better reply
883 * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
886 void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
887 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
889 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
890 yerror("Cannot switch workspace while in global fullscreen");
894 long parsed_num = ws_name_to_number(which);
895 if (parsed_num == -1) {
896 yerror("Could not parse initial part of \"%s\" as a number.", which);
900 Con *workspace = get_existing_workspace_by_num(parsed_num);
902 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
904 workspace_show_by_name(which);
905 cmd_output->needs_tree_render = true;
908 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name)) {
912 workspace_show(workspace);
914 cmd_output->needs_tree_render = true;
915 // XXX: default reply for now, make this a better reply
920 * Implementation of 'workspace back_and_forth'.
923 void cmd_workspace_back_and_forth(I3_CMD) {
924 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
925 yerror("Cannot switch workspace while in global fullscreen");
929 workspace_back_and_forth();
931 cmd_output->needs_tree_render = true;
932 // XXX: default reply for now, make this a better reply
937 * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
940 void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
941 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
943 if (strncasecmp(name, "__", strlen("__")) == 0) {
944 yerror("You cannot switch to the i3-internal workspaces (\"%s\").", name);
948 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
949 yerror("Cannot switch workspace while in global fullscreen");
953 DLOG("should switch to workspace %s\n", name);
954 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name)) {
958 workspace_show_by_name(name);
960 cmd_output->needs_tree_render = true;
961 // XXX: default reply for now, make this a better reply
966 * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
969 void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
972 owindow *current = TAILQ_FIRST(&owindows);
973 if (current == NULL) {
974 yerror("Given criteria don't match a window");
978 /* Marks must be unique, i.e., no two windows must have the same mark. */
979 if (current != TAILQ_LAST(&owindows, owindows_head)) {
980 yerror("A mark must not be put onto more than one window");
984 DLOG("matching: %p / %s\n", current->con, current->con->name);
986 mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
987 if (toggle != NULL) {
988 con_mark_toggle(current->con, mark, mark_mode);
990 con_mark(current->con, mark, mark_mode);
993 cmd_output->needs_tree_render = true;
994 // XXX: default reply for now, make this a better reply
999 * Implementation of 'unmark [mark]'
1002 void cmd_unmark(I3_CMD, const char *mark) {
1003 if (match_is_empty(current_match)) {
1004 con_unmark(NULL, mark);
1007 TAILQ_FOREACH(current, &owindows, owindows) {
1008 con_unmark(current->con, mark);
1012 cmd_output->needs_tree_render = true;
1013 // XXX: default reply for now, make this a better reply
1018 * Implementation of 'mode <string>'.
1021 void cmd_mode(I3_CMD, const char *mode) {
1022 DLOG("mode=%s\n", mode);
1025 // XXX: default reply for now, make this a better reply
1030 * Implementation of 'move [window|container] [to] output <str>'.
1033 void cmd_move_con_to_output(I3_CMD, const char *name) {
1034 DLOG("Should move window to output \"%s\".\n", name);
1038 bool had_error = false;
1039 TAILQ_FOREACH(current, &owindows, owindows) {
1040 DLOG("matching: %p / %s\n", current->con, current->con->name);
1042 had_error |= !con_move_to_output_name(current->con, name, true);
1045 cmd_output->needs_tree_render = true;
1046 ysuccess(!had_error);
1050 * Implementation of 'move [container|window] [to] mark <str>'.
1053 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1054 DLOG("moving window to mark \"%s\"\n", mark);
1060 TAILQ_FOREACH(current, &owindows, owindows) {
1061 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1062 result &= con_move_to_mark(current->con, mark);
1065 cmd_output->needs_tree_render = true;
1070 * Implementation of 'floating enable|disable|toggle'
1073 void cmd_floating(I3_CMD, const char *floating_mode) {
1076 DLOG("floating_mode=%s\n", floating_mode);
1080 TAILQ_FOREACH(current, &owindows, owindows) {
1081 DLOG("matching: %p / %s\n", current->con, current->con->name);
1082 if (strcmp(floating_mode, "toggle") == 0) {
1083 DLOG("should toggle mode\n");
1084 toggle_floating_mode(current->con, false);
1086 DLOG("should switch mode to %s\n", floating_mode);
1087 if (strcmp(floating_mode, "enable") == 0) {
1088 floating_enable(current->con, false);
1090 floating_disable(current->con, false);
1095 cmd_output->needs_tree_render = true;
1096 // XXX: default reply for now, make this a better reply
1101 * Implementation of 'move workspace to [output] <str>'.
1104 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1105 DLOG("should move workspace to output %s\n", name);
1110 TAILQ_FOREACH(current, &owindows, owindows) {
1111 Con *ws = con_get_workspace(current->con);
1112 if (con_is_internal(ws)) {
1116 Output *current_output = get_output_for_con(ws);
1117 Output *target_output = get_output_from_string(current_output, name);
1118 if (!target_output) {
1119 yerror("Could not get output from string \"%s\"", name);
1123 workspace_move_to_output(ws, target_output);
1126 cmd_output->needs_tree_render = true;
1131 * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1134 void cmd_split(I3_CMD, const char *direction) {
1138 LOG("splitting in direction %c\n", direction[0]);
1139 TAILQ_FOREACH(current, &owindows, owindows) {
1140 if (con_is_docked(current->con)) {
1141 ELOG("Cannot split a docked container, skipping.\n");
1145 DLOG("matching: %p / %s\n", current->con, current->con->name);
1146 if (direction[0] == 't') {
1147 layout_t current_layout;
1148 if (current->con->type == CT_WORKSPACE) {
1149 current_layout = current->con->layout;
1151 current_layout = current->con->parent->layout;
1153 /* toggling split orientation */
1154 if (current_layout == L_SPLITH) {
1155 tree_split(current->con, VERT);
1157 tree_split(current->con, HORIZ);
1160 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1164 cmd_output->needs_tree_render = true;
1165 // XXX: default reply for now, make this a better reply
1170 * Implementation of 'kill [window|client]'.
1173 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1174 if (kill_mode_str == NULL)
1175 kill_mode_str = "window";
1177 DLOG("kill_mode=%s\n", kill_mode_str);
1180 if (strcmp(kill_mode_str, "window") == 0)
1181 kill_mode = KILL_WINDOW;
1182 else if (strcmp(kill_mode_str, "client") == 0)
1183 kill_mode = KILL_CLIENT;
1185 yerror("BUG: called with kill_mode=%s", kill_mode_str);
1192 TAILQ_FOREACH(current, &owindows, owindows) {
1193 con_close(current->con, kill_mode);
1196 cmd_output->needs_tree_render = true;
1197 // XXX: default reply for now, make this a better reply
1202 * Implementation of 'exec [--no-startup-id] <command>'.
1205 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1206 bool no_startup_id = (nosn != NULL);
1208 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1209 start_application(command, no_startup_id);
1215 * Implementation of 'focus left|right|up|down'.
1218 void cmd_focus_direction(I3_CMD, const char *direction) {
1219 switch (parse_direction(direction)) {
1221 tree_next('p', HORIZ);
1224 tree_next('n', HORIZ);
1227 tree_next('p', VERT);
1230 tree_next('n', VERT);
1234 cmd_output->needs_tree_render = true;
1235 // XXX: default reply for now, make this a better reply
1240 * Focus a container and disable any other fullscreen container not permitting the focus.
1243 static void cmd_focus_force_focus(Con *con) {
1244 /* Disable fullscreen container in workspace with container to be focused. */
1245 Con *ws = con_get_workspace(con);
1246 Con *fullscreen_on_ws = con_get_fullscreen_covering_ws(ws);
1247 if (fullscreen_on_ws && fullscreen_on_ws != con && !con_has_parent(con, fullscreen_on_ws)) {
1248 con_disable_fullscreen(fullscreen_on_ws);
1254 * Implementation of 'focus tiling|floating|mode_toggle'.
1257 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1258 DLOG("window_mode = %s\n", window_mode);
1260 bool to_floating = false;
1261 if (strcmp(window_mode, "mode_toggle") == 0) {
1262 to_floating = !con_inside_floating(focused);
1263 } else if (strcmp(window_mode, "floating") == 0) {
1265 } else if (strcmp(window_mode, "tiling") == 0) {
1266 to_floating = false;
1269 Con *ws = con_get_workspace(focused);
1271 bool success = false;
1272 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1273 if ((to_floating && current->type != CT_FLOATING_CON) ||
1274 (!to_floating && current->type == CT_FLOATING_CON))
1277 cmd_focus_force_focus(con_descend_focused(current));
1283 cmd_output->needs_tree_render = true;
1286 yerror("Failed to find a %s container in workspace.", to_floating ? "floating" : "tiling");
1291 * Implementation of 'focus parent|child'.
1294 void cmd_focus_level(I3_CMD, const char *level) {
1295 DLOG("level = %s\n", level);
1296 bool success = false;
1298 /* Focusing the parent can only be allowed if the newly
1299 * focused container won't escape the fullscreen container. */
1300 if (strcmp(level, "parent") == 0) {
1301 if (focused && focused->parent) {
1302 if (con_fullscreen_permits_focusing(focused->parent))
1303 success = level_up();
1305 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1309 /* Focusing a child should always be allowed. */
1311 success = level_down();
1313 cmd_output->needs_tree_render = success;
1314 // XXX: default reply for now, make this a better reply
1319 * Implementation of 'focus'.
1322 void cmd_focus(I3_CMD) {
1323 DLOG("current_match = %p\n", current_match);
1325 if (match_is_empty(current_match)) {
1326 ELOG("You have to specify which window/container should be focused.\n");
1327 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1329 yerror("You have to specify which window/container should be focused");
1334 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1337 TAILQ_FOREACH(current, &owindows, owindows) {
1338 Con *ws = con_get_workspace(current->con);
1339 /* If no workspace could be found, this was a dock window.
1340 * Just skip it, you cannot focus dock windows. */
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 cmd_focus_force_focus(current->con);
1368 con_activate(currently_focused);
1370 /* Now switch to the workspace, then focus */
1372 LOG("focusing %p / %s\n", current->con, current->con->name);
1373 con_activate(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_str, long move_px) {
1460 Con *initially_focused = focused;
1461 direction_t direction = parse_direction(direction_str);
1463 TAILQ_FOREACH(current, &owindows, owindows) {
1464 DLOG("moving in direction %s, px %ld\n", direction_str, move_px);
1465 if (con_is_floating(current->con)) {
1466 DLOG("floating move with %ld pixels\n", move_px);
1467 Rect newrect = current->con->parent->rect;
1469 switch (direction) {
1471 newrect.x -= move_px;
1474 newrect.x += move_px;
1477 newrect.y -= move_px;
1480 newrect.y += move_px;
1484 floating_reposition(current->con->parent, newrect);
1486 tree_move(current->con, direction);
1487 cmd_output->needs_tree_render = true;
1491 /* the move command should not disturb focus */
1492 if (focused != initially_focused)
1493 con_activate(initially_focused);
1495 // XXX: default reply for now, make this a better reply
1500 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1503 void cmd_layout(I3_CMD, const char *layout_str) {
1507 if (!layout_from_name(layout_str, &layout)) {
1508 yerror("Unknown layout \"%s\", this is a mismatch between code and parser spec.", layout_str);
1512 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1515 TAILQ_FOREACH(current, &owindows, owindows) {
1516 if (con_is_docked(current->con)) {
1517 ELOG("cannot change layout of a docked container, skipping it.\n");
1521 DLOG("matching: %p / %s\n", current->con, current->con->name);
1522 con_set_layout(current->con, layout);
1525 cmd_output->needs_tree_render = true;
1526 // XXX: default reply for now, make this a better reply
1531 * Implementation of 'layout toggle [all|split]'.
1534 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1537 if (toggle_mode == NULL)
1538 toggle_mode = "default";
1540 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1542 /* check if the match is empty, not if the result is empty */
1543 if (match_is_empty(current_match))
1544 con_toggle_layout(focused, toggle_mode);
1546 TAILQ_FOREACH(current, &owindows, owindows) {
1547 DLOG("matching: %p / %s\n", current->con, current->con->name);
1548 con_toggle_layout(current->con, toggle_mode);
1552 cmd_output->needs_tree_render = true;
1553 // XXX: default reply for now, make this a better reply
1558 * Implementation of 'exit'.
1561 void cmd_exit(I3_CMD) {
1562 LOG("Exiting due to user command.\n");
1569 * Implementation of 'reload'.
1572 void cmd_reload(I3_CMD) {
1574 kill_nagbar(&config_error_nagbar_pid, false);
1575 kill_nagbar(&command_error_nagbar_pid, false);
1576 load_configuration(NULL, C_RELOAD);
1578 /* Send an IPC event just in case the ws names have changed */
1579 ipc_send_workspace_event("reload", NULL, NULL);
1580 /* Send an update event for the barconfig just in case it has changed */
1583 // XXX: default reply for now, make this a better reply
1588 * Implementation of 'restart'.
1591 void cmd_restart(I3_CMD) {
1592 LOG("restarting i3\n");
1593 ipc_shutdown(SHUTDOWN_REASON_RESTART);
1594 unlink(config.ipc_socket_path);
1595 /* We need to call this manually since atexit handlers don’t get called
1597 purge_zerobyte_logfile();
1600 // XXX: default reply for now, make this a better reply
1605 * Implementation of 'open'.
1608 void cmd_open(I3_CMD) {
1609 LOG("opening new container\n");
1610 Con *con = tree_open_con(NULL, NULL);
1611 con->layout = L_SPLITH;
1618 y(integer, (uintptr_t)con);
1621 cmd_output->needs_tree_render = true;
1625 * Implementation of 'focus output <output>'.
1628 void cmd_focus_output(I3_CMD, const char *name) {
1631 if (TAILQ_EMPTY(&owindows)) {
1636 Output *current_output = get_output_for_con(TAILQ_FIRST(&owindows)->con);
1637 Output *output = get_output_from_string(current_output, name);
1640 yerror("Output %s not found.", name);
1644 /* get visible workspace on output */
1646 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1648 yerror("BUG: No workspace found on output.");
1654 cmd_output->needs_tree_render = true;
1659 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1662 void cmd_move_window_to_position(I3_CMD, long x, long y) {
1663 bool has_error = false;
1668 TAILQ_FOREACH(current, &owindows, owindows) {
1669 if (!con_is_floating(current->con)) {
1670 ELOG("Cannot change position. The window/container is not floating\n");
1673 yerror("Cannot change position of a window/container because it is not floating.");
1680 Rect newrect = current->con->parent->rect;
1682 DLOG("moving to position %ld %ld\n", x, y);
1686 if (!floating_reposition(current->con->parent, newrect)) {
1687 yerror("Cannot move window/container out of bounds.");
1697 * Implementation of 'move [window|container] [to] [absolute] position center
1700 void cmd_move_window_to_center(I3_CMD, const char *method) {
1701 bool has_error = false;
1705 TAILQ_FOREACH(current, &owindows, owindows) {
1706 Con *floating_con = con_inside_floating(current->con);
1707 if (floating_con == NULL) {
1708 ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1709 current->con, current->con->name);
1712 yerror("Cannot change position of a window/container because it is not floating.");
1719 if (strcmp(method, "absolute") == 0) {
1720 DLOG("moving to absolute center\n");
1721 floating_center(floating_con, croot->rect);
1723 floating_maybe_reassign_ws(floating_con);
1724 cmd_output->needs_tree_render = true;
1727 if (strcmp(method, "position") == 0) {
1728 DLOG("moving to center\n");
1729 floating_center(floating_con, con_get_workspace(floating_con)->rect);
1731 cmd_output->needs_tree_render = true;
1735 // XXX: default reply for now, make this a better reply
1741 * Implementation of 'move [window|container] [to] position mouse'
1744 void cmd_move_window_to_mouse(I3_CMD) {
1748 TAILQ_FOREACH(current, &owindows, owindows) {
1749 Con *floating_con = con_inside_floating(current->con);
1750 if (floating_con == NULL) {
1751 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1752 current->con, current->con->name);
1756 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1757 floating_move_to_pointer(floating_con);
1760 cmd_output->needs_tree_render = true;
1765 * Implementation of 'move scratchpad'.
1768 void cmd_move_scratchpad(I3_CMD) {
1769 DLOG("should move window to scratchpad\n");
1774 TAILQ_FOREACH(current, &owindows, owindows) {
1775 DLOG("matching: %p / %s\n", current->con, current->con->name);
1776 scratchpad_move(current->con);
1779 cmd_output->needs_tree_render = true;
1780 // XXX: default reply for now, make this a better reply
1785 * Implementation of 'scratchpad show'.
1788 void cmd_scratchpad_show(I3_CMD) {
1789 DLOG("should show scratchpad window\n");
1791 bool result = false;
1793 if (match_is_empty(current_match)) {
1794 result = scratchpad_show(NULL);
1796 TAILQ_FOREACH(current, &owindows, owindows) {
1797 DLOG("matching: %p / %s\n", current->con, current->con->name);
1798 result |= scratchpad_show(current->con);
1802 cmd_output->needs_tree_render = true;
1808 * Implementation of 'swap [container] [with] id|con_id|mark <arg>'.
1811 void cmd_swap(I3_CMD, const char *mode, const char *arg) {
1814 owindow *match = TAILQ_FIRST(&owindows);
1815 if (match == NULL) {
1816 yerror("No match found for swapping.");
1819 if (match->con == NULL) {
1820 yerror("Match %p has no container.", match);
1825 if (strcmp(mode, "id") == 0) {
1827 if (!parse_long(arg, &target, 0)) {
1828 yerror("Failed to parse %s into a window id.", arg);
1832 con = con_by_window_id(target);
1833 } else if (strcmp(mode, "con_id") == 0) {
1835 if (!parse_long(arg, &target, 0)) {
1836 yerror("Failed to parse %s into a container id.", arg);
1840 con = con_by_con_id(target);
1841 } else if (strcmp(mode, "mark") == 0) {
1842 con = con_by_mark(arg);
1844 yerror("Unhandled swap mode \"%s\". This is a bug.", mode);
1849 yerror("Could not find container for %s = %s", mode, arg);
1853 if (match != TAILQ_LAST(&owindows, owindows_head)) {
1854 LOG("More than one container matched the swap command, only using the first one.");
1857 DLOG("Swapping %p with %p.\n", match->con, con);
1858 bool result = con_swap(match->con, con);
1860 cmd_output->needs_tree_render = true;
1861 // XXX: default reply for now, make this a better reply
1866 * Implementation of 'title_format <format>'
1869 void cmd_title_format(I3_CMD, const char *format) {
1870 DLOG("setting title_format to \"%s\"\n", format);
1874 TAILQ_FOREACH(current, &owindows, owindows) {
1875 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1876 FREE(current->con->title_format);
1878 /* If we only display the title without anything else, we can skip the parsing step,
1879 * so we remove the title format altogether. */
1880 if (strcasecmp(format, "%title") != 0) {
1881 current->con->title_format = sstrdup(format);
1883 if (current->con->window != NULL) {
1884 i3String *formatted_title = con_parse_title_format(current->con);
1885 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1886 I3STRING_FREE(formatted_title);
1889 if (current->con->window != NULL) {
1890 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1891 ewmh_update_visible_name(current->con->window->id, NULL);
1895 if (current->con->window != NULL) {
1896 /* Make sure the window title is redrawn immediately. */
1897 current->con->window->name_x_changed = true;
1899 /* For windowless containers we also need to force the redrawing. */
1900 FREE(current->con->deco_render_params);
1904 cmd_output->needs_tree_render = true;
1909 * Implementation of 'rename workspace [<name>] to <name>'
1912 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1913 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1914 yerror("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1918 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1920 LOG("Renaming current workspace to \"%s\"\n", new_name);
1925 workspace = get_existing_workspace_by_name(old_name);
1927 workspace = con_get_workspace(focused);
1928 old_name = workspace->name;
1932 yerror("Old workspace \"%s\" not found", old_name);
1936 Con *check_dest = get_existing_workspace_by_name(new_name);
1938 /* If check_dest == workspace, the user might be changing the case of the
1939 * workspace, or it might just be a no-op. */
1940 if (check_dest != NULL && check_dest != workspace) {
1941 yerror("New workspace \"%s\" already exists", new_name);
1945 /* Change the name and try to parse it as a number. */
1946 /* old_name might refer to workspace->name, so copy it before free()ing */
1947 char *old_name_copy = sstrdup(old_name);
1948 FREE(workspace->name);
1949 workspace->name = sstrdup(new_name);
1951 workspace->num = ws_name_to_number(new_name);
1952 LOG("num = %d\n", workspace->num);
1954 /* By re-attaching, the sort order will be correct afterwards. */
1955 Con *previously_focused = focused;
1956 Con *previously_focused_content = focused->type == CT_WORKSPACE ? focused->parent : NULL;
1957 Con *parent = workspace->parent;
1958 con_detach(workspace);
1959 con_attach(workspace, parent, false);
1960 ipc_send_workspace_event("rename", workspace, NULL);
1962 /* Move the workspace to the correct output if it has an assignment */
1963 struct Workspace_Assignment *assignment = NULL;
1964 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1965 if (assignment->output == NULL)
1967 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1971 Output *target_output = get_output_by_name(assignment->output, true);
1972 if (!target_output) {
1973 LOG("Could not get output named \"%s\"\n", assignment->output);
1976 if (!output_triggers_assignment(target_output, assignment)) {
1979 workspace_move_to_output(workspace, target_output);
1984 bool can_restore_focus = previously_focused != NULL;
1985 /* NB: If previously_focused is a workspace we can't work directly with it
1986 * since it might have been cleaned up by workspace_show() already,
1987 * depending on the focus order/number of other workspaces on the output.
1988 * Instead, we loop through the available workspaces and only focus
1989 * previously_focused if we still find it. */
1990 if (previously_focused_content) {
1991 Con *workspace = NULL;
1992 GREP_FIRST(workspace, previously_focused_content, child == previously_focused);
1993 can_restore_focus &= (workspace != NULL);
1996 if (can_restore_focus) {
1997 /* Restore the previous focus since con_attach messes with the focus. */
1998 workspace_show(con_get_workspace(previously_focused));
1999 con_focus(previously_focused);
2002 cmd_output->needs_tree_render = true;
2005 ewmh_update_desktop_names();
2006 ewmh_update_desktop_viewport();
2007 ewmh_update_current_desktop();
2009 startup_sequence_rename_workspace(old_name_copy, new_name);
2010 free(old_name_copy);
2014 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2017 static bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2019 bool toggle = false;
2020 if (strcmp(bar_mode, "dock") == 0)
2022 else if (strcmp(bar_mode, "hide") == 0)
2024 else if (strcmp(bar_mode, "invisible") == 0)
2026 else if (strcmp(bar_mode, "toggle") == 0)
2029 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2033 bool changed_sth = false;
2034 Barconfig *current = NULL;
2035 TAILQ_FOREACH(current, &barconfigs, configs) {
2036 if (bar_id && strcmp(current->id, bar_id) != 0)
2040 mode = (current->mode + 1) % 2;
2042 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2043 current->mode = mode;
2050 if (bar_id && !changed_sth) {
2051 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2059 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2062 static bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2063 int hidden_state = S_SHOW;
2064 bool toggle = false;
2065 if (strcmp(bar_hidden_state, "hide") == 0)
2066 hidden_state = S_HIDE;
2067 else if (strcmp(bar_hidden_state, "show") == 0)
2068 hidden_state = S_SHOW;
2069 else if (strcmp(bar_hidden_state, "toggle") == 0)
2072 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2076 bool changed_sth = false;
2077 Barconfig *current = NULL;
2078 TAILQ_FOREACH(current, &barconfigs, configs) {
2079 if (bar_id && strcmp(current->id, bar_id) != 0)
2083 hidden_state = (current->hidden_state + 1) % 2;
2085 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2086 current->hidden_state = hidden_state;
2093 if (bar_id && !changed_sth) {
2094 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2102 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2105 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2107 if (strcmp(bar_type, "mode") == 0)
2108 ret = cmd_bar_mode(bar_value, bar_id);
2109 else if (strcmp(bar_type, "hidden_state") == 0)
2110 ret = cmd_bar_hidden_state(bar_value, bar_id);
2112 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2124 * Implementation of 'shmlog <size>|toggle|on|off'
2127 void cmd_shmlog(I3_CMD, const char *argument) {
2128 if (!strcmp(argument, "toggle"))
2129 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2130 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2131 else if (!strcmp(argument, "on"))
2132 shmlog_size = default_shmlog_size;
2133 else if (!strcmp(argument, "off"))
2137 if (!parse_long(argument, &new_size, 0)) {
2138 yerror("Failed to parse %s into a shmlog size.", argument);
2141 /* If shm logging now, restart logging with the new size. */
2142 if (shmlog_size > 0) {
2144 LOG("Restarting shm logging...\n");
2147 shmlog_size = (int)new_size;
2149 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2151 update_shmlog_atom();
2156 * Implementation of 'debuglog toggle|on|off'
2159 void cmd_debuglog(I3_CMD, const char *argument) {
2160 bool logging = get_debug_logging();
2161 if (!strcmp(argument, "toggle")) {
2162 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2163 set_debug_logging(!logging);
2164 } else if (!strcmp(argument, "on") && !logging) {
2165 LOG("Enabling debug logging\n");
2166 set_debug_logging(true);
2167 } else if (!strcmp(argument, "off") && logging) {
2168 LOG("Disabling debug logging\n");
2169 set_debug_logging(false);
2171 // XXX: default reply for now, make this a better reply