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 while (!TAILQ_EMPTY(&owindows)) { \
66 owindow *ow = TAILQ_FIRST(&owindows); \
67 TAILQ_REMOVE(&owindows, ow, owindows); \
70 owindow *ow = smalloc(sizeof(owindow)); \
72 TAILQ_INIT(&owindows); \
73 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
78 * Returns true if a is definitely greater than b (using the given epsilon)
81 static bool definitelyGreaterThan(float a, float b, float epsilon) {
82 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
86 * Returns the output containing the given container.
88 static Output *get_output_of_con(Con *con) {
89 Con *output_con = con_get_output(con);
90 Output *output = get_output_by_name(output_con->name);
91 assert(output != NULL);
97 * Checks whether we switched to a new workspace and returns false in that case,
98 * signaling that further workspace switching should be done by the calling function
99 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
100 * and return true, signaling that no further workspace switching should occur in the calling function.
103 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
104 Con *ws = con_get_workspace(focused);
106 /* If we switched to a different workspace, do nothing */
107 if (strcmp(ws->name, name) != 0)
110 DLOG("This workspace is already focused.\n");
111 if (config.workspace_auto_back_and_forth) {
112 workspace_back_and_forth();
113 cmd_output->needs_tree_render = true;
119 * Return the passed workspace unless it is the current one and auto back and
120 * forth is enabled, in which case the back_and_forth workspace is returned.
122 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
125 if (!config.workspace_auto_back_and_forth)
128 current = con_get_workspace(focused);
130 if (current == workspace) {
131 baf = workspace_back_and_forth_get();
133 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
141 // This code is commented out because we might recycle it for popping up error
142 // messages on parser errors.
144 static pid_t migration_pid = -1;
147 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
148 * it exited (or could not be started, depending on the exit code).
151 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
152 ev_child_stop(EV_A_ watcher);
153 if (!WIFEXITED(watcher->rstatus)) {
154 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
158 int exitcode = WEXITSTATUS(watcher->rstatus);
159 printf("i3-nagbar process exited with status %d\n", exitcode);
161 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
167 /* We need ev >= 4 for the following code. Since it is not *that* important (it
168 * only makes sure that there are no i3-nagbar instances left behind) we still
169 * support old systems with libev 3. */
170 #if EV_VERSION_MAJOR >= 4
172 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
173 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
176 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
177 if (migration_pid != -1) {
178 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
179 kill(migration_pid, SIGKILL);
184 void cmd_MIGRATION_start_nagbar(void) {
185 if (migration_pid != -1) {
186 fprintf(stderr, "i3-nagbar already running.\n");
189 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
190 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
191 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
192 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
193 migration_pid = fork();
194 if (migration_pid == -1) {
195 warn("Could not fork()");
200 if (migration_pid == 0) {
202 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
204 NULL, /* will be replaced by the executable path */
208 "You found a parsing error. Please, please, please, report it!",
214 exec_i3_utility("i3-nagbar", argv);
218 /* install a child watcher */
219 ev_child *child = smalloc(sizeof(ev_child));
220 ev_child_init(child, &nagbar_exited, migration_pid, 0);
221 ev_child_start(main_loop, child);
223 /* We need ev >= 4 for the following code. Since it is not *that* important (it
224 * only makes sure that there are no i3-nagbar instances left behind) we still
225 * support old systems with libev 3. */
226 #if EV_VERSION_MAJOR >= 4
227 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
229 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
230 ev_cleanup_init(cleanup, nagbar_cleanup);
231 ev_cleanup_start(main_loop, cleanup);
237 /*******************************************************************************
238 * Criteria functions.
239 ******************************************************************************/
242 * Helper data structure for an operation window (window on which the operation
243 * will be performed). Used to build the TAILQ owindows.
246 typedef struct owindow {
248 TAILQ_ENTRY(owindow) owindows;
251 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
253 static owindows_head owindows;
256 * Initializes the specified 'Match' data structure and the initial state of
257 * commands.c for matching target windows of a command.
260 void cmd_criteria_init(I3_CMD) {
264 DLOG("Initializing criteria, current_match = %p\n", current_match);
265 match_free(current_match);
266 match_init(current_match);
267 while (!TAILQ_EMPTY(&owindows)) {
268 ow = TAILQ_FIRST(&owindows);
269 TAILQ_REMOVE(&owindows, ow, owindows);
272 TAILQ_INIT(&owindows);
274 TAILQ_FOREACH(con, &all_cons, all_cons) {
275 ow = smalloc(sizeof(owindow));
277 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
282 * A match specification just finished (the closing square bracket was found),
283 * so we filter the list of owindows.
286 void cmd_criteria_match_windows(I3_CMD) {
287 owindow *next, *current;
289 DLOG("match specification finished, matching...\n");
290 /* copy the old list head to iterate through it and start with a fresh
291 * list which will contain only matching windows */
292 struct owindows_head old = owindows;
293 TAILQ_INIT(&owindows);
294 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
295 /* make a copy of the next pointer and advance the pointer to the
296 * next element as we are going to invalidate the element’s
297 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
299 next = TAILQ_NEXT(next, owindows);
301 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
303 /* We use this flag to prevent matching on window-less containers if
304 * only window-specific criteria were specified. */
305 bool accept_match = false;
307 if (current_match->con_id != NULL) {
310 if (current_match->con_id == current->con) {
311 DLOG("con_id matched.\n");
313 DLOG("con_id does not match.\n");
319 if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
321 bool matched_by_mark = false;
324 TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
325 if (!regex_matches(current_match->mark, mark->name))
328 DLOG("match by mark\n");
329 matched_by_mark = true;
333 if (!matched_by_mark) {
334 DLOG("mark does not match.\n");
340 if (current->con->window != NULL) {
341 if (match_matches_window(current_match, current->con->window)) {
342 DLOG("matches window!\n");
345 DLOG("doesnt match\n");
352 TAILQ_INSERT_TAIL(&owindows, current, owindows);
359 TAILQ_FOREACH(current, &owindows, owindows) {
360 DLOG("matching: %p / %s\n", current->con, current->con->name);
365 * Interprets a ctype=cvalue pair and adds it to the current match
369 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
370 match_parse_property(current_match, ctype, cvalue);
374 * Implementation of 'move [window|container] [to] workspace
375 * next|prev|next_on_output|prev_on_output|current'.
378 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
381 DLOG("which=%s\n", which);
383 /* We have nothing to move:
384 * when criteria was specified but didn't match any window or
385 * when criteria wasn't specified and we don't have any window focused. */
386 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
387 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
388 !con_has_children(focused))) {
395 /* get the workspace */
397 if (strcmp(which, "next") == 0)
398 ws = workspace_next();
399 else if (strcmp(which, "prev") == 0)
400 ws = workspace_prev();
401 else if (strcmp(which, "next_on_output") == 0)
402 ws = workspace_next_on_output();
403 else if (strcmp(which, "prev_on_output") == 0)
404 ws = workspace_prev_on_output();
405 else if (strcmp(which, "current") == 0)
406 ws = con_get_workspace(focused);
408 ELOG("BUG: called with which=%s\n", which);
413 TAILQ_FOREACH(current, &owindows, owindows) {
414 DLOG("matching: %p / %s\n", current->con, current->con->name);
415 con_move_to_workspace(current->con, ws, true, false, false);
418 cmd_output->needs_tree_render = true;
419 // XXX: default reply for now, make this a better reply
424 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
427 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
431 ws = workspace_back_and_forth_get();
434 yerror("No workspace was previously active.");
440 TAILQ_FOREACH(current, &owindows, owindows) {
441 DLOG("matching: %p / %s\n", current->con, current->con->name);
442 con_move_to_workspace(current->con, ws, true, false, false);
445 cmd_output->needs_tree_render = true;
446 // XXX: default reply for now, make this a better reply
451 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
454 void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
455 if (strncasecmp(name, "__", strlen("__")) == 0) {
456 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
461 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
464 /* We have nothing to move:
465 * when criteria was specified but didn't match any window or
466 * when criteria wasn't specified and we don't have any window focused. */
467 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
468 ELOG("No windows match your criteria, cannot move.\n");
471 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
472 !con_has_children(focused)) {
477 LOG("should move window to workspace %s\n", name);
478 /* get the workspace */
479 Con *ws = workspace_get(name, NULL);
481 if (!no_auto_back_and_forth)
482 ws = maybe_auto_back_and_forth_workspace(ws);
486 TAILQ_FOREACH(current, &owindows, owindows) {
487 DLOG("matching: %p / %s\n", current->con, current->con->name);
488 con_move_to_workspace(current->con, ws, true, false, false);
491 cmd_output->needs_tree_render = true;
492 // XXX: default reply for now, make this a better reply
497 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
500 void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
501 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
504 /* We have nothing to move:
505 * when criteria was specified but didn't match any window or
506 * when criteria wasn't specified and we don't have any window focused. */
507 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
508 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
509 !con_has_children(focused))) {
514 LOG("should move window to workspace %s\n", which);
515 /* get the workspace */
516 Con *output, *workspace = NULL;
518 long parsed_num = ws_name_to_number(which);
520 if (parsed_num == -1) {
521 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
522 yerror("Could not parse number \"%s\"", which);
526 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
527 GREP_FIRST(workspace, output_get_content(output),
528 child->num == parsed_num);
531 workspace = workspace_get(which, NULL);
534 if (!no_auto_back_and_forth)
535 workspace = maybe_auto_back_and_forth_workspace(workspace);
539 TAILQ_FOREACH(current, &owindows, owindows) {
540 DLOG("matching: %p / %s\n", current->con, current->con->name);
541 con_move_to_workspace(current->con, workspace, true, false, false);
544 cmd_output->needs_tree_render = true;
545 // XXX: default reply for now, make this a better reply
549 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) {
550 LOG("floating resize\n");
551 Rect old_rect = floating_con->rect;
552 Con *focused_con = con_descend_focused(floating_con);
554 /* ensure that resize will take place even if pixel increment is smaller than
555 * height increment or width increment.
557 const i3Window *window = focused_con->window;
558 if (window != NULL) {
559 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
560 strcmp(direction, "height") == 0) {
562 px = (-px < window->height_increment) ? -window->height_increment : px;
564 px = (px < window->height_increment) ? window->height_increment : px;
565 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
567 px = (-px < window->width_increment) ? -window->width_increment : px;
569 px = (px < window->width_increment) ? window->width_increment : px;
573 if (strcmp(direction, "up") == 0) {
574 floating_con->rect.height += px;
575 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
576 floating_con->rect.height += px;
577 } else if (strcmp(direction, "left") == 0) {
578 floating_con->rect.width += px;
580 floating_con->rect.width += px;
583 floating_check_size(floating_con);
585 /* Did we actually resize anything or did the size constraints prevent us?
586 * If we could not resize, exit now to not move the window. */
587 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
590 if (strcmp(direction, "up") == 0) {
591 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
592 } else if (strcmp(direction, "left") == 0) {
593 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
596 /* If this is a scratchpad window, don't auto center it from now on. */
597 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
598 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
601 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
602 LOG("tiling resize\n");
604 Con *first = current;
605 direction_t search_direction;
606 if (!strcmp(direction, "left"))
607 search_direction = D_LEFT;
608 else if (!strcmp(direction, "right"))
609 search_direction = D_RIGHT;
610 else if (!strcmp(direction, "up"))
611 search_direction = D_UP;
613 search_direction = D_DOWN;
615 bool res = resize_find_tiling_participants(&first, &second, search_direction);
617 LOG("No second container in this direction found.\n");
622 /* get the default percentage */
623 int children = con_num_children(first->parent);
624 LOG("ins. %d children\n", children);
625 double percentage = 1.0 / children;
626 LOG("default percentage = %f\n", percentage);
629 LOG("second->percent = %f\n", second->percent);
630 LOG("first->percent before = %f\n", first->percent);
631 if (first->percent == 0.0)
632 first->percent = percentage;
633 if (second->percent == 0.0)
634 second->percent = percentage;
635 double new_first_percent = first->percent + ((double)ppt / 100.0);
636 double new_second_percent = second->percent - ((double)ppt / 100.0);
637 LOG("new_first_percent = %f\n", new_first_percent);
638 LOG("new_second_percent = %f\n", new_second_percent);
639 /* Ensure that the new percentages are positive and greater than
640 * 0.05 to have a reasonable minimum size. */
641 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
642 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
643 first->percent += ((double)ppt / 100.0);
644 second->percent -= ((double)ppt / 100.0);
645 LOG("first->percent after = %f\n", first->percent);
646 LOG("second->percent after = %f\n", second->percent);
648 LOG("Not resizing, already at minimum size\n");
654 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
655 LOG("width/height resize\n");
656 /* get the appropriate current container (skip stacked/tabbed cons) */
657 while (current->parent->layout == L_STACKED ||
658 current->parent->layout == L_TABBED)
659 current = current->parent;
661 /* Then further go up until we find one with the matching orientation. */
662 orientation_t search_orientation =
663 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
665 while (current->type != CT_WORKSPACE &&
666 current->type != CT_FLOATING_CON &&
667 (con_orientation(current->parent) != search_orientation || con_num_children(current->parent) == 1))
668 current = current->parent;
670 /* get the default percentage */
671 int children = con_num_children(current->parent);
672 LOG("ins. %d children\n", children);
673 double percentage = 1.0 / children;
674 LOG("default percentage = %f\n", percentage);
676 orientation_t orientation = con_orientation(current->parent);
678 if ((orientation == HORIZ &&
679 strcmp(direction, "height") == 0) ||
680 (orientation == VERT &&
681 strcmp(direction, "width") == 0)) {
682 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
683 (orientation == HORIZ ? "horizontal" : "vertical"));
689 LOG("This is the only container, cannot resize.\n");
694 /* Ensure all the other children have a percentage set. */
696 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
697 LOG("child->percent = %f (child %p)\n", child->percent, child);
698 if (child->percent == 0.0)
699 child->percent = percentage;
702 double new_current_percent = current->percent + ((double)ppt / 100.0);
703 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
704 LOG("new_current_percent = %f\n", new_current_percent);
705 LOG("subtract_percent = %f\n", subtract_percent);
706 /* Ensure that the new percentages are positive and greater than
707 * 0.05 to have a reasonable minimum size. */
708 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
709 if (child == current)
711 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
712 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
717 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
718 LOG("Not resizing, already at minimum size\n");
723 current->percent += ((double)ppt / 100.0);
724 LOG("current->percent after = %f\n", current->percent);
726 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
727 if (child == current)
729 child->percent -= subtract_percent;
730 LOG("child->percent after (%p) = %f\n", child, child->percent);
737 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
740 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
741 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
742 if (strcmp(way, "shrink") == 0) {
750 TAILQ_FOREACH(current, &owindows, owindows) {
751 /* Don't handle dock windows (issue #1201) */
752 if (current->con->window && current->con->window->dock) {
753 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
758 if ((floating_con = con_inside_floating(current->con))) {
759 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
761 if (strcmp(direction, "width") == 0 ||
762 strcmp(direction, "height") == 0) {
763 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
764 current->con, way, direction, resize_ppt))
767 if (!cmd_resize_tiling_direction(current_match, cmd_output,
768 current->con, way, direction, resize_ppt))
774 cmd_output->needs_tree_render = true;
775 // XXX: default reply for now, make this a better reply
780 * Implementation of 'resize set <px> [px] <px> [px]'.
783 void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
784 DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
785 if (cwidth <= 0 || cheight <= 0) {
786 ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
793 TAILQ_FOREACH(current, &owindows, owindows) {
795 if ((floating_con = con_inside_floating(current->con))) {
796 floating_resize(floating_con, cwidth, cheight);
798 ELOG("Resize failed: %p not a floating container\n", current->con);
802 cmd_output->needs_tree_render = true;
803 // XXX: default reply for now, make this a better reply
808 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
811 void cmd_border(I3_CMD, const char *border_style_str, const char *border_width) {
812 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
817 TAILQ_FOREACH(current, &owindows, owindows) {
818 DLOG("matching: %p / %s\n", current->con, current->con->name);
819 int border_style = current->con->border_style;
821 int tmp_border_width = -1;
822 tmp_border_width = strtol(border_width, &end, 10);
823 if (end == border_width) {
824 /* no valid digits found */
825 tmp_border_width = -1;
827 if (strcmp(border_style_str, "toggle") == 0) {
830 if (border_style == BS_NORMAL)
831 tmp_border_width = 2;
832 else if (border_style == BS_NONE)
833 tmp_border_width = 0;
834 else if (border_style == BS_PIXEL)
835 tmp_border_width = 1;
837 if (strcmp(border_style_str, "normal") == 0)
838 border_style = BS_NORMAL;
839 else if (strcmp(border_style_str, "pixel") == 0)
840 border_style = BS_PIXEL;
841 else if (strcmp(border_style_str, "1pixel") == 0) {
842 border_style = BS_PIXEL;
843 tmp_border_width = 1;
844 } else if (strcmp(border_style_str, "none") == 0)
845 border_style = BS_NONE;
847 ELOG("BUG: called with border_style=%s\n", border_style_str);
852 con_set_border_style(current->con, border_style, tmp_border_width);
855 cmd_output->needs_tree_render = true;
856 // XXX: default reply for now, make this a better reply
861 * Implementation of 'nop <comment>'.
864 void cmd_nop(I3_CMD, const char *comment) {
865 LOG("-------------------------------------------------\n");
866 LOG(" NOP: %s\n", comment);
867 LOG("-------------------------------------------------\n");
871 * Implementation of 'append_layout <path>'.
874 void cmd_append_layout(I3_CMD, const char *cpath) {
875 char *path = sstrdup(cpath);
876 LOG("Appending layout \"%s\"\n", path);
878 /* Make sure we allow paths like '~/.i3/layout.json' */
879 path = resolve_tilde(path);
881 json_content_t content = json_determine_content(path);
882 LOG("JSON content = %d\n", content);
883 if (content == JSON_CONTENT_UNKNOWN) {
884 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
885 yerror("Could not determine the contents of \"%s\".", path);
890 Con *parent = focused;
891 if (content == JSON_CONTENT_WORKSPACE) {
892 parent = output_get_content(con_get_output(parent));
894 /* We need to append the layout to a split container, since a leaf
895 * container must not have any children (by definition).
896 * Note that we explicitly check for workspaces, since they are okay for
897 * this purpose, but con_accepts_window() returns false for workspaces. */
898 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
899 parent = parent->parent;
901 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
902 char *errormsg = NULL;
903 tree_append_json(parent, path, &errormsg);
904 if (errormsg != NULL) {
907 /* Note that we continue executing since tree_append_json() has
908 * side-effects — user-provided layouts can be partly valid, partly
909 * invalid, leading to half of the placeholder containers being
915 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
916 // false); should be enough, but when sending 'workspace 4; append_layout
917 // /tmp/foo.json', the needs_tree_render == true of the workspace command
918 // is not executed yet and will be batched with append_layout’s
919 // needs_tree_render after the parser finished. We should check if that is
921 render_con(croot, false);
923 restore_open_placeholder_windows(parent);
925 if (content == JSON_CONTENT_WORKSPACE)
926 ipc_send_workspace_event("restored", parent, NULL);
929 cmd_output->needs_tree_render = true;
933 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
936 void cmd_workspace(I3_CMD, const char *which) {
939 DLOG("which=%s\n", which);
941 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
942 LOG("Cannot switch workspace while in global fullscreen\n");
947 if (strcmp(which, "next") == 0)
948 ws = workspace_next();
949 else if (strcmp(which, "prev") == 0)
950 ws = workspace_prev();
951 else if (strcmp(which, "next_on_output") == 0)
952 ws = workspace_next_on_output();
953 else if (strcmp(which, "prev_on_output") == 0)
954 ws = workspace_prev_on_output();
956 ELOG("BUG: called with which=%s\n", which);
963 cmd_output->needs_tree_render = true;
964 // XXX: default reply for now, make this a better reply
969 * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
972 void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
973 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
974 Con *output, *workspace = NULL;
976 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
977 LOG("Cannot switch workspace while in global fullscreen\n");
982 long parsed_num = ws_name_to_number(which);
984 if (parsed_num == -1) {
985 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
986 yerror("Could not parse number \"%s\"", which);
990 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
991 GREP_FIRST(workspace, output_get_content(output),
992 child->num == parsed_num);
995 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
997 workspace_show_by_name(which);
998 cmd_output->needs_tree_render = true;
1001 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name))
1003 workspace_show(workspace);
1005 cmd_output->needs_tree_render = true;
1006 // XXX: default reply for now, make this a better reply
1011 * Implementation of 'workspace back_and_forth'.
1014 void cmd_workspace_back_and_forth(I3_CMD) {
1015 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1016 LOG("Cannot switch workspace while in global fullscreen\n");
1021 workspace_back_and_forth();
1023 cmd_output->needs_tree_render = true;
1024 // XXX: default reply for now, make this a better reply
1029 * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
1032 void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
1033 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
1035 if (strncasecmp(name, "__", strlen("__")) == 0) {
1036 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1041 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1042 LOG("Cannot switch workspace while in global fullscreen\n");
1047 DLOG("should switch to workspace %s\n", name);
1048 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name))
1050 workspace_show_by_name(name);
1052 cmd_output->needs_tree_render = true;
1053 // XXX: default reply for now, make this a better reply
1058 * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
1061 void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
1064 owindow *current = TAILQ_FIRST(&owindows);
1065 if (current == NULL) {
1070 /* Marks must be unique, i.e., no two windows must have the same mark. */
1071 if (current != TAILQ_LAST(&owindows, owindows_head)) {
1072 yerror("A mark must not be put onto more than one window");
1076 DLOG("matching: %p / %s\n", current->con, current->con->name);
1078 mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
1079 if (toggle != NULL) {
1080 con_mark_toggle(current->con, mark, mark_mode);
1082 con_mark(current->con, mark, mark_mode);
1085 cmd_output->needs_tree_render = true;
1086 // XXX: default reply for now, make this a better reply
1091 * Implementation of 'unmark [mark]'
1094 void cmd_unmark(I3_CMD, const char *mark) {
1095 if (match_is_empty(current_match)) {
1096 con_unmark(NULL, mark);
1099 TAILQ_FOREACH(current, &owindows, owindows) {
1100 con_unmark(current->con, mark);
1104 cmd_output->needs_tree_render = true;
1105 // XXX: default reply for now, make this a better reply
1110 * Implementation of 'mode <string>'.
1113 void cmd_mode(I3_CMD, const char *mode) {
1114 DLOG("mode=%s\n", mode);
1117 // XXX: default reply for now, make this a better reply
1122 * Implementation of 'move [window|container] [to] output <str>'.
1125 void cmd_move_con_to_output(I3_CMD, const char *name) {
1126 DLOG("Should move window to output \"%s\".\n", name);
1130 bool had_error = false;
1131 TAILQ_FOREACH(current, &owindows, owindows) {
1132 DLOG("matching: %p / %s\n", current->con, current->con->name);
1134 Output *current_output = get_output_of_con(current->con);
1135 assert(current_output != NULL);
1137 Output *output = get_output_from_string(current_output, name);
1138 if (output == NULL) {
1139 ELOG("Could not find output \"%s\", skipping.\n", name);
1145 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1147 ELOG("Could not find a visible workspace on output %p.\n", output);
1152 con_move_to_workspace(current->con, ws, true, false, false);
1155 cmd_output->needs_tree_render = true;
1156 ysuccess(!had_error);
1160 * Implementation of 'move [container|window] [to] mark <str>'.
1163 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1164 DLOG("moving window to mark \"%s\"\n", mark);
1170 TAILQ_FOREACH(current, &owindows, owindows) {
1171 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1172 result &= con_move_to_mark(current->con, mark);
1175 cmd_output->needs_tree_render = true;
1180 * Implementation of 'floating enable|disable|toggle'
1183 void cmd_floating(I3_CMD, const char *floating_mode) {
1186 DLOG("floating_mode=%s\n", floating_mode);
1190 TAILQ_FOREACH(current, &owindows, owindows) {
1191 DLOG("matching: %p / %s\n", current->con, current->con->name);
1192 if (strcmp(floating_mode, "toggle") == 0) {
1193 DLOG("should toggle mode\n");
1194 toggle_floating_mode(current->con, false);
1196 DLOG("should switch mode to %s\n", floating_mode);
1197 if (strcmp(floating_mode, "enable") == 0) {
1198 floating_enable(current->con, false);
1200 floating_disable(current->con, false);
1205 cmd_output->needs_tree_render = true;
1206 // XXX: default reply for now, make this a better reply
1211 * Implementation of 'move workspace to [output] <str>'.
1214 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1215 DLOG("should move workspace to output %s\n", name);
1220 TAILQ_FOREACH(current, &owindows, owindows) {
1221 Con *ws = con_get_workspace(current->con);
1222 bool success = workspace_move_to_output(ws, name);
1224 ELOG("Failed to move workspace to output.\n");
1230 cmd_output->needs_tree_render = true;
1231 // XXX: default reply for now, make this a better reply
1236 * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1239 void cmd_split(I3_CMD, const char *direction) {
1243 LOG("splitting in direction %c\n", direction[0]);
1244 TAILQ_FOREACH(current, &owindows, owindows) {
1245 if (con_is_docked(current->con)) {
1246 ELOG("Cannot split a docked container, skipping.\n");
1250 DLOG("matching: %p / %s\n", current->con, current->con->name);
1251 if (direction[0] == 't') {
1252 layout_t current_layout;
1253 if (current->con->type == CT_WORKSPACE) {
1254 current_layout = current->con->layout;
1256 current_layout = current->con->parent->layout;
1258 /* toggling split orientation */
1259 if (current_layout == L_SPLITH) {
1260 tree_split(current->con, VERT);
1262 tree_split(current->con, HORIZ);
1265 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1269 cmd_output->needs_tree_render = true;
1270 // XXX: default reply for now, make this a better reply
1275 * Implementation of 'kill [window|client]'.
1278 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1279 if (kill_mode_str == NULL)
1280 kill_mode_str = "window";
1282 DLOG("kill_mode=%s\n", kill_mode_str);
1285 if (strcmp(kill_mode_str, "window") == 0)
1286 kill_mode = KILL_WINDOW;
1287 else if (strcmp(kill_mode_str, "client") == 0)
1288 kill_mode = KILL_CLIENT;
1290 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1298 TAILQ_FOREACH(current, &owindows, owindows) {
1299 con_close(current->con, kill_mode);
1302 cmd_output->needs_tree_render = true;
1303 // XXX: default reply for now, make this a better reply
1308 * Implementation of 'exec [--no-startup-id] <command>'.
1311 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1312 bool no_startup_id = (nosn != NULL);
1314 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1315 start_application(command, no_startup_id);
1317 // XXX: default reply for now, make this a better reply
1322 * Implementation of 'focus left|right|up|down'.
1325 void cmd_focus_direction(I3_CMD, const char *direction) {
1326 DLOG("direction = *%s*\n", direction);
1328 if (strcmp(direction, "left") == 0)
1329 tree_next('p', HORIZ);
1330 else if (strcmp(direction, "right") == 0)
1331 tree_next('n', HORIZ);
1332 else if (strcmp(direction, "up") == 0)
1333 tree_next('p', VERT);
1334 else if (strcmp(direction, "down") == 0)
1335 tree_next('n', VERT);
1337 ELOG("Invalid focus direction (%s)\n", direction);
1342 cmd_output->needs_tree_render = true;
1343 // XXX: default reply for now, make this a better reply
1348 * Implementation of 'focus tiling|floating|mode_toggle'.
1351 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1352 DLOG("window_mode = %s\n", window_mode);
1354 Con *ws = con_get_workspace(focused);
1356 if (strcmp(window_mode, "mode_toggle") == 0) {
1357 if (con_inside_floating(focused))
1358 window_mode = "tiling";
1360 window_mode = "floating";
1363 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1364 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1365 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1368 con_focus(con_descend_focused(current));
1373 cmd_output->needs_tree_render = true;
1374 // XXX: default reply for now, make this a better reply
1379 * Implementation of 'focus parent|child'.
1382 void cmd_focus_level(I3_CMD, const char *level) {
1383 DLOG("level = %s\n", level);
1384 bool success = false;
1386 /* Focusing the parent can only be allowed if the newly
1387 * focused container won't escape the fullscreen container. */
1388 if (strcmp(level, "parent") == 0) {
1389 if (focused && focused->parent) {
1390 if (con_fullscreen_permits_focusing(focused->parent))
1391 success = level_up();
1393 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1397 /* Focusing a child should always be allowed. */
1399 success = level_down();
1401 cmd_output->needs_tree_render = success;
1402 // XXX: default reply for now, make this a better reply
1407 * Implementation of 'focus'.
1410 void cmd_focus(I3_CMD) {
1411 DLOG("current_match = %p\n", current_match);
1413 if (match_is_empty(current_match)) {
1414 ELOG("You have to specify which window/container should be focused.\n");
1415 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1417 yerror("You have to specify which window/container should be focused");
1422 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1425 TAILQ_FOREACH(current, &owindows, owindows) {
1426 Con *ws = con_get_workspace(current->con);
1427 /* If no workspace could be found, this was a dock window.
1428 * Just skip it, you cannot focus dock windows. */
1432 /* Check the fullscreen focus constraints. */
1433 if (!con_fullscreen_permits_focusing(current->con)) {
1434 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1439 /* In case this is a scratchpad window, call scratchpad_show(). */
1440 if (ws == __i3_scratch) {
1441 scratchpad_show(current->con);
1443 /* While for the normal focus case we can change focus multiple
1444 * times and only a single window ends up focused, we could show
1445 * multiple scratchpad windows. So, rather break here. */
1449 /* If the container is not on the current workspace,
1450 * workspace_show() will switch to a different workspace and (if
1451 * enabled) trigger a mouse pointer warp to the currently focused
1452 * container (!) on the target workspace.
1454 * Therefore, before calling workspace_show(), we make sure that
1455 * 'current' will be focused on the workspace. However, we cannot
1456 * just con_focus(current) because then the pointer will not be
1457 * warped at all (the code thinks we are already there).
1459 * So we focus 'current' to make it the currently focused window of
1460 * the target workspace, then revert focus. */
1461 Con *currently_focused = focused;
1462 con_focus(current->con);
1463 con_focus(currently_focused);
1465 /* Now switch to the workspace, then focus */
1467 LOG("focusing %p / %s\n", current->con, current->con->name);
1468 con_focus(current->con);
1473 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1474 "while only exactly one container can be focused at a time.\n",
1477 cmd_output->needs_tree_render = true;
1478 ysuccess(count > 0);
1482 * Implementation of 'fullscreen enable|toggle [global]' and
1483 * 'fullscreen disable'
1486 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1487 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1488 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1493 TAILQ_FOREACH(current, &owindows, owindows) {
1494 DLOG("matching: %p / %s\n", current->con, current->con->name);
1495 if (strcmp(action, "toggle") == 0) {
1496 con_toggle_fullscreen(current->con, mode);
1497 } else if (strcmp(action, "enable") == 0) {
1498 con_enable_fullscreen(current->con, mode);
1499 } else if (strcmp(action, "disable") == 0) {
1500 con_disable_fullscreen(current->con);
1504 cmd_output->needs_tree_render = true;
1505 // XXX: default reply for now, make this a better reply
1510 * Implementation of 'sticky enable|disable|toggle'.
1513 void cmd_sticky(I3_CMD, const char *action) {
1514 DLOG("%s sticky on window\n", action);
1518 TAILQ_FOREACH(current, &owindows, owindows) {
1519 if (current->con->window == NULL) {
1520 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1523 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1525 bool sticky = false;
1526 if (strcmp(action, "enable") == 0)
1528 else if (strcmp(action, "disable") == 0)
1530 else if (strcmp(action, "toggle") == 0)
1531 sticky = !current->con->sticky;
1533 current->con->sticky = sticky;
1534 ewmh_update_sticky(current->con->window->id, sticky);
1537 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1538 * sure it gets pushed to the front now. */
1539 output_push_sticky_windows(focused);
1541 cmd_output->needs_tree_render = true;
1546 * Implementation of 'move <direction> [<pixels> [px]]'.
1549 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1553 Con *initially_focused = focused;
1555 TAILQ_FOREACH(current, &owindows, owindows) {
1556 DLOG("moving in direction %s, px %ld\n", direction, move_px);
1557 if (con_is_floating(current->con)) {
1558 DLOG("floating move with %ld pixels\n", move_px);
1559 Rect newrect = current->con->parent->rect;
1560 if (strcmp(direction, "left") == 0) {
1561 newrect.x -= move_px;
1562 } else if (strcmp(direction, "right") == 0) {
1563 newrect.x += move_px;
1564 } else if (strcmp(direction, "up") == 0) {
1565 newrect.y -= move_px;
1566 } else if (strcmp(direction, "down") == 0) {
1567 newrect.y += move_px;
1569 floating_reposition(current->con->parent, newrect);
1571 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1572 cmd_output->needs_tree_render = true;
1576 /* the move command should not disturb focus */
1577 if (focused != initially_focused)
1578 con_focus(initially_focused);
1580 // XXX: default reply for now, make this a better reply
1585 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1588 void cmd_layout(I3_CMD, const char *layout_str) {
1591 if (strcmp(layout_str, "stacking") == 0)
1592 layout_str = "stacked";
1594 /* default is a special case which will be handled in con_set_layout(). */
1595 if (strcmp(layout_str, "default") == 0)
1597 else if (strcmp(layout_str, "stacked") == 0)
1599 else if (strcmp(layout_str, "tabbed") == 0)
1601 else if (strcmp(layout_str, "splitv") == 0)
1603 else if (strcmp(layout_str, "splith") == 0)
1606 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1610 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1613 TAILQ_FOREACH(current, &owindows, owindows) {
1614 if (con_is_docked(current->con)) {
1615 ELOG("cannot change layout of a docked container, skipping it.\n");
1619 DLOG("matching: %p / %s\n", current->con, current->con->name);
1620 con_set_layout(current->con, layout);
1623 cmd_output->needs_tree_render = true;
1624 // XXX: default reply for now, make this a better reply
1629 * Implementation of 'layout toggle [all|split]'.
1632 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1635 if (toggle_mode == NULL)
1636 toggle_mode = "default";
1638 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1640 /* check if the match is empty, not if the result is empty */
1641 if (match_is_empty(current_match))
1642 con_toggle_layout(focused, toggle_mode);
1644 TAILQ_FOREACH(current, &owindows, owindows) {
1645 DLOG("matching: %p / %s\n", current->con, current->con->name);
1646 con_toggle_layout(current->con, toggle_mode);
1650 cmd_output->needs_tree_render = true;
1651 // XXX: default reply for now, make this a better reply
1656 * Implementation of 'exit'.
1659 void cmd_exit(I3_CMD) {
1660 LOG("Exiting due to user command.\n");
1662 unlink(config.ipc_socket_path);
1663 xcb_disconnect(conn);
1670 * Implementation of 'reload'.
1673 void cmd_reload(I3_CMD) {
1675 kill_nagbar(&config_error_nagbar_pid, false);
1676 kill_nagbar(&command_error_nagbar_pid, false);
1677 load_configuration(conn, NULL, true);
1679 /* Send an IPC event just in case the ws names have changed */
1680 ipc_send_workspace_event("reload", NULL, NULL);
1681 /* Send an update event for the barconfig just in case it has changed */
1684 // XXX: default reply for now, make this a better reply
1689 * Implementation of 'restart'.
1692 void cmd_restart(I3_CMD) {
1693 LOG("restarting i3\n");
1695 unlink(config.ipc_socket_path);
1696 /* We need to call this manually since atexit handlers don’t get called
1698 purge_zerobyte_logfile();
1701 // XXX: default reply for now, make this a better reply
1706 * Implementation of 'open'.
1709 void cmd_open(I3_CMD) {
1710 LOG("opening new container\n");
1711 Con *con = tree_open_con(NULL, NULL);
1712 con->layout = L_SPLITH;
1719 y(integer, (long int)con);
1722 cmd_output->needs_tree_render = true;
1726 * Implementation of 'focus output <output>'.
1729 void cmd_focus_output(I3_CMD, const char *name) {
1732 DLOG("name = %s\n", name);
1736 /* get the output */
1737 Output *current_output = NULL;
1740 TAILQ_FOREACH(current, &owindows, owindows)
1741 current_output = get_output_of_con(current->con);
1742 assert(current_output != NULL);
1744 output = get_output_from_string(current_output, name);
1747 LOG("No such output found.\n");
1752 /* get visible workspace on output */
1754 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1762 cmd_output->needs_tree_render = true;
1763 // XXX: default reply for now, make this a better reply
1768 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1771 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1772 bool has_error = false;
1777 TAILQ_FOREACH(current, &owindows, owindows) {
1778 if (!con_is_floating(current->con)) {
1779 ELOG("Cannot change position. The window/container is not floating\n");
1782 yerror("Cannot change position of a window/container because it is not floating.");
1789 if (strcmp(method, "absolute") == 0) {
1790 current->con->parent->rect.x = x;
1791 current->con->parent->rect.y = y;
1793 DLOG("moving to absolute position %ld %ld\n", x, y);
1794 floating_maybe_reassign_ws(current->con->parent);
1795 cmd_output->needs_tree_render = true;
1798 if (strcmp(method, "position") == 0) {
1799 Rect newrect = current->con->parent->rect;
1801 DLOG("moving to position %ld %ld\n", x, y);
1805 floating_reposition(current->con->parent, newrect);
1809 // XXX: default reply for now, make this a better reply
1815 * Implementation of 'move [window|container] [to] [absolute] position center
1818 void cmd_move_window_to_center(I3_CMD, const char *method) {
1819 bool has_error = false;
1823 TAILQ_FOREACH(current, &owindows, owindows) {
1824 Con *floating_con = con_inside_floating(current->con);
1825 if (floating_con == NULL) {
1826 ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1827 current->con, current->con->name);
1830 yerror("Cannot change position of a window/container because it is not floating.");
1837 if (strcmp(method, "absolute") == 0) {
1838 DLOG("moving to absolute center\n");
1839 floating_center(floating_con, croot->rect);
1841 floating_maybe_reassign_ws(floating_con);
1842 cmd_output->needs_tree_render = true;
1845 if (strcmp(method, "position") == 0) {
1846 DLOG("moving to center\n");
1847 floating_center(floating_con, con_get_workspace(floating_con)->rect);
1849 cmd_output->needs_tree_render = true;
1853 // XXX: default reply for now, make this a better reply
1859 * Implementation of 'move [window|container] [to] position mouse'
1862 void cmd_move_window_to_mouse(I3_CMD) {
1866 TAILQ_FOREACH(current, &owindows, owindows) {
1867 Con *floating_con = con_inside_floating(current->con);
1868 if (floating_con == NULL) {
1869 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1870 current->con, current->con->name);
1874 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1875 floating_move_to_pointer(floating_con);
1878 cmd_output->needs_tree_render = true;
1883 * Implementation of 'move scratchpad'.
1886 void cmd_move_scratchpad(I3_CMD) {
1887 DLOG("should move window to scratchpad\n");
1892 TAILQ_FOREACH(current, &owindows, owindows) {
1893 DLOG("matching: %p / %s\n", current->con, current->con->name);
1894 scratchpad_move(current->con);
1897 cmd_output->needs_tree_render = true;
1898 // XXX: default reply for now, make this a better reply
1903 * Implementation of 'scratchpad show'.
1906 void cmd_scratchpad_show(I3_CMD) {
1907 DLOG("should show scratchpad window\n");
1910 if (match_is_empty(current_match)) {
1911 scratchpad_show(NULL);
1913 TAILQ_FOREACH(current, &owindows, owindows) {
1914 DLOG("matching: %p / %s\n", current->con, current->con->name);
1915 scratchpad_show(current->con);
1919 cmd_output->needs_tree_render = true;
1920 // XXX: default reply for now, make this a better reply
1925 * Implementation of 'title_format <format>'
1928 void cmd_title_format(I3_CMD, const char *format) {
1929 DLOG("setting title_format to \"%s\"\n", format);
1933 TAILQ_FOREACH(current, &owindows, owindows) {
1934 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1935 FREE(current->con->title_format);
1937 /* If we only display the title without anything else, we can skip the parsing step,
1938 * so we remove the title format altogether. */
1939 if (strcasecmp(format, "%title") != 0) {
1940 current->con->title_format = sstrdup(format);
1942 if (current->con->window != NULL) {
1943 i3String *formatted_title = con_parse_title_format(current->con);
1944 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1945 I3STRING_FREE(formatted_title);
1948 if (current->con->window != NULL) {
1949 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1950 ewmh_update_visible_name(current->con->window->id, NULL);
1954 if (current->con->window != NULL) {
1955 /* Make sure the window title is redrawn immediately. */
1956 current->con->window->name_x_changed = true;
1958 /* For windowless containers we also need to force the redrawing. */
1959 FREE(current->con->deco_render_params);
1963 cmd_output->needs_tree_render = true;
1968 * Implementation of 'rename workspace [<name>] to <name>'
1971 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1972 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1973 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1978 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1980 LOG("Renaming current workspace to \"%s\"\n", new_name);
1983 Con *output, *workspace = NULL;
1985 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1986 GREP_FIRST(workspace, output_get_content(output),
1987 !strcasecmp(child->name, old_name));
1989 workspace = con_get_workspace(focused);
1990 old_name = workspace->name;
1994 yerror("Old workspace \"%s\" not found", old_name);
1998 Con *check_dest = NULL;
1999 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2000 GREP_FIRST(check_dest, output_get_content(output),
2001 !strcasecmp(child->name, new_name));
2003 if (check_dest != NULL) {
2004 yerror("New workspace \"%s\" already exists", new_name);
2008 /* Change the name and try to parse it as a number. */
2009 /* old_name might refer to workspace->name, so copy it before free()ing */
2010 char *old_name_copy = sstrdup(old_name);
2011 FREE(workspace->name);
2012 workspace->name = sstrdup(new_name);
2014 workspace->num = ws_name_to_number(new_name);
2015 LOG("num = %d\n", workspace->num);
2017 /* By re-attaching, the sort order will be correct afterwards. */
2018 Con *previously_focused = focused;
2019 Con *parent = workspace->parent;
2020 con_detach(workspace);
2021 con_attach(workspace, parent, false);
2023 /* Move the workspace to the correct output if it has an assignment */
2024 struct Workspace_Assignment *assignment = NULL;
2025 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2026 if (assignment->output == NULL)
2028 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2032 workspace_move_to_output(workspace, assignment->output);
2034 if (previously_focused)
2035 workspace_show(con_get_workspace(previously_focused));
2040 /* Restore the previous focus since con_attach messes with the focus. */
2041 con_focus(previously_focused);
2043 cmd_output->needs_tree_render = true;
2046 ipc_send_workspace_event("rename", workspace, NULL);
2047 ewmh_update_desktop_names();
2048 ewmh_update_desktop_viewport();
2049 ewmh_update_current_desktop();
2051 startup_sequence_rename_workspace(old_name_copy, new_name);
2052 free(old_name_copy);
2056 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2059 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2061 bool toggle = false;
2062 if (strcmp(bar_mode, "dock") == 0)
2064 else if (strcmp(bar_mode, "hide") == 0)
2066 else if (strcmp(bar_mode, "invisible") == 0)
2068 else if (strcmp(bar_mode, "toggle") == 0)
2071 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2075 bool changed_sth = false;
2076 Barconfig *current = NULL;
2077 TAILQ_FOREACH(current, &barconfigs, configs) {
2078 if (bar_id && strcmp(current->id, bar_id) != 0)
2082 mode = (current->mode + 1) % 2;
2084 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2085 current->mode = mode;
2092 if (bar_id && !changed_sth) {
2093 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2101 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2104 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2105 int hidden_state = S_SHOW;
2106 bool toggle = false;
2107 if (strcmp(bar_hidden_state, "hide") == 0)
2108 hidden_state = S_HIDE;
2109 else if (strcmp(bar_hidden_state, "show") == 0)
2110 hidden_state = S_SHOW;
2111 else if (strcmp(bar_hidden_state, "toggle") == 0)
2114 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2118 bool changed_sth = false;
2119 Barconfig *current = NULL;
2120 TAILQ_FOREACH(current, &barconfigs, configs) {
2121 if (bar_id && strcmp(current->id, bar_id) != 0)
2125 hidden_state = (current->hidden_state + 1) % 2;
2127 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2128 current->hidden_state = hidden_state;
2135 if (bar_id && !changed_sth) {
2136 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2144 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2147 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2149 if (strcmp(bar_type, "mode") == 0)
2150 ret = cmd_bar_mode(bar_value, bar_id);
2151 else if (strcmp(bar_type, "hidden_state") == 0)
2152 ret = cmd_bar_hidden_state(bar_value, bar_id);
2154 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2166 * Implementation of 'shmlog <size>|toggle|on|off'
2169 void cmd_shmlog(I3_CMD, const char *argument) {
2170 if (!strcmp(argument, "toggle"))
2171 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2172 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2173 else if (!strcmp(argument, "on"))
2174 shmlog_size = default_shmlog_size;
2175 else if (!strcmp(argument, "off"))
2178 /* If shm logging now, restart logging with the new size. */
2179 if (shmlog_size > 0) {
2181 LOG("Restarting shm logging...\n");
2184 shmlog_size = atoi(argument);
2185 /* Make a weakly attempt at ensuring the argument is valid. */
2186 if (shmlog_size <= 0)
2187 shmlog_size = default_shmlog_size;
2189 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2191 update_shmlog_atom();
2192 // XXX: default reply for now, make this a better reply
2197 * Implementation of 'debuglog toggle|on|off'
2200 void cmd_debuglog(I3_CMD, const char *argument) {
2201 bool logging = get_debug_logging();
2202 if (!strcmp(argument, "toggle")) {
2203 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2204 set_debug_logging(!logging);
2205 } else if (!strcmp(argument, "on") && !logging) {
2206 LOG("Enabling debug logging\n");
2207 set_debug_logging(true);
2208 } else if (!strcmp(argument, "off") && logging) {
2209 LOG("Disabling debug logging\n");
2210 set_debug_logging(false);
2212 // XXX: default reply for now, make this a better reply