2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * commands.c: all command functions (see commands_parser.c)
15 /** When the command did not include match criteria (!), we use the currently
16 * focused command. Do not confuse this case with a command which included
17 * criteria but which did not match any windows. This macro has to be called in
20 #define HANDLE_EMPTY_MATCH do { \
21 if (match_is_empty(current_match)) { \
22 owindow *ow = smalloc(sizeof(owindow)); \
24 TAILQ_INIT(&owindows); \
25 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
29 static owindows_head owindows;
32 * Returns true if a is definitely greater than b (using the given epsilon)
35 static bool definitelyGreaterThan(float a, float b, float epsilon) {
36 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
40 * Returns an 'output' corresponding to one of left/right/down/up or a specific
44 static Output *get_output_from_string(Output *current_output, const char *output_str) {
47 if (strcasecmp(output_str, "left") == 0) {
48 output = get_output_next(D_LEFT, current_output);
50 output = get_output_most(D_RIGHT, current_output);
51 } else if (strcasecmp(output_str, "right") == 0) {
52 output = get_output_next(D_RIGHT, current_output);
54 output = get_output_most(D_LEFT, current_output);
55 } else if (strcasecmp(output_str, "up") == 0) {
56 output = get_output_next(D_UP, current_output);
58 output = get_output_most(D_DOWN, current_output);
59 } else if (strcasecmp(output_str, "down") == 0) {
60 output = get_output_next(D_DOWN, current_output);
62 output = get_output_most(D_UP, current_output);
63 } else output = get_output_by_name(output_str);
68 // This code is commented out because we might recycle it for popping up error
69 // messages on parser errors.
71 static pid_t migration_pid = -1;
74 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
75 * it exited (or could not be started, depending on the exit code).
78 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
79 ev_child_stop(EV_A_ watcher);
80 if (!WIFEXITED(watcher->rstatus)) {
81 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
85 int exitcode = WEXITSTATUS(watcher->rstatus);
86 printf("i3-nagbar process exited with status %d\n", exitcode);
88 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
94 /* We need ev >= 4 for the following code. Since it is not *that* important (it
95 * only makes sure that there are no i3-nagbar instances left behind) we still
96 * support old systems with libev 3. */
97 #if EV_VERSION_MAJOR >= 4
99 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
100 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
103 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
104 if (migration_pid != -1) {
105 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
106 kill(migration_pid, SIGKILL);
111 void cmd_MIGRATION_start_nagbar(void) {
112 if (migration_pid != -1) {
113 fprintf(stderr, "i3-nagbar already running.\n");
116 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
117 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
118 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
119 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
120 migration_pid = fork();
121 if (migration_pid == -1) {
122 warn("Could not fork()");
127 if (migration_pid == 0) {
129 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
131 NULL, /* will be replaced by the executable path */
135 "You found a parsing error. Please, please, please, report it!",
141 exec_i3_utility("i3-nagbar", argv);
145 /* install a child watcher */
146 ev_child *child = smalloc(sizeof(ev_child));
147 ev_child_init(child, &nagbar_exited, migration_pid, 0);
148 ev_child_start(main_loop, child);
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151 * only makes sure that there are no i3-nagbar instances left behind) we still
152 * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
154 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
156 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
157 ev_cleanup_init(cleanup, nagbar_cleanup);
158 ev_cleanup_start(main_loop, cleanup);
164 /*******************************************************************************
165 * Criteria functions.
166 ******************************************************************************/
169 * Initializes the specified 'Match' data structure and the initial state of
170 * commands.c for matching target windows of a command.
173 void cmd_criteria_init(I3_CMD) {
177 DLOG("Initializing criteria, current_match = %p\n", current_match);
178 match_init(current_match);
179 while (!TAILQ_EMPTY(&owindows)) {
180 ow = TAILQ_FIRST(&owindows);
181 TAILQ_REMOVE(&owindows, ow, owindows);
184 TAILQ_INIT(&owindows);
186 TAILQ_FOREACH(con, &all_cons, all_cons) {
187 ow = smalloc(sizeof(owindow));
189 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
194 * A match specification just finished (the closing square bracket was found),
195 * so we filter the list of owindows.
198 void cmd_criteria_match_windows(I3_CMD) {
199 owindow *next, *current;
201 DLOG("match specification finished, matching...\n");
202 /* copy the old list head to iterate through it and start with a fresh
203 * list which will contain only matching windows */
204 struct owindows_head old = owindows;
205 TAILQ_INIT(&owindows);
206 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
207 /* make a copy of the next pointer and advance the pointer to the
208 * next element as we are going to invalidate the element’s
209 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
211 next = TAILQ_NEXT(next, owindows);
213 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
214 if (current_match->con_id != NULL) {
215 if (current_match->con_id == current->con) {
216 DLOG("matches container!\n");
217 TAILQ_INSERT_TAIL(&owindows, current, owindows);
219 } else if (current_match->mark != NULL && current->con->mark != NULL &&
220 regex_matches(current_match->mark, current->con->mark)) {
221 DLOG("match by mark\n");
222 TAILQ_INSERT_TAIL(&owindows, current, owindows);
224 if (current->con->window == NULL)
226 if (match_matches_window(current_match, current->con->window)) {
227 DLOG("matches window!\n");
228 TAILQ_INSERT_TAIL(&owindows, current, owindows);
230 DLOG("doesnt match\n");
236 TAILQ_FOREACH(current, &owindows, owindows) {
237 DLOG("matching: %p / %s\n", current->con, current->con->name);
242 * Interprets a ctype=cvalue pair and adds it to the current match
246 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
247 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
249 if (strcmp(ctype, "class") == 0) {
250 current_match->class = regex_new(cvalue);
254 if (strcmp(ctype, "instance") == 0) {
255 current_match->instance = regex_new(cvalue);
259 if (strcmp(ctype, "window_role") == 0) {
260 current_match->role = regex_new(cvalue);
264 if (strcmp(ctype, "con_id") == 0) {
266 long parsed = strtol(cvalue, &end, 10);
267 if (parsed == LONG_MIN ||
268 parsed == LONG_MAX ||
270 (end && *end != '\0')) {
271 ELOG("Could not parse con id \"%s\"\n", cvalue);
273 current_match->con_id = (Con*)parsed;
274 printf("id as int = %p\n", current_match->con_id);
279 if (strcmp(ctype, "id") == 0) {
281 long parsed = strtol(cvalue, &end, 10);
282 if (parsed == LONG_MIN ||
283 parsed == LONG_MAX ||
285 (end && *end != '\0')) {
286 ELOG("Could not parse window id \"%s\"\n", cvalue);
288 current_match->id = parsed;
289 printf("window id as int = %d\n", current_match->id);
294 if (strcmp(ctype, "con_mark") == 0) {
295 current_match->mark = regex_new(cvalue);
299 if (strcmp(ctype, "title") == 0) {
300 current_match->title = regex_new(cvalue);
304 if (strcmp(ctype, "urgent") == 0) {
305 if (strcasecmp(cvalue, "latest") == 0 ||
306 strcasecmp(cvalue, "newest") == 0 ||
307 strcasecmp(cvalue, "recent") == 0 ||
308 strcasecmp(cvalue, "last") == 0) {
309 current_match->urgent = U_LATEST;
310 } else if (strcasecmp(cvalue, "oldest") == 0 ||
311 strcasecmp(cvalue, "first") == 0) {
312 current_match->urgent = U_OLDEST;
317 ELOG("Unknown criterion: %s\n", ctype);
321 * Implementation of 'move [window|container] [to] workspace
322 * next|prev|next_on_output|prev_on_output'.
325 void cmd_move_con_to_workspace(I3_CMD, char *which) {
328 DLOG("which=%s\n", which);
332 /* get the workspace */
334 if (strcmp(which, "next") == 0)
335 ws = workspace_next();
336 else if (strcmp(which, "prev") == 0)
337 ws = workspace_prev();
338 else if (strcmp(which, "next_on_output") == 0)
339 ws = workspace_next_on_output();
340 else if (strcmp(which, "prev_on_output") == 0)
341 ws = workspace_prev_on_output();
343 ELOG("BUG: called with which=%s\n", which);
344 cmd_output->json_output = sstrdup("{\"sucess\": false}");
348 TAILQ_FOREACH(current, &owindows, owindows) {
349 DLOG("matching: %p / %s\n", current->con, current->con->name);
350 con_move_to_workspace(current->con, ws, true, false);
353 cmd_output->needs_tree_render = true;
354 // XXX: default reply for now, make this a better reply
355 cmd_output->json_output = sstrdup("{\"success\": true}");
359 * Implementation of 'move [window|container] [to] workspace <name>'.
362 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
363 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
364 LOG("You cannot switch to the i3 internal workspaces.\n");
365 cmd_output->json_output = sstrdup("{\"sucess\": false}");
371 /* Error out early to not create a non-existing workspace (in
372 * workspace_get()) if we are not actually able to move anything. */
373 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
374 cmd_output->json_output = sstrdup("{\"sucess\": false}");
378 LOG("should move window to workspace %s\n", name);
379 /* get the workspace */
380 Con *ws = workspace_get(name, NULL);
384 TAILQ_FOREACH(current, &owindows, owindows) {
385 DLOG("matching: %p / %s\n", current->con, current->con->name);
386 con_move_to_workspace(current->con, ws, true, false);
389 cmd_output->needs_tree_render = true;
390 // XXX: default reply for now, make this a better reply
391 cmd_output->json_output = sstrdup("{\"success\": true}");
395 * Implementation of 'move [window|container] [to] workspace number <number>'.
398 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
401 /* Error out early to not create a non-existing workspace (in
402 * workspace_get()) if we are not actually able to move anything. */
403 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
404 cmd_output->json_output = sstrdup("{\"sucess\": false}");
408 LOG("should move window to workspace with number %d\n", which);
409 /* get the workspace */
410 Con *output, *workspace = NULL;
413 long parsed_num = strtol(which, &endptr, 10);
414 if (parsed_num == LONG_MIN ||
415 parsed_num == LONG_MAX ||
418 LOG("Could not parse \"%s\" as a number.\n", which);
419 cmd_output->json_output = sstrdup("{\"success\": false, "
420 "\"error\": \"Could not parse number\"}");
424 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
425 GREP_FIRST(workspace, output_get_content(output),
426 child->num == parsed_num);
429 cmd_output->json_output = sstrdup("{\"success\": false, "
430 "\"error\": \"No such workspace\"}");
436 TAILQ_FOREACH(current, &owindows, owindows) {
437 DLOG("matching: %p / %s\n", current->con, current->con->name);
438 con_move_to_workspace(current->con, workspace, true, false);
441 cmd_output->needs_tree_render = true;
442 // XXX: default reply for now, make this a better reply
443 cmd_output->json_output = sstrdup("{\"success\": true}");
446 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
447 LOG("floating resize\n");
448 if (strcmp(direction, "up") == 0) {
449 floating_con->rect.y -= px;
450 floating_con->rect.height += px;
451 } else if (strcmp(direction, "down") == 0) {
452 floating_con->rect.height += px;
453 } else if (strcmp(direction, "left") == 0) {
454 floating_con->rect.x -= px;
455 floating_con->rect.width += px;
457 floating_con->rect.width += px;
461 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
462 LOG("tiling resize\n");
463 /* get the appropriate current container (skip stacked/tabbed cons) */
464 Con *current = focused;
465 while (current->parent->layout == L_STACKED ||
466 current->parent->layout == L_TABBED)
467 current = current->parent;
469 /* Then further go up until we find one with the matching orientation. */
470 orientation_t search_orientation =
471 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
473 while (current->type != CT_WORKSPACE &&
474 current->type != CT_FLOATING_CON &&
475 current->parent->orientation != search_orientation)
476 current = current->parent;
478 /* get the default percentage */
479 int children = con_num_children(current->parent);
481 LOG("ins. %d children\n", children);
482 double percentage = 1.0 / children;
483 LOG("default percentage = %f\n", percentage);
485 orientation_t orientation = current->parent->orientation;
487 if ((orientation == HORIZ &&
488 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
489 (orientation == VERT &&
490 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
491 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
492 (orientation == HORIZ ? "horizontal" : "vertical"));
493 cmd_output->json_output = sstrdup("{\"sucess\": false}");
497 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
498 other = TAILQ_PREV(current, nodes_head, nodes);
500 other = TAILQ_NEXT(current, nodes);
502 if (other == TAILQ_END(workspaces)) {
503 LOG("No other container in this direction found, cannot resize.\n");
504 cmd_output->json_output = sstrdup("{\"sucess\": false}");
507 LOG("other->percent = %f\n", other->percent);
508 LOG("current->percent before = %f\n", current->percent);
509 if (current->percent == 0.0)
510 current->percent = percentage;
511 if (other->percent == 0.0)
512 other->percent = percentage;
513 double new_current_percent = current->percent + ((double)ppt / 100.0);
514 double new_other_percent = other->percent - ((double)ppt / 100.0);
515 LOG("new_current_percent = %f\n", new_current_percent);
516 LOG("new_other_percent = %f\n", new_other_percent);
517 /* Ensure that the new percentages are positive and greater than
518 * 0.05 to have a reasonable minimum size. */
519 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
520 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
521 current->percent += ((double)ppt / 100.0);
522 other->percent -= ((double)ppt / 100.0);
523 LOG("current->percent after = %f\n", current->percent);
524 LOG("other->percent after = %f\n", other->percent);
526 LOG("Not resizing, already at minimum size\n");
530 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
531 LOG("width/height resize\n");
532 /* get the appropriate current container (skip stacked/tabbed cons) */
533 Con *current = focused;
534 while (current->parent->layout == L_STACKED ||
535 current->parent->layout == L_TABBED)
536 current = current->parent;
538 /* Then further go up until we find one with the matching orientation. */
539 orientation_t search_orientation =
540 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
542 while (current->type != CT_WORKSPACE &&
543 current->type != CT_FLOATING_CON &&
544 current->parent->orientation != search_orientation)
545 current = current->parent;
547 /* get the default percentage */
548 int children = con_num_children(current->parent);
549 LOG("ins. %d children\n", children);
550 double percentage = 1.0 / children;
551 LOG("default percentage = %f\n", percentage);
553 orientation_t orientation = current->parent->orientation;
555 if ((orientation == HORIZ &&
556 strcmp(direction, "height") == 0) ||
557 (orientation == VERT &&
558 strcmp(direction, "width") == 0)) {
559 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
560 (orientation == HORIZ ? "horizontal" : "vertical"));
561 cmd_output->json_output = sstrdup("{\"sucess\": false}");
566 LOG("This is the only container, cannot resize.\n");
567 cmd_output->json_output = sstrdup("{\"sucess\": false}");
571 /* Ensure all the other children have a percentage set. */
573 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
574 LOG("child->percent = %f (child %p)\n", child->percent, child);
575 if (child->percent == 0.0)
576 child->percent = percentage;
579 double new_current_percent = current->percent + ((double)ppt / 100.0);
580 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
581 LOG("new_current_percent = %f\n", new_current_percent);
582 LOG("subtract_percent = %f\n", subtract_percent);
583 /* Ensure that the new percentages are positive and greater than
584 * 0.05 to have a reasonable minimum size. */
585 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
586 if (child == current)
588 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
589 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
590 cmd_output->json_output = sstrdup("{\"sucess\": false}");
594 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
595 LOG("Not resizing, already at minimum size\n");
596 cmd_output->json_output = sstrdup("{\"sucess\": false}");
600 current->percent += ((double)ppt / 100.0);
601 LOG("current->percent after = %f\n", current->percent);
603 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
604 if (child == current)
606 child->percent -= subtract_percent;
607 LOG("child->percent after (%p) = %f\n", child, child->percent);
612 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
615 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
616 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
617 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
618 // 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
619 int px = atoi(resize_px);
620 int ppt = atoi(resize_ppt);
621 if (strcmp(way, "shrink") == 0) {
627 if ((floating_con = con_inside_floating(focused))) {
628 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
630 if (strcmp(direction, "width") == 0 ||
631 strcmp(direction, "height") == 0)
632 cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
633 else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
636 cmd_output->needs_tree_render = true;
637 // XXX: default reply for now, make this a better reply
638 cmd_output->json_output = sstrdup("{\"success\": true}");
642 * Implementation of 'border normal|none|1pixel|toggle'.
645 void cmd_border(I3_CMD, char *border_style_str) {
646 DLOG("border style should be changed to %s\n", border_style_str);
651 TAILQ_FOREACH(current, &owindows, owindows) {
652 DLOG("matching: %p / %s\n", current->con, current->con->name);
653 int border_style = current->con->border_style;
654 if (strcmp(border_style_str, "toggle") == 0) {
658 if (strcmp(border_style_str, "normal") == 0)
659 border_style = BS_NORMAL;
660 else if (strcmp(border_style_str, "none") == 0)
661 border_style = BS_NONE;
662 else if (strcmp(border_style_str, "1pixel") == 0)
663 border_style = BS_1PIXEL;
665 ELOG("BUG: called with border_style=%s\n", border_style_str);
666 cmd_output->json_output = sstrdup("{\"sucess\": false}");
670 con_set_border_style(current->con, border_style);
673 cmd_output->needs_tree_render = true;
674 // XXX: default reply for now, make this a better reply
675 cmd_output->json_output = sstrdup("{\"success\": true}");
679 * Implementation of 'nop <comment>'.
682 void cmd_nop(I3_CMD, char *comment) {
683 LOG("-------------------------------------------------\n");
684 LOG(" NOP: %s\n", comment);
685 LOG("-------------------------------------------------\n");
689 * Implementation of 'append_layout <path>'.
692 void cmd_append_layout(I3_CMD, char *path) {
693 LOG("Appending layout \"%s\"\n", path);
694 tree_append_json(path);
696 cmd_output->needs_tree_render = true;
697 // XXX: default reply for now, make this a better reply
698 cmd_output->json_output = sstrdup("{\"success\": true}");
702 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
705 void cmd_workspace(I3_CMD, char *which) {
708 DLOG("which=%s\n", which);
710 if (strcmp(which, "next") == 0)
711 ws = workspace_next();
712 else if (strcmp(which, "prev") == 0)
713 ws = workspace_prev();
714 else if (strcmp(which, "next_on_output") == 0)
715 ws = workspace_next_on_output();
716 else if (strcmp(which, "prev_on_output") == 0)
717 ws = workspace_prev_on_output();
719 ELOG("BUG: called with which=%s\n", which);
720 cmd_output->json_output = sstrdup("{\"sucess\": false}");
726 cmd_output->needs_tree_render = true;
727 // XXX: default reply for now, make this a better reply
728 cmd_output->json_output = sstrdup("{\"success\": true}");
732 * Implementation of 'workspace number <number>'
735 void cmd_workspace_number(I3_CMD, char *which) {
736 Con *output, *workspace = NULL;
739 long parsed_num = strtol(which, &endptr, 10);
740 if (parsed_num == LONG_MIN ||
741 parsed_num == LONG_MAX ||
744 LOG("Could not parse \"%s\" as a number.\n", which);
745 cmd_output->json_output = sstrdup("{\"success\": false, "
746 "\"error\": \"Could not parse number\"}");
750 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
751 GREP_FIRST(workspace, output_get_content(output),
752 child->num == parsed_num);
755 LOG("There is no workspace with number %d.\n", parsed_num);
756 cmd_output->json_output = sstrdup("{\"success\": false, "
757 "\"error\": \"No such workspace\"}");
761 workspace_show(workspace);
763 cmd_output->needs_tree_render = true;
764 // XXX: default reply for now, make this a better reply
765 cmd_output->json_output = sstrdup("{\"success\": true}");
769 * Implementation of 'workspace back_and_forth'.
772 void cmd_workspace_back_and_forth(I3_CMD) {
773 workspace_back_and_forth();
775 cmd_output->needs_tree_render = true;
776 // XXX: default reply for now, make this a better reply
777 cmd_output->json_output = sstrdup("{\"success\": true}");
781 * Implementation of 'workspace <name>'
784 void cmd_workspace_name(I3_CMD, char *name) {
785 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
786 LOG("You cannot switch to the i3 internal workspaces.\n");
787 cmd_output->json_output = sstrdup("{\"sucess\": false}");
791 DLOG("should switch to workspace %s\n", name);
793 Con *ws = con_get_workspace(focused);
795 /* Check if the command wants to switch to the current workspace */
796 if (strcmp(ws->name, name) == 0) {
797 DLOG("This workspace is already focused.\n");
798 if (config.workspace_auto_back_and_forth) {
799 workspace_back_and_forth();
802 cmd_output->json_output = sstrdup("{\"sucess\": false}");
806 workspace_show_by_name(name);
808 cmd_output->needs_tree_render = true;
809 // XXX: default reply for now, make this a better reply
810 cmd_output->json_output = sstrdup("{\"success\": true}");
814 * Implementation of 'mark <mark>'
817 void cmd_mark(I3_CMD, char *mark) {
818 DLOG("Clearing all windows which have that mark first\n");
821 TAILQ_FOREACH(con, &all_cons, all_cons) {
822 if (con->mark && strcmp(con->mark, mark) == 0)
826 DLOG("marking window with str %s\n", mark);
831 TAILQ_FOREACH(current, &owindows, owindows) {
832 DLOG("matching: %p / %s\n", current->con, current->con->name);
833 current->con->mark = sstrdup(mark);
836 cmd_output->needs_tree_render = true;
837 // XXX: default reply for now, make this a better reply
838 cmd_output->json_output = sstrdup("{\"success\": true}");
842 * Implementation of 'mode <string>'.
845 void cmd_mode(I3_CMD, char *mode) {
846 DLOG("mode=%s\n", mode);
849 // XXX: default reply for now, make this a better reply
850 cmd_output->json_output = sstrdup("{\"success\": true}");
854 * Implementation of 'move [window|container] [to] output <str>'.
857 void cmd_move_con_to_output(I3_CMD, char *name) {
860 DLOG("should move window to output %s\n", name);
865 Output *current_output = NULL;
868 // TODO: fix the handling of criteria
869 TAILQ_FOREACH(current, &owindows, owindows)
870 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
872 assert(current_output != NULL);
874 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
875 if (strcasecmp(name, "up") == 0)
876 output = get_output_next(D_UP, current_output);
877 else if (strcasecmp(name, "down") == 0)
878 output = get_output_next(D_DOWN, current_output);
879 else if (strcasecmp(name, "left") == 0)
880 output = get_output_next(D_LEFT, current_output);
881 else if (strcasecmp(name, "right") == 0)
882 output = get_output_next(D_RIGHT, current_output);
884 output = get_output_by_name(name);
887 LOG("No such output found.\n");
888 cmd_output->json_output = sstrdup("{\"sucess\": false}");
892 /* get visible workspace on output */
894 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
896 cmd_output->json_output = sstrdup("{\"sucess\": false}");
900 TAILQ_FOREACH(current, &owindows, owindows) {
901 DLOG("matching: %p / %s\n", current->con, current->con->name);
902 con_move_to_workspace(current->con, ws, true, false);
905 cmd_output->needs_tree_render = true;
906 // XXX: default reply for now, make this a better reply
907 cmd_output->json_output = sstrdup("{\"success\": true}");
911 * Implementation of 'floating enable|disable|toggle'
914 void cmd_floating(I3_CMD, char *floating_mode) {
917 DLOG("floating_mode=%s\n", floating_mode);
921 TAILQ_FOREACH(current, &owindows, owindows) {
922 DLOG("matching: %p / %s\n", current->con, current->con->name);
923 if (strcmp(floating_mode, "toggle") == 0) {
924 DLOG("should toggle mode\n");
925 toggle_floating_mode(current->con, false);
927 DLOG("should switch mode to %s\n", floating_mode);
928 if (strcmp(floating_mode, "enable") == 0) {
929 floating_enable(current->con, false);
931 floating_disable(current->con, false);
936 cmd_output->needs_tree_render = true;
937 // XXX: default reply for now, make this a better reply
938 cmd_output->json_output = sstrdup("{\"success\": true}");
942 * Implementation of 'move workspace to [output] <str>'.
945 void cmd_move_workspace_to_output(I3_CMD, char *name) {
946 DLOG("should move workspace to output %s\n", name);
951 TAILQ_FOREACH(current, &owindows, owindows) {
952 Output *current_output = get_output_containing(current->con->rect.x,
953 current->con->rect.y);
954 Output *output = get_output_from_string(current_output, name);
956 LOG("No such output\n");
957 cmd_output->json_output = sstrdup("{\"sucess\": false}");
961 Con *content = output_get_content(output->con);
962 LOG("got output %p with content %p\n", output, content);
964 Con *ws = con_get_workspace(current->con);
965 LOG("should move workspace %p / %s\n", ws, ws->name);
967 if (con_num_children(ws->parent) == 1) {
968 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
970 /* check if we can find a workspace assigned to this output */
971 bool used_assignment = false;
972 struct Workspace_Assignment *assignment;
973 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
974 if (strcmp(assignment->output, current_output->name) != 0)
977 /* check if this workspace is already attached to the tree */
978 Con *workspace = NULL, *out;
979 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
980 GREP_FIRST(workspace, output_get_content(out),
981 !strcasecmp(child->name, assignment->name));
982 if (workspace != NULL)
985 /* so create the workspace referenced to by this assignment */
986 LOG("Creating workspace from assignment %s.\n", assignment->name);
987 workspace_get(assignment->name, NULL);
988 used_assignment = true;
992 /* if we couldn't create the workspace using an assignment, create
993 * it on the output */
994 if (!used_assignment)
995 create_workspace_on_output(current_output, ws->parent);
997 /* notify the IPC listeners */
998 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1001 /* detach from the old output and attach to the new output */
1002 bool workspace_was_visible = workspace_is_visible(ws);
1003 Con *old_content = ws->parent;
1005 if (workspace_was_visible) {
1006 /* The workspace which we just detached was visible, so focus
1007 * the next one in the focus-stack. */
1008 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1009 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1010 workspace_show(focus_ws);
1012 con_attach(ws, content, false);
1014 /* fix the coordinates of the floating containers */
1016 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1017 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1019 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1020 if (workspace_was_visible) {
1021 /* Focus the moved workspace on the destination output. */
1026 cmd_output->needs_tree_render = true;
1027 // XXX: default reply for now, make this a better reply
1028 cmd_output->json_output = sstrdup("{\"success\": true}");
1032 * Implementation of 'split v|h|vertical|horizontal'.
1035 void cmd_split(I3_CMD, char *direction) {
1036 /* TODO: use matches */
1037 LOG("splitting in direction %c\n", direction[0]);
1038 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1040 cmd_output->needs_tree_render = true;
1041 // XXX: default reply for now, make this a better reply
1042 cmd_output->json_output = sstrdup("{\"success\": true}");
1046 * Implementaiton of 'kill [window|client]'.
1049 void cmd_kill(I3_CMD, char *kill_mode_str) {
1050 if (kill_mode_str == NULL)
1051 kill_mode_str = "window";
1054 DLOG("kill_mode=%s\n", kill_mode_str);
1057 if (strcmp(kill_mode_str, "window") == 0)
1058 kill_mode = KILL_WINDOW;
1059 else if (strcmp(kill_mode_str, "client") == 0)
1060 kill_mode = KILL_CLIENT;
1062 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1063 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1067 /* check if the match is empty, not if the result is empty */
1068 if (match_is_empty(current_match))
1069 tree_close_con(kill_mode);
1071 TAILQ_FOREACH(current, &owindows, owindows) {
1072 DLOG("matching: %p / %s\n", current->con, current->con->name);
1073 tree_close(current->con, kill_mode, false, false);
1077 cmd_output->needs_tree_render = true;
1078 // XXX: default reply for now, make this a better reply
1079 cmd_output->json_output = sstrdup("{\"success\": true}");
1083 * Implementation of 'exec [--no-startup-id] <command>'.
1086 void cmd_exec(I3_CMD, char *nosn, char *command) {
1087 bool no_startup_id = (nosn != NULL);
1089 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1090 start_application(command, no_startup_id);
1092 // XXX: default reply for now, make this a better reply
1093 cmd_output->json_output = sstrdup("{\"success\": true}");
1097 * Implementation of 'focus left|right|up|down'.
1100 void cmd_focus_direction(I3_CMD, char *direction) {
1102 focused->type != CT_WORKSPACE &&
1103 focused->fullscreen_mode != CF_NONE) {
1104 LOG("Cannot change focus while in fullscreen mode.\n");
1105 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1109 DLOG("direction = *%s*\n", direction);
1111 if (strcmp(direction, "left") == 0)
1112 tree_next('p', HORIZ);
1113 else if (strcmp(direction, "right") == 0)
1114 tree_next('n', HORIZ);
1115 else if (strcmp(direction, "up") == 0)
1116 tree_next('p', VERT);
1117 else if (strcmp(direction, "down") == 0)
1118 tree_next('n', VERT);
1120 ELOG("Invalid focus direction (%s)\n", direction);
1121 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1125 cmd_output->needs_tree_render = true;
1126 // XXX: default reply for now, make this a better reply
1127 cmd_output->json_output = sstrdup("{\"success\": true}");
1131 * Implementation of 'focus tiling|floating|mode_toggle'.
1134 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1136 focused->type != CT_WORKSPACE &&
1137 focused->fullscreen_mode != CF_NONE) {
1138 LOG("Cannot change focus while in fullscreen mode.\n");
1139 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1143 DLOG("window_mode = %s\n", window_mode);
1145 Con *ws = con_get_workspace(focused);
1148 if (strcmp(window_mode, "mode_toggle") == 0) {
1149 current = TAILQ_FIRST(&(ws->focus_head));
1150 if (current != NULL && current->type == CT_FLOATING_CON)
1151 window_mode = "tiling";
1152 else window_mode = "floating";
1154 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1155 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1156 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1159 con_focus(con_descend_focused(current));
1164 cmd_output->needs_tree_render = true;
1165 // XXX: default reply for now, make this a better reply
1166 cmd_output->json_output = sstrdup("{\"success\": true}");
1170 * Implementation of 'focus parent|child'.
1173 void cmd_focus_level(I3_CMD, char *level) {
1175 focused->type != CT_WORKSPACE &&
1176 focused->fullscreen_mode != CF_NONE) {
1177 LOG("Cannot change focus while in fullscreen mode.\n");
1178 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1182 DLOG("level = %s\n", level);
1184 if (strcmp(level, "parent") == 0)
1188 cmd_output->needs_tree_render = true;
1189 // XXX: default reply for now, make this a better reply
1190 cmd_output->json_output = sstrdup("{\"success\": true}");
1194 * Implementation of 'focus'.
1197 void cmd_focus(I3_CMD) {
1198 DLOG("current_match = %p\n", current_match);
1200 focused->type != CT_WORKSPACE &&
1201 focused->fullscreen_mode != CF_NONE) {
1202 LOG("Cannot change focus while in fullscreen mode.\n");
1203 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1209 if (match_is_empty(current_match)) {
1210 ELOG("You have to specify which window/container should be focused.\n");
1211 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1213 sasprintf(&(cmd_output->json_output),
1214 "{\"success\":false, \"error\":\"You have to "
1215 "specify which window/container should be focused\"}");
1220 TAILQ_FOREACH(current, &owindows, owindows) {
1221 Con *ws = con_get_workspace(current->con);
1222 /* If no workspace could be found, this was a dock window.
1223 * Just skip it, you cannot focus dock windows. */
1227 /* If the container is not on the current workspace,
1228 * workspace_show() will switch to a different workspace and (if
1229 * enabled) trigger a mouse pointer warp to the currently focused
1230 * container (!) on the target workspace.
1232 * Therefore, before calling workspace_show(), we make sure that
1233 * 'current' will be focused on the workspace. However, we cannot
1234 * just con_focus(current) because then the pointer will not be
1235 * warped at all (the code thinks we are already there).
1237 * So we focus 'current' to make it the currently focused window of
1238 * the target workspace, then revert focus. */
1239 Con *currently_focused = focused;
1240 con_focus(current->con);
1241 con_focus(currently_focused);
1243 /* Now switch to the workspace, then focus */
1245 LOG("focusing %p / %s\n", current->con, current->con->name);
1246 con_focus(current->con);
1251 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1252 "while only exactly one container can be focused at a time.\n", count);
1254 cmd_output->needs_tree_render = true;
1255 // XXX: default reply for now, make this a better reply
1256 cmd_output->json_output = sstrdup("{\"success\": true}");
1260 * Implementation of 'fullscreen [global]'.
1263 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1264 if (fullscreen_mode == NULL)
1265 fullscreen_mode = "output";
1266 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1271 TAILQ_FOREACH(current, &owindows, owindows) {
1272 printf("matching: %p / %s\n", current->con, current->con->name);
1273 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1276 cmd_output->needs_tree_render = true;
1277 // XXX: default reply for now, make this a better reply
1278 cmd_output->json_output = sstrdup("{\"success\": true}");
1282 * Implementation of 'move <direction> [<pixels> [px]]'.
1285 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1286 // 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
1287 int px = atoi(move_px);
1289 /* TODO: make 'move' work with criteria. */
1290 DLOG("moving in direction %s, px %s\n", direction, move_px);
1291 if (con_is_floating(focused)) {
1292 DLOG("floating move with %d pixels\n", px);
1293 Rect newrect = focused->parent->rect;
1294 if (strcmp(direction, "left") == 0) {
1296 } else if (strcmp(direction, "right") == 0) {
1298 } else if (strcmp(direction, "up") == 0) {
1300 } else if (strcmp(direction, "down") == 0) {
1303 floating_reposition(focused->parent, newrect);
1305 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1306 (strcmp(direction, "left") == 0 ? D_LEFT :
1307 (strcmp(direction, "up") == 0 ? D_UP :
1309 cmd_output->needs_tree_render = true;
1312 // XXX: default reply for now, make this a better reply
1313 cmd_output->json_output = sstrdup("{\"success\": true}");
1317 * Implementation of 'layout default|stacked|stacking|tabbed'.
1320 void cmd_layout(I3_CMD, char *layout_str) {
1321 if (strcmp(layout_str, "stacking") == 0)
1322 layout_str = "stacked";
1323 DLOG("changing layout to %s\n", layout_str);
1325 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1326 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1329 /* check if the match is empty, not if the result is empty */
1330 if (match_is_empty(current_match))
1331 con_set_layout(focused->parent, layout);
1333 TAILQ_FOREACH(current, &owindows, owindows) {
1334 DLOG("matching: %p / %s\n", current->con, current->con->name);
1335 con_set_layout(current->con, layout);
1339 cmd_output->needs_tree_render = true;
1340 // XXX: default reply for now, make this a better reply
1341 cmd_output->json_output = sstrdup("{\"success\": true}");
1345 * Implementaiton of 'exit'.
1348 void cmd_exit(I3_CMD) {
1349 LOG("Exiting due to user command.\n");
1356 * Implementaiton of 'reload'.
1359 void cmd_reload(I3_CMD) {
1361 kill_configerror_nagbar(false);
1362 load_configuration(conn, NULL, true);
1364 /* Send an IPC event just in case the ws names have changed */
1365 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1367 // XXX: default reply for now, make this a better reply
1368 cmd_output->json_output = sstrdup("{\"success\": true}");
1372 * Implementaiton of 'restart'.
1375 void cmd_restart(I3_CMD) {
1376 LOG("restarting i3\n");
1379 // XXX: default reply for now, make this a better reply
1380 cmd_output->json_output = sstrdup("{\"success\": true}");
1384 * Implementaiton of 'open'.
1387 void cmd_open(I3_CMD) {
1388 LOG("opening new container\n");
1389 Con *con = tree_open_con(NULL, NULL);
1391 sasprintf(&(cmd_output->json_output),
1392 "{\"success\":true, \"id\":%ld}", (long int)con);
1394 cmd_output->needs_tree_render = true;
1398 * Implementation of 'focus output <output>'.
1401 void cmd_focus_output(I3_CMD, char *name) {
1404 DLOG("name = %s\n", name);
1408 /* get the output */
1409 Output *current_output = NULL;
1412 TAILQ_FOREACH(current, &owindows, owindows)
1413 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1414 assert(current_output != NULL);
1416 output = get_output_from_string(current_output, name);
1419 LOG("No such output found.\n");
1420 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1424 /* get visible workspace on output */
1426 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1428 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1434 cmd_output->needs_tree_render = true;
1435 // XXX: default reply for now, make this a better reply
1436 cmd_output->json_output = sstrdup("{\"success\": true}");
1440 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1443 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1448 if (!con_is_floating(focused)) {
1449 ELOG("Cannot change position. The window/container is not floating\n");
1450 sasprintf(&(cmd_output->json_output),
1451 "{\"success\":false, \"error\":\"Cannot change position. "
1452 "The window/container is not floating.\"}");
1456 if (strcmp(method, "absolute") == 0) {
1457 focused->parent->rect.x = x;
1458 focused->parent->rect.y = y;
1460 DLOG("moving to absolute position %d %d\n", x, y);
1461 floating_maybe_reassign_ws(focused->parent);
1462 cmd_output->needs_tree_render = true;
1465 if (strcmp(method, "position") == 0) {
1466 Rect newrect = focused->parent->rect;
1468 DLOG("moving to position %d %d\n", x, y);
1472 floating_reposition(focused->parent, newrect);
1475 // XXX: default reply for now, make this a better reply
1476 cmd_output->json_output = sstrdup("{\"success\": true}");
1480 * Implementation of 'move [window|container] [to] [absolute] position center
1483 void cmd_move_window_to_center(I3_CMD, char *method) {
1485 if (!con_is_floating(focused)) {
1486 ELOG("Cannot change position. The window/container is not floating\n");
1487 sasprintf(&(cmd_output->json_output),
1488 "{\"success\":false, \"error\":\"Cannot change position. "
1489 "The window/container is not floating.\"}");
1492 if (strcmp(method, "absolute") == 0) {
1493 Rect *rect = &focused->parent->rect;
1495 DLOG("moving to absolute center\n");
1496 rect->x = croot->rect.width/2 - rect->width/2;
1497 rect->y = croot->rect.height/2 - rect->height/2;
1499 floating_maybe_reassign_ws(focused->parent);
1500 cmd_output->needs_tree_render = true;
1503 if (strcmp(method, "position") == 0) {
1504 Rect *wsrect = &con_get_workspace(focused)->rect;
1505 Rect newrect = focused->parent->rect;
1507 DLOG("moving to center\n");
1508 newrect.x = wsrect->width/2 - newrect.width/2;
1509 newrect.y = wsrect->height/2 - newrect.height/2;
1511 floating_reposition(focused->parent, newrect);
1514 // XXX: default reply for now, make this a better reply
1515 cmd_output->json_output = sstrdup("{\"success\": true}");
1519 * Implementation of 'move scratchpad'.
1522 void cmd_move_scratchpad(I3_CMD) {
1523 DLOG("should move window to scratchpad\n");
1528 TAILQ_FOREACH(current, &owindows, owindows) {
1529 DLOG("matching: %p / %s\n", current->con, current->con->name);
1530 scratchpad_move(current->con);
1533 cmd_output->needs_tree_render = true;
1534 // XXX: default reply for now, make this a better reply
1535 cmd_output->json_output = sstrdup("{\"success\": true}");
1539 * Implementation of 'scratchpad show'.
1542 void cmd_scratchpad_show(I3_CMD) {
1543 DLOG("should show scratchpad window\n");
1546 if (match_is_empty(current_match)) {
1547 scratchpad_show(NULL);
1549 TAILQ_FOREACH(current, &owindows, owindows) {
1550 DLOG("matching: %p / %s\n", current->con, current->con->name);
1551 scratchpad_show(current->con);
1555 cmd_output->needs_tree_render = true;
1556 // XXX: default reply for now, make this a better reply
1557 cmd_output->json_output = sstrdup("{\"success\": true}");
1561 * Implementation of 'rename workspace <name> to <name>'
1564 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1565 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1567 Con *output, *workspace = NULL;
1568 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1569 GREP_FIRST(workspace, output_get_content(output),
1570 !strcasecmp(child->name, old_name));
1573 // TODO: we should include the old workspace name here and use yajl for
1574 // generating the reply.
1575 cmd_output->json_output = sstrdup("{\"success\": false, "
1576 "\"error\":\"Old workspace not found\"}");
1580 Con *check_dest = NULL;
1581 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1582 GREP_FIRST(check_dest, output_get_content(output),
1583 !strcasecmp(child->name, new_name));
1585 if (check_dest != NULL) {
1586 // TODO: we should include the new workspace name here and use yajl for
1587 // generating the reply.
1588 cmd_output->json_output = sstrdup("{\"success\": false, "
1589 "\"error\":\"New workspace already exists\"}");
1593 /* Change the name and try to parse it as a number. */
1594 FREE(workspace->name);
1595 workspace->name = sstrdup(new_name);
1596 char *endptr = NULL;
1597 long parsed_num = strtol(new_name, &endptr, 10);
1598 if (parsed_num == LONG_MIN ||
1599 parsed_num == LONG_MAX ||
1602 workspace->num = -1;
1603 else workspace->num = parsed_num;
1604 LOG("num = %d\n", workspace->num);
1606 /* By re-attaching, the sort order will be correct afterwards. */
1607 Con *previously_focused = focused;
1608 Con *parent = workspace->parent;
1609 con_detach(workspace);
1610 con_attach(workspace, parent, false);
1611 /* Restore the previous focus since con_attach messes with the focus. */
1612 con_focus(previously_focused);
1614 cmd_output->needs_tree_render = true;
1615 cmd_output->json_output = sstrdup("{\"success\": true}");
1617 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");