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); \
43 * Returns true if a is definitely greater than b (using the given epsilon)
46 static bool definitelyGreaterThan(float a, float b, float epsilon) {
47 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
51 * Returns an 'output' corresponding to one of left/right/down/up or a specific
55 static Output *get_output_from_string(Output *current_output, const char *output_str) {
58 if (strcasecmp(output_str, "left") == 0) {
59 output = get_output_next(D_LEFT, current_output, CLOSEST_OUTPUT);
61 output = get_output_most(D_RIGHT, current_output);
62 } else if (strcasecmp(output_str, "right") == 0) {
63 output = get_output_next(D_RIGHT, current_output, CLOSEST_OUTPUT);
65 output = get_output_most(D_LEFT, current_output);
66 } else if (strcasecmp(output_str, "up") == 0) {
67 output = get_output_next(D_UP, current_output, CLOSEST_OUTPUT);
69 output = get_output_most(D_DOWN, current_output);
70 } else if (strcasecmp(output_str, "down") == 0) {
71 output = get_output_next(D_DOWN, current_output, CLOSEST_OUTPUT);
73 output = get_output_most(D_UP, current_output);
74 } else output = get_output_by_name(output_str);
80 * Checks whether we switched to a new workspace and returns false in that case,
81 * signaling that further workspace switching should be done by the calling function
82 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
83 * and return true, signaling that no further workspace switching should occur in the calling function.
86 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
87 Con *ws = con_get_workspace(focused);
89 /* If we switched to a different workspace, do nothing */
90 if (strcmp(ws->name, name) != 0)
93 DLOG("This workspace is already focused.\n");
94 if (config.workspace_auto_back_and_forth) {
95 workspace_back_and_forth();
96 cmd_output->needs_tree_render = true;
102 * Return the passed workspace unless it is the current one and auto back and
103 * forth is enabled, in which case the back_and_forth workspace is returned.
105 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
108 if (!config.workspace_auto_back_and_forth)
111 current = con_get_workspace(focused);
113 if (current == workspace) {
114 baf = workspace_back_and_forth_get();
116 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
124 // This code is commented out because we might recycle it for popping up error
125 // messages on parser errors.
127 static pid_t migration_pid = -1;
130 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
131 * it exited (or could not be started, depending on the exit code).
134 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
135 ev_child_stop(EV_A_ watcher);
136 if (!WIFEXITED(watcher->rstatus)) {
137 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
141 int exitcode = WEXITSTATUS(watcher->rstatus);
142 printf("i3-nagbar process exited with status %d\n", exitcode);
144 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151 * only makes sure that there are no i3-nagbar instances left behind) we still
152 * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
155 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
156 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
159 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
160 if (migration_pid != -1) {
161 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
162 kill(migration_pid, SIGKILL);
167 void cmd_MIGRATION_start_nagbar(void) {
168 if (migration_pid != -1) {
169 fprintf(stderr, "i3-nagbar already running.\n");
172 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
173 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
174 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
175 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
176 migration_pid = fork();
177 if (migration_pid == -1) {
178 warn("Could not fork()");
183 if (migration_pid == 0) {
185 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
187 NULL, /* will be replaced by the executable path */
191 "You found a parsing error. Please, please, please, report it!",
197 exec_i3_utility("i3-nagbar", argv);
201 /* install a child watcher */
202 ev_child *child = smalloc(sizeof(ev_child));
203 ev_child_init(child, &nagbar_exited, migration_pid, 0);
204 ev_child_start(main_loop, child);
206 /* We need ev >= 4 for the following code. Since it is not *that* important (it
207 * only makes sure that there are no i3-nagbar instances left behind) we still
208 * support old systems with libev 3. */
209 #if EV_VERSION_MAJOR >= 4
210 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
212 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
213 ev_cleanup_init(cleanup, nagbar_cleanup);
214 ev_cleanup_start(main_loop, cleanup);
220 /*******************************************************************************
221 * Criteria functions.
222 ******************************************************************************/
225 * Helper data structure for an operation window (window on which the operation
226 * will be performed). Used to build the TAILQ owindows.
229 typedef struct owindow {
231 TAILQ_ENTRY(owindow) owindows;
234 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
236 static owindows_head owindows;
239 * Initializes the specified 'Match' data structure and the initial state of
240 * commands.c for matching target windows of a command.
243 void cmd_criteria_init(I3_CMD) {
247 DLOG("Initializing criteria, current_match = %p\n", current_match);
248 match_init(current_match);
249 while (!TAILQ_EMPTY(&owindows)) {
250 ow = TAILQ_FIRST(&owindows);
251 TAILQ_REMOVE(&owindows, ow, owindows);
254 TAILQ_INIT(&owindows);
256 TAILQ_FOREACH(con, &all_cons, all_cons) {
257 ow = smalloc(sizeof(owindow));
259 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
264 * A match specification just finished (the closing square bracket was found),
265 * so we filter the list of owindows.
268 void cmd_criteria_match_windows(I3_CMD) {
269 owindow *next, *current;
271 DLOG("match specification finished, matching...\n");
272 /* copy the old list head to iterate through it and start with a fresh
273 * list which will contain only matching windows */
274 struct owindows_head old = owindows;
275 TAILQ_INIT(&owindows);
276 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
277 /* make a copy of the next pointer and advance the pointer to the
278 * next element as we are going to invalidate the element’s
279 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
281 next = TAILQ_NEXT(next, owindows);
283 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
284 if (current_match->con_id != NULL) {
285 if (current_match->con_id == current->con) {
286 DLOG("matches container!\n");
287 TAILQ_INSERT_TAIL(&owindows, current, owindows);
289 } else if (current_match->mark != NULL && current->con->mark != NULL &&
290 regex_matches(current_match->mark, current->con->mark)) {
291 DLOG("match by mark\n");
292 TAILQ_INSERT_TAIL(&owindows, current, owindows);
294 if (current->con->window == NULL)
296 if (match_matches_window(current_match, current->con->window)) {
297 DLOG("matches window!\n");
298 TAILQ_INSERT_TAIL(&owindows, current, owindows);
300 DLOG("doesnt match\n");
306 TAILQ_FOREACH(current, &owindows, owindows) {
307 DLOG("matching: %p / %s\n", current->con, current->con->name);
312 * Interprets a ctype=cvalue pair and adds it to the current match
316 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
317 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
319 if (strcmp(ctype, "class") == 0) {
320 current_match->class = regex_new(cvalue);
324 if (strcmp(ctype, "instance") == 0) {
325 current_match->instance = regex_new(cvalue);
329 if (strcmp(ctype, "window_role") == 0) {
330 current_match->role = regex_new(cvalue);
334 if (strcmp(ctype, "con_id") == 0) {
336 long parsed = strtol(cvalue, &end, 10);
337 if (parsed == LONG_MIN ||
338 parsed == LONG_MAX ||
340 (end && *end != '\0')) {
341 ELOG("Could not parse con id \"%s\"\n", cvalue);
343 current_match->con_id = (Con*)parsed;
344 printf("id as int = %p\n", current_match->con_id);
349 if (strcmp(ctype, "id") == 0) {
351 long parsed = strtol(cvalue, &end, 10);
352 if (parsed == LONG_MIN ||
353 parsed == LONG_MAX ||
355 (end && *end != '\0')) {
356 ELOG("Could not parse window id \"%s\"\n", cvalue);
358 current_match->id = parsed;
359 printf("window id as int = %d\n", current_match->id);
364 if (strcmp(ctype, "con_mark") == 0) {
365 current_match->mark = regex_new(cvalue);
369 if (strcmp(ctype, "title") == 0) {
370 current_match->title = regex_new(cvalue);
374 if (strcmp(ctype, "urgent") == 0) {
375 if (strcasecmp(cvalue, "latest") == 0 ||
376 strcasecmp(cvalue, "newest") == 0 ||
377 strcasecmp(cvalue, "recent") == 0 ||
378 strcasecmp(cvalue, "last") == 0) {
379 current_match->urgent = U_LATEST;
380 } else if (strcasecmp(cvalue, "oldest") == 0 ||
381 strcasecmp(cvalue, "first") == 0) {
382 current_match->urgent = U_OLDEST;
387 ELOG("Unknown criterion: %s\n", ctype);
391 * Implementation of 'move [window|container] [to] workspace
392 * next|prev|next_on_output|prev_on_output|current'.
395 void cmd_move_con_to_workspace(I3_CMD, char *which) {
398 DLOG("which=%s\n", which);
400 /* We have nothing to move:
401 * when criteria was specified but didn't match any window or
402 * when criteria wasn't specified and we don't have any window focused. */
403 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
404 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
405 !con_has_children(focused))) {
412 /* get the workspace */
414 if (strcmp(which, "next") == 0)
415 ws = workspace_next();
416 else if (strcmp(which, "prev") == 0)
417 ws = workspace_prev();
418 else if (strcmp(which, "next_on_output") == 0)
419 ws = workspace_next_on_output();
420 else if (strcmp(which, "prev_on_output") == 0)
421 ws = workspace_prev_on_output();
422 else if (strcmp(which, "current") == 0)
423 ws = con_get_workspace(focused);
425 ELOG("BUG: called with which=%s\n", which);
430 TAILQ_FOREACH(current, &owindows, owindows) {
431 DLOG("matching: %p / %s\n", current->con, current->con->name);
432 con_move_to_workspace(current->con, ws, true, false);
435 cmd_output->needs_tree_render = true;
436 // XXX: default reply for now, make this a better reply
441 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
444 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
448 ws = workspace_back_and_forth_get();
455 ystr("No workspace was previously active.");
462 TAILQ_FOREACH(current, &owindows, owindows) {
463 DLOG("matching: %p / %s\n", current->con, current->con->name);
464 con_move_to_workspace(current->con, ws, true, false);
467 cmd_output->needs_tree_render = true;
468 // XXX: default reply for now, make this a better reply
473 * Implementation of 'move [window|container] [to] workspace <name>'.
476 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
477 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
478 LOG("You cannot switch to the i3 internal workspaces.\n");
485 /* We have nothing to move:
486 * when criteria was specified but didn't match any window or
487 * when criteria wasn't specified and we don't have any window focused. */
488 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
489 ELOG("No windows match your criteria, cannot move.\n");
493 else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
494 !con_has_children(focused)) {
499 LOG("should move window to workspace %s\n", name);
500 /* get the workspace */
501 Con *ws = workspace_get(name, NULL);
503 ws = maybe_auto_back_and_forth_workspace(ws);
507 TAILQ_FOREACH(current, &owindows, owindows) {
508 DLOG("matching: %p / %s\n", current->con, current->con->name);
509 con_move_to_workspace(current->con, ws, true, false);
512 cmd_output->needs_tree_render = true;
513 // XXX: default reply for now, make this a better reply
518 * Implementation of 'move [window|container] [to] workspace number <name>'.
521 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
524 /* We have nothing to move:
525 * when criteria was specified but didn't match any window or
526 * when criteria wasn't specified and we don't have any window focused. */
527 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
528 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
529 !con_has_children(focused))) {
534 LOG("should move window to workspace %s\n", which);
535 /* get the workspace */
536 Con *output, *workspace = NULL;
539 long parsed_num = strtol(which, &endptr, 10);
540 if (parsed_num == LONG_MIN ||
541 parsed_num == LONG_MAX ||
544 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
549 // TODO: better error message
550 ystr("Could not parse number");
555 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
556 GREP_FIRST(workspace, output_get_content(output),
557 child->num == parsed_num);
560 workspace = workspace_get(which, NULL);
563 workspace = maybe_auto_back_and_forth_workspace(workspace);
567 TAILQ_FOREACH(current, &owindows, owindows) {
568 DLOG("matching: %p / %s\n", current->con, current->con->name);
569 con_move_to_workspace(current->con, workspace, true, false);
572 cmd_output->needs_tree_render = true;
573 // XXX: default reply for now, make this a better reply
577 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
578 LOG("floating resize\n");
579 if (strcmp(direction, "up") == 0) {
580 floating_con->rect.y -= px;
581 floating_con->rect.height += px;
582 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
583 floating_con->rect.height += px;
584 } else if (strcmp(direction, "left") == 0) {
585 floating_con->rect.x -= px;
586 floating_con->rect.width += px;
588 floating_con->rect.width += px;
591 floating_check_size(floating_con);
594 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
595 LOG("tiling resize\n");
596 /* get the appropriate current container (skip stacked/tabbed cons) */
598 double percentage = 0;
599 while (current->parent->layout == L_STACKED ||
600 current->parent->layout == L_TABBED)
601 current = current->parent;
603 /* Then further go up until we find one with the matching orientation. */
604 orientation_t search_orientation =
605 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
608 if (con_orientation(current->parent) != search_orientation) {
609 current = current->parent;
613 /* get the default percentage */
614 int children = con_num_children(current->parent);
615 LOG("ins. %d children\n", children);
616 percentage = 1.0 / children;
617 LOG("default percentage = %f\n", percentage);
619 orientation_t orientation = con_orientation(current->parent);
621 if ((orientation == HORIZ &&
622 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
623 (orientation == VERT &&
624 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
625 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
626 (orientation == HORIZ ? "horizontal" : "vertical"));
631 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
632 other = TAILQ_PREV(current, nodes_head, nodes);
634 other = TAILQ_NEXT(current, nodes);
636 if (other == TAILQ_END(workspaces)) {
637 LOG("No other container in this direction found, trying to look further up in the tree...\n");
638 current = current->parent;
642 } while (current->type != CT_WORKSPACE &&
643 current->type != CT_FLOATING_CON);
646 LOG("No other container in this direction found, trying to look further up in the tree...\n");
651 LOG("other->percent = %f\n", other->percent);
652 LOG("current->percent before = %f\n", current->percent);
653 if (current->percent == 0.0)
654 current->percent = percentage;
655 if (other->percent == 0.0)
656 other->percent = percentage;
657 double new_current_percent = current->percent + ((double)ppt / 100.0);
658 double new_other_percent = other->percent - ((double)ppt / 100.0);
659 LOG("new_current_percent = %f\n", new_current_percent);
660 LOG("new_other_percent = %f\n", new_other_percent);
661 /* Ensure that the new percentages are positive and greater than
662 * 0.05 to have a reasonable minimum size. */
663 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
664 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
665 current->percent += ((double)ppt / 100.0);
666 other->percent -= ((double)ppt / 100.0);
667 LOG("current->percent after = %f\n", current->percent);
668 LOG("other->percent after = %f\n", other->percent);
670 LOG("Not resizing, already at minimum size\n");
676 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
677 LOG("width/height resize\n");
678 /* get the appropriate current container (skip stacked/tabbed cons) */
679 while (current->parent->layout == L_STACKED ||
680 current->parent->layout == L_TABBED)
681 current = current->parent;
683 /* Then further go up until we find one with the matching orientation. */
684 orientation_t search_orientation =
685 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
687 while (current->type != CT_WORKSPACE &&
688 current->type != CT_FLOATING_CON &&
689 con_orientation(current->parent) != search_orientation)
690 current = current->parent;
692 /* get the default percentage */
693 int children = con_num_children(current->parent);
694 LOG("ins. %d children\n", children);
695 double percentage = 1.0 / children;
696 LOG("default percentage = %f\n", percentage);
698 orientation_t orientation = con_orientation(current->parent);
700 if ((orientation == HORIZ &&
701 strcmp(direction, "height") == 0) ||
702 (orientation == VERT &&
703 strcmp(direction, "width") == 0)) {
704 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
705 (orientation == HORIZ ? "horizontal" : "vertical"));
711 LOG("This is the only container, cannot resize.\n");
716 /* Ensure all the other children have a percentage set. */
718 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
719 LOG("child->percent = %f (child %p)\n", child->percent, child);
720 if (child->percent == 0.0)
721 child->percent = percentage;
724 double new_current_percent = current->percent + ((double)ppt / 100.0);
725 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
726 LOG("new_current_percent = %f\n", new_current_percent);
727 LOG("subtract_percent = %f\n", subtract_percent);
728 /* Ensure that the new percentages are positive and greater than
729 * 0.05 to have a reasonable minimum size. */
730 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
731 if (child == current)
733 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
734 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
739 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
740 LOG("Not resizing, already at minimum size\n");
745 current->percent += ((double)ppt / 100.0);
746 LOG("current->percent after = %f\n", current->percent);
748 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
749 if (child == current)
751 child->percent -= subtract_percent;
752 LOG("child->percent after (%p) = %f\n", child, child->percent);
759 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
762 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
763 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
764 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
765 // 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
766 int px = atoi(resize_px);
767 int ppt = atoi(resize_ppt);
768 if (strcmp(way, "shrink") == 0) {
776 TAILQ_FOREACH(current, &owindows, owindows) {
778 if ((floating_con = con_inside_floating(current->con))) {
779 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
781 if (strcmp(direction, "width") == 0 ||
782 strcmp(direction, "height") == 0) {
783 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
786 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
792 cmd_output->needs_tree_render = true;
793 // XXX: default reply for now, make this a better reply
798 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
801 void cmd_border(I3_CMD, char *border_style_str, char *border_width ) {
802 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
807 TAILQ_FOREACH(current, &owindows, owindows) {
808 DLOG("matching: %p / %s\n", current->con, current->con->name);
809 int border_style = current->con->border_style;
811 int tmp_border_width = -1;
812 tmp_border_width = strtol(border_width, &end, 10);
813 if (end == border_width) {
814 /* no valid digits found */
815 tmp_border_width = -1;
817 if (strcmp(border_style_str, "toggle") == 0) {
820 if (border_style == BS_NORMAL)
821 tmp_border_width = 2;
822 else if (border_style == BS_NONE)
823 tmp_border_width = 0;
824 else if (border_style == BS_PIXEL)
825 tmp_border_width = 1;
827 if (strcmp(border_style_str, "normal") == 0)
828 border_style = BS_NORMAL;
829 else if (strcmp(border_style_str, "pixel") == 0)
830 border_style = BS_PIXEL;
831 else if (strcmp(border_style_str, "1pixel") == 0){
832 border_style = BS_PIXEL;
833 tmp_border_width = 1;
834 } else if (strcmp(border_style_str, "none") == 0)
835 border_style = BS_NONE;
837 ELOG("BUG: called with border_style=%s\n", border_style_str);
842 con_set_border_style(current->con, border_style, tmp_border_width);
845 cmd_output->needs_tree_render = true;
846 // XXX: default reply for now, make this a better reply
851 * Implementation of 'nop <comment>'.
854 void cmd_nop(I3_CMD, char *comment) {
855 LOG("-------------------------------------------------\n");
856 LOG(" NOP: %s\n", comment);
857 LOG("-------------------------------------------------\n");
861 * Implementation of 'append_layout <path>'.
864 void cmd_append_layout(I3_CMD, char *path) {
865 LOG("Appending layout \"%s\"\n", path);
866 tree_append_json(path);
868 cmd_output->needs_tree_render = true;
869 // XXX: default reply for now, make this a better reply
874 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
877 void cmd_workspace(I3_CMD, char *which) {
880 DLOG("which=%s\n", which);
882 if (strcmp(which, "next") == 0)
883 ws = workspace_next();
884 else if (strcmp(which, "prev") == 0)
885 ws = workspace_prev();
886 else if (strcmp(which, "next_on_output") == 0)
887 ws = workspace_next_on_output();
888 else if (strcmp(which, "prev_on_output") == 0)
889 ws = workspace_prev_on_output();
891 ELOG("BUG: called with which=%s\n", which);
898 cmd_output->needs_tree_render = true;
899 // XXX: default reply for now, make this a better reply
904 * Implementation of 'workspace number <name>'
907 void cmd_workspace_number(I3_CMD, char *which) {
908 Con *output, *workspace = NULL;
911 long parsed_num = strtol(which, &endptr, 10);
912 if (parsed_num == LONG_MIN ||
913 parsed_num == LONG_MAX ||
916 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
921 // TODO: better error message
922 ystr("Could not parse number");
928 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
929 GREP_FIRST(workspace, output_get_content(output),
930 child->num == parsed_num);
933 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
935 workspace_show_by_name(which);
936 cmd_output->needs_tree_render = true;
939 if (maybe_back_and_forth(cmd_output, workspace->name))
941 workspace_show(workspace);
943 cmd_output->needs_tree_render = true;
944 // XXX: default reply for now, make this a better reply
949 * Implementation of 'workspace back_and_forth'.
952 void cmd_workspace_back_and_forth(I3_CMD) {
953 workspace_back_and_forth();
955 cmd_output->needs_tree_render = true;
956 // XXX: default reply for now, make this a better reply
961 * Implementation of 'workspace <name>'
964 void cmd_workspace_name(I3_CMD, char *name) {
965 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
966 LOG("You cannot switch to the i3 internal workspaces.\n");
971 DLOG("should switch to workspace %s\n", name);
972 if (maybe_back_and_forth(cmd_output, name))
974 workspace_show_by_name(name);
976 cmd_output->needs_tree_render = true;
977 // XXX: default reply for now, make this a better reply
982 * Implementation of 'mark <mark>'
985 void cmd_mark(I3_CMD, char *mark) {
986 DLOG("Clearing all windows which have that mark first\n");
989 TAILQ_FOREACH(con, &all_cons, all_cons) {
990 if (con->mark && strcmp(con->mark, mark) == 0)
994 DLOG("marking window with str %s\n", mark);
999 TAILQ_FOREACH(current, &owindows, owindows) {
1000 DLOG("matching: %p / %s\n", current->con, current->con->name);
1001 current->con->mark = sstrdup(mark);
1004 cmd_output->needs_tree_render = true;
1005 // XXX: default reply for now, make this a better reply
1010 * Implementation of 'mode <string>'.
1013 void cmd_mode(I3_CMD, char *mode) {
1014 DLOG("mode=%s\n", mode);
1017 // XXX: default reply for now, make this a better reply
1022 * Implementation of 'move [window|container] [to] output <str>'.
1025 void cmd_move_con_to_output(I3_CMD, char *name) {
1028 DLOG("should move window to output %s\n", name);
1032 /* get the output */
1033 Output *current_output = NULL;
1036 // TODO: fix the handling of criteria
1037 TAILQ_FOREACH(current, &owindows, owindows)
1038 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1040 assert(current_output != NULL);
1042 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1043 if (strcasecmp(name, "up") == 0)
1044 output = get_output_next(D_UP, current_output, CLOSEST_OUTPUT);
1045 else if (strcasecmp(name, "down") == 0)
1046 output = get_output_next(D_DOWN, current_output, CLOSEST_OUTPUT);
1047 else if (strcasecmp(name, "left") == 0)
1048 output = get_output_next(D_LEFT, current_output, CLOSEST_OUTPUT);
1049 else if (strcasecmp(name, "right") == 0)
1050 output = get_output_next(D_RIGHT, current_output, CLOSEST_OUTPUT);
1052 output = get_output_by_name(name);
1055 LOG("No such output found.\n");
1060 /* get visible workspace on output */
1062 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1068 TAILQ_FOREACH(current, &owindows, owindows) {
1069 DLOG("matching: %p / %s\n", current->con, current->con->name);
1070 con_move_to_workspace(current->con, ws, true, false);
1073 cmd_output->needs_tree_render = true;
1074 // XXX: default reply for now, make this a better reply
1079 * Implementation of 'floating enable|disable|toggle'
1082 void cmd_floating(I3_CMD, char *floating_mode) {
1085 DLOG("floating_mode=%s\n", floating_mode);
1089 TAILQ_FOREACH(current, &owindows, owindows) {
1090 DLOG("matching: %p / %s\n", current->con, current->con->name);
1091 if (strcmp(floating_mode, "toggle") == 0) {
1092 DLOG("should toggle mode\n");
1093 toggle_floating_mode(current->con, false);
1095 DLOG("should switch mode to %s\n", floating_mode);
1096 if (strcmp(floating_mode, "enable") == 0) {
1097 floating_enable(current->con, false);
1099 floating_disable(current->con, false);
1104 cmd_output->needs_tree_render = true;
1105 // XXX: default reply for now, make this a better reply
1110 * Implementation of 'move workspace to [output] <str>'.
1113 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1114 DLOG("should move workspace to output %s\n", name);
1119 TAILQ_FOREACH(current, &owindows, owindows) {
1120 Output *current_output = get_output_containing(current->con->rect.x,
1121 current->con->rect.y);
1122 if (!current_output) {
1123 ELOG("Cannot get current output. This is a bug in i3.\n");
1127 Output *output = get_output_from_string(current_output, name);
1129 ELOG("Could not get output from string \"%s\"\n", name);
1134 Con *content = output_get_content(output->con);
1135 LOG("got output %p with content %p\n", output, content);
1137 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1138 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1140 Con *ws = con_get_workspace(current->con);
1141 LOG("should move workspace %p / %s\n", ws, ws->name);
1142 bool workspace_was_visible = workspace_is_visible(ws);
1144 if (con_num_children(ws->parent) == 1) {
1145 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1147 /* check if we can find a workspace assigned to this output */
1148 bool used_assignment = false;
1149 struct Workspace_Assignment *assignment;
1150 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1151 if (strcmp(assignment->output, current_output->name) != 0)
1154 /* check if this workspace is already attached to the tree */
1155 Con *workspace = NULL, *out;
1156 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1157 GREP_FIRST(workspace, output_get_content(out),
1158 !strcasecmp(child->name, assignment->name));
1159 if (workspace != NULL)
1162 /* so create the workspace referenced to by this assignment */
1163 LOG("Creating workspace from assignment %s.\n", assignment->name);
1164 workspace_get(assignment->name, NULL);
1165 used_assignment = true;
1169 /* if we couldn't create the workspace using an assignment, create
1170 * it on the output */
1171 if (!used_assignment)
1172 create_workspace_on_output(current_output, ws->parent);
1174 /* notify the IPC listeners */
1175 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1177 DLOG("Detaching\n");
1179 /* detach from the old output and attach to the new output */
1180 Con *old_content = ws->parent;
1182 if (workspace_was_visible) {
1183 /* The workspace which we just detached was visible, so focus
1184 * the next one in the focus-stack. */
1185 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1186 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1187 workspace_show(focus_ws);
1189 con_attach(ws, content, false);
1191 /* fix the coordinates of the floating containers */
1193 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1194 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1196 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1197 if (workspace_was_visible) {
1198 /* Focus the moved workspace on the destination output. */
1202 /* NB: We cannot simply work with previously_visible_ws since it might
1203 * have been cleaned up by workspace_show() already, depending on the
1204 * focus order/number of other workspaces on the output.
1205 * Instead, we loop through the available workspaces and only work with
1206 * previously_visible_ws if we still find it. */
1207 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1208 if (ws != previously_visible_ws)
1211 /* Call the on_remove_child callback of the workspace which previously
1212 * was visible on the destination output. Since it is no longer
1213 * visible, it might need to get cleaned up. */
1214 CALL(previously_visible_ws, on_remove_child);
1219 cmd_output->needs_tree_render = true;
1220 // XXX: default reply for now, make this a better reply
1225 * Implementation of 'split v|h|vertical|horizontal'.
1228 void cmd_split(I3_CMD, char *direction) {
1230 /* TODO: use matches */
1231 LOG("splitting in direction %c\n", direction[0]);
1232 if (match_is_empty(current_match))
1233 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1235 TAILQ_FOREACH(current, &owindows, owindows) {
1236 DLOG("matching: %p / %s\n", current->con, current->con->name);
1237 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1241 cmd_output->needs_tree_render = true;
1242 // XXX: default reply for now, make this a better reply
1247 * Implementation of 'kill [window|client]'.
1250 void cmd_kill(I3_CMD, char *kill_mode_str) {
1251 if (kill_mode_str == NULL)
1252 kill_mode_str = "window";
1255 DLOG("kill_mode=%s\n", kill_mode_str);
1258 if (strcmp(kill_mode_str, "window") == 0)
1259 kill_mode = KILL_WINDOW;
1260 else if (strcmp(kill_mode_str, "client") == 0)
1261 kill_mode = KILL_CLIENT;
1263 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1268 /* check if the match is empty, not if the result is empty */
1269 if (match_is_empty(current_match))
1270 tree_close_con(kill_mode);
1272 TAILQ_FOREACH(current, &owindows, owindows) {
1273 DLOG("matching: %p / %s\n", current->con, current->con->name);
1274 tree_close(current->con, kill_mode, false, false);
1278 cmd_output->needs_tree_render = true;
1279 // XXX: default reply for now, make this a better reply
1284 * Implementation of 'exec [--no-startup-id] <command>'.
1287 void cmd_exec(I3_CMD, char *nosn, char *command) {
1288 bool no_startup_id = (nosn != NULL);
1290 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1291 start_application(command, no_startup_id);
1293 // XXX: default reply for now, make this a better reply
1298 * Implementation of 'focus left|right|up|down'.
1301 void cmd_focus_direction(I3_CMD, char *direction) {
1302 DLOG("direction = *%s*\n", direction);
1304 if (strcmp(direction, "left") == 0)
1305 tree_next('p', HORIZ);
1306 else if (strcmp(direction, "right") == 0)
1307 tree_next('n', HORIZ);
1308 else if (strcmp(direction, "up") == 0)
1309 tree_next('p', VERT);
1310 else if (strcmp(direction, "down") == 0)
1311 tree_next('n', VERT);
1313 ELOG("Invalid focus direction (%s)\n", direction);
1318 cmd_output->needs_tree_render = true;
1319 // XXX: default reply for now, make this a better reply
1324 * Implementation of 'focus tiling|floating|mode_toggle'.
1327 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1328 DLOG("window_mode = %s\n", window_mode);
1330 Con *ws = con_get_workspace(focused);
1333 if (strcmp(window_mode, "mode_toggle") == 0) {
1334 current = TAILQ_FIRST(&(ws->focus_head));
1335 if (current != NULL && current->type == CT_FLOATING_CON)
1336 window_mode = "tiling";
1337 else window_mode = "floating";
1339 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1340 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1341 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1344 con_focus(con_descend_focused(current));
1349 cmd_output->needs_tree_render = true;
1350 // XXX: default reply for now, make this a better reply
1355 * Implementation of 'focus parent|child'.
1358 void cmd_focus_level(I3_CMD, char *level) {
1359 DLOG("level = %s\n", level);
1360 bool success = false;
1362 /* Focusing the parent can only be allowed if the newly
1363 * focused container won't escape the fullscreen container. */
1364 if (strcmp(level, "parent") == 0) {
1365 if (focused && focused->parent) {
1366 if (con_fullscreen_permits_focusing(focused->parent))
1367 success = level_up();
1369 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1373 /* Focusing a child should always be allowed. */
1374 else success = level_down();
1376 cmd_output->needs_tree_render = success;
1377 // XXX: default reply for now, make this a better reply
1382 * Implementation of 'focus'.
1385 void cmd_focus(I3_CMD) {
1386 DLOG("current_match = %p\n", current_match);
1388 if (match_is_empty(current_match)) {
1389 ELOG("You have to specify which window/container should be focused.\n");
1390 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1396 ystr("You have to specify which window/container should be focused");
1404 TAILQ_FOREACH(current, &owindows, owindows) {
1405 Con *ws = con_get_workspace(current->con);
1406 /* If no workspace could be found, this was a dock window.
1407 * Just skip it, you cannot focus dock windows. */
1411 /* Check the fullscreen focus constraints. */
1412 if (!con_fullscreen_permits_focusing(current->con)) {
1413 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1418 /* If the container is not on the current workspace,
1419 * workspace_show() will switch to a different workspace and (if
1420 * enabled) trigger a mouse pointer warp to the currently focused
1421 * container (!) on the target workspace.
1423 * Therefore, before calling workspace_show(), we make sure that
1424 * 'current' will be focused on the workspace. However, we cannot
1425 * just con_focus(current) because then the pointer will not be
1426 * warped at all (the code thinks we are already there).
1428 * So we focus 'current' to make it the currently focused window of
1429 * the target workspace, then revert focus. */
1430 Con *currently_focused = focused;
1431 con_focus(current->con);
1432 con_focus(currently_focused);
1434 /* Now switch to the workspace, then focus */
1436 LOG("focusing %p / %s\n", current->con, current->con->name);
1437 con_focus(current->con);
1442 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1443 "while only exactly one container can be focused at a time.\n", count);
1445 cmd_output->needs_tree_render = true;
1446 // XXX: default reply for now, make this a better reply
1451 * Implementation of 'fullscreen [global]'.
1454 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1455 if (fullscreen_mode == NULL)
1456 fullscreen_mode = "output";
1457 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1462 TAILQ_FOREACH(current, &owindows, owindows) {
1463 printf("matching: %p / %s\n", current->con, current->con->name);
1464 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1467 cmd_output->needs_tree_render = true;
1468 // XXX: default reply for now, make this a better reply
1473 * Implementation of 'move <direction> [<pixels> [px]]'.
1476 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1477 // 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
1478 int px = atoi(move_px);
1480 /* TODO: make 'move' work with criteria. */
1481 DLOG("moving in direction %s, px %s\n", direction, move_px);
1482 if (con_is_floating(focused)) {
1483 DLOG("floating move with %d pixels\n", px);
1484 Rect newrect = focused->parent->rect;
1485 if (strcmp(direction, "left") == 0) {
1487 } else if (strcmp(direction, "right") == 0) {
1489 } else if (strcmp(direction, "up") == 0) {
1491 } else if (strcmp(direction, "down") == 0) {
1494 floating_reposition(focused->parent, newrect);
1496 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1497 (strcmp(direction, "left") == 0 ? D_LEFT :
1498 (strcmp(direction, "up") == 0 ? D_UP :
1500 cmd_output->needs_tree_render = true;
1503 // XXX: default reply for now, make this a better reply
1508 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1511 void cmd_layout(I3_CMD, char *layout_str) {
1512 if (strcmp(layout_str, "stacking") == 0)
1513 layout_str = "stacked";
1516 /* default is a special case which will be handled in con_set_layout(). */
1517 if (strcmp(layout_str, "default") == 0)
1519 else if (strcmp(layout_str, "stacked") == 0)
1521 else if (strcmp(layout_str, "tabbed") == 0)
1523 else if (strcmp(layout_str, "splitv") == 0)
1525 else if (strcmp(layout_str, "splith") == 0)
1528 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1532 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1534 /* check if the match is empty, not if the result is empty */
1535 if (match_is_empty(current_match))
1536 con_set_layout(focused, layout);
1538 TAILQ_FOREACH(current, &owindows, owindows) {
1539 DLOG("matching: %p / %s\n", current->con, current->con->name);
1540 con_set_layout(current->con, layout);
1544 cmd_output->needs_tree_render = true;
1545 // XXX: default reply for now, make this a better reply
1550 * Implementation of 'layout toggle [all|split]'.
1553 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1556 if (toggle_mode == NULL)
1557 toggle_mode = "default";
1559 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1561 /* check if the match is empty, not if the result is empty */
1562 if (match_is_empty(current_match))
1563 con_toggle_layout(focused, toggle_mode);
1565 TAILQ_FOREACH(current, &owindows, owindows) {
1566 DLOG("matching: %p / %s\n", current->con, current->con->name);
1567 con_toggle_layout(current->con, toggle_mode);
1571 cmd_output->needs_tree_render = true;
1572 // XXX: default reply for now, make this a better reply
1577 * Implementation of 'exit'.
1580 void cmd_exit(I3_CMD) {
1581 LOG("Exiting due to user command.\n");
1582 xcb_disconnect(conn);
1589 * Implementation of 'reload'.
1592 void cmd_reload(I3_CMD) {
1594 kill_configerror_nagbar(false);
1595 kill_commanderror_nagbar(false);
1596 load_configuration(conn, NULL, true);
1598 /* Send an IPC event just in case the ws names have changed */
1599 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1601 // XXX: default reply for now, make this a better reply
1606 * Implementation of 'restart'.
1609 void cmd_restart(I3_CMD) {
1610 LOG("restarting i3\n");
1613 // XXX: default reply for now, make this a better reply
1618 * Implementation of 'open'.
1621 void cmd_open(I3_CMD) {
1622 LOG("opening new container\n");
1623 Con *con = tree_open_con(NULL, NULL);
1624 con->layout = L_SPLITH;
1631 y(integer, (long int)con);
1634 cmd_output->needs_tree_render = true;
1638 * Implementation of 'focus output <output>'.
1641 void cmd_focus_output(I3_CMD, char *name) {
1644 DLOG("name = %s\n", name);
1648 /* get the output */
1649 Output *current_output = NULL;
1652 TAILQ_FOREACH(current, &owindows, owindows)
1653 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1654 assert(current_output != NULL);
1656 output = get_output_from_string(current_output, name);
1659 LOG("No such output found.\n");
1664 /* get visible workspace on output */
1666 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1674 cmd_output->needs_tree_render = true;
1675 // XXX: default reply for now, make this a better reply
1680 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1683 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1688 if (!con_is_floating(focused)) {
1689 ELOG("Cannot change position. The window/container is not floating\n");
1694 ystr("Cannot change position. The window/container is not floating.");
1699 if (strcmp(method, "absolute") == 0) {
1700 focused->parent->rect.x = x;
1701 focused->parent->rect.y = y;
1703 DLOG("moving to absolute position %d %d\n", x, y);
1704 floating_maybe_reassign_ws(focused->parent);
1705 cmd_output->needs_tree_render = true;
1708 if (strcmp(method, "position") == 0) {
1709 Rect newrect = focused->parent->rect;
1711 DLOG("moving to position %d %d\n", x, y);
1715 floating_reposition(focused->parent, newrect);
1718 // XXX: default reply for now, make this a better reply
1723 * Implementation of 'move [window|container] [to] [absolute] position center
1726 void cmd_move_window_to_center(I3_CMD, char *method) {
1728 if (!con_is_floating(focused)) {
1729 ELOG("Cannot change position. The window/container is not floating\n");
1734 ystr("Cannot change position. The window/container is not floating.");
1738 if (strcmp(method, "absolute") == 0) {
1739 Rect *rect = &focused->parent->rect;
1741 DLOG("moving to absolute center\n");
1742 rect->x = croot->rect.width/2 - rect->width/2;
1743 rect->y = croot->rect.height/2 - rect->height/2;
1745 floating_maybe_reassign_ws(focused->parent);
1746 cmd_output->needs_tree_render = true;
1749 if (strcmp(method, "position") == 0) {
1750 Rect *wsrect = &con_get_workspace(focused)->rect;
1751 Rect newrect = focused->parent->rect;
1753 DLOG("moving to center\n");
1754 newrect.x = wsrect->width/2 - newrect.width/2;
1755 newrect.y = wsrect->height/2 - newrect.height/2;
1757 floating_reposition(focused->parent, newrect);
1760 // XXX: default reply for now, make this a better reply
1765 * Implementation of 'move scratchpad'.
1768 void cmd_move_scratchpad(I3_CMD) {
1769 DLOG("should move window to scratchpad\n");
1774 TAILQ_FOREACH(current, &owindows, owindows) {
1775 DLOG("matching: %p / %s\n", current->con, current->con->name);
1776 scratchpad_move(current->con);
1779 cmd_output->needs_tree_render = true;
1780 // XXX: default reply for now, make this a better reply
1785 * Implementation of 'scratchpad show'.
1788 void cmd_scratchpad_show(I3_CMD) {
1789 DLOG("should show scratchpad window\n");
1792 if (match_is_empty(current_match)) {
1793 scratchpad_show(NULL);
1795 TAILQ_FOREACH(current, &owindows, owindows) {
1796 DLOG("matching: %p / %s\n", current->con, current->con->name);
1797 scratchpad_show(current->con);
1801 cmd_output->needs_tree_render = true;
1802 // XXX: default reply for now, make this a better reply
1807 * Implementation of 'rename workspace [<name>] to <name>'
1810 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1812 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1814 LOG("Renaming current workspace to \"%s\"\n", new_name);
1817 Con *output, *workspace = NULL;
1819 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1820 GREP_FIRST(workspace, output_get_content(output),
1821 !strcasecmp(child->name, old_name));
1823 workspace = con_get_workspace(focused);
1827 // TODO: we should include the old workspace name here and use yajl for
1828 // generating the reply.
1833 // TODO: better error message
1834 ystr("Old workspace not found");
1839 Con *check_dest = NULL;
1840 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1841 GREP_FIRST(check_dest, output_get_content(output),
1842 !strcasecmp(child->name, new_name));
1844 if (check_dest != NULL) {
1845 // TODO: we should include the new workspace name here and use yajl for
1846 // generating the reply.
1851 // TODO: better error message
1852 ystr("New workspace already exists");
1857 /* Change the name and try to parse it as a number. */
1858 FREE(workspace->name);
1859 workspace->name = sstrdup(new_name);
1860 char *endptr = NULL;
1861 long parsed_num = strtol(new_name, &endptr, 10);
1862 if (parsed_num == LONG_MIN ||
1863 parsed_num == LONG_MAX ||
1866 workspace->num = -1;
1867 else workspace->num = parsed_num;
1868 LOG("num = %d\n", workspace->num);
1870 /* By re-attaching, the sort order will be correct afterwards. */
1871 Con *previously_focused = focused;
1872 Con *parent = workspace->parent;
1873 con_detach(workspace);
1874 con_attach(workspace, parent, false);
1875 /* Restore the previous focus since con_attach messes with the focus. */
1876 con_focus(previously_focused);
1878 cmd_output->needs_tree_render = true;
1881 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");