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_wrap(D_LEFT, current_output);
60 else if (strcasecmp(output_str, "right") == 0)
61 output = get_output_next_wrap(D_RIGHT, current_output);
62 else if (strcasecmp(output_str, "up") == 0)
63 output = get_output_next_wrap(D_UP, current_output);
64 else if (strcasecmp(output_str, "down") == 0)
65 output = get_output_next_wrap(D_DOWN, current_output);
66 else output = get_output_by_name(output_str);
72 * Checks whether we switched to a new workspace and returns false in that case,
73 * signaling that further workspace switching should be done by the calling function
74 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
75 * and return true, signaling that no further workspace switching should occur in the calling function.
78 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
79 Con *ws = con_get_workspace(focused);
81 /* If we switched to a different workspace, do nothing */
82 if (strcmp(ws->name, name) != 0)
85 DLOG("This workspace is already focused.\n");
86 if (config.workspace_auto_back_and_forth) {
87 workspace_back_and_forth();
88 cmd_output->needs_tree_render = true;
94 * Return the passed workspace unless it is the current one and auto back and
95 * forth is enabled, in which case the back_and_forth workspace is returned.
97 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
100 if (!config.workspace_auto_back_and_forth)
103 current = con_get_workspace(focused);
105 if (current == workspace) {
106 baf = workspace_back_and_forth_get();
108 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
116 // This code is commented out because we might recycle it for popping up error
117 // messages on parser errors.
119 static pid_t migration_pid = -1;
122 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
123 * it exited (or could not be started, depending on the exit code).
126 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
127 ev_child_stop(EV_A_ watcher);
128 if (!WIFEXITED(watcher->rstatus)) {
129 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
133 int exitcode = WEXITSTATUS(watcher->rstatus);
134 printf("i3-nagbar process exited with status %d\n", exitcode);
136 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
142 /* We need ev >= 4 for the following code. Since it is not *that* important (it
143 * only makes sure that there are no i3-nagbar instances left behind) we still
144 * support old systems with libev 3. */
145 #if EV_VERSION_MAJOR >= 4
147 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
148 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
151 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
152 if (migration_pid != -1) {
153 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
154 kill(migration_pid, SIGKILL);
159 void cmd_MIGRATION_start_nagbar(void) {
160 if (migration_pid != -1) {
161 fprintf(stderr, "i3-nagbar already running.\n");
164 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
165 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
166 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
167 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
168 migration_pid = fork();
169 if (migration_pid == -1) {
170 warn("Could not fork()");
175 if (migration_pid == 0) {
177 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
179 NULL, /* will be replaced by the executable path */
183 "You found a parsing error. Please, please, please, report it!",
189 exec_i3_utility("i3-nagbar", argv);
193 /* install a child watcher */
194 ev_child *child = smalloc(sizeof(ev_child));
195 ev_child_init(child, &nagbar_exited, migration_pid, 0);
196 ev_child_start(main_loop, child);
198 /* We need ev >= 4 for the following code. Since it is not *that* important (it
199 * only makes sure that there are no i3-nagbar instances left behind) we still
200 * support old systems with libev 3. */
201 #if EV_VERSION_MAJOR >= 4
202 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
204 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
205 ev_cleanup_init(cleanup, nagbar_cleanup);
206 ev_cleanup_start(main_loop, cleanup);
212 /*******************************************************************************
213 * Criteria functions.
214 ******************************************************************************/
217 * Helper data structure for an operation window (window on which the operation
218 * will be performed). Used to build the TAILQ owindows.
221 typedef struct owindow {
223 TAILQ_ENTRY(owindow) owindows;
226 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
228 static owindows_head owindows;
231 * Initializes the specified 'Match' data structure and the initial state of
232 * commands.c for matching target windows of a command.
235 void cmd_criteria_init(I3_CMD) {
239 DLOG("Initializing criteria, current_match = %p\n", current_match);
240 match_init(current_match);
241 while (!TAILQ_EMPTY(&owindows)) {
242 ow = TAILQ_FIRST(&owindows);
243 TAILQ_REMOVE(&owindows, ow, owindows);
246 TAILQ_INIT(&owindows);
248 TAILQ_FOREACH(con, &all_cons, all_cons) {
249 ow = smalloc(sizeof(owindow));
251 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
256 * A match specification just finished (the closing square bracket was found),
257 * so we filter the list of owindows.
260 void cmd_criteria_match_windows(I3_CMD) {
261 owindow *next, *current;
263 DLOG("match specification finished, matching...\n");
264 /* copy the old list head to iterate through it and start with a fresh
265 * list which will contain only matching windows */
266 struct owindows_head old = owindows;
267 TAILQ_INIT(&owindows);
268 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
269 /* make a copy of the next pointer and advance the pointer to the
270 * next element as we are going to invalidate the element’s
271 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
273 next = TAILQ_NEXT(next, owindows);
275 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
276 if (current_match->con_id != NULL) {
277 if (current_match->con_id == current->con) {
278 DLOG("matches container!\n");
279 TAILQ_INSERT_TAIL(&owindows, current, owindows);
281 } else if (current_match->mark != NULL && current->con->mark != NULL &&
282 regex_matches(current_match->mark, current->con->mark)) {
283 DLOG("match by mark\n");
284 TAILQ_INSERT_TAIL(&owindows, current, owindows);
286 if (current->con->window == NULL)
288 if (match_matches_window(current_match, current->con->window)) {
289 DLOG("matches window!\n");
290 TAILQ_INSERT_TAIL(&owindows, current, owindows);
292 DLOG("doesnt match\n");
298 TAILQ_FOREACH(current, &owindows, owindows) {
299 DLOG("matching: %p / %s\n", current->con, current->con->name);
304 * Interprets a ctype=cvalue pair and adds it to the current match
308 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
309 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
311 if (strcmp(ctype, "class") == 0) {
312 current_match->class = regex_new(cvalue);
316 if (strcmp(ctype, "instance") == 0) {
317 current_match->instance = regex_new(cvalue);
321 if (strcmp(ctype, "window_role") == 0) {
322 current_match->role = regex_new(cvalue);
326 if (strcmp(ctype, "con_id") == 0) {
328 long parsed = strtol(cvalue, &end, 10);
329 if (parsed == LONG_MIN ||
330 parsed == LONG_MAX ||
332 (end && *end != '\0')) {
333 ELOG("Could not parse con id \"%s\"\n", cvalue);
335 current_match->con_id = (Con*)parsed;
336 printf("id as int = %p\n", current_match->con_id);
341 if (strcmp(ctype, "id") == 0) {
343 long parsed = strtol(cvalue, &end, 10);
344 if (parsed == LONG_MIN ||
345 parsed == LONG_MAX ||
347 (end && *end != '\0')) {
348 ELOG("Could not parse window id \"%s\"\n", cvalue);
350 current_match->id = parsed;
351 printf("window id as int = %d\n", current_match->id);
356 if (strcmp(ctype, "con_mark") == 0) {
357 current_match->mark = regex_new(cvalue);
361 if (strcmp(ctype, "title") == 0) {
362 current_match->title = regex_new(cvalue);
366 if (strcmp(ctype, "urgent") == 0) {
367 if (strcasecmp(cvalue, "latest") == 0 ||
368 strcasecmp(cvalue, "newest") == 0 ||
369 strcasecmp(cvalue, "recent") == 0 ||
370 strcasecmp(cvalue, "last") == 0) {
371 current_match->urgent = U_LATEST;
372 } else if (strcasecmp(cvalue, "oldest") == 0 ||
373 strcasecmp(cvalue, "first") == 0) {
374 current_match->urgent = U_OLDEST;
379 ELOG("Unknown criterion: %s\n", ctype);
383 * Implementation of 'move [window|container] [to] workspace
384 * next|prev|next_on_output|prev_on_output|current'.
387 void cmd_move_con_to_workspace(I3_CMD, char *which) {
390 DLOG("which=%s\n", which);
392 /* We have nothing to move:
393 * when criteria was specified but didn't match any window or
394 * when criteria wasn't specified and we don't have any window focused. */
395 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
396 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
397 !con_has_children(focused))) {
404 /* get the workspace */
406 if (strcmp(which, "next") == 0)
407 ws = workspace_next();
408 else if (strcmp(which, "prev") == 0)
409 ws = workspace_prev();
410 else if (strcmp(which, "next_on_output") == 0)
411 ws = workspace_next_on_output();
412 else if (strcmp(which, "prev_on_output") == 0)
413 ws = workspace_prev_on_output();
414 else if (strcmp(which, "current") == 0)
415 ws = con_get_workspace(focused);
417 ELOG("BUG: called with which=%s\n", which);
422 TAILQ_FOREACH(current, &owindows, owindows) {
423 DLOG("matching: %p / %s\n", current->con, current->con->name);
424 con_move_to_workspace(current->con, ws, true, false);
427 cmd_output->needs_tree_render = true;
428 // XXX: default reply for now, make this a better reply
433 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
436 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
440 ws = workspace_back_and_forth_get();
447 ystr("No workspace was previously active.");
454 TAILQ_FOREACH(current, &owindows, owindows) {
455 DLOG("matching: %p / %s\n", current->con, current->con->name);
456 con_move_to_workspace(current->con, ws, true, false);
459 cmd_output->needs_tree_render = true;
460 // XXX: default reply for now, make this a better reply
465 * Implementation of 'move [window|container] [to] workspace <name>'.
468 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
469 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
470 LOG("You cannot switch to the i3 internal workspaces.\n");
477 /* We have nothing to move:
478 * when criteria was specified but didn't match any window or
479 * when criteria wasn't specified and we don't have any window focused. */
480 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
481 ELOG("No windows match your criteria, cannot move.\n");
485 else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
486 !con_has_children(focused)) {
491 LOG("should move window to workspace %s\n", name);
492 /* get the workspace */
493 Con *ws = workspace_get(name, NULL);
495 ws = maybe_auto_back_and_forth_workspace(ws);
499 TAILQ_FOREACH(current, &owindows, owindows) {
500 DLOG("matching: %p / %s\n", current->con, current->con->name);
501 con_move_to_workspace(current->con, ws, true, false);
504 cmd_output->needs_tree_render = true;
505 // XXX: default reply for now, make this a better reply
510 * Implementation of 'move [window|container] [to] workspace number <name>'.
513 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
516 /* We have nothing to move:
517 * when criteria was specified but didn't match any window or
518 * when criteria wasn't specified and we don't have any window focused. */
519 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
520 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
521 !con_has_children(focused))) {
526 LOG("should move window to workspace %s\n", which);
527 /* get the workspace */
528 Con *output, *workspace = NULL;
531 long parsed_num = strtol(which, &endptr, 10);
532 if (parsed_num == LONG_MIN ||
533 parsed_num == LONG_MAX ||
536 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
541 // TODO: better error message
542 ystr("Could not parse number");
547 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
548 GREP_FIRST(workspace, output_get_content(output),
549 child->num == parsed_num);
552 workspace = workspace_get(which, NULL);
555 workspace = maybe_auto_back_and_forth_workspace(workspace);
559 TAILQ_FOREACH(current, &owindows, owindows) {
560 DLOG("matching: %p / %s\n", current->con, current->con->name);
561 con_move_to_workspace(current->con, workspace, true, false);
564 cmd_output->needs_tree_render = true;
565 // XXX: default reply for now, make this a better reply
569 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
570 LOG("floating resize\n");
571 Rect old_rect = floating_con->rect;
573 if (strcmp(direction, "up") == 0) {
574 floating_con->rect.height += px;
575 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
576 floating_con->rect.height += px;
577 } else if (strcmp(direction, "left") == 0) {
578 floating_con->rect.width += px;
580 floating_con->rect.width += px;
583 floating_check_size(floating_con);
585 /* Did we actually resize anything or did the size constraints prevent us?
586 * If we could not resize, exit now to not move the window. */
587 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
590 if (strcmp(direction, "up") == 0) {
591 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
592 } else if (strcmp(direction, "left") == 0) {
593 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
597 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
598 LOG("tiling resize\n");
599 /* get the appropriate current container (skip stacked/tabbed cons) */
601 double percentage = 0;
602 while (current->parent->layout == L_STACKED ||
603 current->parent->layout == L_TABBED)
604 current = current->parent;
606 /* Then further go up until we find one with the matching orientation. */
607 orientation_t search_orientation =
608 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
611 if (con_orientation(current->parent) != search_orientation) {
612 current = current->parent;
616 /* get the default percentage */
617 int children = con_num_children(current->parent);
618 LOG("ins. %d children\n", children);
619 percentage = 1.0 / children;
620 LOG("default percentage = %f\n", percentage);
622 orientation_t orientation = con_orientation(current->parent);
624 if ((orientation == HORIZ &&
625 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
626 (orientation == VERT &&
627 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
628 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
629 (orientation == HORIZ ? "horizontal" : "vertical"));
634 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
635 other = TAILQ_PREV(current, nodes_head, nodes);
637 other = TAILQ_NEXT(current, nodes);
639 if (other == TAILQ_END(workspaces)) {
640 LOG("No other container in this direction found, trying to look further up in the tree...\n");
641 current = current->parent;
645 } while (current->type != CT_WORKSPACE &&
646 current->type != CT_FLOATING_CON);
649 LOG("No other container in this direction found, trying to look further up in the tree...\n");
654 LOG("other->percent = %f\n", other->percent);
655 LOG("current->percent before = %f\n", current->percent);
656 if (current->percent == 0.0)
657 current->percent = percentage;
658 if (other->percent == 0.0)
659 other->percent = percentage;
660 double new_current_percent = current->percent + ((double)ppt / 100.0);
661 double new_other_percent = other->percent - ((double)ppt / 100.0);
662 LOG("new_current_percent = %f\n", new_current_percent);
663 LOG("new_other_percent = %f\n", new_other_percent);
664 /* Ensure that the new percentages are positive and greater than
665 * 0.05 to have a reasonable minimum size. */
666 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
667 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
668 current->percent += ((double)ppt / 100.0);
669 other->percent -= ((double)ppt / 100.0);
670 LOG("current->percent after = %f\n", current->percent);
671 LOG("other->percent after = %f\n", other->percent);
673 LOG("Not resizing, already at minimum size\n");
679 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
680 LOG("width/height resize\n");
681 /* get the appropriate current container (skip stacked/tabbed cons) */
682 while (current->parent->layout == L_STACKED ||
683 current->parent->layout == L_TABBED)
684 current = current->parent;
686 /* Then further go up until we find one with the matching orientation. */
687 orientation_t search_orientation =
688 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
690 while (current->type != CT_WORKSPACE &&
691 current->type != CT_FLOATING_CON &&
692 con_orientation(current->parent) != search_orientation)
693 current = current->parent;
695 /* get the default percentage */
696 int children = con_num_children(current->parent);
697 LOG("ins. %d children\n", children);
698 double percentage = 1.0 / children;
699 LOG("default percentage = %f\n", percentage);
701 orientation_t orientation = con_orientation(current->parent);
703 if ((orientation == HORIZ &&
704 strcmp(direction, "height") == 0) ||
705 (orientation == VERT &&
706 strcmp(direction, "width") == 0)) {
707 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
708 (orientation == HORIZ ? "horizontal" : "vertical"));
714 LOG("This is the only container, cannot resize.\n");
719 /* Ensure all the other children have a percentage set. */
721 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
722 LOG("child->percent = %f (child %p)\n", child->percent, child);
723 if (child->percent == 0.0)
724 child->percent = percentage;
727 double new_current_percent = current->percent + ((double)ppt / 100.0);
728 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
729 LOG("new_current_percent = %f\n", new_current_percent);
730 LOG("subtract_percent = %f\n", subtract_percent);
731 /* Ensure that the new percentages are positive and greater than
732 * 0.05 to have a reasonable minimum size. */
733 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
734 if (child == current)
736 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
737 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
742 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
743 LOG("Not resizing, already at minimum size\n");
748 current->percent += ((double)ppt / 100.0);
749 LOG("current->percent after = %f\n", current->percent);
751 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
752 if (child == current)
754 child->percent -= subtract_percent;
755 LOG("child->percent after (%p) = %f\n", child, child->percent);
762 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
765 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
766 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
767 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
768 // 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
769 int px = atoi(resize_px);
770 int ppt = atoi(resize_ppt);
771 if (strcmp(way, "shrink") == 0) {
779 TAILQ_FOREACH(current, &owindows, owindows) {
781 if ((floating_con = con_inside_floating(current->con))) {
782 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
784 if (strcmp(direction, "width") == 0 ||
785 strcmp(direction, "height") == 0) {
786 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
789 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
795 cmd_output->needs_tree_render = true;
796 // XXX: default reply for now, make this a better reply
801 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
804 void cmd_border(I3_CMD, char *border_style_str, char *border_width ) {
805 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
810 TAILQ_FOREACH(current, &owindows, owindows) {
811 DLOG("matching: %p / %s\n", current->con, current->con->name);
812 int border_style = current->con->border_style;
814 int tmp_border_width = -1;
815 tmp_border_width = strtol(border_width, &end, 10);
816 if (end == border_width) {
817 /* no valid digits found */
818 tmp_border_width = -1;
820 if (strcmp(border_style_str, "toggle") == 0) {
823 if (border_style == BS_NORMAL)
824 tmp_border_width = 2;
825 else if (border_style == BS_NONE)
826 tmp_border_width = 0;
827 else if (border_style == BS_PIXEL)
828 tmp_border_width = 1;
830 if (strcmp(border_style_str, "normal") == 0)
831 border_style = BS_NORMAL;
832 else if (strcmp(border_style_str, "pixel") == 0)
833 border_style = BS_PIXEL;
834 else if (strcmp(border_style_str, "1pixel") == 0){
835 border_style = BS_PIXEL;
836 tmp_border_width = 1;
837 } else if (strcmp(border_style_str, "none") == 0)
838 border_style = BS_NONE;
840 ELOG("BUG: called with border_style=%s\n", border_style_str);
845 con_set_border_style(current->con, border_style, tmp_border_width);
848 cmd_output->needs_tree_render = true;
849 // XXX: default reply for now, make this a better reply
854 * Implementation of 'nop <comment>'.
857 void cmd_nop(I3_CMD, char *comment) {
858 LOG("-------------------------------------------------\n");
859 LOG(" NOP: %s\n", comment);
860 LOG("-------------------------------------------------\n");
864 * Implementation of 'append_layout <path>'.
867 void cmd_append_layout(I3_CMD, char *path) {
868 LOG("Appending layout \"%s\"\n", path);
869 tree_append_json(path);
871 cmd_output->needs_tree_render = true;
872 // XXX: default reply for now, make this a better reply
877 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
880 void cmd_workspace(I3_CMD, char *which) {
883 DLOG("which=%s\n", which);
885 if (strcmp(which, "next") == 0)
886 ws = workspace_next();
887 else if (strcmp(which, "prev") == 0)
888 ws = workspace_prev();
889 else if (strcmp(which, "next_on_output") == 0)
890 ws = workspace_next_on_output();
891 else if (strcmp(which, "prev_on_output") == 0)
892 ws = workspace_prev_on_output();
894 ELOG("BUG: called with which=%s\n", which);
901 cmd_output->needs_tree_render = true;
902 // XXX: default reply for now, make this a better reply
907 * Implementation of 'workspace number <name>'
910 void cmd_workspace_number(I3_CMD, char *which) {
911 Con *output, *workspace = NULL;
914 long parsed_num = strtol(which, &endptr, 10);
915 if (parsed_num == LONG_MIN ||
916 parsed_num == LONG_MAX ||
919 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
924 // TODO: better error message
925 ystr("Could not parse number");
931 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
932 GREP_FIRST(workspace, output_get_content(output),
933 child->num == parsed_num);
936 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
938 workspace_show_by_name(which);
939 cmd_output->needs_tree_render = true;
942 if (maybe_back_and_forth(cmd_output, workspace->name))
944 workspace_show(workspace);
946 cmd_output->needs_tree_render = true;
947 // XXX: default reply for now, make this a better reply
952 * Implementation of 'workspace back_and_forth'.
955 void cmd_workspace_back_and_forth(I3_CMD) {
956 workspace_back_and_forth();
958 cmd_output->needs_tree_render = true;
959 // XXX: default reply for now, make this a better reply
964 * Implementation of 'workspace <name>'
967 void cmd_workspace_name(I3_CMD, char *name) {
968 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
969 LOG("You cannot switch to the i3 internal workspaces.\n");
974 DLOG("should switch to workspace %s\n", name);
975 if (maybe_back_and_forth(cmd_output, name))
977 workspace_show_by_name(name);
979 cmd_output->needs_tree_render = true;
980 // XXX: default reply for now, make this a better reply
985 * Implementation of 'mark <mark>'
988 void cmd_mark(I3_CMD, char *mark) {
989 DLOG("Clearing all windows which have that mark first\n");
992 TAILQ_FOREACH(con, &all_cons, all_cons) {
993 if (con->mark && strcmp(con->mark, mark) == 0)
997 DLOG("marking window with str %s\n", mark);
1002 TAILQ_FOREACH(current, &owindows, owindows) {
1003 DLOG("matching: %p / %s\n", current->con, current->con->name);
1004 current->con->mark = sstrdup(mark);
1007 cmd_output->needs_tree_render = true;
1008 // XXX: default reply for now, make this a better reply
1013 * Implementation of 'mode <string>'.
1016 void cmd_mode(I3_CMD, char *mode) {
1017 DLOG("mode=%s\n", mode);
1020 // XXX: default reply for now, make this a better reply
1025 * Implementation of 'move [window|container] [to] output <str>'.
1028 void cmd_move_con_to_output(I3_CMD, char *name) {
1031 DLOG("should move window to output %s\n", name);
1035 /* get the output */
1036 Output *current_output = NULL;
1039 // TODO: fix the handling of criteria
1040 TAILQ_FOREACH(current, &owindows, owindows)
1041 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1043 assert(current_output != NULL);
1045 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1046 if (strcasecmp(name, "up") == 0)
1047 output = get_output_next_wrap(D_UP, current_output);
1048 else if (strcasecmp(name, "down") == 0)
1049 output = get_output_next_wrap(D_DOWN, current_output);
1050 else if (strcasecmp(name, "left") == 0)
1051 output = get_output_next_wrap(D_LEFT, current_output);
1052 else if (strcasecmp(name, "right") == 0)
1053 output = get_output_next_wrap(D_RIGHT, current_output);
1055 output = get_output_by_name(name);
1058 LOG("No such output found.\n");
1063 /* get visible workspace on output */
1065 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1071 TAILQ_FOREACH(current, &owindows, owindows) {
1072 DLOG("matching: %p / %s\n", current->con, current->con->name);
1073 con_move_to_workspace(current->con, ws, true, false);
1076 cmd_output->needs_tree_render = true;
1077 // XXX: default reply for now, make this a better reply
1082 * Implementation of 'floating enable|disable|toggle'
1085 void cmd_floating(I3_CMD, char *floating_mode) {
1088 DLOG("floating_mode=%s\n", floating_mode);
1092 TAILQ_FOREACH(current, &owindows, owindows) {
1093 DLOG("matching: %p / %s\n", current->con, current->con->name);
1094 if (strcmp(floating_mode, "toggle") == 0) {
1095 DLOG("should toggle mode\n");
1096 toggle_floating_mode(current->con, false);
1098 DLOG("should switch mode to %s\n", floating_mode);
1099 if (strcmp(floating_mode, "enable") == 0) {
1100 floating_enable(current->con, false);
1102 floating_disable(current->con, false);
1107 cmd_output->needs_tree_render = true;
1108 // XXX: default reply for now, make this a better reply
1113 * Implementation of 'move workspace to [output] <str>'.
1116 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1117 DLOG("should move workspace to output %s\n", name);
1122 TAILQ_FOREACH(current, &owindows, owindows) {
1123 Output *current_output = get_output_containing(current->con->rect.x,
1124 current->con->rect.y);
1125 if (!current_output) {
1126 ELOG("Cannot get current output. This is a bug in i3.\n");
1130 Output *output = get_output_from_string(current_output, name);
1132 ELOG("Could not get output from string \"%s\"\n", name);
1137 Con *content = output_get_content(output->con);
1138 LOG("got output %p with content %p\n", output, content);
1140 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1141 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1143 Con *ws = con_get_workspace(current->con);
1144 LOG("should move workspace %p / %s\n", ws, ws->name);
1145 bool workspace_was_visible = workspace_is_visible(ws);
1147 if (con_num_children(ws->parent) == 1) {
1148 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1150 /* check if we can find a workspace assigned to this output */
1151 bool used_assignment = false;
1152 struct Workspace_Assignment *assignment;
1153 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1154 if (strcmp(assignment->output, current_output->name) != 0)
1157 /* check if this workspace is already attached to the tree */
1158 Con *workspace = NULL, *out;
1159 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1160 GREP_FIRST(workspace, output_get_content(out),
1161 !strcasecmp(child->name, assignment->name));
1162 if (workspace != NULL)
1165 /* so create the workspace referenced to by this assignment */
1166 LOG("Creating workspace from assignment %s.\n", assignment->name);
1167 workspace_get(assignment->name, NULL);
1168 used_assignment = true;
1172 /* if we couldn't create the workspace using an assignment, create
1173 * it on the output */
1174 if (!used_assignment)
1175 create_workspace_on_output(current_output, ws->parent);
1177 /* notify the IPC listeners */
1178 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1180 DLOG("Detaching\n");
1182 /* detach from the old output and attach to the new output */
1183 Con *old_content = ws->parent;
1185 if (workspace_was_visible) {
1186 /* The workspace which we just detached was visible, so focus
1187 * the next one in the focus-stack. */
1188 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1189 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1190 workspace_show(focus_ws);
1192 con_attach(ws, content, false);
1194 /* fix the coordinates of the floating containers */
1196 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1197 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1199 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1200 if (workspace_was_visible) {
1201 /* Focus the moved workspace on the destination output. */
1205 /* NB: We cannot simply work with previously_visible_ws since it might
1206 * have been cleaned up by workspace_show() already, depending on the
1207 * focus order/number of other workspaces on the output.
1208 * Instead, we loop through the available workspaces and only work with
1209 * previously_visible_ws if we still find it. */
1210 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1211 if (ws != previously_visible_ws)
1214 /* Call the on_remove_child callback of the workspace which previously
1215 * was visible on the destination output. Since it is no longer
1216 * visible, it might need to get cleaned up. */
1217 CALL(previously_visible_ws, on_remove_child);
1222 cmd_output->needs_tree_render = true;
1223 // XXX: default reply for now, make this a better reply
1228 * Implementation of 'split v|h|vertical|horizontal'.
1231 void cmd_split(I3_CMD, char *direction) {
1233 /* TODO: use matches */
1234 LOG("splitting in direction %c\n", direction[0]);
1235 if (match_is_empty(current_match))
1236 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1238 TAILQ_FOREACH(current, &owindows, owindows) {
1239 DLOG("matching: %p / %s\n", current->con, current->con->name);
1240 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1244 cmd_output->needs_tree_render = true;
1245 // XXX: default reply for now, make this a better reply
1250 * Implementation of 'kill [window|client]'.
1253 void cmd_kill(I3_CMD, char *kill_mode_str) {
1254 if (kill_mode_str == NULL)
1255 kill_mode_str = "window";
1258 DLOG("kill_mode=%s\n", kill_mode_str);
1261 if (strcmp(kill_mode_str, "window") == 0)
1262 kill_mode = KILL_WINDOW;
1263 else if (strcmp(kill_mode_str, "client") == 0)
1264 kill_mode = KILL_CLIENT;
1266 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1271 /* check if the match is empty, not if the result is empty */
1272 if (match_is_empty(current_match))
1273 tree_close_con(kill_mode);
1275 TAILQ_FOREACH(current, &owindows, owindows) {
1276 DLOG("matching: %p / %s\n", current->con, current->con->name);
1277 tree_close(current->con, kill_mode, false, false);
1281 cmd_output->needs_tree_render = true;
1282 // XXX: default reply for now, make this a better reply
1287 * Implementation of 'exec [--no-startup-id] <command>'.
1290 void cmd_exec(I3_CMD, char *nosn, char *command) {
1291 bool no_startup_id = (nosn != NULL);
1293 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1294 start_application(command, no_startup_id);
1296 // XXX: default reply for now, make this a better reply
1301 * Implementation of 'focus left|right|up|down'.
1304 void cmd_focus_direction(I3_CMD, char *direction) {
1305 DLOG("direction = *%s*\n", direction);
1307 if (strcmp(direction, "left") == 0)
1308 tree_next('p', HORIZ);
1309 else if (strcmp(direction, "right") == 0)
1310 tree_next('n', HORIZ);
1311 else if (strcmp(direction, "up") == 0)
1312 tree_next('p', VERT);
1313 else if (strcmp(direction, "down") == 0)
1314 tree_next('n', VERT);
1316 ELOG("Invalid focus direction (%s)\n", direction);
1321 cmd_output->needs_tree_render = true;
1322 // XXX: default reply for now, make this a better reply
1327 * Implementation of 'focus tiling|floating|mode_toggle'.
1330 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1331 DLOG("window_mode = %s\n", window_mode);
1333 Con *ws = con_get_workspace(focused);
1336 if (strcmp(window_mode, "mode_toggle") == 0) {
1337 current = TAILQ_FIRST(&(ws->focus_head));
1338 if (current != NULL && current->type == CT_FLOATING_CON)
1339 window_mode = "tiling";
1340 else window_mode = "floating";
1342 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1343 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1344 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1347 con_focus(con_descend_focused(current));
1352 cmd_output->needs_tree_render = true;
1353 // XXX: default reply for now, make this a better reply
1358 * Implementation of 'focus parent|child'.
1361 void cmd_focus_level(I3_CMD, char *level) {
1362 DLOG("level = %s\n", level);
1363 bool success = false;
1365 /* Focusing the parent can only be allowed if the newly
1366 * focused container won't escape the fullscreen container. */
1367 if (strcmp(level, "parent") == 0) {
1368 if (focused && focused->parent) {
1369 if (con_fullscreen_permits_focusing(focused->parent))
1370 success = level_up();
1372 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1376 /* Focusing a child should always be allowed. */
1377 else success = level_down();
1379 cmd_output->needs_tree_render = success;
1380 // XXX: default reply for now, make this a better reply
1385 * Implementation of 'focus'.
1388 void cmd_focus(I3_CMD) {
1389 DLOG("current_match = %p\n", current_match);
1391 if (match_is_empty(current_match)) {
1392 ELOG("You have to specify which window/container should be focused.\n");
1393 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1399 ystr("You have to specify which window/container should be focused");
1405 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1408 TAILQ_FOREACH(current, &owindows, owindows) {
1409 Con *ws = con_get_workspace(current->con);
1410 /* If no workspace could be found, this was a dock window.
1411 * Just skip it, you cannot focus dock windows. */
1415 /* Check the fullscreen focus constraints. */
1416 if (!con_fullscreen_permits_focusing(current->con)) {
1417 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1422 /* In case this is a scratchpad window, call scratchpad_show(). */
1423 if (ws == __i3_scratch) {
1424 scratchpad_show(current->con);
1426 /* While for the normal focus case we can change focus multiple
1427 * times and only a single window ends up focused, we could show
1428 * multiple scratchpad windows. So, rather break here. */
1432 /* If the container is not on the current workspace,
1433 * workspace_show() will switch to a different workspace and (if
1434 * enabled) trigger a mouse pointer warp to the currently focused
1435 * container (!) on the target workspace.
1437 * Therefore, before calling workspace_show(), we make sure that
1438 * 'current' will be focused on the workspace. However, we cannot
1439 * just con_focus(current) because then the pointer will not be
1440 * warped at all (the code thinks we are already there).
1442 * So we focus 'current' to make it the currently focused window of
1443 * the target workspace, then revert focus. */
1444 Con *currently_focused = focused;
1445 con_focus(current->con);
1446 con_focus(currently_focused);
1448 /* Now switch to the workspace, then focus */
1450 LOG("focusing %p / %s\n", current->con, current->con->name);
1451 con_focus(current->con);
1456 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1457 "while only exactly one container can be focused at a time.\n", count);
1459 cmd_output->needs_tree_render = true;
1460 // XXX: default reply for now, make this a better reply
1465 * Implementation of 'fullscreen [global]'.
1468 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1469 if (fullscreen_mode == NULL)
1470 fullscreen_mode = "output";
1471 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1476 TAILQ_FOREACH(current, &owindows, owindows) {
1477 printf("matching: %p / %s\n", current->con, current->con->name);
1478 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1481 cmd_output->needs_tree_render = true;
1482 // XXX: default reply for now, make this a better reply
1487 * Implementation of 'move <direction> [<pixels> [px]]'.
1490 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1491 // 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
1492 int px = atoi(move_px);
1494 /* TODO: make 'move' work with criteria. */
1495 DLOG("moving in direction %s, px %s\n", direction, move_px);
1496 if (con_is_floating(focused)) {
1497 DLOG("floating move with %d pixels\n", px);
1498 Rect newrect = focused->parent->rect;
1499 if (strcmp(direction, "left") == 0) {
1501 } else if (strcmp(direction, "right") == 0) {
1503 } else if (strcmp(direction, "up") == 0) {
1505 } else if (strcmp(direction, "down") == 0) {
1508 floating_reposition(focused->parent, newrect);
1510 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1511 (strcmp(direction, "left") == 0 ? D_LEFT :
1512 (strcmp(direction, "up") == 0 ? D_UP :
1514 cmd_output->needs_tree_render = true;
1517 // XXX: default reply for now, make this a better reply
1522 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1525 void cmd_layout(I3_CMD, char *layout_str) {
1526 if (strcmp(layout_str, "stacking") == 0)
1527 layout_str = "stacked";
1530 /* default is a special case which will be handled in con_set_layout(). */
1531 if (strcmp(layout_str, "default") == 0)
1533 else if (strcmp(layout_str, "stacked") == 0)
1535 else if (strcmp(layout_str, "tabbed") == 0)
1537 else if (strcmp(layout_str, "splitv") == 0)
1539 else if (strcmp(layout_str, "splith") == 0)
1542 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1546 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1548 /* check if the match is empty, not if the result is empty */
1549 if (match_is_empty(current_match))
1550 con_set_layout(focused, layout);
1552 TAILQ_FOREACH(current, &owindows, owindows) {
1553 DLOG("matching: %p / %s\n", current->con, current->con->name);
1554 con_set_layout(current->con, layout);
1558 cmd_output->needs_tree_render = true;
1559 // XXX: default reply for now, make this a better reply
1564 * Implementation of 'layout toggle [all|split]'.
1567 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1570 if (toggle_mode == NULL)
1571 toggle_mode = "default";
1573 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1575 /* check if the match is empty, not if the result is empty */
1576 if (match_is_empty(current_match))
1577 con_toggle_layout(focused, toggle_mode);
1579 TAILQ_FOREACH(current, &owindows, owindows) {
1580 DLOG("matching: %p / %s\n", current->con, current->con->name);
1581 con_toggle_layout(current->con, toggle_mode);
1585 cmd_output->needs_tree_render = true;
1586 // XXX: default reply for now, make this a better reply
1591 * Implementation of 'exit'.
1594 void cmd_exit(I3_CMD) {
1595 LOG("Exiting due to user command.\n");
1596 xcb_disconnect(conn);
1603 * Implementation of 'reload'.
1606 void cmd_reload(I3_CMD) {
1608 kill_nagbar(&config_error_nagbar_pid, false);
1609 kill_nagbar(&command_error_nagbar_pid, false);
1610 load_configuration(conn, NULL, true);
1612 /* Send an IPC event just in case the ws names have changed */
1613 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1615 // XXX: default reply for now, make this a better reply
1620 * Implementation of 'restart'.
1623 void cmd_restart(I3_CMD) {
1624 LOG("restarting i3\n");
1627 // XXX: default reply for now, make this a better reply
1632 * Implementation of 'open'.
1635 void cmd_open(I3_CMD) {
1636 LOG("opening new container\n");
1637 Con *con = tree_open_con(NULL, NULL);
1638 con->layout = L_SPLITH;
1645 y(integer, (long int)con);
1648 cmd_output->needs_tree_render = true;
1652 * Implementation of 'focus output <output>'.
1655 void cmd_focus_output(I3_CMD, char *name) {
1658 DLOG("name = %s\n", name);
1662 /* get the output */
1663 Output *current_output = NULL;
1666 TAILQ_FOREACH(current, &owindows, owindows)
1667 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1668 assert(current_output != NULL);
1670 output = get_output_from_string(current_output, name);
1673 LOG("No such output found.\n");
1678 /* get visible workspace on output */
1680 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1688 cmd_output->needs_tree_render = true;
1689 // XXX: default reply for now, make this a better reply
1694 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1697 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1702 if (!con_is_floating(focused)) {
1703 ELOG("Cannot change position. The window/container is not floating\n");
1708 ystr("Cannot change position. The window/container is not floating.");
1713 if (strcmp(method, "absolute") == 0) {
1714 focused->parent->rect.x = x;
1715 focused->parent->rect.y = y;
1717 DLOG("moving to absolute position %d %d\n", x, y);
1718 floating_maybe_reassign_ws(focused->parent);
1719 cmd_output->needs_tree_render = true;
1722 if (strcmp(method, "position") == 0) {
1723 Rect newrect = focused->parent->rect;
1725 DLOG("moving to position %d %d\n", x, y);
1729 floating_reposition(focused->parent, newrect);
1732 // XXX: default reply for now, make this a better reply
1737 * Implementation of 'move [window|container] [to] [absolute] position center
1740 void cmd_move_window_to_center(I3_CMD, char *method) {
1742 if (!con_is_floating(focused)) {
1743 ELOG("Cannot change position. The window/container is not floating\n");
1748 ystr("Cannot change position. The window/container is not floating.");
1752 if (strcmp(method, "absolute") == 0) {
1753 Rect *rect = &focused->parent->rect;
1755 DLOG("moving to absolute center\n");
1756 rect->x = croot->rect.width/2 - rect->width/2;
1757 rect->y = croot->rect.height/2 - rect->height/2;
1759 floating_maybe_reassign_ws(focused->parent);
1760 cmd_output->needs_tree_render = true;
1763 if (strcmp(method, "position") == 0) {
1764 Rect *wsrect = &con_get_workspace(focused)->rect;
1765 Rect newrect = focused->parent->rect;
1767 DLOG("moving to center\n");
1768 newrect.x = wsrect->width/2 - newrect.width/2;
1769 newrect.y = wsrect->height/2 - newrect.height/2;
1771 floating_reposition(focused->parent, newrect);
1774 // XXX: default reply for now, make this a better reply
1779 * Implementation of 'move scratchpad'.
1782 void cmd_move_scratchpad(I3_CMD) {
1783 DLOG("should move window to scratchpad\n");
1788 TAILQ_FOREACH(current, &owindows, owindows) {
1789 DLOG("matching: %p / %s\n", current->con, current->con->name);
1790 scratchpad_move(current->con);
1793 cmd_output->needs_tree_render = true;
1794 // XXX: default reply for now, make this a better reply
1799 * Implementation of 'scratchpad show'.
1802 void cmd_scratchpad_show(I3_CMD) {
1803 DLOG("should show scratchpad window\n");
1806 if (match_is_empty(current_match)) {
1807 scratchpad_show(NULL);
1809 TAILQ_FOREACH(current, &owindows, owindows) {
1810 DLOG("matching: %p / %s\n", current->con, current->con->name);
1811 scratchpad_show(current->con);
1815 cmd_output->needs_tree_render = true;
1816 // XXX: default reply for now, make this a better reply
1821 * Implementation of 'rename workspace [<name>] to <name>'
1824 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1826 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1828 LOG("Renaming current workspace to \"%s\"\n", new_name);
1831 Con *output, *workspace = NULL;
1833 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1834 GREP_FIRST(workspace, output_get_content(output),
1835 !strcasecmp(child->name, old_name));
1837 workspace = con_get_workspace(focused);
1841 // TODO: we should include the old workspace name here and use yajl for
1842 // generating the reply.
1847 // TODO: better error message
1848 ystr("Old workspace not found");
1853 Con *check_dest = NULL;
1854 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1855 GREP_FIRST(check_dest, output_get_content(output),
1856 !strcasecmp(child->name, new_name));
1858 if (check_dest != NULL) {
1859 // TODO: we should include the new workspace name here and use yajl for
1860 // generating the reply.
1865 // TODO: better error message
1866 ystr("New workspace already exists");
1871 /* Change the name and try to parse it as a number. */
1872 FREE(workspace->name);
1873 workspace->name = sstrdup(new_name);
1874 char *endptr = NULL;
1875 long parsed_num = strtol(new_name, &endptr, 10);
1876 if (parsed_num == LONG_MIN ||
1877 parsed_num == LONG_MAX ||
1880 workspace->num = -1;
1881 else workspace->num = parsed_num;
1882 LOG("num = %d\n", workspace->num);
1884 /* By re-attaching, the sort order will be correct afterwards. */
1885 Con *previously_focused = focused;
1886 Con *parent = workspace->parent;
1887 con_detach(workspace);
1888 con_attach(workspace, parent, false);
1889 /* Restore the previous focus since con_attach messes with the focus. */
1890 con_focus(previously_focused);
1892 cmd_output->needs_tree_render = true;
1895 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");