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)
15 #ifdef I3_ASAN_ENABLED
16 #include <sanitizer/lsan_interface.h>
22 // Macros to make the YAJL API a bit easier to use.
23 #define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
24 #define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
25 #define ysuccess(success) \
27 if (cmd_output->json_gen != NULL) { \
34 #define yerror(format, ...) \
36 if (cmd_output->json_gen != NULL) { \
38 sasprintf(&message, format, ##__VA_ARGS__); \
49 /** If an error occured during parsing of the criteria, we want to exit instead
50 * of relying on fallback behavior. See #2091. */
51 #define HANDLE_INVALID_MATCH \
53 if (current_match->error != NULL) { \
54 yerror("Invalid match: %s", current_match->error); \
59 /** When the command did not include match criteria (!), we use the currently
60 * focused container. Do not confuse this case with a command which included
61 * criteria but which did not match any windows. This macro has to be called in
64 #define HANDLE_EMPTY_MATCH \
66 HANDLE_INVALID_MATCH; \
68 if (match_is_empty(current_match)) { \
69 while (!TAILQ_EMPTY(&owindows)) { \
70 owindow *ow = TAILQ_FIRST(&owindows); \
71 TAILQ_REMOVE(&owindows, ow, owindows); \
74 owindow *ow = smalloc(sizeof(owindow)); \
76 TAILQ_INIT(&owindows); \
77 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
82 * Returns true if a is definitely greater than b (using the given epsilon)
85 static bool definitelyGreaterThan(float a, float b, float epsilon) {
86 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
90 * Returns the output containing the given container.
92 static Output *get_output_of_con(Con *con) {
93 Con *output_con = con_get_output(con);
94 Output *output = get_output_by_name(output_con->name);
95 assert(output != NULL);
101 * Checks whether we switched to a new workspace and returns false in that case,
102 * signaling that further workspace switching should be done by the calling function
103 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
104 * and return true, signaling that no further workspace switching should occur in the calling function.
107 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
108 Con *ws = con_get_workspace(focused);
110 /* If we switched to a different workspace, do nothing */
111 if (strcmp(ws->name, name) != 0)
114 DLOG("This workspace is already focused.\n");
115 if (config.workspace_auto_back_and_forth) {
116 workspace_back_and_forth();
117 cmd_output->needs_tree_render = true;
123 * Return the passed workspace unless it is the current one and auto back and
124 * forth is enabled, in which case the back_and_forth workspace is returned.
126 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
129 if (!config.workspace_auto_back_and_forth)
132 current = con_get_workspace(focused);
134 if (current == workspace) {
135 baf = workspace_back_and_forth_get();
137 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
145 // This code is commented out because we might recycle it for popping up error
146 // messages on parser errors.
148 static pid_t migration_pid = -1;
151 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
152 * it exited (or could not be started, depending on the exit code).
155 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
156 ev_child_stop(EV_A_ watcher);
157 if (!WIFEXITED(watcher->rstatus)) {
158 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
162 int exitcode = WEXITSTATUS(watcher->rstatus);
163 printf("i3-nagbar process exited with status %d\n", exitcode);
165 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
171 /* We need ev >= 4 for the following code. Since it is not *that* important (it
172 * only makes sure that there are no i3-nagbar instances left behind) we still
173 * support old systems with libev 3. */
174 #if EV_VERSION_MAJOR >= 4
176 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
177 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
180 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
181 if (migration_pid != -1) {
182 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
183 kill(migration_pid, SIGKILL);
188 void cmd_MIGRATION_start_nagbar(void) {
189 if (migration_pid != -1) {
190 fprintf(stderr, "i3-nagbar already running.\n");
193 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
194 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
195 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
196 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
197 migration_pid = fork();
198 if (migration_pid == -1) {
199 warn("Could not fork()");
204 if (migration_pid == 0) {
206 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
208 NULL, /* will be replaced by the executable path */
212 "You found a parsing error. Please, please, please, report it!",
218 exec_i3_utility("i3-nagbar", argv);
222 /* install a child watcher */
223 ev_child *child = smalloc(sizeof(ev_child));
224 ev_child_init(child, &nagbar_exited, migration_pid, 0);
225 ev_child_start(main_loop, child);
227 /* We need ev >= 4 for the following code. Since it is not *that* important (it
228 * only makes sure that there are no i3-nagbar instances left behind) we still
229 * support old systems with libev 3. */
230 #if EV_VERSION_MAJOR >= 4
231 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
233 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
234 ev_cleanup_init(cleanup, nagbar_cleanup);
235 ev_cleanup_start(main_loop, cleanup);
241 /*******************************************************************************
242 * Criteria functions.
243 ******************************************************************************/
246 * Helper data structure for an operation window (window on which the operation
247 * will be performed). Used to build the TAILQ owindows.
250 typedef struct owindow {
252 TAILQ_ENTRY(owindow) owindows;
255 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
257 static owindows_head owindows;
260 * Initializes the specified 'Match' data structure and the initial state of
261 * commands.c for matching target windows of a command.
264 void cmd_criteria_init(I3_CMD) {
268 DLOG("Initializing criteria, current_match = %p\n", current_match);
269 match_free(current_match);
270 match_init(current_match);
271 while (!TAILQ_EMPTY(&owindows)) {
272 ow = TAILQ_FIRST(&owindows);
273 TAILQ_REMOVE(&owindows, ow, owindows);
276 TAILQ_INIT(&owindows);
278 TAILQ_FOREACH(con, &all_cons, all_cons) {
279 ow = smalloc(sizeof(owindow));
281 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
286 * A match specification just finished (the closing square bracket was found),
287 * so we filter the list of owindows.
290 void cmd_criteria_match_windows(I3_CMD) {
291 owindow *next, *current;
293 DLOG("match specification finished, matching...\n");
294 /* copy the old list head to iterate through it and start with a fresh
295 * list which will contain only matching windows */
296 struct owindows_head old = owindows;
297 TAILQ_INIT(&owindows);
298 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
299 /* make a copy of the next pointer and advance the pointer to the
300 * next element as we are going to invalidate the element’s
301 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
303 next = TAILQ_NEXT(next, owindows);
305 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
307 /* We use this flag to prevent matching on window-less containers if
308 * only window-specific criteria were specified. */
309 bool accept_match = false;
311 if (current_match->con_id != NULL) {
314 if (current_match->con_id == current->con) {
315 DLOG("con_id matched.\n");
317 DLOG("con_id does not match.\n");
323 if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
325 bool matched_by_mark = false;
328 TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
329 if (!regex_matches(current_match->mark, mark->name))
332 DLOG("match by mark\n");
333 matched_by_mark = true;
337 if (!matched_by_mark) {
338 DLOG("mark does not match.\n");
344 if (current->con->window != NULL) {
345 if (match_matches_window(current_match, current->con->window)) {
346 DLOG("matches window!\n");
349 DLOG("doesnt match\n");
356 TAILQ_INSERT_TAIL(&owindows, current, owindows);
363 TAILQ_FOREACH(current, &owindows, owindows) {
364 DLOG("matching: %p / %s\n", current->con, current->con->name);
369 * Interprets a ctype=cvalue pair and adds it to the current match
373 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
374 match_parse_property(current_match, ctype, cvalue);
378 * Implementation of 'move [window|container] [to] workspace
379 * next|prev|next_on_output|prev_on_output|current'.
382 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
385 DLOG("which=%s\n", which);
387 /* We have nothing to move:
388 * when criteria was specified but didn't match any window or
389 * when criteria wasn't specified and we don't have any window focused. */
390 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
391 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
392 !con_has_children(focused))) {
399 /* get the workspace */
401 if (strcmp(which, "next") == 0)
402 ws = workspace_next();
403 else if (strcmp(which, "prev") == 0)
404 ws = workspace_prev();
405 else if (strcmp(which, "next_on_output") == 0)
406 ws = workspace_next_on_output();
407 else if (strcmp(which, "prev_on_output") == 0)
408 ws = workspace_prev_on_output();
409 else if (strcmp(which, "current") == 0)
410 ws = con_get_workspace(focused);
412 ELOG("BUG: called with which=%s\n", which);
417 TAILQ_FOREACH(current, &owindows, owindows) {
418 DLOG("matching: %p / %s\n", current->con, current->con->name);
419 con_move_to_workspace(current->con, ws, true, false, false);
422 cmd_output->needs_tree_render = true;
423 // XXX: default reply for now, make this a better reply
428 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
431 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
435 ws = workspace_back_and_forth_get();
438 yerror("No workspace was previously active.");
444 TAILQ_FOREACH(current, &owindows, owindows) {
445 DLOG("matching: %p / %s\n", current->con, current->con->name);
446 con_move_to_workspace(current->con, ws, true, false, false);
449 cmd_output->needs_tree_render = true;
450 // XXX: default reply for now, make this a better reply
455 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
458 void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
459 if (strncasecmp(name, "__", strlen("__")) == 0) {
460 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
465 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
468 /* We have nothing to move:
469 * when criteria was specified but didn't match any window or
470 * when criteria wasn't specified and we don't have any window focused. */
471 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
472 ELOG("No windows match your criteria, cannot move.\n");
475 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
476 !con_has_children(focused)) {
481 LOG("should move window to workspace %s\n", name);
482 /* get the workspace */
483 Con *ws = workspace_get(name, NULL);
485 if (!no_auto_back_and_forth)
486 ws = maybe_auto_back_and_forth_workspace(ws);
490 TAILQ_FOREACH(current, &owindows, owindows) {
491 DLOG("matching: %p / %s\n", current->con, current->con->name);
492 con_move_to_workspace(current->con, ws, true, false, false);
495 cmd_output->needs_tree_render = true;
496 // XXX: default reply for now, make this a better reply
501 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
504 void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
505 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
508 /* We have nothing to move:
509 * when criteria was specified but didn't match any window or
510 * when criteria wasn't specified and we don't have any window focused. */
511 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
512 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
513 !con_has_children(focused))) {
518 LOG("should move window to workspace %s\n", which);
519 /* get the workspace */
520 Con *output, *workspace = NULL;
522 long parsed_num = ws_name_to_number(which);
524 if (parsed_num == -1) {
525 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
526 yerror("Could not parse number \"%s\"", which);
530 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
531 GREP_FIRST(workspace, output_get_content(output),
532 child->num == parsed_num);
535 workspace = workspace_get(which, NULL);
538 if (!no_auto_back_and_forth)
539 workspace = maybe_auto_back_and_forth_workspace(workspace);
543 TAILQ_FOREACH(current, &owindows, owindows) {
544 DLOG("matching: %p / %s\n", current->con, current->con->name);
545 con_move_to_workspace(current->con, workspace, true, false, false);
548 cmd_output->needs_tree_render = true;
549 // XXX: default reply for now, make this a better reply
553 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) {
554 LOG("floating resize\n");
555 Rect old_rect = floating_con->rect;
556 Con *focused_con = con_descend_focused(floating_con);
558 /* ensure that resize will take place even if pixel increment is smaller than
559 * height increment or width increment.
561 const i3Window *window = focused_con->window;
562 if (window != NULL) {
563 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
564 strcmp(direction, "height") == 0) {
566 px = (-px < window->height_increment) ? -window->height_increment : px;
568 px = (px < window->height_increment) ? window->height_increment : px;
569 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
571 px = (-px < window->width_increment) ? -window->width_increment : px;
573 px = (px < window->width_increment) ? window->width_increment : px;
577 if (strcmp(direction, "up") == 0) {
578 floating_con->rect.height += px;
579 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
580 floating_con->rect.height += px;
581 } else if (strcmp(direction, "left") == 0) {
582 floating_con->rect.width += px;
584 floating_con->rect.width += px;
587 floating_check_size(floating_con);
589 /* Did we actually resize anything or did the size constraints prevent us?
590 * If we could not resize, exit now to not move the window. */
591 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
594 if (strcmp(direction, "up") == 0) {
595 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
596 } else if (strcmp(direction, "left") == 0) {
597 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
600 /* If this is a scratchpad window, don't auto center it from now on. */
601 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
602 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
605 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
606 LOG("tiling resize\n");
608 Con *first = current;
609 direction_t search_direction;
610 if (!strcmp(direction, "left"))
611 search_direction = D_LEFT;
612 else if (!strcmp(direction, "right"))
613 search_direction = D_RIGHT;
614 else if (!strcmp(direction, "up"))
615 search_direction = D_UP;
617 search_direction = D_DOWN;
619 bool res = resize_find_tiling_participants(&first, &second, search_direction);
621 LOG("No second container in this direction found.\n");
626 /* get the default percentage */
627 int children = con_num_children(first->parent);
628 LOG("ins. %d children\n", children);
629 double percentage = 1.0 / children;
630 LOG("default percentage = %f\n", percentage);
633 LOG("second->percent = %f\n", second->percent);
634 LOG("first->percent before = %f\n", first->percent);
635 if (first->percent == 0.0)
636 first->percent = percentage;
637 if (second->percent == 0.0)
638 second->percent = percentage;
639 double new_first_percent = first->percent + ((double)ppt / 100.0);
640 double new_second_percent = second->percent - ((double)ppt / 100.0);
641 LOG("new_first_percent = %f\n", new_first_percent);
642 LOG("new_second_percent = %f\n", new_second_percent);
643 /* Ensure that the new percentages are positive and greater than
644 * 0.05 to have a reasonable minimum size. */
645 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
646 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
647 first->percent += ((double)ppt / 100.0);
648 second->percent -= ((double)ppt / 100.0);
649 LOG("first->percent after = %f\n", first->percent);
650 LOG("second->percent after = %f\n", second->percent);
652 LOG("Not resizing, already at minimum size\n");
658 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
659 LOG("width/height resize\n");
660 /* get the appropriate current container (skip stacked/tabbed cons) */
661 while (current->parent->layout == L_STACKED ||
662 current->parent->layout == L_TABBED)
663 current = current->parent;
665 /* Then further go up until we find one with the matching orientation. */
666 orientation_t search_orientation =
667 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
669 while (current->type != CT_WORKSPACE &&
670 current->type != CT_FLOATING_CON &&
671 (con_orientation(current->parent) != search_orientation || con_num_children(current->parent) == 1))
672 current = current->parent;
674 /* get the default percentage */
675 int children = con_num_children(current->parent);
676 LOG("ins. %d children\n", children);
677 double percentage = 1.0 / children;
678 LOG("default percentage = %f\n", percentage);
680 orientation_t orientation = con_orientation(current->parent);
682 if ((orientation == HORIZ &&
683 strcmp(direction, "height") == 0) ||
684 (orientation == VERT &&
685 strcmp(direction, "width") == 0)) {
686 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
687 (orientation == HORIZ ? "horizontal" : "vertical"));
693 LOG("This is the only container, cannot resize.\n");
698 /* Ensure all the other children have a percentage set. */
700 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
701 LOG("child->percent = %f (child %p)\n", child->percent, child);
702 if (child->percent == 0.0)
703 child->percent = percentage;
706 double new_current_percent = current->percent + ((double)ppt / 100.0);
707 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
708 LOG("new_current_percent = %f\n", new_current_percent);
709 LOG("subtract_percent = %f\n", subtract_percent);
710 /* Ensure that the new percentages are positive and greater than
711 * 0.05 to have a reasonable minimum size. */
712 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
713 if (child == current)
715 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
716 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
721 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
722 LOG("Not resizing, already at minimum size\n");
727 current->percent += ((double)ppt / 100.0);
728 LOG("current->percent after = %f\n", current->percent);
730 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
731 if (child == current)
733 child->percent -= subtract_percent;
734 LOG("child->percent after (%p) = %f\n", child, child->percent);
741 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
744 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
745 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
746 if (strcmp(way, "shrink") == 0) {
754 TAILQ_FOREACH(current, &owindows, owindows) {
755 /* Don't handle dock windows (issue #1201) */
756 if (current->con->window && current->con->window->dock) {
757 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
762 if ((floating_con = con_inside_floating(current->con))) {
763 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
765 if (strcmp(direction, "width") == 0 ||
766 strcmp(direction, "height") == 0) {
767 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
768 current->con, way, direction, resize_ppt))
771 if (!cmd_resize_tiling_direction(current_match, cmd_output,
772 current->con, way, direction, resize_ppt))
778 cmd_output->needs_tree_render = true;
779 // XXX: default reply for now, make this a better reply
784 * Implementation of 'resize set <px> [px] <px> [px]'.
787 void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
788 DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
789 if (cwidth <= 0 || cheight <= 0) {
790 ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
797 TAILQ_FOREACH(current, &owindows, owindows) {
799 if ((floating_con = con_inside_floating(current->con))) {
800 floating_resize(floating_con, cwidth, cheight);
802 ELOG("Resize failed: %p not a floating container\n", current->con);
806 cmd_output->needs_tree_render = true;
807 // XXX: default reply for now, make this a better reply
812 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
815 void cmd_border(I3_CMD, const char *border_style_str, const char *border_width) {
816 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
821 TAILQ_FOREACH(current, &owindows, owindows) {
822 DLOG("matching: %p / %s\n", current->con, current->con->name);
823 int border_style = current->con->border_style;
825 int tmp_border_width = -1;
826 tmp_border_width = strtol(border_width, &end, 10);
827 if (end == border_width) {
828 /* no valid digits found */
829 tmp_border_width = -1;
831 if (strcmp(border_style_str, "toggle") == 0) {
834 if (border_style == BS_NORMAL)
835 tmp_border_width = 2;
836 else if (border_style == BS_NONE)
837 tmp_border_width = 0;
838 else if (border_style == BS_PIXEL)
839 tmp_border_width = 1;
841 if (strcmp(border_style_str, "normal") == 0)
842 border_style = BS_NORMAL;
843 else if (strcmp(border_style_str, "pixel") == 0)
844 border_style = BS_PIXEL;
845 else if (strcmp(border_style_str, "1pixel") == 0) {
846 border_style = BS_PIXEL;
847 tmp_border_width = 1;
848 } else if (strcmp(border_style_str, "none") == 0)
849 border_style = BS_NONE;
851 ELOG("BUG: called with border_style=%s\n", border_style_str);
856 con_set_border_style(current->con, border_style, tmp_border_width);
859 cmd_output->needs_tree_render = true;
860 // XXX: default reply for now, make this a better reply
865 * Implementation of 'nop <comment>'.
868 void cmd_nop(I3_CMD, const char *comment) {
869 LOG("-------------------------------------------------\n");
870 LOG(" NOP: %s\n", comment);
871 LOG("-------------------------------------------------\n");
875 * Implementation of 'append_layout <path>'.
878 void cmd_append_layout(I3_CMD, const char *cpath) {
879 char *path = sstrdup(cpath);
880 LOG("Appending layout \"%s\"\n", path);
882 /* Make sure we allow paths like '~/.i3/layout.json' */
883 path = resolve_tilde(path);
885 json_content_t content = json_determine_content(path);
886 LOG("JSON content = %d\n", content);
887 if (content == JSON_CONTENT_UNKNOWN) {
888 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
889 yerror("Could not determine the contents of \"%s\".", path);
894 Con *parent = focused;
895 if (content == JSON_CONTENT_WORKSPACE) {
896 parent = output_get_content(con_get_output(parent));
898 /* We need to append the layout to a split container, since a leaf
899 * container must not have any children (by definition).
900 * Note that we explicitly check for workspaces, since they are okay for
901 * this purpose, but con_accepts_window() returns false for workspaces. */
902 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
903 parent = parent->parent;
905 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
906 char *errormsg = NULL;
907 tree_append_json(parent, path, &errormsg);
908 if (errormsg != NULL) {
911 /* Note that we continue executing since tree_append_json() has
912 * side-effects — user-provided layouts can be partly valid, partly
913 * invalid, leading to half of the placeholder containers being
919 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
920 // false); should be enough, but when sending 'workspace 4; append_layout
921 // /tmp/foo.json', the needs_tree_render == true of the workspace command
922 // is not executed yet and will be batched with append_layout’s
923 // needs_tree_render after the parser finished. We should check if that is
925 render_con(croot, false);
927 restore_open_placeholder_windows(parent);
929 if (content == JSON_CONTENT_WORKSPACE)
930 ipc_send_workspace_event("restored", parent, NULL);
933 cmd_output->needs_tree_render = true;
937 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
940 void cmd_workspace(I3_CMD, const char *which) {
943 DLOG("which=%s\n", which);
945 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
946 LOG("Cannot switch workspace while in global fullscreen\n");
951 if (strcmp(which, "next") == 0)
952 ws = workspace_next();
953 else if (strcmp(which, "prev") == 0)
954 ws = workspace_prev();
955 else if (strcmp(which, "next_on_output") == 0)
956 ws = workspace_next_on_output();
957 else if (strcmp(which, "prev_on_output") == 0)
958 ws = workspace_prev_on_output();
960 ELOG("BUG: called with which=%s\n", which);
967 cmd_output->needs_tree_render = true;
968 // XXX: default reply for now, make this a better reply
973 * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
976 void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
977 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
978 Con *output, *workspace = NULL;
980 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
981 LOG("Cannot switch workspace while in global fullscreen\n");
986 long parsed_num = ws_name_to_number(which);
988 if (parsed_num == -1) {
989 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
990 yerror("Could not parse number \"%s\"", which);
994 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
995 GREP_FIRST(workspace, output_get_content(output),
996 child->num == parsed_num);
999 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1001 workspace_show_by_name(which);
1002 cmd_output->needs_tree_render = true;
1005 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name))
1007 workspace_show(workspace);
1009 cmd_output->needs_tree_render = true;
1010 // XXX: default reply for now, make this a better reply
1015 * Implementation of 'workspace back_and_forth'.
1018 void cmd_workspace_back_and_forth(I3_CMD) {
1019 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1020 LOG("Cannot switch workspace while in global fullscreen\n");
1025 workspace_back_and_forth();
1027 cmd_output->needs_tree_render = true;
1028 // XXX: default reply for now, make this a better reply
1033 * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
1036 void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
1037 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
1039 if (strncasecmp(name, "__", strlen("__")) == 0) {
1040 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1045 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1046 LOG("Cannot switch workspace while in global fullscreen\n");
1051 DLOG("should switch to workspace %s\n", name);
1052 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name))
1054 workspace_show_by_name(name);
1056 cmd_output->needs_tree_render = true;
1057 // XXX: default reply for now, make this a better reply
1062 * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
1065 void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
1068 owindow *current = TAILQ_FIRST(&owindows);
1069 if (current == NULL) {
1074 /* Marks must be unique, i.e., no two windows must have the same mark. */
1075 if (current != TAILQ_LAST(&owindows, owindows_head)) {
1076 yerror("A mark must not be put onto more than one window");
1080 DLOG("matching: %p / %s\n", current->con, current->con->name);
1082 mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
1083 if (toggle != NULL) {
1084 con_mark_toggle(current->con, mark, mark_mode);
1086 con_mark(current->con, mark, mark_mode);
1089 cmd_output->needs_tree_render = true;
1090 // XXX: default reply for now, make this a better reply
1095 * Implementation of 'unmark [mark]'
1098 void cmd_unmark(I3_CMD, const char *mark) {
1099 if (match_is_empty(current_match)) {
1100 con_unmark(NULL, mark);
1103 TAILQ_FOREACH(current, &owindows, owindows) {
1104 con_unmark(current->con, mark);
1108 cmd_output->needs_tree_render = true;
1109 // XXX: default reply for now, make this a better reply
1114 * Implementation of 'mode <string>'.
1117 void cmd_mode(I3_CMD, const char *mode) {
1118 DLOG("mode=%s\n", mode);
1121 // XXX: default reply for now, make this a better reply
1126 * Implementation of 'move [window|container] [to] output <str>'.
1129 void cmd_move_con_to_output(I3_CMD, const char *name) {
1130 DLOG("Should move window to output \"%s\".\n", name);
1134 bool had_error = false;
1135 TAILQ_FOREACH(current, &owindows, owindows) {
1136 DLOG("matching: %p / %s\n", current->con, current->con->name);
1138 Output *current_output = get_output_of_con(current->con);
1139 assert(current_output != NULL);
1141 Output *output = get_output_from_string(current_output, name);
1142 if (output == NULL) {
1143 ELOG("Could not find output \"%s\", skipping.\n", name);
1149 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1151 ELOG("Could not find a visible workspace on output %p.\n", output);
1156 con_move_to_workspace(current->con, ws, true, false, false);
1159 cmd_output->needs_tree_render = true;
1160 ysuccess(!had_error);
1164 * Implementation of 'move [container|window] [to] mark <str>'.
1167 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1168 DLOG("moving window to mark \"%s\"\n", mark);
1174 TAILQ_FOREACH(current, &owindows, owindows) {
1175 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1176 result &= con_move_to_mark(current->con, mark);
1179 cmd_output->needs_tree_render = true;
1184 * Implementation of 'floating enable|disable|toggle'
1187 void cmd_floating(I3_CMD, const char *floating_mode) {
1190 DLOG("floating_mode=%s\n", floating_mode);
1194 TAILQ_FOREACH(current, &owindows, owindows) {
1195 DLOG("matching: %p / %s\n", current->con, current->con->name);
1196 if (strcmp(floating_mode, "toggle") == 0) {
1197 DLOG("should toggle mode\n");
1198 toggle_floating_mode(current->con, false);
1200 DLOG("should switch mode to %s\n", floating_mode);
1201 if (strcmp(floating_mode, "enable") == 0) {
1202 floating_enable(current->con, false);
1204 floating_disable(current->con, false);
1209 cmd_output->needs_tree_render = true;
1210 // XXX: default reply for now, make this a better reply
1215 * Implementation of 'move workspace to [output] <str>'.
1218 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1219 DLOG("should move workspace to output %s\n", name);
1224 TAILQ_FOREACH(current, &owindows, owindows) {
1225 Con *ws = con_get_workspace(current->con);
1226 bool success = workspace_move_to_output(ws, name);
1228 ELOG("Failed to move workspace to output.\n");
1234 cmd_output->needs_tree_render = true;
1235 // XXX: default reply for now, make this a better reply
1240 * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1243 void cmd_split(I3_CMD, const char *direction) {
1247 LOG("splitting in direction %c\n", direction[0]);
1248 TAILQ_FOREACH(current, &owindows, owindows) {
1249 if (con_is_docked(current->con)) {
1250 ELOG("Cannot split a docked container, skipping.\n");
1254 DLOG("matching: %p / %s\n", current->con, current->con->name);
1255 if (direction[0] == 't') {
1256 layout_t current_layout;
1257 if (current->con->type == CT_WORKSPACE) {
1258 current_layout = current->con->layout;
1260 current_layout = current->con->parent->layout;
1262 /* toggling split orientation */
1263 if (current_layout == L_SPLITH) {
1264 tree_split(current->con, VERT);
1266 tree_split(current->con, HORIZ);
1269 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1273 cmd_output->needs_tree_render = true;
1274 // XXX: default reply for now, make this a better reply
1279 * Implementation of 'kill [window|client]'.
1282 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1283 if (kill_mode_str == NULL)
1284 kill_mode_str = "window";
1286 DLOG("kill_mode=%s\n", kill_mode_str);
1289 if (strcmp(kill_mode_str, "window") == 0)
1290 kill_mode = KILL_WINDOW;
1291 else if (strcmp(kill_mode_str, "client") == 0)
1292 kill_mode = KILL_CLIENT;
1294 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1302 TAILQ_FOREACH(current, &owindows, owindows) {
1303 con_close(current->con, kill_mode);
1306 cmd_output->needs_tree_render = true;
1307 // XXX: default reply for now, make this a better reply
1312 * Implementation of 'exec [--no-startup-id] <command>'.
1315 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1316 bool no_startup_id = (nosn != NULL);
1318 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1319 start_application(command, no_startup_id);
1321 // XXX: default reply for now, make this a better reply
1326 * Implementation of 'focus left|right|up|down'.
1329 void cmd_focus_direction(I3_CMD, const char *direction) {
1330 DLOG("direction = *%s*\n", direction);
1332 if (strcmp(direction, "left") == 0)
1333 tree_next('p', HORIZ);
1334 else if (strcmp(direction, "right") == 0)
1335 tree_next('n', HORIZ);
1336 else if (strcmp(direction, "up") == 0)
1337 tree_next('p', VERT);
1338 else if (strcmp(direction, "down") == 0)
1339 tree_next('n', VERT);
1341 ELOG("Invalid focus direction (%s)\n", direction);
1346 cmd_output->needs_tree_render = true;
1347 // XXX: default reply for now, make this a better reply
1352 * Implementation of 'focus tiling|floating|mode_toggle'.
1355 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1356 DLOG("window_mode = %s\n", window_mode);
1358 Con *ws = con_get_workspace(focused);
1360 if (strcmp(window_mode, "mode_toggle") == 0) {
1361 if (con_inside_floating(focused))
1362 window_mode = "tiling";
1364 window_mode = "floating";
1367 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1368 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1369 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1372 con_focus(con_descend_focused(current));
1377 cmd_output->needs_tree_render = true;
1378 // XXX: default reply for now, make this a better reply
1383 * Implementation of 'focus parent|child'.
1386 void cmd_focus_level(I3_CMD, const char *level) {
1387 DLOG("level = %s\n", level);
1388 bool success = false;
1390 /* Focusing the parent can only be allowed if the newly
1391 * focused container won't escape the fullscreen container. */
1392 if (strcmp(level, "parent") == 0) {
1393 if (focused && focused->parent) {
1394 if (con_fullscreen_permits_focusing(focused->parent))
1395 success = level_up();
1397 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1401 /* Focusing a child should always be allowed. */
1403 success = level_down();
1405 cmd_output->needs_tree_render = success;
1406 // XXX: default reply for now, make this a better reply
1411 * Implementation of 'focus'.
1414 void cmd_focus(I3_CMD) {
1415 DLOG("current_match = %p\n", current_match);
1417 if (match_is_empty(current_match)) {
1418 ELOG("You have to specify which window/container should be focused.\n");
1419 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1421 yerror("You have to specify which window/container should be focused");
1426 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1429 TAILQ_FOREACH(current, &owindows, owindows) {
1430 Con *ws = con_get_workspace(current->con);
1431 /* If no workspace could be found, this was a dock window.
1432 * Just skip it, you cannot focus dock windows. */
1436 /* Check the fullscreen focus constraints. */
1437 if (!con_fullscreen_permits_focusing(current->con)) {
1438 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1443 /* In case this is a scratchpad window, call scratchpad_show(). */
1444 if (ws == __i3_scratch) {
1445 scratchpad_show(current->con);
1447 /* While for the normal focus case we can change focus multiple
1448 * times and only a single window ends up focused, we could show
1449 * multiple scratchpad windows. So, rather break here. */
1453 /* If the container is not on the current workspace,
1454 * workspace_show() will switch to a different workspace and (if
1455 * enabled) trigger a mouse pointer warp to the currently focused
1456 * container (!) on the target workspace.
1458 * Therefore, before calling workspace_show(), we make sure that
1459 * 'current' will be focused on the workspace. However, we cannot
1460 * just con_focus(current) because then the pointer will not be
1461 * warped at all (the code thinks we are already there).
1463 * So we focus 'current' to make it the currently focused window of
1464 * the target workspace, then revert focus. */
1465 Con *currently_focused = focused;
1466 con_focus(current->con);
1467 con_focus(currently_focused);
1469 /* Now switch to the workspace, then focus */
1471 LOG("focusing %p / %s\n", current->con, current->con->name);
1472 con_focus(current->con);
1477 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1478 "while only exactly one container can be focused at a time.\n",
1481 cmd_output->needs_tree_render = true;
1482 ysuccess(count > 0);
1486 * Implementation of 'fullscreen enable|toggle [global]' and
1487 * 'fullscreen disable'
1490 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1491 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1492 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1497 TAILQ_FOREACH(current, &owindows, owindows) {
1498 DLOG("matching: %p / %s\n", current->con, current->con->name);
1499 if (strcmp(action, "toggle") == 0) {
1500 con_toggle_fullscreen(current->con, mode);
1501 } else if (strcmp(action, "enable") == 0) {
1502 con_enable_fullscreen(current->con, mode);
1503 } else if (strcmp(action, "disable") == 0) {
1504 con_disable_fullscreen(current->con);
1508 cmd_output->needs_tree_render = true;
1509 // XXX: default reply for now, make this a better reply
1514 * Implementation of 'sticky enable|disable|toggle'.
1517 void cmd_sticky(I3_CMD, const char *action) {
1518 DLOG("%s sticky on window\n", action);
1522 TAILQ_FOREACH(current, &owindows, owindows) {
1523 if (current->con->window == NULL) {
1524 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1527 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1529 bool sticky = false;
1530 if (strcmp(action, "enable") == 0)
1532 else if (strcmp(action, "disable") == 0)
1534 else if (strcmp(action, "toggle") == 0)
1535 sticky = !current->con->sticky;
1537 current->con->sticky = sticky;
1538 ewmh_update_sticky(current->con->window->id, sticky);
1541 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1542 * sure it gets pushed to the front now. */
1543 output_push_sticky_windows(focused);
1545 cmd_output->needs_tree_render = true;
1550 * Implementation of 'move <direction> [<pixels> [px]]'.
1553 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1557 Con *initially_focused = focused;
1559 TAILQ_FOREACH(current, &owindows, owindows) {
1560 DLOG("moving in direction %s, px %ld\n", direction, move_px);
1561 if (con_is_floating(current->con)) {
1562 DLOG("floating move with %ld pixels\n", move_px);
1563 Rect newrect = current->con->parent->rect;
1564 if (strcmp(direction, "left") == 0) {
1565 newrect.x -= move_px;
1566 } else if (strcmp(direction, "right") == 0) {
1567 newrect.x += move_px;
1568 } else if (strcmp(direction, "up") == 0) {
1569 newrect.y -= move_px;
1570 } else if (strcmp(direction, "down") == 0) {
1571 newrect.y += move_px;
1573 floating_reposition(current->con->parent, newrect);
1575 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1576 cmd_output->needs_tree_render = true;
1580 /* the move command should not disturb focus */
1581 if (focused != initially_focused)
1582 con_focus(initially_focused);
1584 // XXX: default reply for now, make this a better reply
1589 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1592 void cmd_layout(I3_CMD, const char *layout_str) {
1595 if (strcmp(layout_str, "stacking") == 0)
1596 layout_str = "stacked";
1598 /* default is a special case which will be handled in con_set_layout(). */
1599 if (strcmp(layout_str, "default") == 0)
1601 else if (strcmp(layout_str, "stacked") == 0)
1603 else if (strcmp(layout_str, "tabbed") == 0)
1605 else if (strcmp(layout_str, "splitv") == 0)
1607 else if (strcmp(layout_str, "splith") == 0)
1610 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1614 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1617 TAILQ_FOREACH(current, &owindows, owindows) {
1618 if (con_is_docked(current->con)) {
1619 ELOG("cannot change layout of a docked container, skipping it.\n");
1623 DLOG("matching: %p / %s\n", current->con, current->con->name);
1624 con_set_layout(current->con, layout);
1627 cmd_output->needs_tree_render = true;
1628 // XXX: default reply for now, make this a better reply
1633 * Implementation of 'layout toggle [all|split]'.
1636 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1639 if (toggle_mode == NULL)
1640 toggle_mode = "default";
1642 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1644 /* check if the match is empty, not if the result is empty */
1645 if (match_is_empty(current_match))
1646 con_toggle_layout(focused, toggle_mode);
1648 TAILQ_FOREACH(current, &owindows, owindows) {
1649 DLOG("matching: %p / %s\n", current->con, current->con->name);
1650 con_toggle_layout(current->con, toggle_mode);
1654 cmd_output->needs_tree_render = true;
1655 // XXX: default reply for now, make this a better reply
1660 * Implementation of 'exit'.
1663 void cmd_exit(I3_CMD) {
1664 LOG("Exiting due to user command.\n");
1665 #ifdef I3_ASAN_ENABLED
1666 __lsan_do_leak_check();
1669 unlink(config.ipc_socket_path);
1670 xcb_disconnect(conn);
1677 * Implementation of 'reload'.
1680 void cmd_reload(I3_CMD) {
1682 kill_nagbar(&config_error_nagbar_pid, false);
1683 kill_nagbar(&command_error_nagbar_pid, false);
1684 load_configuration(conn, NULL, true);
1686 /* Send an IPC event just in case the ws names have changed */
1687 ipc_send_workspace_event("reload", NULL, NULL);
1688 /* Send an update event for the barconfig just in case it has changed */
1691 // XXX: default reply for now, make this a better reply
1696 * Implementation of 'restart'.
1699 void cmd_restart(I3_CMD) {
1700 LOG("restarting i3\n");
1702 unlink(config.ipc_socket_path);
1703 /* We need to call this manually since atexit handlers don’t get called
1705 purge_zerobyte_logfile();
1708 // XXX: default reply for now, make this a better reply
1713 * Implementation of 'open'.
1716 void cmd_open(I3_CMD) {
1717 LOG("opening new container\n");
1718 Con *con = tree_open_con(NULL, NULL);
1719 con->layout = L_SPLITH;
1726 y(integer, (long int)con);
1729 cmd_output->needs_tree_render = true;
1733 * Implementation of 'focus output <output>'.
1736 void cmd_focus_output(I3_CMD, const char *name) {
1739 DLOG("name = %s\n", name);
1743 /* get the output */
1744 Output *current_output = NULL;
1747 TAILQ_FOREACH(current, &owindows, owindows)
1748 current_output = get_output_of_con(current->con);
1749 assert(current_output != NULL);
1751 output = get_output_from_string(current_output, name);
1754 LOG("No such output found.\n");
1759 /* get visible workspace on output */
1761 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1769 cmd_output->needs_tree_render = true;
1770 // XXX: default reply for now, make this a better reply
1775 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1778 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1779 bool has_error = false;
1784 TAILQ_FOREACH(current, &owindows, owindows) {
1785 if (!con_is_floating(current->con)) {
1786 ELOG("Cannot change position. The window/container is not floating\n");
1789 yerror("Cannot change position of a window/container because it is not floating.");
1796 if (strcmp(method, "absolute") == 0) {
1797 current->con->parent->rect.x = x;
1798 current->con->parent->rect.y = y;
1800 DLOG("moving to absolute position %ld %ld\n", x, y);
1801 floating_maybe_reassign_ws(current->con->parent);
1802 cmd_output->needs_tree_render = true;
1805 if (strcmp(method, "position") == 0) {
1806 Rect newrect = current->con->parent->rect;
1808 DLOG("moving to position %ld %ld\n", x, y);
1812 floating_reposition(current->con->parent, newrect);
1816 // XXX: default reply for now, make this a better reply
1822 * Implementation of 'move [window|container] [to] [absolute] position center
1825 void cmd_move_window_to_center(I3_CMD, const char *method) {
1826 bool has_error = false;
1830 TAILQ_FOREACH(current, &owindows, owindows) {
1831 Con *floating_con = con_inside_floating(current->con);
1832 if (floating_con == NULL) {
1833 ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1834 current->con, current->con->name);
1837 yerror("Cannot change position of a window/container because it is not floating.");
1844 if (strcmp(method, "absolute") == 0) {
1845 DLOG("moving to absolute center\n");
1846 floating_center(floating_con, croot->rect);
1848 floating_maybe_reassign_ws(floating_con);
1849 cmd_output->needs_tree_render = true;
1852 if (strcmp(method, "position") == 0) {
1853 DLOG("moving to center\n");
1854 floating_center(floating_con, con_get_workspace(floating_con)->rect);
1856 cmd_output->needs_tree_render = true;
1860 // XXX: default reply for now, make this a better reply
1866 * Implementation of 'move [window|container] [to] position mouse'
1869 void cmd_move_window_to_mouse(I3_CMD) {
1873 TAILQ_FOREACH(current, &owindows, owindows) {
1874 Con *floating_con = con_inside_floating(current->con);
1875 if (floating_con == NULL) {
1876 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1877 current->con, current->con->name);
1881 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1882 floating_move_to_pointer(floating_con);
1885 cmd_output->needs_tree_render = true;
1890 * Implementation of 'move scratchpad'.
1893 void cmd_move_scratchpad(I3_CMD) {
1894 DLOG("should move window to scratchpad\n");
1899 TAILQ_FOREACH(current, &owindows, owindows) {
1900 DLOG("matching: %p / %s\n", current->con, current->con->name);
1901 scratchpad_move(current->con);
1904 cmd_output->needs_tree_render = true;
1905 // XXX: default reply for now, make this a better reply
1910 * Implementation of 'scratchpad show'.
1913 void cmd_scratchpad_show(I3_CMD) {
1914 DLOG("should show scratchpad window\n");
1917 if (match_is_empty(current_match)) {
1918 scratchpad_show(NULL);
1920 TAILQ_FOREACH(current, &owindows, owindows) {
1921 DLOG("matching: %p / %s\n", current->con, current->con->name);
1922 scratchpad_show(current->con);
1926 cmd_output->needs_tree_render = true;
1927 // XXX: default reply for now, make this a better reply
1932 * Implementation of 'title_format <format>'
1935 void cmd_title_format(I3_CMD, const char *format) {
1936 DLOG("setting title_format to \"%s\"\n", format);
1940 TAILQ_FOREACH(current, &owindows, owindows) {
1941 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1942 FREE(current->con->title_format);
1944 /* If we only display the title without anything else, we can skip the parsing step,
1945 * so we remove the title format altogether. */
1946 if (strcasecmp(format, "%title") != 0) {
1947 current->con->title_format = sstrdup(format);
1949 if (current->con->window != NULL) {
1950 i3String *formatted_title = con_parse_title_format(current->con);
1951 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1952 I3STRING_FREE(formatted_title);
1955 if (current->con->window != NULL) {
1956 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1957 ewmh_update_visible_name(current->con->window->id, NULL);
1961 if (current->con->window != NULL) {
1962 /* Make sure the window title is redrawn immediately. */
1963 current->con->window->name_x_changed = true;
1965 /* For windowless containers we also need to force the redrawing. */
1966 FREE(current->con->deco_render_params);
1970 cmd_output->needs_tree_render = true;
1975 * Implementation of 'rename workspace [<name>] to <name>'
1978 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1979 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1980 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1985 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1987 LOG("Renaming current workspace to \"%s\"\n", new_name);
1990 Con *output, *workspace = NULL;
1992 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1993 GREP_FIRST(workspace, output_get_content(output),
1994 !strcasecmp(child->name, old_name));
1996 workspace = con_get_workspace(focused);
1997 old_name = workspace->name;
2001 yerror("Old workspace \"%s\" not found", old_name);
2005 Con *check_dest = NULL;
2006 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2007 GREP_FIRST(check_dest, output_get_content(output),
2008 !strcasecmp(child->name, new_name));
2010 if (check_dest != NULL) {
2011 yerror("New workspace \"%s\" already exists", new_name);
2015 /* Change the name and try to parse it as a number. */
2016 /* old_name might refer to workspace->name, so copy it before free()ing */
2017 char *old_name_copy = sstrdup(old_name);
2018 FREE(workspace->name);
2019 workspace->name = sstrdup(new_name);
2021 workspace->num = ws_name_to_number(new_name);
2022 LOG("num = %d\n", workspace->num);
2024 /* By re-attaching, the sort order will be correct afterwards. */
2025 Con *previously_focused = focused;
2026 Con *parent = workspace->parent;
2027 con_detach(workspace);
2028 con_attach(workspace, parent, false);
2030 /* Move the workspace to the correct output if it has an assignment */
2031 struct Workspace_Assignment *assignment = NULL;
2032 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2033 if (assignment->output == NULL)
2035 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2039 workspace_move_to_output(workspace, assignment->output);
2041 if (previously_focused)
2042 workspace_show(con_get_workspace(previously_focused));
2047 /* Restore the previous focus since con_attach messes with the focus. */
2048 con_focus(previously_focused);
2050 cmd_output->needs_tree_render = true;
2053 ipc_send_workspace_event("rename", workspace, NULL);
2054 ewmh_update_desktop_names();
2055 ewmh_update_desktop_viewport();
2056 ewmh_update_current_desktop();
2058 startup_sequence_rename_workspace(old_name_copy, new_name);
2059 free(old_name_copy);
2063 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2066 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2068 bool toggle = false;
2069 if (strcmp(bar_mode, "dock") == 0)
2071 else if (strcmp(bar_mode, "hide") == 0)
2073 else if (strcmp(bar_mode, "invisible") == 0)
2075 else if (strcmp(bar_mode, "toggle") == 0)
2078 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2082 bool changed_sth = false;
2083 Barconfig *current = NULL;
2084 TAILQ_FOREACH(current, &barconfigs, configs) {
2085 if (bar_id && strcmp(current->id, bar_id) != 0)
2089 mode = (current->mode + 1) % 2;
2091 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2092 current->mode = mode;
2099 if (bar_id && !changed_sth) {
2100 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2108 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2111 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2112 int hidden_state = S_SHOW;
2113 bool toggle = false;
2114 if (strcmp(bar_hidden_state, "hide") == 0)
2115 hidden_state = S_HIDE;
2116 else if (strcmp(bar_hidden_state, "show") == 0)
2117 hidden_state = S_SHOW;
2118 else if (strcmp(bar_hidden_state, "toggle") == 0)
2121 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2125 bool changed_sth = false;
2126 Barconfig *current = NULL;
2127 TAILQ_FOREACH(current, &barconfigs, configs) {
2128 if (bar_id && strcmp(current->id, bar_id) != 0)
2132 hidden_state = (current->hidden_state + 1) % 2;
2134 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2135 current->hidden_state = hidden_state;
2142 if (bar_id && !changed_sth) {
2143 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2151 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2154 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2156 if (strcmp(bar_type, "mode") == 0)
2157 ret = cmd_bar_mode(bar_value, bar_id);
2158 else if (strcmp(bar_type, "hidden_state") == 0)
2159 ret = cmd_bar_hidden_state(bar_value, bar_id);
2161 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2173 * Implementation of 'shmlog <size>|toggle|on|off'
2176 void cmd_shmlog(I3_CMD, const char *argument) {
2177 if (!strcmp(argument, "toggle"))
2178 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2179 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2180 else if (!strcmp(argument, "on"))
2181 shmlog_size = default_shmlog_size;
2182 else if (!strcmp(argument, "off"))
2185 /* If shm logging now, restart logging with the new size. */
2186 if (shmlog_size > 0) {
2188 LOG("Restarting shm logging...\n");
2191 shmlog_size = atoi(argument);
2192 /* Make a weakly attempt at ensuring the argument is valid. */
2193 if (shmlog_size <= 0)
2194 shmlog_size = default_shmlog_size;
2196 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2198 update_shmlog_atom();
2199 // XXX: default reply for now, make this a better reply
2204 * Implementation of 'debuglog toggle|on|off'
2207 void cmd_debuglog(I3_CMD, const char *argument) {
2208 bool logging = get_debug_logging();
2209 if (!strcmp(argument, "toggle")) {
2210 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2211 set_debug_logging(!logging);
2212 } else if (!strcmp(argument, "on") && !logging) {
2213 LOG("Enabling debug logging\n");
2214 set_debug_logging(true);
2215 } else if (!strcmp(argument, "off") && logging) {
2216 LOG("Disabling debug logging\n");
2217 set_debug_logging(false);
2219 // XXX: default reply for now, make this a better reply