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 /** When the command did not include match criteria (!), we use the currently
46 * focused container. Do not confuse this case with a command which included
47 * criteria but which did not match any windows. This macro has to be called in
50 #define HANDLE_EMPTY_MATCH \
52 if (match_is_empty(current_match)) { \
53 owindow *ow = smalloc(sizeof(owindow)); \
55 TAILQ_INIT(&owindows); \
56 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
61 * Returns true if a is definitely greater than b (using the given epsilon)
64 static bool definitelyGreaterThan(float a, float b, float epsilon) {
65 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
69 * Returns the output containing the given container.
71 static Output *get_output_of_con(Con *con) {
72 Con *output_con = con_get_output(con);
73 Output *output = get_output_by_name(output_con->name);
74 assert(output != NULL);
80 * Checks whether we switched to a new workspace and returns false in that case,
81 * signaling that further workspace switching should be done by the calling function
82 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
83 * and return true, signaling that no further workspace switching should occur in the calling function.
86 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
87 Con *ws = con_get_workspace(focused);
89 /* If we switched to a different workspace, do nothing */
90 if (strcmp(ws->name, name) != 0)
93 DLOG("This workspace is already focused.\n");
94 if (config.workspace_auto_back_and_forth) {
95 workspace_back_and_forth();
96 cmd_output->needs_tree_render = true;
102 * Return the passed workspace unless it is the current one and auto back and
103 * forth is enabled, in which case the back_and_forth workspace is returned.
105 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
108 if (!config.workspace_auto_back_and_forth)
111 current = con_get_workspace(focused);
113 if (current == workspace) {
114 baf = workspace_back_and_forth_get();
116 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
124 // This code is commented out because we might recycle it for popping up error
125 // messages on parser errors.
127 static pid_t migration_pid = -1;
130 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
131 * it exited (or could not be started, depending on the exit code).
134 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
135 ev_child_stop(EV_A_ watcher);
136 if (!WIFEXITED(watcher->rstatus)) {
137 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
141 int exitcode = WEXITSTATUS(watcher->rstatus);
142 printf("i3-nagbar process exited with status %d\n", exitcode);
144 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151 * only makes sure that there are no i3-nagbar instances left behind) we still
152 * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
155 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
156 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
159 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
160 if (migration_pid != -1) {
161 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
162 kill(migration_pid, SIGKILL);
167 void cmd_MIGRATION_start_nagbar(void) {
168 if (migration_pid != -1) {
169 fprintf(stderr, "i3-nagbar already running.\n");
172 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
173 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
174 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
175 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
176 migration_pid = fork();
177 if (migration_pid == -1) {
178 warn("Could not fork()");
183 if (migration_pid == 0) {
185 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
187 NULL, /* will be replaced by the executable path */
191 "You found a parsing error. Please, please, please, report it!",
197 exec_i3_utility("i3-nagbar", argv);
201 /* install a child watcher */
202 ev_child *child = smalloc(sizeof(ev_child));
203 ev_child_init(child, &nagbar_exited, migration_pid, 0);
204 ev_child_start(main_loop, child);
206 /* We need ev >= 4 for the following code. Since it is not *that* important (it
207 * only makes sure that there are no i3-nagbar instances left behind) we still
208 * support old systems with libev 3. */
209 #if EV_VERSION_MAJOR >= 4
210 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
212 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
213 ev_cleanup_init(cleanup, nagbar_cleanup);
214 ev_cleanup_start(main_loop, cleanup);
220 /*******************************************************************************
221 * Criteria functions.
222 ******************************************************************************/
225 * Helper data structure for an operation window (window on which the operation
226 * will be performed). Used to build the TAILQ owindows.
229 typedef struct owindow {
231 TAILQ_ENTRY(owindow) owindows;
234 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
236 static owindows_head owindows;
239 * Initializes the specified 'Match' data structure and the initial state of
240 * commands.c for matching target windows of a command.
243 void cmd_criteria_init(I3_CMD) {
247 DLOG("Initializing criteria, current_match = %p\n", current_match);
248 match_free(current_match);
249 match_init(current_match);
250 while (!TAILQ_EMPTY(&owindows)) {
251 ow = TAILQ_FIRST(&owindows);
252 TAILQ_REMOVE(&owindows, ow, owindows);
255 TAILQ_INIT(&owindows);
257 TAILQ_FOREACH(con, &all_cons, all_cons) {
258 ow = smalloc(sizeof(owindow));
260 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
265 * A match specification just finished (the closing square bracket was found),
266 * so we filter the list of owindows.
269 void cmd_criteria_match_windows(I3_CMD) {
270 owindow *next, *current;
272 DLOG("match specification finished, matching...\n");
273 /* copy the old list head to iterate through it and start with a fresh
274 * list which will contain only matching windows */
275 struct owindows_head old = owindows;
276 TAILQ_INIT(&owindows);
277 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
278 /* make a copy of the next pointer and advance the pointer to the
279 * next element as we are going to invalidate the element’s
280 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
282 next = TAILQ_NEXT(next, owindows);
284 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
285 if (current_match->con_id != NULL) {
286 if (current_match->con_id == current->con) {
287 DLOG("matches container!\n");
288 TAILQ_INSERT_TAIL(&owindows, current, owindows);
290 DLOG("doesnt match\n");
293 } else if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
295 TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
296 if (!regex_matches(current_match->mark, mark->name))
299 DLOG("match by mark\n");
300 TAILQ_INSERT_TAIL(&owindows, current, owindows);
304 if (current->con->window && match_matches_window(current_match, current->con->window)) {
305 DLOG("matches window!\n");
306 TAILQ_INSERT_TAIL(&owindows, current, owindows);
308 DLOG("doesnt match\n");
314 TAILQ_FOREACH(current, &owindows, owindows) {
315 DLOG("matching: %p / %s\n", current->con, current->con->name);
320 * Interprets a ctype=cvalue pair and adds it to the current match
324 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
325 match_parse_property(current_match, ctype, cvalue);
329 * Implementation of 'move [window|container] [to] workspace
330 * next|prev|next_on_output|prev_on_output|current'.
333 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
336 DLOG("which=%s\n", which);
338 /* We have nothing to move:
339 * when criteria was specified but didn't match any window or
340 * when criteria wasn't specified and we don't have any window focused. */
341 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
342 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
343 !con_has_children(focused))) {
350 /* get the workspace */
352 if (strcmp(which, "next") == 0)
353 ws = workspace_next();
354 else if (strcmp(which, "prev") == 0)
355 ws = workspace_prev();
356 else if (strcmp(which, "next_on_output") == 0)
357 ws = workspace_next_on_output();
358 else if (strcmp(which, "prev_on_output") == 0)
359 ws = workspace_prev_on_output();
360 else if (strcmp(which, "current") == 0)
361 ws = con_get_workspace(focused);
363 ELOG("BUG: called with which=%s\n", which);
368 TAILQ_FOREACH(current, &owindows, owindows) {
369 DLOG("matching: %p / %s\n", current->con, current->con->name);
370 con_move_to_workspace(current->con, ws, true, false, false);
373 cmd_output->needs_tree_render = true;
374 // XXX: default reply for now, make this a better reply
379 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
382 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
386 ws = workspace_back_and_forth_get();
389 yerror("No workspace was previously active.");
395 TAILQ_FOREACH(current, &owindows, owindows) {
396 DLOG("matching: %p / %s\n", current->con, current->con->name);
397 con_move_to_workspace(current->con, ws, true, false, false);
400 cmd_output->needs_tree_render = true;
401 // XXX: default reply for now, make this a better reply
406 * Implementation of 'move [window|container] [to] workspace <name>'.
409 void cmd_move_con_to_workspace_name(I3_CMD, const char *name) {
410 if (strncasecmp(name, "__", strlen("__")) == 0) {
411 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
418 /* We have nothing to move:
419 * when criteria was specified but didn't match any window or
420 * when criteria wasn't specified and we don't have any window focused. */
421 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
422 ELOG("No windows match your criteria, cannot move.\n");
425 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
426 !con_has_children(focused)) {
431 LOG("should move window to workspace %s\n", name);
432 /* get the workspace */
433 Con *ws = workspace_get(name, NULL);
435 ws = maybe_auto_back_and_forth_workspace(ws);
439 TAILQ_FOREACH(current, &owindows, owindows) {
440 DLOG("matching: %p / %s\n", current->con, current->con->name);
441 con_move_to_workspace(current->con, ws, true, false, false);
444 cmd_output->needs_tree_render = true;
445 // XXX: default reply for now, make this a better reply
450 * Implementation of 'move [window|container] [to] workspace number <name>'.
453 void cmd_move_con_to_workspace_number(I3_CMD, const char *which) {
456 /* We have nothing to move:
457 * when criteria was specified but didn't match any window or
458 * when criteria wasn't specified and we don't have any window focused. */
459 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
460 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
461 !con_has_children(focused))) {
466 LOG("should move window to workspace %s\n", which);
467 /* get the workspace */
468 Con *output, *workspace = NULL;
470 long parsed_num = ws_name_to_number(which);
472 if (parsed_num == -1) {
473 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
474 yerror("Could not parse number \"%s\"", which);
478 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
479 GREP_FIRST(workspace, output_get_content(output),
480 child->num == parsed_num);
483 workspace = workspace_get(which, NULL);
486 workspace = maybe_auto_back_and_forth_workspace(workspace);
490 TAILQ_FOREACH(current, &owindows, owindows) {
491 DLOG("matching: %p / %s\n", current->con, current->con->name);
492 con_move_to_workspace(current->con, workspace, true, false, false);
495 cmd_output->needs_tree_render = true;
496 // XXX: default reply for now, make this a better reply
500 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) {
501 LOG("floating resize\n");
502 Rect old_rect = floating_con->rect;
503 Con *focused_con = con_descend_focused(floating_con);
505 /* ensure that resize will take place even if pixel increment is smaller than
506 * height increment or width increment.
508 const i3Window *window = focused_con->window;
509 if (window != NULL) {
510 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
511 strcmp(direction, "height") == 0) {
513 px = (-px < window->height_increment) ? -window->height_increment : px;
515 px = (px < window->height_increment) ? window->height_increment : px;
516 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
518 px = (-px < window->width_increment) ? -window->width_increment : px;
520 px = (px < window->width_increment) ? window->width_increment : px;
524 if (strcmp(direction, "up") == 0) {
525 floating_con->rect.height += px;
526 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
527 floating_con->rect.height += px;
528 } else if (strcmp(direction, "left") == 0) {
529 floating_con->rect.width += px;
531 floating_con->rect.width += px;
534 floating_check_size(floating_con);
536 /* Did we actually resize anything or did the size constraints prevent us?
537 * If we could not resize, exit now to not move the window. */
538 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
541 if (strcmp(direction, "up") == 0) {
542 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
543 } else if (strcmp(direction, "left") == 0) {
544 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
547 /* If this is a scratchpad window, don't auto center it from now on. */
548 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
549 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
552 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
553 LOG("tiling resize\n");
555 Con *first = current;
556 direction_t search_direction;
557 if (!strcmp(direction, "left"))
558 search_direction = D_LEFT;
559 else if (!strcmp(direction, "right"))
560 search_direction = D_RIGHT;
561 else if (!strcmp(direction, "up"))
562 search_direction = D_UP;
564 search_direction = D_DOWN;
566 bool res = resize_find_tiling_participants(&first, &second, search_direction);
568 LOG("No second container in this direction found.\n");
573 /* get the default percentage */
574 int children = con_num_children(first->parent);
575 LOG("ins. %d children\n", children);
576 double percentage = 1.0 / children;
577 LOG("default percentage = %f\n", percentage);
580 LOG("second->percent = %f\n", second->percent);
581 LOG("first->percent before = %f\n", first->percent);
582 if (first->percent == 0.0)
583 first->percent = percentage;
584 if (second->percent == 0.0)
585 second->percent = percentage;
586 double new_first_percent = first->percent + ((double)ppt / 100.0);
587 double new_second_percent = second->percent - ((double)ppt / 100.0);
588 LOG("new_first_percent = %f\n", new_first_percent);
589 LOG("new_second_percent = %f\n", new_second_percent);
590 /* Ensure that the new percentages are positive and greater than
591 * 0.05 to have a reasonable minimum size. */
592 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
593 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
594 first->percent += ((double)ppt / 100.0);
595 second->percent -= ((double)ppt / 100.0);
596 LOG("first->percent after = %f\n", first->percent);
597 LOG("second->percent after = %f\n", second->percent);
599 LOG("Not resizing, already at minimum size\n");
605 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
606 LOG("width/height resize\n");
607 /* get the appropriate current container (skip stacked/tabbed cons) */
608 while (current->parent->layout == L_STACKED ||
609 current->parent->layout == L_TABBED)
610 current = current->parent;
612 /* Then further go up until we find one with the matching orientation. */
613 orientation_t search_orientation =
614 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
616 while (current->type != CT_WORKSPACE &&
617 current->type != CT_FLOATING_CON &&
618 con_orientation(current->parent) != search_orientation)
619 current = current->parent;
621 /* get the default percentage */
622 int children = con_num_children(current->parent);
623 LOG("ins. %d children\n", children);
624 double percentage = 1.0 / children;
625 LOG("default percentage = %f\n", percentage);
627 orientation_t orientation = con_orientation(current->parent);
629 if ((orientation == HORIZ &&
630 strcmp(direction, "height") == 0) ||
631 (orientation == VERT &&
632 strcmp(direction, "width") == 0)) {
633 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
634 (orientation == HORIZ ? "horizontal" : "vertical"));
640 LOG("This is the only container, cannot resize.\n");
645 /* Ensure all the other children have a percentage set. */
647 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
648 LOG("child->percent = %f (child %p)\n", child->percent, child);
649 if (child->percent == 0.0)
650 child->percent = percentage;
653 double new_current_percent = current->percent + ((double)ppt / 100.0);
654 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
655 LOG("new_current_percent = %f\n", new_current_percent);
656 LOG("subtract_percent = %f\n", subtract_percent);
657 /* Ensure that the new percentages are positive and greater than
658 * 0.05 to have a reasonable minimum size. */
659 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
660 if (child == current)
662 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
663 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
668 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
669 LOG("Not resizing, already at minimum size\n");
674 current->percent += ((double)ppt / 100.0);
675 LOG("current->percent after = %f\n", current->percent);
677 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
678 if (child == current)
680 child->percent -= subtract_percent;
681 LOG("child->percent after (%p) = %f\n", child, child->percent);
688 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
691 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
692 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
693 if (strcmp(way, "shrink") == 0) {
701 TAILQ_FOREACH(current, &owindows, owindows) {
702 /* Don't handle dock windows (issue #1201) */
703 if (current->con->window && current->con->window->dock) {
704 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
709 if ((floating_con = con_inside_floating(current->con))) {
710 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
712 if (strcmp(direction, "width") == 0 ||
713 strcmp(direction, "height") == 0) {
714 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
715 current->con, way, direction, resize_ppt))
718 if (!cmd_resize_tiling_direction(current_match, cmd_output,
719 current->con, way, direction, resize_ppt))
725 cmd_output->needs_tree_render = true;
726 // XXX: default reply for now, make this a better reply
731 * Implementation of 'resize set <px> [px] <px> [px]'.
734 void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
735 DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
736 if (cwidth <= 0 || cheight <= 0) {
737 ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
744 TAILQ_FOREACH(current, &owindows, owindows) {
746 if ((floating_con = con_inside_floating(current->con))) {
747 floating_resize(floating_con, cwidth, cheight);
749 ELOG("Resize failed: %p not a floating container\n", current->con);
753 cmd_output->needs_tree_render = true;
754 // XXX: default reply for now, make this a better reply
759 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
762 void cmd_border(I3_CMD, const char *border_style_str, const char *border_width) {
763 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
768 TAILQ_FOREACH(current, &owindows, owindows) {
769 DLOG("matching: %p / %s\n", current->con, current->con->name);
770 int border_style = current->con->border_style;
772 int tmp_border_width = -1;
773 tmp_border_width = strtol(border_width, &end, 10);
774 if (end == border_width) {
775 /* no valid digits found */
776 tmp_border_width = -1;
778 if (strcmp(border_style_str, "toggle") == 0) {
781 if (border_style == BS_NORMAL)
782 tmp_border_width = 2;
783 else if (border_style == BS_NONE)
784 tmp_border_width = 0;
785 else if (border_style == BS_PIXEL)
786 tmp_border_width = 1;
788 if (strcmp(border_style_str, "normal") == 0)
789 border_style = BS_NORMAL;
790 else if (strcmp(border_style_str, "pixel") == 0)
791 border_style = BS_PIXEL;
792 else if (strcmp(border_style_str, "1pixel") == 0) {
793 border_style = BS_PIXEL;
794 tmp_border_width = 1;
795 } else if (strcmp(border_style_str, "none") == 0)
796 border_style = BS_NONE;
798 ELOG("BUG: called with border_style=%s\n", border_style_str);
803 con_set_border_style(current->con, border_style, tmp_border_width);
806 cmd_output->needs_tree_render = true;
807 // XXX: default reply for now, make this a better reply
812 * Implementation of 'nop <comment>'.
815 void cmd_nop(I3_CMD, const char *comment) {
816 LOG("-------------------------------------------------\n");
817 LOG(" NOP: %s\n", comment);
818 LOG("-------------------------------------------------\n");
822 * Implementation of 'append_layout <path>'.
825 void cmd_append_layout(I3_CMD, const char *cpath) {
826 char *path = sstrdup(cpath);
827 LOG("Appending layout \"%s\"\n", path);
829 /* Make sure we allow paths like '~/.i3/layout.json' */
830 path = resolve_tilde(path);
832 json_content_t content = json_determine_content(path);
833 LOG("JSON content = %d\n", content);
834 if (content == JSON_CONTENT_UNKNOWN) {
835 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
836 yerror("Could not determine the contents of \"%s\".", path);
841 Con *parent = focused;
842 if (content == JSON_CONTENT_WORKSPACE) {
843 parent = output_get_content(con_get_output(parent));
845 /* We need to append the layout to a split container, since a leaf
846 * container must not have any children (by definition).
847 * Note that we explicitly check for workspaces, since they are okay for
848 * this purpose, but con_accepts_window() returns false for workspaces. */
849 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
850 parent = parent->parent;
852 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
853 char *errormsg = NULL;
854 tree_append_json(parent, path, &errormsg);
855 if (errormsg != NULL) {
858 /* Note that we continue executing since tree_append_json() has
859 * side-effects — user-provided layouts can be partly valid, partly
860 * invalid, leading to half of the placeholder containers being
866 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
867 // false); should be enough, but when sending 'workspace 4; append_layout
868 // /tmp/foo.json', the needs_tree_render == true of the workspace command
869 // is not executed yet and will be batched with append_layout’s
870 // needs_tree_render after the parser finished. We should check if that is
872 render_con(croot, false);
874 restore_open_placeholder_windows(parent);
876 if (content == JSON_CONTENT_WORKSPACE)
877 ipc_send_workspace_event("restored", parent, NULL);
880 cmd_output->needs_tree_render = true;
884 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
887 void cmd_workspace(I3_CMD, const char *which) {
890 DLOG("which=%s\n", which);
892 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
893 LOG("Cannot switch workspace while in global fullscreen\n");
898 if (strcmp(which, "next") == 0)
899 ws = workspace_next();
900 else if (strcmp(which, "prev") == 0)
901 ws = workspace_prev();
902 else if (strcmp(which, "next_on_output") == 0)
903 ws = workspace_next_on_output();
904 else if (strcmp(which, "prev_on_output") == 0)
905 ws = workspace_prev_on_output();
907 ELOG("BUG: called with which=%s\n", which);
914 cmd_output->needs_tree_render = true;
915 // XXX: default reply for now, make this a better reply
920 * Implementation of 'workspace number <name>'
923 void cmd_workspace_number(I3_CMD, const char *which) {
924 Con *output, *workspace = NULL;
926 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
927 LOG("Cannot switch workspace while in global fullscreen\n");
932 long parsed_num = ws_name_to_number(which);
934 if (parsed_num == -1) {
935 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
936 yerror("Could not parse number \"%s\"", which);
940 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
941 GREP_FIRST(workspace, output_get_content(output),
942 child->num == parsed_num);
945 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
947 workspace_show_by_name(which);
948 cmd_output->needs_tree_render = true;
951 if (maybe_back_and_forth(cmd_output, workspace->name))
953 workspace_show(workspace);
955 cmd_output->needs_tree_render = true;
956 // XXX: default reply for now, make this a better reply
961 * Implementation of 'workspace back_and_forth'.
964 void cmd_workspace_back_and_forth(I3_CMD) {
965 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
966 LOG("Cannot switch workspace while in global fullscreen\n");
971 workspace_back_and_forth();
973 cmd_output->needs_tree_render = true;
974 // XXX: default reply for now, make this a better reply
979 * Implementation of 'workspace <name>'
982 void cmd_workspace_name(I3_CMD, const char *name) {
983 if (strncasecmp(name, "__", strlen("__")) == 0) {
984 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
989 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
990 LOG("Cannot switch workspace while in global fullscreen\n");
995 DLOG("should switch to workspace %s\n", name);
996 if (maybe_back_and_forth(cmd_output, name))
998 workspace_show_by_name(name);
1000 cmd_output->needs_tree_render = true;
1001 // XXX: default reply for now, make this a better reply
1006 * Implementation of 'mark [--toggle] <mark>'
1009 void cmd_mark(I3_CMD, const char *mark, const char *toggle) {
1012 owindow *current = TAILQ_FIRST(&owindows);
1013 if (current == NULL) {
1018 /* Marks must be unique, i.e., no two windows must have the same mark. */
1019 if (current != TAILQ_LAST(&owindows, owindows_head)) {
1020 yerror("A mark must not be put onto more than one window");
1024 DLOG("matching: %p / %s\n", current->con, current->con->name);
1025 if (toggle != NULL) {
1026 con_mark_toggle(current->con, mark);
1028 con_mark(current->con, mark);
1031 cmd_output->needs_tree_render = true;
1032 // XXX: default reply for now, make this a better reply
1037 * Implementation of 'unmark [mark]'
1040 void cmd_unmark(I3_CMD, const char *mark) {
1043 cmd_output->needs_tree_render = true;
1044 // XXX: default reply for now, make this a better reply
1049 * Implementation of 'mode <string>'.
1052 void cmd_mode(I3_CMD, const char *mode) {
1053 DLOG("mode=%s\n", mode);
1056 // XXX: default reply for now, make this a better reply
1061 * Implementation of 'move [window|container] [to] output <str>'.
1064 void cmd_move_con_to_output(I3_CMD, const char *name) {
1065 DLOG("Should move window to output \"%s\".\n", name);
1069 bool had_error = false;
1070 TAILQ_FOREACH(current, &owindows, owindows) {
1071 DLOG("matching: %p / %s\n", current->con, current->con->name);
1073 Output *current_output = get_output_of_con(current->con);
1074 assert(current_output != NULL);
1076 Output *output = get_output_from_string(current_output, name);
1077 if (output == NULL) {
1078 ELOG("Could not find output \"%s\", skipping.\n", name);
1084 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1086 ELOG("Could not find a visible workspace on output %p.\n", output);
1091 con_move_to_workspace(current->con, ws, true, false, false);
1094 cmd_output->needs_tree_render = true;
1095 ysuccess(!had_error);
1099 * Implementation of 'move [container|window] [to] mark <str>'.
1102 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1103 DLOG("moving window to mark \"%s\"\n", mark);
1109 TAILQ_FOREACH(current, &owindows, owindows) {
1110 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1111 result &= con_move_to_mark(current->con, mark);
1114 cmd_output->needs_tree_render = true;
1119 * Implementation of 'floating enable|disable|toggle'
1122 void cmd_floating(I3_CMD, const char *floating_mode) {
1125 DLOG("floating_mode=%s\n", floating_mode);
1129 TAILQ_FOREACH(current, &owindows, owindows) {
1130 DLOG("matching: %p / %s\n", current->con, current->con->name);
1131 if (strcmp(floating_mode, "toggle") == 0) {
1132 DLOG("should toggle mode\n");
1133 toggle_floating_mode(current->con, false);
1135 DLOG("should switch mode to %s\n", floating_mode);
1136 if (strcmp(floating_mode, "enable") == 0) {
1137 floating_enable(current->con, false);
1139 floating_disable(current->con, false);
1144 cmd_output->needs_tree_render = true;
1145 // XXX: default reply for now, make this a better reply
1150 * Implementation of 'move workspace to [output] <str>'.
1153 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1154 DLOG("should move workspace to output %s\n", name);
1159 TAILQ_FOREACH(current, &owindows, owindows) {
1160 Con *ws = con_get_workspace(current->con);
1161 bool success = workspace_move_to_output(ws, name);
1163 ELOG("Failed to move workspace to output.\n");
1169 cmd_output->needs_tree_render = true;
1170 // XXX: default reply for now, make this a better reply
1175 * Implementation of 'split v|h|vertical|horizontal'.
1178 void cmd_split(I3_CMD, const char *direction) {
1180 /* TODO: use matches */
1181 LOG("splitting in direction %c\n", direction[0]);
1182 if (match_is_empty(current_match))
1183 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1185 TAILQ_FOREACH(current, &owindows, owindows) {
1186 DLOG("matching: %p / %s\n", current->con, current->con->name);
1187 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1191 cmd_output->needs_tree_render = true;
1192 // XXX: default reply for now, make this a better reply
1197 * Implementation of 'kill [window|client]'.
1200 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1201 if (kill_mode_str == NULL)
1202 kill_mode_str = "window";
1205 DLOG("kill_mode=%s\n", kill_mode_str);
1208 if (strcmp(kill_mode_str, "window") == 0)
1209 kill_mode = KILL_WINDOW;
1210 else if (strcmp(kill_mode_str, "client") == 0)
1211 kill_mode = KILL_CLIENT;
1213 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1218 /* check if the match is empty, not if the result is empty */
1219 if (match_is_empty(current_match))
1220 tree_close_con(kill_mode);
1222 TAILQ_FOREACH(current, &owindows, owindows) {
1223 DLOG("matching: %p / %s\n", current->con, current->con->name);
1224 tree_close(current->con, kill_mode, false, false);
1228 cmd_output->needs_tree_render = true;
1229 // XXX: default reply for now, make this a better reply
1234 * Implementation of 'exec [--no-startup-id] <command>'.
1237 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1238 bool no_startup_id = (nosn != NULL);
1240 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1241 start_application(command, no_startup_id);
1243 // XXX: default reply for now, make this a better reply
1248 * Implementation of 'focus left|right|up|down'.
1251 void cmd_focus_direction(I3_CMD, const char *direction) {
1252 DLOG("direction = *%s*\n", direction);
1254 if (strcmp(direction, "left") == 0)
1255 tree_next('p', HORIZ);
1256 else if (strcmp(direction, "right") == 0)
1257 tree_next('n', HORIZ);
1258 else if (strcmp(direction, "up") == 0)
1259 tree_next('p', VERT);
1260 else if (strcmp(direction, "down") == 0)
1261 tree_next('n', VERT);
1263 ELOG("Invalid focus direction (%s)\n", direction);
1268 cmd_output->needs_tree_render = true;
1269 // XXX: default reply for now, make this a better reply
1274 * Implementation of 'focus tiling|floating|mode_toggle'.
1277 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1278 DLOG("window_mode = %s\n", window_mode);
1280 Con *ws = con_get_workspace(focused);
1282 if (strcmp(window_mode, "mode_toggle") == 0) {
1283 if (con_inside_floating(focused))
1284 window_mode = "tiling";
1286 window_mode = "floating";
1289 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1290 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1291 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1294 con_focus(con_descend_focused(current));
1299 cmd_output->needs_tree_render = true;
1300 // XXX: default reply for now, make this a better reply
1305 * Implementation of 'focus parent|child'.
1308 void cmd_focus_level(I3_CMD, const char *level) {
1309 DLOG("level = %s\n", level);
1310 bool success = false;
1312 /* Focusing the parent can only be allowed if the newly
1313 * focused container won't escape the fullscreen container. */
1314 if (strcmp(level, "parent") == 0) {
1315 if (focused && focused->parent) {
1316 if (con_fullscreen_permits_focusing(focused->parent))
1317 success = level_up();
1319 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1323 /* Focusing a child should always be allowed. */
1325 success = level_down();
1327 cmd_output->needs_tree_render = success;
1328 // XXX: default reply for now, make this a better reply
1333 * Implementation of 'focus'.
1336 void cmd_focus(I3_CMD) {
1337 DLOG("current_match = %p\n", current_match);
1339 if (match_is_empty(current_match)) {
1340 ELOG("You have to specify which window/container should be focused.\n");
1341 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1343 yerror("You have to specify which window/container should be focused");
1348 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1351 TAILQ_FOREACH(current, &owindows, owindows) {
1352 Con *ws = con_get_workspace(current->con);
1353 /* If no workspace could be found, this was a dock window.
1354 * Just skip it, you cannot focus dock windows. */
1358 /* Check the fullscreen focus constraints. */
1359 if (!con_fullscreen_permits_focusing(current->con)) {
1360 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1365 /* In case this is a scratchpad window, call scratchpad_show(). */
1366 if (ws == __i3_scratch) {
1367 scratchpad_show(current->con);
1369 /* While for the normal focus case we can change focus multiple
1370 * times and only a single window ends up focused, we could show
1371 * multiple scratchpad windows. So, rather break here. */
1375 /* If the container is not on the current workspace,
1376 * workspace_show() will switch to a different workspace and (if
1377 * enabled) trigger a mouse pointer warp to the currently focused
1378 * container (!) on the target workspace.
1380 * Therefore, before calling workspace_show(), we make sure that
1381 * 'current' will be focused on the workspace. However, we cannot
1382 * just con_focus(current) because then the pointer will not be
1383 * warped at all (the code thinks we are already there).
1385 * So we focus 'current' to make it the currently focused window of
1386 * the target workspace, then revert focus. */
1387 Con *currently_focused = focused;
1388 con_focus(current->con);
1389 con_focus(currently_focused);
1391 /* Now switch to the workspace, then focus */
1393 LOG("focusing %p / %s\n", current->con, current->con->name);
1394 con_focus(current->con);
1399 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1400 "while only exactly one container can be focused at a time.\n",
1403 cmd_output->needs_tree_render = true;
1404 ysuccess(count > 0);
1408 * Implementation of 'fullscreen enable|toggle [global]' and
1409 * 'fullscreen disable'
1412 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1413 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1414 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1419 TAILQ_FOREACH(current, &owindows, owindows) {
1420 DLOG("matching: %p / %s\n", current->con, current->con->name);
1421 if (strcmp(action, "toggle") == 0) {
1422 con_toggle_fullscreen(current->con, mode);
1423 } else if (strcmp(action, "enable") == 0) {
1424 con_enable_fullscreen(current->con, mode);
1425 } else if (strcmp(action, "disable") == 0) {
1426 con_disable_fullscreen(current->con);
1430 cmd_output->needs_tree_render = true;
1431 // XXX: default reply for now, make this a better reply
1436 * Implementation of 'sticky enable|disable|toggle'.
1439 void cmd_sticky(I3_CMD, const char *action) {
1440 DLOG("%s sticky on window\n", action);
1444 TAILQ_FOREACH(current, &owindows, owindows) {
1445 if (current->con->window == NULL) {
1446 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1449 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1451 bool sticky = false;
1452 if (strcmp(action, "enable") == 0)
1454 else if (strcmp(action, "disable") == 0)
1456 else if (strcmp(action, "toggle") == 0)
1457 sticky = !current->con->sticky;
1459 current->con->sticky = sticky;
1460 ewmh_update_sticky(current->con->window->id, sticky);
1463 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1464 * sure it gets pushed to the front now. */
1465 output_push_sticky_windows(focused);
1467 cmd_output->needs_tree_render = true;
1472 * Implementation of 'move <direction> [<pixels> [px]]'.
1475 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1479 Con *initially_focused = focused;
1481 TAILQ_FOREACH(current, &owindows, owindows) {
1482 DLOG("moving in direction %s, px %ld\n", direction, move_px);
1483 if (con_is_floating(current->con)) {
1484 DLOG("floating move with %ld pixels\n", move_px);
1485 Rect newrect = current->con->parent->rect;
1486 if (strcmp(direction, "left") == 0) {
1487 newrect.x -= move_px;
1488 } else if (strcmp(direction, "right") == 0) {
1489 newrect.x += move_px;
1490 } else if (strcmp(direction, "up") == 0) {
1491 newrect.y -= move_px;
1492 } else if (strcmp(direction, "down") == 0) {
1493 newrect.y += move_px;
1495 floating_reposition(current->con->parent, newrect);
1497 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1498 cmd_output->needs_tree_render = true;
1502 /* the move command should not disturb focus */
1503 if (focused != initially_focused)
1504 con_focus(initially_focused);
1506 // XXX: default reply for now, make this a better reply
1511 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1514 void cmd_layout(I3_CMD, const char *layout_str) {
1515 if (strcmp(layout_str, "stacking") == 0)
1516 layout_str = "stacked";
1519 /* default is a special case which will be handled in con_set_layout(). */
1520 if (strcmp(layout_str, "default") == 0)
1522 else if (strcmp(layout_str, "stacked") == 0)
1524 else if (strcmp(layout_str, "tabbed") == 0)
1526 else if (strcmp(layout_str, "splitv") == 0)
1528 else if (strcmp(layout_str, "splith") == 0)
1531 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1535 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1537 /* check if the match is empty, not if the result is empty */
1538 if (match_is_empty(current_match))
1539 con_set_layout(focused, layout);
1541 TAILQ_FOREACH(current, &owindows, owindows) {
1542 DLOG("matching: %p / %s\n", current->con, current->con->name);
1543 con_set_layout(current->con, layout);
1547 cmd_output->needs_tree_render = true;
1548 // XXX: default reply for now, make this a better reply
1553 * Implementation of 'layout toggle [all|split]'.
1556 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1559 if (toggle_mode == NULL)
1560 toggle_mode = "default";
1562 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1564 /* check if the match is empty, not if the result is empty */
1565 if (match_is_empty(current_match))
1566 con_toggle_layout(focused, toggle_mode);
1568 TAILQ_FOREACH(current, &owindows, owindows) {
1569 DLOG("matching: %p / %s\n", current->con, current->con->name);
1570 con_toggle_layout(current->con, toggle_mode);
1574 cmd_output->needs_tree_render = true;
1575 // XXX: default reply for now, make this a better reply
1580 * Implementation of 'exit'.
1583 void cmd_exit(I3_CMD) {
1584 LOG("Exiting due to user command.\n");
1586 unlink(config.ipc_socket_path);
1587 xcb_disconnect(conn);
1594 * Implementation of 'reload'.
1597 void cmd_reload(I3_CMD) {
1599 kill_nagbar(&config_error_nagbar_pid, false);
1600 kill_nagbar(&command_error_nagbar_pid, false);
1601 load_configuration(conn, NULL, true);
1603 /* Send an IPC event just in case the ws names have changed */
1604 ipc_send_workspace_event("reload", NULL, NULL);
1605 /* Send an update event for the barconfig just in case it has changed */
1608 // XXX: default reply for now, make this a better reply
1613 * Implementation of 'restart'.
1616 void cmd_restart(I3_CMD) {
1617 LOG("restarting i3\n");
1619 unlink(config.ipc_socket_path);
1620 /* We need to call this manually since atexit handlers don’t get called
1622 purge_zerobyte_logfile();
1625 // XXX: default reply for now, make this a better reply
1630 * Implementation of 'open'.
1633 void cmd_open(I3_CMD) {
1634 LOG("opening new container\n");
1635 Con *con = tree_open_con(NULL, NULL);
1636 con->layout = L_SPLITH;
1643 y(integer, (long int)con);
1646 cmd_output->needs_tree_render = true;
1650 * Implementation of 'focus output <output>'.
1653 void cmd_focus_output(I3_CMD, const char *name) {
1656 DLOG("name = %s\n", name);
1660 /* get the output */
1661 Output *current_output = NULL;
1664 TAILQ_FOREACH(current, &owindows, owindows)
1665 current_output = get_output_of_con(current->con);
1666 assert(current_output != NULL);
1668 output = get_output_from_string(current_output, name);
1671 LOG("No such output found.\n");
1676 /* get visible workspace on output */
1678 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1686 cmd_output->needs_tree_render = true;
1687 // XXX: default reply for now, make this a better reply
1692 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1695 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1696 bool has_error = false;
1701 TAILQ_FOREACH(current, &owindows, owindows) {
1702 if (!con_is_floating(current->con)) {
1703 ELOG("Cannot change position. The window/container is not floating\n");
1706 yerror("Cannot change position of a window/container because it is not floating.");
1713 if (strcmp(method, "absolute") == 0) {
1714 current->con->parent->rect.x = x;
1715 current->con->parent->rect.y = y;
1717 DLOG("moving to absolute position %ld %ld\n", x, y);
1718 floating_maybe_reassign_ws(current->con->parent);
1719 cmd_output->needs_tree_render = true;
1722 if (strcmp(method, "position") == 0) {
1723 Rect newrect = current->con->parent->rect;
1725 DLOG("moving to position %ld %ld\n", x, y);
1729 floating_reposition(current->con->parent, newrect);
1733 // XXX: default reply for now, make this a better reply
1739 * Implementation of 'move [window|container] [to] [absolute] position center
1742 void cmd_move_window_to_center(I3_CMD, const char *method) {
1743 if (!con_is_floating(focused)) {
1744 ELOG("Cannot change position. The window/container is not floating\n");
1745 yerror("Cannot change position. The window/container is not floating.");
1749 if (strcmp(method, "absolute") == 0) {
1750 DLOG("moving to absolute center\n");
1751 floating_center(focused->parent, croot->rect);
1753 floating_maybe_reassign_ws(focused->parent);
1754 cmd_output->needs_tree_render = true;
1757 if (strcmp(method, "position") == 0) {
1758 DLOG("moving to center\n");
1759 floating_center(focused->parent, con_get_workspace(focused)->rect);
1761 cmd_output->needs_tree_render = true;
1764 // XXX: default reply for now, make this a better reply
1769 * Implementation of 'move [window|container] [to] position mouse'
1772 void cmd_move_window_to_mouse(I3_CMD) {
1776 TAILQ_FOREACH(current, &owindows, owindows) {
1777 Con *floating_con = con_inside_floating(current->con);
1778 if (floating_con == NULL) {
1779 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1780 current->con, current->con->name);
1784 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1785 floating_move_to_pointer(floating_con);
1788 cmd_output->needs_tree_render = true;
1793 * Implementation of 'move scratchpad'.
1796 void cmd_move_scratchpad(I3_CMD) {
1797 DLOG("should move window to scratchpad\n");
1802 TAILQ_FOREACH(current, &owindows, owindows) {
1803 DLOG("matching: %p / %s\n", current->con, current->con->name);
1804 scratchpad_move(current->con);
1807 cmd_output->needs_tree_render = true;
1808 // XXX: default reply for now, make this a better reply
1813 * Implementation of 'scratchpad show'.
1816 void cmd_scratchpad_show(I3_CMD) {
1817 DLOG("should show scratchpad window\n");
1820 if (match_is_empty(current_match)) {
1821 scratchpad_show(NULL);
1823 TAILQ_FOREACH(current, &owindows, owindows) {
1824 DLOG("matching: %p / %s\n", current->con, current->con->name);
1825 scratchpad_show(current->con);
1829 cmd_output->needs_tree_render = true;
1830 // XXX: default reply for now, make this a better reply
1835 * Implementation of 'title_format <format>'
1838 void cmd_title_format(I3_CMD, const char *format) {
1839 DLOG("setting title_format to \"%s\"\n", format);
1843 TAILQ_FOREACH(current, &owindows, owindows) {
1844 if (current->con->window == NULL)
1847 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1848 FREE(current->con->window->title_format);
1850 /* If we only display the title without anything else, we can skip the parsing step,
1851 * so we remove the title format altogether. */
1852 if (strcasecmp(format, "%title") != 0) {
1853 current->con->window->title_format = sstrdup(format);
1855 i3String *formatted_title = window_parse_title_format(current->con->window);
1856 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1857 I3STRING_FREE(formatted_title);
1859 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1860 ewmh_update_visible_name(current->con->window->id, NULL);
1863 /* Make sure the window title is redrawn immediately. */
1864 current->con->window->name_x_changed = true;
1867 cmd_output->needs_tree_render = true;
1872 * Implementation of 'rename workspace [<name>] to <name>'
1875 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1876 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1877 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1882 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1884 LOG("Renaming current workspace to \"%s\"\n", new_name);
1887 Con *output, *workspace = NULL;
1889 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1890 GREP_FIRST(workspace, output_get_content(output),
1891 !strcasecmp(child->name, old_name));
1893 workspace = con_get_workspace(focused);
1894 old_name = workspace->name;
1898 yerror("Old workspace \"%s\" not found", old_name);
1902 Con *check_dest = NULL;
1903 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1904 GREP_FIRST(check_dest, output_get_content(output),
1905 !strcasecmp(child->name, new_name));
1907 if (check_dest != NULL) {
1908 yerror("New workspace \"%s\" already exists", new_name);
1912 /* Change the name and try to parse it as a number. */
1913 FREE(workspace->name);
1914 workspace->name = sstrdup(new_name);
1916 workspace->num = ws_name_to_number(new_name);
1917 LOG("num = %d\n", workspace->num);
1919 /* By re-attaching, the sort order will be correct afterwards. */
1920 Con *previously_focused = focused;
1921 Con *parent = workspace->parent;
1922 con_detach(workspace);
1923 con_attach(workspace, parent, false);
1925 /* Move the workspace to the correct output if it has an assignment */
1926 struct Workspace_Assignment *assignment = NULL;
1927 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1928 if (assignment->output == NULL)
1930 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1934 workspace_move_to_output(workspace, assignment->output);
1936 if (previously_focused)
1937 workspace_show(con_get_workspace(previously_focused));
1942 /* Restore the previous focus since con_attach messes with the focus. */
1943 con_focus(previously_focused);
1945 cmd_output->needs_tree_render = true;
1948 ipc_send_workspace_event("rename", workspace, NULL);
1949 ewmh_update_desktop_names();
1950 ewmh_update_desktop_viewport();
1951 ewmh_update_current_desktop();
1953 startup_sequence_rename_workspace(old_name, new_name);
1957 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1960 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
1962 bool toggle = false;
1963 if (strcmp(bar_mode, "dock") == 0)
1965 else if (strcmp(bar_mode, "hide") == 0)
1967 else if (strcmp(bar_mode, "invisible") == 0)
1969 else if (strcmp(bar_mode, "toggle") == 0)
1972 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1976 bool changed_sth = false;
1977 Barconfig *current = NULL;
1978 TAILQ_FOREACH(current, &barconfigs, configs) {
1979 if (bar_id && strcmp(current->id, bar_id) != 0)
1983 mode = (current->mode + 1) % 2;
1985 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1986 current->mode = mode;
1993 if (bar_id && !changed_sth) {
1994 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2002 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2005 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2006 int hidden_state = S_SHOW;
2007 bool toggle = false;
2008 if (strcmp(bar_hidden_state, "hide") == 0)
2009 hidden_state = S_HIDE;
2010 else if (strcmp(bar_hidden_state, "show") == 0)
2011 hidden_state = S_SHOW;
2012 else if (strcmp(bar_hidden_state, "toggle") == 0)
2015 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2019 bool changed_sth = false;
2020 Barconfig *current = NULL;
2021 TAILQ_FOREACH(current, &barconfigs, configs) {
2022 if (bar_id && strcmp(current->id, bar_id) != 0)
2026 hidden_state = (current->hidden_state + 1) % 2;
2028 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2029 current->hidden_state = hidden_state;
2036 if (bar_id && !changed_sth) {
2037 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2045 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2048 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2050 if (strcmp(bar_type, "mode") == 0)
2051 ret = cmd_bar_mode(bar_value, bar_id);
2052 else if (strcmp(bar_type, "hidden_state") == 0)
2053 ret = cmd_bar_hidden_state(bar_value, bar_id);
2055 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2067 * Implementation of 'shmlog <size>|toggle|on|off'
2070 void cmd_shmlog(I3_CMD, const char *argument) {
2071 if (!strcmp(argument, "toggle"))
2072 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2073 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2074 else if (!strcmp(argument, "on"))
2075 shmlog_size = default_shmlog_size;
2076 else if (!strcmp(argument, "off"))
2079 /* If shm logging now, restart logging with the new size. */
2080 if (shmlog_size > 0) {
2082 LOG("Restarting shm logging...\n");
2085 shmlog_size = atoi(argument);
2086 /* Make a weakly attempt at ensuring the argument is valid. */
2087 if (shmlog_size <= 0)
2088 shmlog_size = default_shmlog_size;
2090 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2092 update_shmlog_atom();
2093 // XXX: default reply for now, make this a better reply
2098 * Implementation of 'debuglog toggle|on|off'
2101 void cmd_debuglog(I3_CMD, const char *argument) {
2102 bool logging = get_debug_logging();
2103 if (!strcmp(argument, "toggle")) {
2104 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2105 set_debug_logging(!logging);
2106 } else if (!strcmp(argument, "on") && !logging) {
2107 LOG("Enabling debug logging\n");
2108 set_debug_logging(true);
2109 } else if (!strcmp(argument, "off") && logging) {
2110 LOG("Disabling debug logging\n");
2111 set_debug_logging(false);
2113 // XXX: default reply for now, make this a better reply