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)
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 occured 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 owindow *ow = smalloc(sizeof(owindow)); \
67 TAILQ_INIT(&owindows); \
68 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
73 * Returns true if a is definitely greater than b (using the given epsilon)
76 static bool definitelyGreaterThan(float a, float b, float epsilon) {
77 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
81 * Returns the output containing the given container.
83 static Output *get_output_of_con(Con *con) {
84 Con *output_con = con_get_output(con);
85 Output *output = get_output_by_name(output_con->name);
86 assert(output != NULL);
92 * Checks whether we switched to a new workspace and returns false in that case,
93 * signaling that further workspace switching should be done by the calling function
94 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
95 * and return true, signaling that no further workspace switching should occur in the calling function.
98 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
99 Con *ws = con_get_workspace(focused);
101 /* If we switched to a different workspace, do nothing */
102 if (strcmp(ws->name, name) != 0)
105 DLOG("This workspace is already focused.\n");
106 if (config.workspace_auto_back_and_forth) {
107 workspace_back_and_forth();
108 cmd_output->needs_tree_render = true;
114 * Return the passed workspace unless it is the current one and auto back and
115 * forth is enabled, in which case the back_and_forth workspace is returned.
117 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
120 if (!config.workspace_auto_back_and_forth)
123 current = con_get_workspace(focused);
125 if (current == workspace) {
126 baf = workspace_back_and_forth_get();
128 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
136 // This code is commented out because we might recycle it for popping up error
137 // messages on parser errors.
139 static pid_t migration_pid = -1;
142 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
143 * it exited (or could not be started, depending on the exit code).
146 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
147 ev_child_stop(EV_A_ watcher);
148 if (!WIFEXITED(watcher->rstatus)) {
149 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
153 int exitcode = WEXITSTATUS(watcher->rstatus);
154 printf("i3-nagbar process exited with status %d\n", exitcode);
156 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
162 /* We need ev >= 4 for the following code. Since it is not *that* important (it
163 * only makes sure that there are no i3-nagbar instances left behind) we still
164 * support old systems with libev 3. */
165 #if EV_VERSION_MAJOR >= 4
167 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
168 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
171 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
172 if (migration_pid != -1) {
173 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
174 kill(migration_pid, SIGKILL);
179 void cmd_MIGRATION_start_nagbar(void) {
180 if (migration_pid != -1) {
181 fprintf(stderr, "i3-nagbar already running.\n");
184 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
185 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
186 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
187 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
188 migration_pid = fork();
189 if (migration_pid == -1) {
190 warn("Could not fork()");
195 if (migration_pid == 0) {
197 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
199 NULL, /* will be replaced by the executable path */
203 "You found a parsing error. Please, please, please, report it!",
209 exec_i3_utility("i3-nagbar", argv);
213 /* install a child watcher */
214 ev_child *child = smalloc(sizeof(ev_child));
215 ev_child_init(child, &nagbar_exited, migration_pid, 0);
216 ev_child_start(main_loop, child);
218 /* We need ev >= 4 for the following code. Since it is not *that* important (it
219 * only makes sure that there are no i3-nagbar instances left behind) we still
220 * support old systems with libev 3. */
221 #if EV_VERSION_MAJOR >= 4
222 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
224 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
225 ev_cleanup_init(cleanup, nagbar_cleanup);
226 ev_cleanup_start(main_loop, cleanup);
232 /*******************************************************************************
233 * Criteria functions.
234 ******************************************************************************/
237 * Helper data structure for an operation window (window on which the operation
238 * will be performed). Used to build the TAILQ owindows.
241 typedef struct owindow {
243 TAILQ_ENTRY(owindow) owindows;
246 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
248 static owindows_head owindows;
251 * Initializes the specified 'Match' data structure and the initial state of
252 * commands.c for matching target windows of a command.
255 void cmd_criteria_init(I3_CMD) {
259 DLOG("Initializing criteria, current_match = %p\n", current_match);
260 match_free(current_match);
261 match_init(current_match);
262 while (!TAILQ_EMPTY(&owindows)) {
263 ow = TAILQ_FIRST(&owindows);
264 TAILQ_REMOVE(&owindows, ow, owindows);
267 TAILQ_INIT(&owindows);
269 TAILQ_FOREACH(con, &all_cons, all_cons) {
270 ow = smalloc(sizeof(owindow));
272 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
277 * A match specification just finished (the closing square bracket was found),
278 * so we filter the list of owindows.
281 void cmd_criteria_match_windows(I3_CMD) {
282 owindow *next, *current;
284 DLOG("match specification finished, matching...\n");
285 /* copy the old list head to iterate through it and start with a fresh
286 * list which will contain only matching windows */
287 struct owindows_head old = owindows;
288 TAILQ_INIT(&owindows);
289 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
290 /* make a copy of the next pointer and advance the pointer to the
291 * next element as we are going to invalidate the element’s
292 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
294 next = TAILQ_NEXT(next, owindows);
296 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
298 /* We use this flag to prevent matching on window-less containers if
299 * only window-specific criteria were specified. */
300 bool accept_match = false;
302 if (current_match->con_id != NULL) {
305 if (current_match->con_id == current->con) {
306 DLOG("con_id matched.\n");
308 DLOG("con_id does not match.\n");
314 if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
316 bool matched_by_mark = false;
319 TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
320 if (!regex_matches(current_match->mark, mark->name))
323 DLOG("match by mark\n");
324 matched_by_mark = true;
328 if (!matched_by_mark) {
329 DLOG("mark does not match.\n");
335 if (current->con->window != NULL) {
336 if (match_matches_window(current_match, current->con->window)) {
337 DLOG("matches window!\n");
340 DLOG("doesnt match\n");
347 TAILQ_INSERT_TAIL(&owindows, current, owindows);
354 TAILQ_FOREACH(current, &owindows, owindows) {
355 DLOG("matching: %p / %s\n", current->con, current->con->name);
360 * Interprets a ctype=cvalue pair and adds it to the current match
364 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
365 match_parse_property(current_match, ctype, cvalue);
369 * Implementation of 'move [window|container] [to] workspace
370 * next|prev|next_on_output|prev_on_output|current'.
373 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
376 DLOG("which=%s\n", which);
378 /* We have nothing to move:
379 * when criteria was specified but didn't match any window or
380 * when criteria wasn't specified and we don't have any window focused. */
381 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
382 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
383 !con_has_children(focused))) {
390 /* get the workspace */
392 if (strcmp(which, "next") == 0)
393 ws = workspace_next();
394 else if (strcmp(which, "prev") == 0)
395 ws = workspace_prev();
396 else if (strcmp(which, "next_on_output") == 0)
397 ws = workspace_next_on_output();
398 else if (strcmp(which, "prev_on_output") == 0)
399 ws = workspace_prev_on_output();
400 else if (strcmp(which, "current") == 0)
401 ws = con_get_workspace(focused);
403 ELOG("BUG: called with which=%s\n", which);
408 TAILQ_FOREACH(current, &owindows, owindows) {
409 DLOG("matching: %p / %s\n", current->con, current->con->name);
410 con_move_to_workspace(current->con, ws, true, false, false);
413 cmd_output->needs_tree_render = true;
414 // XXX: default reply for now, make this a better reply
419 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
422 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
426 ws = workspace_back_and_forth_get();
429 yerror("No workspace was previously active.");
435 TAILQ_FOREACH(current, &owindows, owindows) {
436 DLOG("matching: %p / %s\n", current->con, current->con->name);
437 con_move_to_workspace(current->con, ws, true, false, false);
440 cmd_output->needs_tree_render = true;
441 // XXX: default reply for now, make this a better reply
446 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
449 void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
450 if (strncasecmp(name, "__", strlen("__")) == 0) {
451 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
456 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
459 /* We have nothing to move:
460 * when criteria was specified but didn't match any window or
461 * when criteria wasn't specified and we don't have any window focused. */
462 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
463 ELOG("No windows match your criteria, cannot move.\n");
466 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
467 !con_has_children(focused)) {
472 LOG("should move window to workspace %s\n", name);
473 /* get the workspace */
474 Con *ws = workspace_get(name, NULL);
476 if (!no_auto_back_and_forth)
477 ws = maybe_auto_back_and_forth_workspace(ws);
481 TAILQ_FOREACH(current, &owindows, owindows) {
482 DLOG("matching: %p / %s\n", current->con, current->con->name);
483 con_move_to_workspace(current->con, ws, true, false, false);
486 cmd_output->needs_tree_render = true;
487 // XXX: default reply for now, make this a better reply
492 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
495 void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
496 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
499 /* We have nothing to move:
500 * when criteria was specified but didn't match any window or
501 * when criteria wasn't specified and we don't have any window focused. */
502 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
503 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
504 !con_has_children(focused))) {
509 LOG("should move window to workspace %s\n", which);
510 /* get the workspace */
511 Con *output, *workspace = NULL;
513 long parsed_num = ws_name_to_number(which);
515 if (parsed_num == -1) {
516 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
517 yerror("Could not parse number \"%s\"", which);
521 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
522 GREP_FIRST(workspace, output_get_content(output),
523 child->num == parsed_num);
526 workspace = workspace_get(which, NULL);
529 if (!no_auto_back_and_forth)
530 workspace = maybe_auto_back_and_forth_workspace(workspace);
534 TAILQ_FOREACH(current, &owindows, owindows) {
535 DLOG("matching: %p / %s\n", current->con, current->con->name);
536 con_move_to_workspace(current->con, workspace, true, false, false);
539 cmd_output->needs_tree_render = true;
540 // XXX: default reply for now, make this a better reply
544 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) {
545 LOG("floating resize\n");
546 Rect old_rect = floating_con->rect;
547 Con *focused_con = con_descend_focused(floating_con);
549 /* ensure that resize will take place even if pixel increment is smaller than
550 * height increment or width increment.
552 const i3Window *window = focused_con->window;
553 if (window != NULL) {
554 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
555 strcmp(direction, "height") == 0) {
557 px = (-px < window->height_increment) ? -window->height_increment : px;
559 px = (px < window->height_increment) ? window->height_increment : px;
560 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
562 px = (-px < window->width_increment) ? -window->width_increment : px;
564 px = (px < window->width_increment) ? window->width_increment : px;
568 if (strcmp(direction, "up") == 0) {
569 floating_con->rect.height += px;
570 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
571 floating_con->rect.height += px;
572 } else if (strcmp(direction, "left") == 0) {
573 floating_con->rect.width += px;
575 floating_con->rect.width += px;
578 floating_check_size(floating_con);
580 /* Did we actually resize anything or did the size constraints prevent us?
581 * If we could not resize, exit now to not move the window. */
582 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
585 if (strcmp(direction, "up") == 0) {
586 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
587 } else if (strcmp(direction, "left") == 0) {
588 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
591 /* If this is a scratchpad window, don't auto center it from now on. */
592 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
593 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
596 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
597 LOG("tiling resize\n");
599 Con *first = current;
600 direction_t search_direction;
601 if (!strcmp(direction, "left"))
602 search_direction = D_LEFT;
603 else if (!strcmp(direction, "right"))
604 search_direction = D_RIGHT;
605 else if (!strcmp(direction, "up"))
606 search_direction = D_UP;
608 search_direction = D_DOWN;
610 bool res = resize_find_tiling_participants(&first, &second, search_direction);
612 LOG("No second container in this direction found.\n");
617 /* get the default percentage */
618 int children = con_num_children(first->parent);
619 LOG("ins. %d children\n", children);
620 double percentage = 1.0 / children;
621 LOG("default percentage = %f\n", percentage);
624 LOG("second->percent = %f\n", second->percent);
625 LOG("first->percent before = %f\n", first->percent);
626 if (first->percent == 0.0)
627 first->percent = percentage;
628 if (second->percent == 0.0)
629 second->percent = percentage;
630 double new_first_percent = first->percent + ((double)ppt / 100.0);
631 double new_second_percent = second->percent - ((double)ppt / 100.0);
632 LOG("new_first_percent = %f\n", new_first_percent);
633 LOG("new_second_percent = %f\n", new_second_percent);
634 /* Ensure that the new percentages are positive and greater than
635 * 0.05 to have a reasonable minimum size. */
636 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
637 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
638 first->percent += ((double)ppt / 100.0);
639 second->percent -= ((double)ppt / 100.0);
640 LOG("first->percent after = %f\n", first->percent);
641 LOG("second->percent after = %f\n", second->percent);
643 LOG("Not resizing, already at minimum size\n");
649 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
650 LOG("width/height resize\n");
651 /* get the appropriate current container (skip stacked/tabbed cons) */
652 while (current->parent->layout == L_STACKED ||
653 current->parent->layout == L_TABBED)
654 current = current->parent;
656 /* Then further go up until we find one with the matching orientation. */
657 orientation_t search_orientation =
658 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
660 while (current->type != CT_WORKSPACE &&
661 current->type != CT_FLOATING_CON &&
662 (con_orientation(current->parent) != search_orientation || con_num_children(current->parent) == 1))
663 current = current->parent;
665 /* get the default percentage */
666 int children = con_num_children(current->parent);
667 LOG("ins. %d children\n", children);
668 double percentage = 1.0 / children;
669 LOG("default percentage = %f\n", percentage);
671 orientation_t orientation = con_orientation(current->parent);
673 if ((orientation == HORIZ &&
674 strcmp(direction, "height") == 0) ||
675 (orientation == VERT &&
676 strcmp(direction, "width") == 0)) {
677 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
678 (orientation == HORIZ ? "horizontal" : "vertical"));
684 LOG("This is the only container, cannot resize.\n");
689 /* Ensure all the other children have a percentage set. */
691 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
692 LOG("child->percent = %f (child %p)\n", child->percent, child);
693 if (child->percent == 0.0)
694 child->percent = percentage;
697 double new_current_percent = current->percent + ((double)ppt / 100.0);
698 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
699 LOG("new_current_percent = %f\n", new_current_percent);
700 LOG("subtract_percent = %f\n", subtract_percent);
701 /* Ensure that the new percentages are positive and greater than
702 * 0.05 to have a reasonable minimum size. */
703 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
704 if (child == current)
706 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
707 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
712 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
713 LOG("Not resizing, already at minimum size\n");
718 current->percent += ((double)ppt / 100.0);
719 LOG("current->percent after = %f\n", current->percent);
721 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
722 if (child == current)
724 child->percent -= subtract_percent;
725 LOG("child->percent after (%p) = %f\n", child, child->percent);
732 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
735 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
736 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
737 if (strcmp(way, "shrink") == 0) {
745 TAILQ_FOREACH(current, &owindows, owindows) {
746 /* Don't handle dock windows (issue #1201) */
747 if (current->con->window && current->con->window->dock) {
748 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
753 if ((floating_con = con_inside_floating(current->con))) {
754 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
756 if (strcmp(direction, "width") == 0 ||
757 strcmp(direction, "height") == 0) {
758 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
759 current->con, way, direction, resize_ppt))
762 if (!cmd_resize_tiling_direction(current_match, cmd_output,
763 current->con, way, direction, resize_ppt))
769 cmd_output->needs_tree_render = true;
770 // XXX: default reply for now, make this a better reply
775 * Implementation of 'resize set <px> [px] <px> [px]'.
778 void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
779 DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
780 if (cwidth <= 0 || cheight <= 0) {
781 ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
788 TAILQ_FOREACH(current, &owindows, owindows) {
790 if ((floating_con = con_inside_floating(current->con))) {
791 floating_resize(floating_con, cwidth, cheight);
793 ELOG("Resize failed: %p not a floating container\n", current->con);
797 cmd_output->needs_tree_render = true;
798 // XXX: default reply for now, make this a better reply
803 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
806 void cmd_border(I3_CMD, const char *border_style_str, const char *border_width) {
807 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
812 TAILQ_FOREACH(current, &owindows, owindows) {
813 DLOG("matching: %p / %s\n", current->con, current->con->name);
814 int border_style = current->con->border_style;
816 int tmp_border_width = -1;
817 tmp_border_width = strtol(border_width, &end, 10);
818 if (end == border_width) {
819 /* no valid digits found */
820 tmp_border_width = -1;
822 if (strcmp(border_style_str, "toggle") == 0) {
825 if (border_style == BS_NORMAL)
826 tmp_border_width = 2;
827 else if (border_style == BS_NONE)
828 tmp_border_width = 0;
829 else if (border_style == BS_PIXEL)
830 tmp_border_width = 1;
832 if (strcmp(border_style_str, "normal") == 0)
833 border_style = BS_NORMAL;
834 else if (strcmp(border_style_str, "pixel") == 0)
835 border_style = BS_PIXEL;
836 else if (strcmp(border_style_str, "1pixel") == 0) {
837 border_style = BS_PIXEL;
838 tmp_border_width = 1;
839 } else if (strcmp(border_style_str, "none") == 0)
840 border_style = BS_NONE;
842 ELOG("BUG: called with border_style=%s\n", border_style_str);
847 con_set_border_style(current->con, border_style, tmp_border_width);
850 cmd_output->needs_tree_render = true;
851 // XXX: default reply for now, make this a better reply
856 * Implementation of 'nop <comment>'.
859 void cmd_nop(I3_CMD, const char *comment) {
860 LOG("-------------------------------------------------\n");
861 LOG(" NOP: %s\n", comment);
862 LOG("-------------------------------------------------\n");
866 * Implementation of 'append_layout <path>'.
869 void cmd_append_layout(I3_CMD, const char *cpath) {
870 char *path = sstrdup(cpath);
871 LOG("Appending layout \"%s\"\n", path);
873 /* Make sure we allow paths like '~/.i3/layout.json' */
874 path = resolve_tilde(path);
876 json_content_t content = json_determine_content(path);
877 LOG("JSON content = %d\n", content);
878 if (content == JSON_CONTENT_UNKNOWN) {
879 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
880 yerror("Could not determine the contents of \"%s\".", path);
885 Con *parent = focused;
886 if (content == JSON_CONTENT_WORKSPACE) {
887 parent = output_get_content(con_get_output(parent));
889 /* We need to append the layout to a split container, since a leaf
890 * container must not have any children (by definition).
891 * Note that we explicitly check for workspaces, since they are okay for
892 * this purpose, but con_accepts_window() returns false for workspaces. */
893 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
894 parent = parent->parent;
896 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
897 char *errormsg = NULL;
898 tree_append_json(parent, path, &errormsg);
899 if (errormsg != NULL) {
902 /* Note that we continue executing since tree_append_json() has
903 * side-effects — user-provided layouts can be partly valid, partly
904 * invalid, leading to half of the placeholder containers being
910 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
911 // false); should be enough, but when sending 'workspace 4; append_layout
912 // /tmp/foo.json', the needs_tree_render == true of the workspace command
913 // is not executed yet and will be batched with append_layout’s
914 // needs_tree_render after the parser finished. We should check if that is
916 render_con(croot, false);
918 restore_open_placeholder_windows(parent);
920 if (content == JSON_CONTENT_WORKSPACE)
921 ipc_send_workspace_event("restored", parent, NULL);
924 cmd_output->needs_tree_render = true;
928 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
931 void cmd_workspace(I3_CMD, const char *which) {
934 DLOG("which=%s\n", which);
936 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
937 LOG("Cannot switch workspace while in global fullscreen\n");
942 if (strcmp(which, "next") == 0)
943 ws = workspace_next();
944 else if (strcmp(which, "prev") == 0)
945 ws = workspace_prev();
946 else if (strcmp(which, "next_on_output") == 0)
947 ws = workspace_next_on_output();
948 else if (strcmp(which, "prev_on_output") == 0)
949 ws = workspace_prev_on_output();
951 ELOG("BUG: called with which=%s\n", which);
958 cmd_output->needs_tree_render = true;
959 // XXX: default reply for now, make this a better reply
964 * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
967 void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
968 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
969 Con *output, *workspace = NULL;
971 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
972 LOG("Cannot switch workspace while in global fullscreen\n");
977 long parsed_num = ws_name_to_number(which);
979 if (parsed_num == -1) {
980 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
981 yerror("Could not parse number \"%s\"", which);
985 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
986 GREP_FIRST(workspace, output_get_content(output),
987 child->num == parsed_num);
990 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
992 workspace_show_by_name(which);
993 cmd_output->needs_tree_render = true;
996 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name))
998 workspace_show(workspace);
1000 cmd_output->needs_tree_render = true;
1001 // XXX: default reply for now, make this a better reply
1006 * Implementation of 'workspace back_and_forth'.
1009 void cmd_workspace_back_and_forth(I3_CMD) {
1010 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1011 LOG("Cannot switch workspace while in global fullscreen\n");
1016 workspace_back_and_forth();
1018 cmd_output->needs_tree_render = true;
1019 // XXX: default reply for now, make this a better reply
1024 * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
1027 void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
1028 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
1030 if (strncasecmp(name, "__", strlen("__")) == 0) {
1031 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1036 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1037 LOG("Cannot switch workspace while in global fullscreen\n");
1042 DLOG("should switch to workspace %s\n", name);
1043 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name))
1045 workspace_show_by_name(name);
1047 cmd_output->needs_tree_render = true;
1048 // XXX: default reply for now, make this a better reply
1053 * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
1056 void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
1059 owindow *current = TAILQ_FIRST(&owindows);
1060 if (current == NULL) {
1065 /* Marks must be unique, i.e., no two windows must have the same mark. */
1066 if (current != TAILQ_LAST(&owindows, owindows_head)) {
1067 yerror("A mark must not be put onto more than one window");
1071 DLOG("matching: %p / %s\n", current->con, current->con->name);
1073 mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
1074 if (toggle != NULL) {
1075 con_mark_toggle(current->con, mark, mark_mode);
1077 con_mark(current->con, mark, mark_mode);
1080 cmd_output->needs_tree_render = true;
1081 // XXX: default reply for now, make this a better reply
1086 * Implementation of 'unmark [mark]'
1089 void cmd_unmark(I3_CMD, const char *mark) {
1090 if (match_is_empty(current_match)) {
1091 con_unmark(NULL, mark);
1094 TAILQ_FOREACH(current, &owindows, owindows) {
1095 con_unmark(current->con, mark);
1099 cmd_output->needs_tree_render = true;
1100 // XXX: default reply for now, make this a better reply
1105 * Implementation of 'mode <string>'.
1108 void cmd_mode(I3_CMD, const char *mode) {
1109 DLOG("mode=%s\n", mode);
1112 // XXX: default reply for now, make this a better reply
1117 * Implementation of 'move [window|container] [to] output <str>'.
1120 void cmd_move_con_to_output(I3_CMD, const char *name) {
1121 DLOG("Should move window to output \"%s\".\n", name);
1125 bool had_error = false;
1126 TAILQ_FOREACH(current, &owindows, owindows) {
1127 DLOG("matching: %p / %s\n", current->con, current->con->name);
1129 Output *current_output = get_output_of_con(current->con);
1130 assert(current_output != NULL);
1132 Output *output = get_output_from_string(current_output, name);
1133 if (output == NULL) {
1134 ELOG("Could not find output \"%s\", skipping.\n", name);
1140 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1142 ELOG("Could not find a visible workspace on output %p.\n", output);
1147 con_move_to_workspace(current->con, ws, true, false, false);
1150 cmd_output->needs_tree_render = true;
1151 ysuccess(!had_error);
1155 * Implementation of 'move [container|window] [to] mark <str>'.
1158 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1159 DLOG("moving window to mark \"%s\"\n", mark);
1165 TAILQ_FOREACH(current, &owindows, owindows) {
1166 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1167 result &= con_move_to_mark(current->con, mark);
1170 cmd_output->needs_tree_render = true;
1175 * Implementation of 'floating enable|disable|toggle'
1178 void cmd_floating(I3_CMD, const char *floating_mode) {
1181 DLOG("floating_mode=%s\n", floating_mode);
1185 TAILQ_FOREACH(current, &owindows, owindows) {
1186 DLOG("matching: %p / %s\n", current->con, current->con->name);
1187 if (strcmp(floating_mode, "toggle") == 0) {
1188 DLOG("should toggle mode\n");
1189 toggle_floating_mode(current->con, false);
1191 DLOG("should switch mode to %s\n", floating_mode);
1192 if (strcmp(floating_mode, "enable") == 0) {
1193 floating_enable(current->con, false);
1195 floating_disable(current->con, false);
1200 cmd_output->needs_tree_render = true;
1201 // XXX: default reply for now, make this a better reply
1206 * Implementation of 'move workspace to [output] <str>'.
1209 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1210 DLOG("should move workspace to output %s\n", name);
1215 TAILQ_FOREACH(current, &owindows, owindows) {
1216 Con *ws = con_get_workspace(current->con);
1217 bool success = workspace_move_to_output(ws, name);
1219 ELOG("Failed to move workspace to output.\n");
1225 cmd_output->needs_tree_render = true;
1226 // XXX: default reply for now, make this a better reply
1231 * Implementation of 'split v|h|vertical|horizontal'.
1234 void cmd_split(I3_CMD, const char *direction) {
1238 LOG("splitting in direction %c\n", direction[0]);
1239 TAILQ_FOREACH(current, &owindows, owindows) {
1240 if (con_is_docked(current->con)) {
1241 ELOG("Cannot split a docked container, skipping.\n");
1245 DLOG("matching: %p / %s\n", current->con, current->con->name);
1246 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1249 cmd_output->needs_tree_render = true;
1250 // XXX: default reply for now, make this a better reply
1255 * Implementation of 'kill [window|client]'.
1258 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1259 if (kill_mode_str == NULL)
1260 kill_mode_str = "window";
1263 DLOG("kill_mode=%s\n", kill_mode_str);
1266 if (strcmp(kill_mode_str, "window") == 0)
1267 kill_mode = KILL_WINDOW;
1268 else if (strcmp(kill_mode_str, "client") == 0)
1269 kill_mode = KILL_CLIENT;
1271 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1276 HANDLE_INVALID_MATCH;
1278 /* check if the match is empty, not if the result is empty */
1279 if (match_is_empty(current_match))
1280 tree_close_con(kill_mode);
1282 TAILQ_FOREACH(current, &owindows, owindows) {
1283 DLOG("matching: %p / %s\n", current->con, current->con->name);
1284 tree_close(current->con, kill_mode, false, false);
1288 cmd_output->needs_tree_render = true;
1289 // XXX: default reply for now, make this a better reply
1294 * Implementation of 'exec [--no-startup-id] <command>'.
1297 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1298 bool no_startup_id = (nosn != NULL);
1300 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1301 start_application(command, no_startup_id);
1303 // XXX: default reply for now, make this a better reply
1308 * Implementation of 'focus left|right|up|down'.
1311 void cmd_focus_direction(I3_CMD, const char *direction) {
1312 DLOG("direction = *%s*\n", direction);
1314 if (strcmp(direction, "left") == 0)
1315 tree_next('p', HORIZ);
1316 else if (strcmp(direction, "right") == 0)
1317 tree_next('n', HORIZ);
1318 else if (strcmp(direction, "up") == 0)
1319 tree_next('p', VERT);
1320 else if (strcmp(direction, "down") == 0)
1321 tree_next('n', VERT);
1323 ELOG("Invalid focus direction (%s)\n", direction);
1328 cmd_output->needs_tree_render = true;
1329 // XXX: default reply for now, make this a better reply
1334 * Implementation of 'focus tiling|floating|mode_toggle'.
1337 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1338 DLOG("window_mode = %s\n", window_mode);
1340 Con *ws = con_get_workspace(focused);
1342 if (strcmp(window_mode, "mode_toggle") == 0) {
1343 if (con_inside_floating(focused))
1344 window_mode = "tiling";
1346 window_mode = "floating";
1349 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1350 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1351 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1354 con_focus(con_descend_focused(current));
1359 cmd_output->needs_tree_render = true;
1360 // XXX: default reply for now, make this a better reply
1365 * Implementation of 'focus parent|child'.
1368 void cmd_focus_level(I3_CMD, const char *level) {
1369 DLOG("level = %s\n", level);
1370 bool success = false;
1372 /* Focusing the parent can only be allowed if the newly
1373 * focused container won't escape the fullscreen container. */
1374 if (strcmp(level, "parent") == 0) {
1375 if (focused && focused->parent) {
1376 if (con_fullscreen_permits_focusing(focused->parent))
1377 success = level_up();
1379 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1383 /* Focusing a child should always be allowed. */
1385 success = level_down();
1387 cmd_output->needs_tree_render = success;
1388 // XXX: default reply for now, make this a better reply
1393 * Implementation of 'focus'.
1396 void cmd_focus(I3_CMD) {
1397 DLOG("current_match = %p\n", current_match);
1399 if (match_is_empty(current_match)) {
1400 ELOG("You have to specify which window/container should be focused.\n");
1401 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1403 yerror("You have to specify which window/container should be focused");
1408 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1411 TAILQ_FOREACH(current, &owindows, owindows) {
1412 Con *ws = con_get_workspace(current->con);
1413 /* If no workspace could be found, this was a dock window.
1414 * Just skip it, you cannot focus dock windows. */
1418 /* Check the fullscreen focus constraints. */
1419 if (!con_fullscreen_permits_focusing(current->con)) {
1420 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1425 /* In case this is a scratchpad window, call scratchpad_show(). */
1426 if (ws == __i3_scratch) {
1427 scratchpad_show(current->con);
1429 /* While for the normal focus case we can change focus multiple
1430 * times and only a single window ends up focused, we could show
1431 * multiple scratchpad windows. So, rather break here. */
1435 /* If the container is not on the current workspace,
1436 * workspace_show() will switch to a different workspace and (if
1437 * enabled) trigger a mouse pointer warp to the currently focused
1438 * container (!) on the target workspace.
1440 * Therefore, before calling workspace_show(), we make sure that
1441 * 'current' will be focused on the workspace. However, we cannot
1442 * just con_focus(current) because then the pointer will not be
1443 * warped at all (the code thinks we are already there).
1445 * So we focus 'current' to make it the currently focused window of
1446 * the target workspace, then revert focus. */
1447 Con *currently_focused = focused;
1448 con_focus(current->con);
1449 con_focus(currently_focused);
1451 /* Now switch to the workspace, then focus */
1453 LOG("focusing %p / %s\n", current->con, current->con->name);
1454 con_focus(current->con);
1459 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1460 "while only exactly one container can be focused at a time.\n",
1463 cmd_output->needs_tree_render = true;
1464 ysuccess(count > 0);
1468 * Implementation of 'fullscreen enable|toggle [global]' and
1469 * 'fullscreen disable'
1472 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1473 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1474 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1479 TAILQ_FOREACH(current, &owindows, owindows) {
1480 DLOG("matching: %p / %s\n", current->con, current->con->name);
1481 if (strcmp(action, "toggle") == 0) {
1482 con_toggle_fullscreen(current->con, mode);
1483 } else if (strcmp(action, "enable") == 0) {
1484 con_enable_fullscreen(current->con, mode);
1485 } else if (strcmp(action, "disable") == 0) {
1486 con_disable_fullscreen(current->con);
1490 cmd_output->needs_tree_render = true;
1491 // XXX: default reply for now, make this a better reply
1496 * Implementation of 'sticky enable|disable|toggle'.
1499 void cmd_sticky(I3_CMD, const char *action) {
1500 DLOG("%s sticky on window\n", action);
1504 TAILQ_FOREACH(current, &owindows, owindows) {
1505 if (current->con->window == NULL) {
1506 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1509 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1511 bool sticky = false;
1512 if (strcmp(action, "enable") == 0)
1514 else if (strcmp(action, "disable") == 0)
1516 else if (strcmp(action, "toggle") == 0)
1517 sticky = !current->con->sticky;
1519 current->con->sticky = sticky;
1520 ewmh_update_sticky(current->con->window->id, sticky);
1523 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1524 * sure it gets pushed to the front now. */
1525 output_push_sticky_windows(focused);
1527 cmd_output->needs_tree_render = true;
1532 * Implementation of 'move <direction> [<pixels> [px]]'.
1535 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1539 Con *initially_focused = focused;
1541 TAILQ_FOREACH(current, &owindows, owindows) {
1542 DLOG("moving in direction %s, px %ld\n", direction, move_px);
1543 if (con_is_floating(current->con)) {
1544 DLOG("floating move with %ld pixels\n", move_px);
1545 Rect newrect = current->con->parent->rect;
1546 if (strcmp(direction, "left") == 0) {
1547 newrect.x -= move_px;
1548 } else if (strcmp(direction, "right") == 0) {
1549 newrect.x += move_px;
1550 } else if (strcmp(direction, "up") == 0) {
1551 newrect.y -= move_px;
1552 } else if (strcmp(direction, "down") == 0) {
1553 newrect.y += move_px;
1555 floating_reposition(current->con->parent, newrect);
1557 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1558 cmd_output->needs_tree_render = true;
1562 /* the move command should not disturb focus */
1563 if (focused != initially_focused)
1564 con_focus(initially_focused);
1566 // XXX: default reply for now, make this a better reply
1571 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1574 void cmd_layout(I3_CMD, const char *layout_str) {
1577 if (strcmp(layout_str, "stacking") == 0)
1578 layout_str = "stacked";
1580 /* default is a special case which will be handled in con_set_layout(). */
1581 if (strcmp(layout_str, "default") == 0)
1583 else if (strcmp(layout_str, "stacked") == 0)
1585 else if (strcmp(layout_str, "tabbed") == 0)
1587 else if (strcmp(layout_str, "splitv") == 0)
1589 else if (strcmp(layout_str, "splith") == 0)
1592 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1596 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1599 TAILQ_FOREACH(current, &owindows, owindows) {
1600 if (con_is_docked(current->con)) {
1601 ELOG("cannot change layout of a docked container, skipping it.\n");
1605 DLOG("matching: %p / %s\n", current->con, current->con->name);
1606 con_set_layout(current->con, layout);
1609 cmd_output->needs_tree_render = true;
1610 // XXX: default reply for now, make this a better reply
1615 * Implementation of 'layout toggle [all|split]'.
1618 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1621 if (toggle_mode == NULL)
1622 toggle_mode = "default";
1624 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1626 /* check if the match is empty, not if the result is empty */
1627 if (match_is_empty(current_match))
1628 con_toggle_layout(focused, toggle_mode);
1630 TAILQ_FOREACH(current, &owindows, owindows) {
1631 DLOG("matching: %p / %s\n", current->con, current->con->name);
1632 con_toggle_layout(current->con, toggle_mode);
1636 cmd_output->needs_tree_render = true;
1637 // XXX: default reply for now, make this a better reply
1642 * Implementation of 'exit'.
1645 void cmd_exit(I3_CMD) {
1646 LOG("Exiting due to user command.\n");
1648 unlink(config.ipc_socket_path);
1649 xcb_disconnect(conn);
1656 * Implementation of 'reload'.
1659 void cmd_reload(I3_CMD) {
1661 kill_nagbar(&config_error_nagbar_pid, false);
1662 kill_nagbar(&command_error_nagbar_pid, false);
1663 load_configuration(conn, NULL, true);
1665 /* Send an IPC event just in case the ws names have changed */
1666 ipc_send_workspace_event("reload", NULL, NULL);
1667 /* Send an update event for the barconfig just in case it has changed */
1670 // XXX: default reply for now, make this a better reply
1675 * Implementation of 'restart'.
1678 void cmd_restart(I3_CMD) {
1679 LOG("restarting i3\n");
1681 unlink(config.ipc_socket_path);
1682 /* We need to call this manually since atexit handlers don’t get called
1684 purge_zerobyte_logfile();
1687 // XXX: default reply for now, make this a better reply
1692 * Implementation of 'open'.
1695 void cmd_open(I3_CMD) {
1696 LOG("opening new container\n");
1697 Con *con = tree_open_con(NULL, NULL);
1698 con->layout = L_SPLITH;
1705 y(integer, (long int)con);
1708 cmd_output->needs_tree_render = true;
1712 * Implementation of 'focus output <output>'.
1715 void cmd_focus_output(I3_CMD, const char *name) {
1718 DLOG("name = %s\n", name);
1722 /* get the output */
1723 Output *current_output = NULL;
1726 TAILQ_FOREACH(current, &owindows, owindows)
1727 current_output = get_output_of_con(current->con);
1728 assert(current_output != NULL);
1730 output = get_output_from_string(current_output, name);
1733 LOG("No such output found.\n");
1738 /* get visible workspace on output */
1740 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1748 cmd_output->needs_tree_render = true;
1749 // XXX: default reply for now, make this a better reply
1754 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1757 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1758 bool has_error = false;
1763 TAILQ_FOREACH(current, &owindows, owindows) {
1764 if (!con_is_floating(current->con)) {
1765 ELOG("Cannot change position. The window/container is not floating\n");
1768 yerror("Cannot change position of a window/container because it is not floating.");
1775 if (strcmp(method, "absolute") == 0) {
1776 current->con->parent->rect.x = x;
1777 current->con->parent->rect.y = y;
1779 DLOG("moving to absolute position %ld %ld\n", x, y);
1780 floating_maybe_reassign_ws(current->con->parent);
1781 cmd_output->needs_tree_render = true;
1784 if (strcmp(method, "position") == 0) {
1785 Rect newrect = current->con->parent->rect;
1787 DLOG("moving to position %ld %ld\n", x, y);
1791 floating_reposition(current->con->parent, newrect);
1795 // XXX: default reply for now, make this a better reply
1801 * Implementation of 'move [window|container] [to] [absolute] position center
1804 void cmd_move_window_to_center(I3_CMD, const char *method) {
1805 bool has_error = false;
1809 TAILQ_FOREACH(current, &owindows, owindows) {
1810 Con *floating_con = con_inside_floating(current->con);
1811 if (floating_con == NULL) {
1812 ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1813 current->con, current->con->name);
1816 yerror("Cannot change position of a window/container because it is not floating.");
1823 if (strcmp(method, "absolute") == 0) {
1824 DLOG("moving to absolute center\n");
1825 floating_center(floating_con, croot->rect);
1827 floating_maybe_reassign_ws(floating_con);
1828 cmd_output->needs_tree_render = true;
1831 if (strcmp(method, "position") == 0) {
1832 DLOG("moving to center\n");
1833 floating_center(floating_con, con_get_workspace(floating_con)->rect);
1835 cmd_output->needs_tree_render = true;
1839 // XXX: default reply for now, make this a better reply
1845 * Implementation of 'move [window|container] [to] position mouse'
1848 void cmd_move_window_to_mouse(I3_CMD) {
1852 TAILQ_FOREACH(current, &owindows, owindows) {
1853 Con *floating_con = con_inside_floating(current->con);
1854 if (floating_con == NULL) {
1855 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1856 current->con, current->con->name);
1860 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1861 floating_move_to_pointer(floating_con);
1864 cmd_output->needs_tree_render = true;
1869 * Implementation of 'move scratchpad'.
1872 void cmd_move_scratchpad(I3_CMD) {
1873 DLOG("should move window to scratchpad\n");
1878 TAILQ_FOREACH(current, &owindows, owindows) {
1879 DLOG("matching: %p / %s\n", current->con, current->con->name);
1880 scratchpad_move(current->con);
1883 cmd_output->needs_tree_render = true;
1884 // XXX: default reply for now, make this a better reply
1889 * Implementation of 'scratchpad show'.
1892 void cmd_scratchpad_show(I3_CMD) {
1893 DLOG("should show scratchpad window\n");
1896 if (match_is_empty(current_match)) {
1897 scratchpad_show(NULL);
1899 TAILQ_FOREACH(current, &owindows, owindows) {
1900 DLOG("matching: %p / %s\n", current->con, current->con->name);
1901 scratchpad_show(current->con);
1905 cmd_output->needs_tree_render = true;
1906 // XXX: default reply for now, make this a better reply
1911 * Implementation of 'title_format <format>'
1914 void cmd_title_format(I3_CMD, const char *format) {
1915 DLOG("setting title_format to \"%s\"\n", format);
1919 TAILQ_FOREACH(current, &owindows, owindows) {
1920 if (current->con->window == NULL)
1923 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1924 FREE(current->con->window->title_format);
1926 /* If we only display the title without anything else, we can skip the parsing step,
1927 * so we remove the title format altogether. */
1928 if (strcasecmp(format, "%title") != 0) {
1929 current->con->window->title_format = sstrdup(format);
1931 i3String *formatted_title = window_parse_title_format(current->con->window);
1932 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1933 I3STRING_FREE(formatted_title);
1935 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1936 ewmh_update_visible_name(current->con->window->id, NULL);
1939 /* Make sure the window title is redrawn immediately. */
1940 current->con->window->name_x_changed = true;
1943 cmd_output->needs_tree_render = true;
1948 * Implementation of 'rename workspace [<name>] to <name>'
1951 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1952 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1953 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1958 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1960 LOG("Renaming current workspace to \"%s\"\n", new_name);
1963 Con *output, *workspace = NULL;
1965 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1966 GREP_FIRST(workspace, output_get_content(output),
1967 !strcasecmp(child->name, old_name));
1969 workspace = con_get_workspace(focused);
1970 old_name = workspace->name;
1974 yerror("Old workspace \"%s\" not found", old_name);
1978 Con *check_dest = NULL;
1979 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1980 GREP_FIRST(check_dest, output_get_content(output),
1981 !strcasecmp(child->name, new_name));
1983 if (check_dest != NULL) {
1984 yerror("New workspace \"%s\" already exists", new_name);
1988 /* Change the name and try to parse it as a number. */
1989 FREE(workspace->name);
1990 workspace->name = sstrdup(new_name);
1992 workspace->num = ws_name_to_number(new_name);
1993 LOG("num = %d\n", workspace->num);
1995 /* By re-attaching, the sort order will be correct afterwards. */
1996 Con *previously_focused = focused;
1997 Con *parent = workspace->parent;
1998 con_detach(workspace);
1999 con_attach(workspace, parent, false);
2001 /* Move the workspace to the correct output if it has an assignment */
2002 struct Workspace_Assignment *assignment = NULL;
2003 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2004 if (assignment->output == NULL)
2006 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2010 workspace_move_to_output(workspace, assignment->output);
2012 if (previously_focused)
2013 workspace_show(con_get_workspace(previously_focused));
2018 /* Restore the previous focus since con_attach messes with the focus. */
2019 con_focus(previously_focused);
2021 cmd_output->needs_tree_render = true;
2024 ipc_send_workspace_event("rename", workspace, NULL);
2025 ewmh_update_desktop_names();
2026 ewmh_update_desktop_viewport();
2027 ewmh_update_current_desktop();
2029 startup_sequence_rename_workspace(old_name, new_name);
2033 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2036 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2038 bool toggle = false;
2039 if (strcmp(bar_mode, "dock") == 0)
2041 else if (strcmp(bar_mode, "hide") == 0)
2043 else if (strcmp(bar_mode, "invisible") == 0)
2045 else if (strcmp(bar_mode, "toggle") == 0)
2048 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2052 bool changed_sth = false;
2053 Barconfig *current = NULL;
2054 TAILQ_FOREACH(current, &barconfigs, configs) {
2055 if (bar_id && strcmp(current->id, bar_id) != 0)
2059 mode = (current->mode + 1) % 2;
2061 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2062 current->mode = mode;
2069 if (bar_id && !changed_sth) {
2070 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2078 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2081 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2082 int hidden_state = S_SHOW;
2083 bool toggle = false;
2084 if (strcmp(bar_hidden_state, "hide") == 0)
2085 hidden_state = S_HIDE;
2086 else if (strcmp(bar_hidden_state, "show") == 0)
2087 hidden_state = S_SHOW;
2088 else if (strcmp(bar_hidden_state, "toggle") == 0)
2091 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2095 bool changed_sth = false;
2096 Barconfig *current = NULL;
2097 TAILQ_FOREACH(current, &barconfigs, configs) {
2098 if (bar_id && strcmp(current->id, bar_id) != 0)
2102 hidden_state = (current->hidden_state + 1) % 2;
2104 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2105 current->hidden_state = hidden_state;
2112 if (bar_id && !changed_sth) {
2113 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2121 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2124 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2126 if (strcmp(bar_type, "mode") == 0)
2127 ret = cmd_bar_mode(bar_value, bar_id);
2128 else if (strcmp(bar_type, "hidden_state") == 0)
2129 ret = cmd_bar_hidden_state(bar_value, bar_id);
2131 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2143 * Implementation of 'shmlog <size>|toggle|on|off'
2146 void cmd_shmlog(I3_CMD, const char *argument) {
2147 if (!strcmp(argument, "toggle"))
2148 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2149 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2150 else if (!strcmp(argument, "on"))
2151 shmlog_size = default_shmlog_size;
2152 else if (!strcmp(argument, "off"))
2155 /* If shm logging now, restart logging with the new size. */
2156 if (shmlog_size > 0) {
2158 LOG("Restarting shm logging...\n");
2161 shmlog_size = atoi(argument);
2162 /* Make a weakly attempt at ensuring the argument is valid. */
2163 if (shmlog_size <= 0)
2164 shmlog_size = default_shmlog_size;
2166 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2168 update_shmlog_atom();
2169 // XXX: default reply for now, make this a better reply
2174 * Implementation of 'debuglog toggle|on|off'
2177 void cmd_debuglog(I3_CMD, const char *argument) {
2178 bool logging = get_debug_logging();
2179 if (!strcmp(argument, "toggle")) {
2180 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2181 set_debug_logging(!logging);
2182 } else if (!strcmp(argument, "on") && !logging) {
2183 LOG("Enabling debug logging\n");
2184 set_debug_logging(true);
2185 } else if (!strcmp(argument, "off") && logging) {
2186 LOG("Disabling debug logging\n");
2187 set_debug_logging(false);
2189 // XXX: default reply for now, make this a better reply