2 #define I3__FILE__ "commands.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * commands.c: all command functions (see commands_parser.c)
17 // Macros to make the YAJL API a bit easier to use.
18 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
19 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
20 #define ysuccess(success) do { \
27 /** When the command did not include match criteria (!), we use the currently
28 * focused container. Do not confuse this case with a command which included
29 * criteria but which did not match any windows. This macro has to be called in
32 #define HANDLE_EMPTY_MATCH do { \
33 if (match_is_empty(current_match)) { \
34 owindow *ow = smalloc(sizeof(owindow)); \
36 TAILQ_INIT(&owindows); \
37 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
41 static owindows_head owindows;
44 * Returns true if a is definitely greater than b (using the given epsilon)
47 static bool definitelyGreaterThan(float a, float b, float epsilon) {
48 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
52 * Returns an 'output' corresponding to one of left/right/down/up or a specific
56 static Output *get_output_from_string(Output *current_output, const char *output_str) {
59 if (strcasecmp(output_str, "left") == 0) {
60 output = get_output_next(D_LEFT, current_output, CLOSEST_OUTPUT);
62 output = get_output_most(D_RIGHT, current_output);
63 } else if (strcasecmp(output_str, "right") == 0) {
64 output = get_output_next(D_RIGHT, current_output, CLOSEST_OUTPUT);
66 output = get_output_most(D_LEFT, current_output);
67 } else if (strcasecmp(output_str, "up") == 0) {
68 output = get_output_next(D_UP, current_output, CLOSEST_OUTPUT);
70 output = get_output_most(D_DOWN, current_output);
71 } else if (strcasecmp(output_str, "down") == 0) {
72 output = get_output_next(D_DOWN, current_output, CLOSEST_OUTPUT);
74 output = get_output_most(D_UP, current_output);
75 } else output = get_output_by_name(output_str);
81 * Checks whether we switched to a new workspace and returns false in that case,
82 * signaling that further workspace switching should be done by the calling function
83 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
84 * and return true, signaling that no further workspace switching should occur in the calling function.
87 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
88 Con *ws = con_get_workspace(focused);
90 /* If we switched to a different workspace, do nothing */
91 if (strcmp(ws->name, name) != 0)
94 DLOG("This workspace is already focused.\n");
95 if (config.workspace_auto_back_and_forth) {
96 workspace_back_and_forth();
97 cmd_output->needs_tree_render = true;
103 * Return the passed workspace unless it is the current one and auto back and
104 * forth is enabled, in which case the back_and_forth workspace is returned.
106 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
109 if (!config.workspace_auto_back_and_forth)
112 current = con_get_workspace(focused);
114 if (current == workspace) {
115 baf = workspace_back_and_forth_get();
117 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
125 // This code is commented out because we might recycle it for popping up error
126 // messages on parser errors.
128 static pid_t migration_pid = -1;
131 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
132 * it exited (or could not be started, depending on the exit code).
135 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
136 ev_child_stop(EV_A_ watcher);
137 if (!WIFEXITED(watcher->rstatus)) {
138 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
142 int exitcode = WEXITSTATUS(watcher->rstatus);
143 printf("i3-nagbar process exited with status %d\n", exitcode);
145 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
151 /* We need ev >= 4 for the following code. Since it is not *that* important (it
152 * only makes sure that there are no i3-nagbar instances left behind) we still
153 * support old systems with libev 3. */
154 #if EV_VERSION_MAJOR >= 4
156 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
157 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
160 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
161 if (migration_pid != -1) {
162 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
163 kill(migration_pid, SIGKILL);
168 void cmd_MIGRATION_start_nagbar(void) {
169 if (migration_pid != -1) {
170 fprintf(stderr, "i3-nagbar already running.\n");
173 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
174 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
175 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
176 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
177 migration_pid = fork();
178 if (migration_pid == -1) {
179 warn("Could not fork()");
184 if (migration_pid == 0) {
186 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
188 NULL, /* will be replaced by the executable path */
192 "You found a parsing error. Please, please, please, report it!",
198 exec_i3_utility("i3-nagbar", argv);
202 /* install a child watcher */
203 ev_child *child = smalloc(sizeof(ev_child));
204 ev_child_init(child, &nagbar_exited, migration_pid, 0);
205 ev_child_start(main_loop, child);
207 /* We need ev >= 4 for the following code. Since it is not *that* important (it
208 * only makes sure that there are no i3-nagbar instances left behind) we still
209 * support old systems with libev 3. */
210 #if EV_VERSION_MAJOR >= 4
211 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
213 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
214 ev_cleanup_init(cleanup, nagbar_cleanup);
215 ev_cleanup_start(main_loop, cleanup);
221 /*******************************************************************************
222 * Criteria functions.
223 ******************************************************************************/
226 * Initializes the specified 'Match' data structure and the initial state of
227 * commands.c for matching target windows of a command.
230 void cmd_criteria_init(I3_CMD) {
234 DLOG("Initializing criteria, current_match = %p\n", current_match);
235 match_init(current_match);
236 while (!TAILQ_EMPTY(&owindows)) {
237 ow = TAILQ_FIRST(&owindows);
238 TAILQ_REMOVE(&owindows, ow, owindows);
241 TAILQ_INIT(&owindows);
243 TAILQ_FOREACH(con, &all_cons, all_cons) {
244 ow = smalloc(sizeof(owindow));
246 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
251 * A match specification just finished (the closing square bracket was found),
252 * so we filter the list of owindows.
255 void cmd_criteria_match_windows(I3_CMD) {
256 owindow *next, *current;
258 DLOG("match specification finished, matching...\n");
259 /* copy the old list head to iterate through it and start with a fresh
260 * list which will contain only matching windows */
261 struct owindows_head old = owindows;
262 TAILQ_INIT(&owindows);
263 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
264 /* make a copy of the next pointer and advance the pointer to the
265 * next element as we are going to invalidate the element’s
266 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
268 next = TAILQ_NEXT(next, owindows);
270 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
271 if (current_match->con_id != NULL) {
272 if (current_match->con_id == current->con) {
273 DLOG("matches container!\n");
274 TAILQ_INSERT_TAIL(&owindows, current, owindows);
276 } else if (current_match->mark != NULL && current->con->mark != NULL &&
277 regex_matches(current_match->mark, current->con->mark)) {
278 DLOG("match by mark\n");
279 TAILQ_INSERT_TAIL(&owindows, current, owindows);
281 if (current->con->window == NULL)
283 if (match_matches_window(current_match, current->con->window)) {
284 DLOG("matches window!\n");
285 TAILQ_INSERT_TAIL(&owindows, current, owindows);
287 DLOG("doesnt match\n");
293 TAILQ_FOREACH(current, &owindows, owindows) {
294 DLOG("matching: %p / %s\n", current->con, current->con->name);
299 * Interprets a ctype=cvalue pair and adds it to the current match
303 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
304 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
306 if (strcmp(ctype, "class") == 0) {
307 current_match->class = regex_new(cvalue);
311 if (strcmp(ctype, "instance") == 0) {
312 current_match->instance = regex_new(cvalue);
316 if (strcmp(ctype, "window_role") == 0) {
317 current_match->role = regex_new(cvalue);
321 if (strcmp(ctype, "con_id") == 0) {
323 long parsed = strtol(cvalue, &end, 10);
324 if (parsed == LONG_MIN ||
325 parsed == LONG_MAX ||
327 (end && *end != '\0')) {
328 ELOG("Could not parse con id \"%s\"\n", cvalue);
330 current_match->con_id = (Con*)parsed;
331 printf("id as int = %p\n", current_match->con_id);
336 if (strcmp(ctype, "id") == 0) {
338 long parsed = strtol(cvalue, &end, 10);
339 if (parsed == LONG_MIN ||
340 parsed == LONG_MAX ||
342 (end && *end != '\0')) {
343 ELOG("Could not parse window id \"%s\"\n", cvalue);
345 current_match->id = parsed;
346 printf("window id as int = %d\n", current_match->id);
351 if (strcmp(ctype, "con_mark") == 0) {
352 current_match->mark = regex_new(cvalue);
356 if (strcmp(ctype, "title") == 0) {
357 current_match->title = regex_new(cvalue);
361 if (strcmp(ctype, "urgent") == 0) {
362 if (strcasecmp(cvalue, "latest") == 0 ||
363 strcasecmp(cvalue, "newest") == 0 ||
364 strcasecmp(cvalue, "recent") == 0 ||
365 strcasecmp(cvalue, "last") == 0) {
366 current_match->urgent = U_LATEST;
367 } else if (strcasecmp(cvalue, "oldest") == 0 ||
368 strcasecmp(cvalue, "first") == 0) {
369 current_match->urgent = U_OLDEST;
374 ELOG("Unknown criterion: %s\n", ctype);
378 * Implementation of 'move [window|container] [to] workspace
379 * next|prev|next_on_output|prev_on_output|current'.
382 void cmd_move_con_to_workspace(I3_CMD, char *which) {
385 DLOG("which=%s\n", which);
387 /* We have nothing to move:
388 * when criteria was specified but didn't match any window or
389 * when criteria wasn't specified and we don't have any window focused. */
390 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
391 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
398 /* get the workspace */
400 if (strcmp(which, "next") == 0)
401 ws = workspace_next();
402 else if (strcmp(which, "prev") == 0)
403 ws = workspace_prev();
404 else if (strcmp(which, "next_on_output") == 0)
405 ws = workspace_next_on_output();
406 else if (strcmp(which, "prev_on_output") == 0)
407 ws = workspace_prev_on_output();
408 else if (strcmp(which, "current") == 0)
409 ws = con_get_workspace(focused);
411 ELOG("BUG: called with which=%s\n", which);
416 TAILQ_FOREACH(current, &owindows, owindows) {
417 DLOG("matching: %p / %s\n", current->con, current->con->name);
418 con_move_to_workspace(current->con, ws, true, false);
421 cmd_output->needs_tree_render = true;
422 // XXX: default reply for now, make this a better reply
427 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
430 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
434 ws = workspace_back_and_forth_get();
441 ystr("No workspace was previously active.");
448 TAILQ_FOREACH(current, &owindows, owindows) {
449 DLOG("matching: %p / %s\n", current->con, current->con->name);
450 con_move_to_workspace(current->con, ws, true, false);
453 cmd_output->needs_tree_render = true;
454 // XXX: default reply for now, make this a better reply
459 * Implementation of 'move [window|container] [to] workspace <name>'.
462 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
463 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
464 LOG("You cannot switch to the i3 internal workspaces.\n");
471 /* We have nothing to move:
472 * when criteria was specified but didn't match any window or
473 * when criteria wasn't specified and we don't have any window focused. */
474 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
475 ELOG("No windows match your criteria, cannot move.\n");
480 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
481 ELOG("No window to move, you have focused a workspace.\n");
486 LOG("should move window to workspace %s\n", name);
487 /* get the workspace */
488 Con *ws = workspace_get(name, NULL);
490 ws = maybe_auto_back_and_forth_workspace(ws);
494 TAILQ_FOREACH(current, &owindows, owindows) {
495 DLOG("matching: %p / %s\n", current->con, current->con->name);
496 con_move_to_workspace(current->con, ws, true, false);
499 cmd_output->needs_tree_render = true;
500 // XXX: default reply for now, make this a better reply
505 * Implementation of 'move [window|container] [to] workspace number <name>'.
508 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
511 /* We have nothing to move:
512 * when criteria was specified but didn't match any window or
513 * when criteria wasn't specified and we don't have any window focused. */
514 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
515 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
520 LOG("should move window to workspace %s\n", which);
521 /* get the workspace */
522 Con *output, *workspace = NULL;
525 long parsed_num = strtol(which, &endptr, 10);
526 if (parsed_num == LONG_MIN ||
527 parsed_num == LONG_MAX ||
530 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
535 // TODO: better error message
536 ystr("Could not parse number");
541 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
542 GREP_FIRST(workspace, output_get_content(output),
543 child->num == parsed_num);
546 workspace = workspace_get(which, NULL);
549 workspace = maybe_auto_back_and_forth_workspace(workspace);
553 TAILQ_FOREACH(current, &owindows, owindows) {
554 DLOG("matching: %p / %s\n", current->con, current->con->name);
555 con_move_to_workspace(current->con, workspace, true, false);
558 cmd_output->needs_tree_render = true;
559 // XXX: default reply for now, make this a better reply
563 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
564 LOG("floating resize\n");
565 if (strcmp(direction, "up") == 0) {
566 floating_con->rect.y -= px;
567 floating_con->rect.height += px;
568 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
569 floating_con->rect.height += px;
570 } else if (strcmp(direction, "left") == 0) {
571 floating_con->rect.x -= px;
572 floating_con->rect.width += px;
574 floating_con->rect.width += px;
578 static bool cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
579 LOG("tiling resize\n");
580 /* get the appropriate current container (skip stacked/tabbed cons) */
581 Con *current = focused;
583 double percentage = 0;
584 while (current->parent->layout == L_STACKED ||
585 current->parent->layout == L_TABBED)
586 current = current->parent;
588 /* Then further go up until we find one with the matching orientation. */
589 orientation_t search_orientation =
590 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
593 if (con_orientation(current->parent) != search_orientation) {
594 current = current->parent;
598 /* get the default percentage */
599 int children = con_num_children(current->parent);
600 LOG("ins. %d children\n", children);
601 percentage = 1.0 / children;
602 LOG("default percentage = %f\n", percentage);
604 orientation_t orientation = con_orientation(current->parent);
606 if ((orientation == HORIZ &&
607 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
608 (orientation == VERT &&
609 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
610 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
611 (orientation == HORIZ ? "horizontal" : "vertical"));
616 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
617 other = TAILQ_PREV(current, nodes_head, nodes);
619 other = TAILQ_NEXT(current, nodes);
621 if (other == TAILQ_END(workspaces)) {
622 LOG("No other container in this direction found, trying to look further up in the tree...\n");
623 current = current->parent;
627 } while (current->type != CT_WORKSPACE &&
628 current->type != CT_FLOATING_CON);
631 LOG("No other container in this direction found, trying to look further up in the tree...\n");
636 LOG("other->percent = %f\n", other->percent);
637 LOG("current->percent before = %f\n", current->percent);
638 if (current->percent == 0.0)
639 current->percent = percentage;
640 if (other->percent == 0.0)
641 other->percent = percentage;
642 double new_current_percent = current->percent + ((double)ppt / 100.0);
643 double new_other_percent = other->percent - ((double)ppt / 100.0);
644 LOG("new_current_percent = %f\n", new_current_percent);
645 LOG("new_other_percent = %f\n", new_other_percent);
646 /* Ensure that the new percentages are positive and greater than
647 * 0.05 to have a reasonable minimum size. */
648 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
649 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
650 current->percent += ((double)ppt / 100.0);
651 other->percent -= ((double)ppt / 100.0);
652 LOG("current->percent after = %f\n", current->percent);
653 LOG("other->percent after = %f\n", other->percent);
655 LOG("Not resizing, already at minimum size\n");
661 static bool cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
662 LOG("width/height resize\n");
663 /* get the appropriate current container (skip stacked/tabbed cons) */
664 Con *current = focused;
665 while (current->parent->layout == L_STACKED ||
666 current->parent->layout == L_TABBED)
667 current = current->parent;
669 /* Then further go up until we find one with the matching orientation. */
670 orientation_t search_orientation =
671 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
673 while (current->type != CT_WORKSPACE &&
674 current->type != CT_FLOATING_CON &&
675 con_orientation(current->parent) != search_orientation)
676 current = current->parent;
678 /* get the default percentage */
679 int children = con_num_children(current->parent);
680 LOG("ins. %d children\n", children);
681 double percentage = 1.0 / children;
682 LOG("default percentage = %f\n", percentage);
684 orientation_t orientation = con_orientation(current->parent);
686 if ((orientation == HORIZ &&
687 strcmp(direction, "height") == 0) ||
688 (orientation == VERT &&
689 strcmp(direction, "width") == 0)) {
690 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
691 (orientation == HORIZ ? "horizontal" : "vertical"));
697 LOG("This is the only container, cannot resize.\n");
702 /* Ensure all the other children have a percentage set. */
704 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
705 LOG("child->percent = %f (child %p)\n", child->percent, child);
706 if (child->percent == 0.0)
707 child->percent = percentage;
710 double new_current_percent = current->percent + ((double)ppt / 100.0);
711 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
712 LOG("new_current_percent = %f\n", new_current_percent);
713 LOG("subtract_percent = %f\n", subtract_percent);
714 /* Ensure that the new percentages are positive and greater than
715 * 0.05 to have a reasonable minimum size. */
716 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
717 if (child == current)
719 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
720 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
725 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
726 LOG("Not resizing, already at minimum size\n");
731 current->percent += ((double)ppt / 100.0);
732 LOG("current->percent after = %f\n", current->percent);
734 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
735 if (child == current)
737 child->percent -= subtract_percent;
738 LOG("child->percent after (%p) = %f\n", child, child->percent);
745 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
748 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
749 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
750 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
751 // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
752 int px = atoi(resize_px);
753 int ppt = atoi(resize_ppt);
754 if (strcmp(way, "shrink") == 0) {
760 if ((floating_con = con_inside_floating(focused))) {
761 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
763 if (strcmp(direction, "width") == 0 ||
764 strcmp(direction, "height") == 0) {
765 if (!cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt))
768 if (!cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt))
773 cmd_output->needs_tree_render = true;
774 // XXX: default reply for now, make this a better reply
779 * Implementation of 'border normal|none|1pixel|toggle'.
782 void cmd_border(I3_CMD, char *border_style_str) {
783 DLOG("border style should be changed to %s\n", border_style_str);
788 TAILQ_FOREACH(current, &owindows, owindows) {
789 DLOG("matching: %p / %s\n", current->con, current->con->name);
790 int border_style = current->con->border_style;
791 if (strcmp(border_style_str, "toggle") == 0) {
795 if (strcmp(border_style_str, "normal") == 0)
796 border_style = BS_NORMAL;
797 else if (strcmp(border_style_str, "none") == 0)
798 border_style = BS_NONE;
799 else if (strcmp(border_style_str, "1pixel") == 0)
800 border_style = BS_1PIXEL;
802 ELOG("BUG: called with border_style=%s\n", border_style_str);
807 con_set_border_style(current->con, border_style);
810 cmd_output->needs_tree_render = true;
811 // XXX: default reply for now, make this a better reply
816 * Implementation of 'nop <comment>'.
819 void cmd_nop(I3_CMD, char *comment) {
820 LOG("-------------------------------------------------\n");
821 LOG(" NOP: %s\n", comment);
822 LOG("-------------------------------------------------\n");
826 * Implementation of 'append_layout <path>'.
829 void cmd_append_layout(I3_CMD, char *path) {
830 LOG("Appending layout \"%s\"\n", path);
831 tree_append_json(path);
833 cmd_output->needs_tree_render = true;
834 // XXX: default reply for now, make this a better reply
839 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
842 void cmd_workspace(I3_CMD, char *which) {
845 DLOG("which=%s\n", which);
847 if (strcmp(which, "next") == 0)
848 ws = workspace_next();
849 else if (strcmp(which, "prev") == 0)
850 ws = workspace_prev();
851 else if (strcmp(which, "next_on_output") == 0)
852 ws = workspace_next_on_output();
853 else if (strcmp(which, "prev_on_output") == 0)
854 ws = workspace_prev_on_output();
856 ELOG("BUG: called with which=%s\n", which);
863 cmd_output->needs_tree_render = true;
864 // XXX: default reply for now, make this a better reply
869 * Implementation of 'workspace number <name>'
872 void cmd_workspace_number(I3_CMD, char *which) {
873 Con *output, *workspace = NULL;
876 long parsed_num = strtol(which, &endptr, 10);
877 if (parsed_num == LONG_MIN ||
878 parsed_num == LONG_MAX ||
881 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
886 // TODO: better error message
887 ystr("Could not parse number");
893 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
894 GREP_FIRST(workspace, output_get_content(output),
895 child->num == parsed_num);
898 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
900 workspace_show_by_name(which);
901 cmd_output->needs_tree_render = true;
904 if (maybe_back_and_forth(cmd_output, workspace->name))
906 workspace_show(workspace);
908 cmd_output->needs_tree_render = true;
909 // XXX: default reply for now, make this a better reply
914 * Implementation of 'workspace back_and_forth'.
917 void cmd_workspace_back_and_forth(I3_CMD) {
918 workspace_back_and_forth();
920 cmd_output->needs_tree_render = true;
921 // XXX: default reply for now, make this a better reply
926 * Implementation of 'workspace <name>'
929 void cmd_workspace_name(I3_CMD, char *name) {
930 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
931 LOG("You cannot switch to the i3 internal workspaces.\n");
936 DLOG("should switch to workspace %s\n", name);
937 if (maybe_back_and_forth(cmd_output, name))
939 workspace_show_by_name(name);
941 cmd_output->needs_tree_render = true;
942 // XXX: default reply for now, make this a better reply
947 * Implementation of 'mark <mark>'
950 void cmd_mark(I3_CMD, char *mark) {
951 DLOG("Clearing all windows which have that mark first\n");
954 TAILQ_FOREACH(con, &all_cons, all_cons) {
955 if (con->mark && strcmp(con->mark, mark) == 0)
959 DLOG("marking window with str %s\n", mark);
964 TAILQ_FOREACH(current, &owindows, owindows) {
965 DLOG("matching: %p / %s\n", current->con, current->con->name);
966 current->con->mark = sstrdup(mark);
969 cmd_output->needs_tree_render = true;
970 // XXX: default reply for now, make this a better reply
975 * Implementation of 'mode <string>'.
978 void cmd_mode(I3_CMD, char *mode) {
979 DLOG("mode=%s\n", mode);
982 // XXX: default reply for now, make this a better reply
987 * Implementation of 'move [window|container] [to] output <str>'.
990 void cmd_move_con_to_output(I3_CMD, char *name) {
993 DLOG("should move window to output %s\n", name);
998 Output *current_output = NULL;
1001 // TODO: fix the handling of criteria
1002 TAILQ_FOREACH(current, &owindows, owindows)
1003 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1005 assert(current_output != NULL);
1007 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1008 if (strcasecmp(name, "up") == 0)
1009 output = get_output_next(D_UP, current_output, CLOSEST_OUTPUT);
1010 else if (strcasecmp(name, "down") == 0)
1011 output = get_output_next(D_DOWN, current_output, CLOSEST_OUTPUT);
1012 else if (strcasecmp(name, "left") == 0)
1013 output = get_output_next(D_LEFT, current_output, CLOSEST_OUTPUT);
1014 else if (strcasecmp(name, "right") == 0)
1015 output = get_output_next(D_RIGHT, current_output, CLOSEST_OUTPUT);
1017 output = get_output_by_name(name);
1020 LOG("No such output found.\n");
1025 /* get visible workspace on output */
1027 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1033 TAILQ_FOREACH(current, &owindows, owindows) {
1034 DLOG("matching: %p / %s\n", current->con, current->con->name);
1035 con_move_to_workspace(current->con, ws, true, false);
1038 cmd_output->needs_tree_render = true;
1039 // XXX: default reply for now, make this a better reply
1044 * Implementation of 'floating enable|disable|toggle'
1047 void cmd_floating(I3_CMD, char *floating_mode) {
1050 DLOG("floating_mode=%s\n", floating_mode);
1054 TAILQ_FOREACH(current, &owindows, owindows) {
1055 DLOG("matching: %p / %s\n", current->con, current->con->name);
1056 if (strcmp(floating_mode, "toggle") == 0) {
1057 DLOG("should toggle mode\n");
1058 toggle_floating_mode(current->con, false);
1060 DLOG("should switch mode to %s\n", floating_mode);
1061 if (strcmp(floating_mode, "enable") == 0) {
1062 floating_enable(current->con, false);
1064 floating_disable(current->con, false);
1069 cmd_output->needs_tree_render = true;
1070 // XXX: default reply for now, make this a better reply
1075 * Implementation of 'move workspace to [output] <str>'.
1078 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1079 DLOG("should move workspace to output %s\n", name);
1084 TAILQ_FOREACH(current, &owindows, owindows) {
1085 Output *current_output = get_output_containing(current->con->rect.x,
1086 current->con->rect.y);
1087 if (!current_output) {
1088 ELOG("Cannot get current output. This is a bug in i3.\n");
1092 Output *output = get_output_from_string(current_output, name);
1094 ELOG("Could not get output from string \"%s\"\n", name);
1099 Con *content = output_get_content(output->con);
1100 LOG("got output %p with content %p\n", output, content);
1102 Con *ws = con_get_workspace(current->con);
1103 LOG("should move workspace %p / %s\n", ws, ws->name);
1105 if (con_num_children(ws->parent) == 1) {
1106 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1108 /* check if we can find a workspace assigned to this output */
1109 bool used_assignment = false;
1110 struct Workspace_Assignment *assignment;
1111 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1112 if (strcmp(assignment->output, current_output->name) != 0)
1115 /* check if this workspace is already attached to the tree */
1116 Con *workspace = NULL, *out;
1117 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1118 GREP_FIRST(workspace, output_get_content(out),
1119 !strcasecmp(child->name, assignment->name));
1120 if (workspace != NULL)
1123 /* so create the workspace referenced to by this assignment */
1124 LOG("Creating workspace from assignment %s.\n", assignment->name);
1125 workspace_get(assignment->name, NULL);
1126 used_assignment = true;
1130 /* if we couldn't create the workspace using an assignment, create
1131 * it on the output */
1132 if (!used_assignment)
1133 create_workspace_on_output(current_output, ws->parent);
1135 /* notify the IPC listeners */
1136 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1139 /* detach from the old output and attach to the new output */
1140 bool workspace_was_visible = workspace_is_visible(ws);
1141 Con *old_content = ws->parent;
1143 if (workspace_was_visible) {
1144 /* The workspace which we just detached was visible, so focus
1145 * the next one in the focus-stack. */
1146 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1147 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1148 workspace_show(focus_ws);
1150 con_attach(ws, content, false);
1152 /* fix the coordinates of the floating containers */
1154 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1155 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1157 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1158 if (workspace_was_visible) {
1159 /* Focus the moved workspace on the destination output. */
1164 cmd_output->needs_tree_render = true;
1165 // XXX: default reply for now, make this a better reply
1170 * Implementation of 'split v|h|vertical|horizontal'.
1173 void cmd_split(I3_CMD, char *direction) {
1175 /* TODO: use matches */
1176 LOG("splitting in direction %c\n", direction[0]);
1177 if (match_is_empty(current_match))
1178 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1180 TAILQ_FOREACH(current, &owindows, owindows) {
1181 DLOG("matching: %p / %s\n", current->con, current->con->name);
1182 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1186 cmd_output->needs_tree_render = true;
1187 // XXX: default reply for now, make this a better reply
1192 * Implementaiton of 'kill [window|client]'.
1195 void cmd_kill(I3_CMD, char *kill_mode_str) {
1196 if (kill_mode_str == NULL)
1197 kill_mode_str = "window";
1200 DLOG("kill_mode=%s\n", kill_mode_str);
1203 if (strcmp(kill_mode_str, "window") == 0)
1204 kill_mode = KILL_WINDOW;
1205 else if (strcmp(kill_mode_str, "client") == 0)
1206 kill_mode = KILL_CLIENT;
1208 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1213 /* check if the match is empty, not if the result is empty */
1214 if (match_is_empty(current_match))
1215 tree_close_con(kill_mode);
1217 TAILQ_FOREACH(current, &owindows, owindows) {
1218 DLOG("matching: %p / %s\n", current->con, current->con->name);
1219 tree_close(current->con, kill_mode, false, false);
1223 cmd_output->needs_tree_render = true;
1224 // XXX: default reply for now, make this a better reply
1229 * Implementation of 'exec [--no-startup-id] <command>'.
1232 void cmd_exec(I3_CMD, char *nosn, char *command) {
1233 bool no_startup_id = (nosn != NULL);
1235 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1236 start_application(command, no_startup_id);
1238 // XXX: default reply for now, make this a better reply
1243 * Implementation of 'focus left|right|up|down'.
1246 void cmd_focus_direction(I3_CMD, char *direction) {
1248 focused->type != CT_WORKSPACE &&
1249 focused->fullscreen_mode != CF_NONE) {
1250 LOG("Cannot change focus while in fullscreen mode.\n");
1255 DLOG("direction = *%s*\n", direction);
1257 if (strcmp(direction, "left") == 0)
1258 tree_next('p', HORIZ);
1259 else if (strcmp(direction, "right") == 0)
1260 tree_next('n', HORIZ);
1261 else if (strcmp(direction, "up") == 0)
1262 tree_next('p', VERT);
1263 else if (strcmp(direction, "down") == 0)
1264 tree_next('n', VERT);
1266 ELOG("Invalid focus direction (%s)\n", direction);
1271 cmd_output->needs_tree_render = true;
1272 // XXX: default reply for now, make this a better reply
1277 * Implementation of 'focus tiling|floating|mode_toggle'.
1280 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1282 focused->type != CT_WORKSPACE &&
1283 focused->fullscreen_mode != CF_NONE) {
1284 LOG("Cannot change focus while in fullscreen mode.\n");
1289 DLOG("window_mode = %s\n", window_mode);
1291 Con *ws = con_get_workspace(focused);
1294 if (strcmp(window_mode, "mode_toggle") == 0) {
1295 current = TAILQ_FIRST(&(ws->focus_head));
1296 if (current != NULL && current->type == CT_FLOATING_CON)
1297 window_mode = "tiling";
1298 else window_mode = "floating";
1300 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1301 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1302 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1305 con_focus(con_descend_focused(current));
1310 cmd_output->needs_tree_render = true;
1311 // XXX: default reply for now, make this a better reply
1316 * Implementation of 'focus parent|child'.
1319 void cmd_focus_level(I3_CMD, char *level) {
1320 DLOG("level = %s\n", level);
1321 bool success = false;
1323 /* Focusing the parent can only be allowed if the newly
1324 * focused container won't escape the fullscreen container. */
1325 if (strcmp(level, "parent") == 0) {
1326 if (focused && focused->parent) {
1327 if (con_fullscreen_permits_focusing(focused->parent))
1328 success = level_up();
1330 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1334 /* Focusing a child should always be allowed. */
1335 else success = level_down();
1337 cmd_output->needs_tree_render = success;
1338 // XXX: default reply for now, make this a better reply
1343 * Implementation of 'focus'.
1346 void cmd_focus(I3_CMD) {
1347 DLOG("current_match = %p\n", current_match);
1349 if (match_is_empty(current_match)) {
1350 ELOG("You have to specify which window/container should be focused.\n");
1351 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1357 ystr("You have to specify which window/container should be focused");
1365 TAILQ_FOREACH(current, &owindows, owindows) {
1366 Con *ws = con_get_workspace(current->con);
1367 /* If no workspace could be found, this was a dock window.
1368 * Just skip it, you cannot focus dock windows. */
1372 /* Check the fullscreen focus constraints. */
1373 if (!con_fullscreen_permits_focusing(current->con)) {
1374 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1379 /* If the container is not on the current workspace,
1380 * workspace_show() will switch to a different workspace and (if
1381 * enabled) trigger a mouse pointer warp to the currently focused
1382 * container (!) on the target workspace.
1384 * Therefore, before calling workspace_show(), we make sure that
1385 * 'current' will be focused on the workspace. However, we cannot
1386 * just con_focus(current) because then the pointer will not be
1387 * warped at all (the code thinks we are already there).
1389 * So we focus 'current' to make it the currently focused window of
1390 * the target workspace, then revert focus. */
1391 Con *currently_focused = focused;
1392 con_focus(current->con);
1393 con_focus(currently_focused);
1395 /* Now switch to the workspace, then focus */
1397 LOG("focusing %p / %s\n", current->con, current->con->name);
1398 con_focus(current->con);
1403 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1404 "while only exactly one container can be focused at a time.\n", count);
1406 cmd_output->needs_tree_render = true;
1407 // XXX: default reply for now, make this a better reply
1412 * Implementation of 'fullscreen [global]'.
1415 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1416 if (fullscreen_mode == NULL)
1417 fullscreen_mode = "output";
1418 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1423 TAILQ_FOREACH(current, &owindows, owindows) {
1424 printf("matching: %p / %s\n", current->con, current->con->name);
1425 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1428 cmd_output->needs_tree_render = true;
1429 // XXX: default reply for now, make this a better reply
1434 * Implementation of 'move <direction> [<pixels> [px]]'.
1437 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1438 // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
1439 int px = atoi(move_px);
1441 /* TODO: make 'move' work with criteria. */
1442 DLOG("moving in direction %s, px %s\n", direction, move_px);
1443 if (con_is_floating(focused)) {
1444 DLOG("floating move with %d pixels\n", px);
1445 Rect newrect = focused->parent->rect;
1446 if (strcmp(direction, "left") == 0) {
1448 } else if (strcmp(direction, "right") == 0) {
1450 } else if (strcmp(direction, "up") == 0) {
1452 } else if (strcmp(direction, "down") == 0) {
1455 floating_reposition(focused->parent, newrect);
1457 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1458 (strcmp(direction, "left") == 0 ? D_LEFT :
1459 (strcmp(direction, "up") == 0 ? D_UP :
1461 cmd_output->needs_tree_render = true;
1464 // XXX: default reply for now, make this a better reply
1469 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1472 void cmd_layout(I3_CMD, char *layout_str) {
1473 if (strcmp(layout_str, "stacking") == 0)
1474 layout_str = "stacked";
1477 /* default is a special case which will be handled in con_set_layout(). */
1478 if (strcmp(layout_str, "default") == 0)
1480 else if (strcmp(layout_str, "stacked") == 0)
1482 else if (strcmp(layout_str, "tabbed") == 0)
1484 else if (strcmp(layout_str, "splitv") == 0)
1486 else if (strcmp(layout_str, "splith") == 0)
1489 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1493 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1495 /* check if the match is empty, not if the result is empty */
1496 if (match_is_empty(current_match))
1497 con_set_layout(focused, layout);
1499 TAILQ_FOREACH(current, &owindows, owindows) {
1500 DLOG("matching: %p / %s\n", current->con, current->con->name);
1501 con_set_layout(current->con, layout);
1505 cmd_output->needs_tree_render = true;
1506 // XXX: default reply for now, make this a better reply
1511 * Implementation of 'layout toggle [all|split]'.
1514 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1517 if (toggle_mode == NULL)
1518 toggle_mode = "default";
1520 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1522 /* check if the match is empty, not if the result is empty */
1523 if (match_is_empty(current_match))
1524 con_toggle_layout(focused, toggle_mode);
1526 TAILQ_FOREACH(current, &owindows, owindows) {
1527 DLOG("matching: %p / %s\n", current->con, current->con->name);
1528 con_toggle_layout(current->con, toggle_mode);
1532 cmd_output->needs_tree_render = true;
1533 // XXX: default reply for now, make this a better reply
1538 * Implementaiton of 'exit'.
1541 void cmd_exit(I3_CMD) {
1542 LOG("Exiting due to user command.\n");
1543 xcb_disconnect(conn);
1550 * Implementaiton of 'reload'.
1553 void cmd_reload(I3_CMD) {
1555 kill_configerror_nagbar(false);
1556 kill_commanderror_nagbar(false);
1557 load_configuration(conn, NULL, true);
1559 /* Send an IPC event just in case the ws names have changed */
1560 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1562 // XXX: default reply for now, make this a better reply
1567 * Implementaiton of 'restart'.
1570 void cmd_restart(I3_CMD) {
1571 LOG("restarting i3\n");
1574 // XXX: default reply for now, make this a better reply
1579 * Implementaiton of 'open'.
1582 void cmd_open(I3_CMD) {
1583 LOG("opening new container\n");
1584 Con *con = tree_open_con(NULL, NULL);
1585 con->layout = L_SPLITH;
1592 y(integer, (long int)con);
1595 cmd_output->needs_tree_render = true;
1599 * Implementation of 'focus output <output>'.
1602 void cmd_focus_output(I3_CMD, char *name) {
1605 DLOG("name = %s\n", name);
1609 /* get the output */
1610 Output *current_output = NULL;
1613 TAILQ_FOREACH(current, &owindows, owindows)
1614 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1615 assert(current_output != NULL);
1617 output = get_output_from_string(current_output, name);
1620 LOG("No such output found.\n");
1625 /* get visible workspace on output */
1627 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1635 cmd_output->needs_tree_render = true;
1636 // XXX: default reply for now, make this a better reply
1641 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1644 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1649 if (!con_is_floating(focused)) {
1650 ELOG("Cannot change position. The window/container is not floating\n");
1655 ystr("Cannot change position. The window/container is not floating.");
1660 if (strcmp(method, "absolute") == 0) {
1661 focused->parent->rect.x = x;
1662 focused->parent->rect.y = y;
1664 DLOG("moving to absolute position %d %d\n", x, y);
1665 floating_maybe_reassign_ws(focused->parent);
1666 cmd_output->needs_tree_render = true;
1669 if (strcmp(method, "position") == 0) {
1670 Rect newrect = focused->parent->rect;
1672 DLOG("moving to position %d %d\n", x, y);
1676 floating_reposition(focused->parent, newrect);
1679 // XXX: default reply for now, make this a better reply
1684 * Implementation of 'move [window|container] [to] [absolute] position center
1687 void cmd_move_window_to_center(I3_CMD, char *method) {
1689 if (!con_is_floating(focused)) {
1690 ELOG("Cannot change position. The window/container is not floating\n");
1695 ystr("Cannot change position. The window/container is not floating.");
1699 if (strcmp(method, "absolute") == 0) {
1700 Rect *rect = &focused->parent->rect;
1702 DLOG("moving to absolute center\n");
1703 rect->x = croot->rect.width/2 - rect->width/2;
1704 rect->y = croot->rect.height/2 - rect->height/2;
1706 floating_maybe_reassign_ws(focused->parent);
1707 cmd_output->needs_tree_render = true;
1710 if (strcmp(method, "position") == 0) {
1711 Rect *wsrect = &con_get_workspace(focused)->rect;
1712 Rect newrect = focused->parent->rect;
1714 DLOG("moving to center\n");
1715 newrect.x = wsrect->width/2 - newrect.width/2;
1716 newrect.y = wsrect->height/2 - newrect.height/2;
1718 floating_reposition(focused->parent, newrect);
1721 // XXX: default reply for now, make this a better reply
1726 * Implementation of 'move scratchpad'.
1729 void cmd_move_scratchpad(I3_CMD) {
1730 DLOG("should move window to scratchpad\n");
1735 TAILQ_FOREACH(current, &owindows, owindows) {
1736 DLOG("matching: %p / %s\n", current->con, current->con->name);
1737 scratchpad_move(current->con);
1740 cmd_output->needs_tree_render = true;
1741 // XXX: default reply for now, make this a better reply
1746 * Implementation of 'scratchpad show'.
1749 void cmd_scratchpad_show(I3_CMD) {
1750 DLOG("should show scratchpad window\n");
1753 if (match_is_empty(current_match)) {
1754 scratchpad_show(NULL);
1756 TAILQ_FOREACH(current, &owindows, owindows) {
1757 DLOG("matching: %p / %s\n", current->con, current->con->name);
1758 scratchpad_show(current->con);
1762 cmd_output->needs_tree_render = true;
1763 // XXX: default reply for now, make this a better reply
1768 * Implementation of 'rename workspace <name> to <name>'
1771 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1772 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1774 Con *output, *workspace = NULL;
1775 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1776 GREP_FIRST(workspace, output_get_content(output),
1777 !strcasecmp(child->name, old_name));
1780 // TODO: we should include the old workspace name here and use yajl for
1781 // generating the reply.
1786 // TODO: better error message
1787 ystr("Old workspace not found");
1792 Con *check_dest = NULL;
1793 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1794 GREP_FIRST(check_dest, output_get_content(output),
1795 !strcasecmp(child->name, new_name));
1797 if (check_dest != NULL) {
1798 // TODO: we should include the new workspace name here and use yajl for
1799 // generating the reply.
1804 // TODO: better error message
1805 ystr("New workspace already exists");
1810 /* Change the name and try to parse it as a number. */
1811 FREE(workspace->name);
1812 workspace->name = sstrdup(new_name);
1813 char *endptr = NULL;
1814 long parsed_num = strtol(new_name, &endptr, 10);
1815 if (parsed_num == LONG_MIN ||
1816 parsed_num == LONG_MAX ||
1819 workspace->num = -1;
1820 else workspace->num = parsed_num;
1821 LOG("num = %d\n", workspace->num);
1823 /* By re-attaching, the sort order will be correct afterwards. */
1824 Con *previously_focused = focused;
1825 Con *parent = workspace->parent;
1826 con_detach(workspace);
1827 con_attach(workspace, parent, false);
1828 /* Restore the previous focus since con_attach messes with the focus. */
1829 con_focus(previously_focused);
1831 cmd_output->needs_tree_render = true;
1834 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");