2 #define I3__FILE__ "commands.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * commands.c: all command functions (see commands_parser.c)
17 // Macros to make the YAJL API a bit easier to use.
18 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
19 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
20 #define ysuccess(success) do { \
27 /** When the command did not include match criteria (!), we use the currently
28 * focused container. Do not confuse this case with a command which included
29 * criteria but which did not match any windows. This macro has to be called in
32 #define HANDLE_EMPTY_MATCH do { \
33 if (match_is_empty(current_match)) { \
34 owindow *ow = smalloc(sizeof(owindow)); \
36 TAILQ_INIT(&owindows); \
37 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
41 static owindows_head owindows;
44 * Returns true if a is definitely greater than b (using the given epsilon)
47 static bool definitelyGreaterThan(float a, float b, float epsilon) {
48 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
52 * Returns an 'output' corresponding to one of left/right/down/up or a specific
56 static Output *get_output_from_string(Output *current_output, const char *output_str) {
59 if (strcasecmp(output_str, "left") == 0) {
60 output = get_output_next(D_LEFT, current_output);
62 output = get_output_most(D_RIGHT, current_output);
63 } else if (strcasecmp(output_str, "right") == 0) {
64 output = get_output_next(D_RIGHT, current_output);
66 output = get_output_most(D_LEFT, current_output);
67 } else if (strcasecmp(output_str, "up") == 0) {
68 output = get_output_next(D_UP, current_output);
70 output = get_output_most(D_DOWN, current_output);
71 } else if (strcasecmp(output_str, "down") == 0) {
72 output = get_output_next(D_DOWN, current_output);
74 output = get_output_most(D_UP, current_output);
75 } else output = get_output_by_name(output_str);
81 * Checks whether we switched to a new workspace and returns false in that case,
82 * signaling that further workspace switching should be done by the calling function
83 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
84 * and return true, signaling that no further workspace switching should occur in the calling function.
87 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
88 Con *ws = con_get_workspace(focused);
90 /* If we switched to a different workspace, do nothing */
91 if (strcmp(ws->name, name) != 0)
94 DLOG("This workspace is already focused.\n");
95 if (config.workspace_auto_back_and_forth) {
96 workspace_back_and_forth();
97 cmd_output->needs_tree_render = true;
102 // This code is commented out because we might recycle it for popping up error
103 // messages on parser errors.
105 static pid_t migration_pid = -1;
108 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
109 * it exited (or could not be started, depending on the exit code).
112 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
113 ev_child_stop(EV_A_ watcher);
114 if (!WIFEXITED(watcher->rstatus)) {
115 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
119 int exitcode = WEXITSTATUS(watcher->rstatus);
120 printf("i3-nagbar process exited with status %d\n", exitcode);
122 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
128 /* We need ev >= 4 for the following code. Since it is not *that* important (it
129 * only makes sure that there are no i3-nagbar instances left behind) we still
130 * support old systems with libev 3. */
131 #if EV_VERSION_MAJOR >= 4
133 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
134 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
137 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
138 if (migration_pid != -1) {
139 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
140 kill(migration_pid, SIGKILL);
145 void cmd_MIGRATION_start_nagbar(void) {
146 if (migration_pid != -1) {
147 fprintf(stderr, "i3-nagbar already running.\n");
150 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
151 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
152 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
153 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
154 migration_pid = fork();
155 if (migration_pid == -1) {
156 warn("Could not fork()");
161 if (migration_pid == 0) {
163 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
165 NULL, /* will be replaced by the executable path */
169 "You found a parsing error. Please, please, please, report it!",
175 exec_i3_utility("i3-nagbar", argv);
179 /* install a child watcher */
180 ev_child *child = smalloc(sizeof(ev_child));
181 ev_child_init(child, &nagbar_exited, migration_pid, 0);
182 ev_child_start(main_loop, child);
184 /* We need ev >= 4 for the following code. Since it is not *that* important (it
185 * only makes sure that there are no i3-nagbar instances left behind) we still
186 * support old systems with libev 3. */
187 #if EV_VERSION_MAJOR >= 4
188 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
190 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
191 ev_cleanup_init(cleanup, nagbar_cleanup);
192 ev_cleanup_start(main_loop, cleanup);
198 /*******************************************************************************
199 * Criteria functions.
200 ******************************************************************************/
203 * Initializes the specified 'Match' data structure and the initial state of
204 * commands.c for matching target windows of a command.
207 void cmd_criteria_init(I3_CMD) {
211 DLOG("Initializing criteria, current_match = %p\n", current_match);
212 match_init(current_match);
213 while (!TAILQ_EMPTY(&owindows)) {
214 ow = TAILQ_FIRST(&owindows);
215 TAILQ_REMOVE(&owindows, ow, owindows);
218 TAILQ_INIT(&owindows);
220 TAILQ_FOREACH(con, &all_cons, all_cons) {
221 ow = smalloc(sizeof(owindow));
223 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
228 * A match specification just finished (the closing square bracket was found),
229 * so we filter the list of owindows.
232 void cmd_criteria_match_windows(I3_CMD) {
233 owindow *next, *current;
235 DLOG("match specification finished, matching...\n");
236 /* copy the old list head to iterate through it and start with a fresh
237 * list which will contain only matching windows */
238 struct owindows_head old = owindows;
239 TAILQ_INIT(&owindows);
240 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
241 /* make a copy of the next pointer and advance the pointer to the
242 * next element as we are going to invalidate the element’s
243 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
245 next = TAILQ_NEXT(next, owindows);
247 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
248 if (current_match->con_id != NULL) {
249 if (current_match->con_id == current->con) {
250 DLOG("matches container!\n");
251 TAILQ_INSERT_TAIL(&owindows, current, owindows);
253 } else if (current_match->mark != NULL && current->con->mark != NULL &&
254 regex_matches(current_match->mark, current->con->mark)) {
255 DLOG("match by mark\n");
256 TAILQ_INSERT_TAIL(&owindows, current, owindows);
258 if (current->con->window == NULL)
260 if (match_matches_window(current_match, current->con->window)) {
261 DLOG("matches window!\n");
262 TAILQ_INSERT_TAIL(&owindows, current, owindows);
264 DLOG("doesnt match\n");
270 TAILQ_FOREACH(current, &owindows, owindows) {
271 DLOG("matching: %p / %s\n", current->con, current->con->name);
276 * Interprets a ctype=cvalue pair and adds it to the current match
280 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
281 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
283 if (strcmp(ctype, "class") == 0) {
284 current_match->class = regex_new(cvalue);
288 if (strcmp(ctype, "instance") == 0) {
289 current_match->instance = regex_new(cvalue);
293 if (strcmp(ctype, "window_role") == 0) {
294 current_match->role = regex_new(cvalue);
298 if (strcmp(ctype, "con_id") == 0) {
300 long parsed = strtol(cvalue, &end, 10);
301 if (parsed == LONG_MIN ||
302 parsed == LONG_MAX ||
304 (end && *end != '\0')) {
305 ELOG("Could not parse con id \"%s\"\n", cvalue);
307 current_match->con_id = (Con*)parsed;
308 printf("id as int = %p\n", current_match->con_id);
313 if (strcmp(ctype, "id") == 0) {
315 long parsed = strtol(cvalue, &end, 10);
316 if (parsed == LONG_MIN ||
317 parsed == LONG_MAX ||
319 (end && *end != '\0')) {
320 ELOG("Could not parse window id \"%s\"\n", cvalue);
322 current_match->id = parsed;
323 printf("window id as int = %d\n", current_match->id);
328 if (strcmp(ctype, "con_mark") == 0) {
329 current_match->mark = regex_new(cvalue);
333 if (strcmp(ctype, "title") == 0) {
334 current_match->title = regex_new(cvalue);
338 if (strcmp(ctype, "urgent") == 0) {
339 if (strcasecmp(cvalue, "latest") == 0 ||
340 strcasecmp(cvalue, "newest") == 0 ||
341 strcasecmp(cvalue, "recent") == 0 ||
342 strcasecmp(cvalue, "last") == 0) {
343 current_match->urgent = U_LATEST;
344 } else if (strcasecmp(cvalue, "oldest") == 0 ||
345 strcasecmp(cvalue, "first") == 0) {
346 current_match->urgent = U_OLDEST;
351 ELOG("Unknown criterion: %s\n", ctype);
355 * Implementation of 'move [window|container] [to] workspace
356 * next|prev|next_on_output|prev_on_output|current'.
359 void cmd_move_con_to_workspace(I3_CMD, char *which) {
362 DLOG("which=%s\n", which);
364 /* We have nothing to move:
365 * when criteria was specified but didn't match any window or
366 * when criteria wasn't specified and we don't have any window focused. */
367 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
368 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
375 /* get the workspace */
377 if (strcmp(which, "next") == 0)
378 ws = workspace_next();
379 else if (strcmp(which, "prev") == 0)
380 ws = workspace_prev();
381 else if (strcmp(which, "next_on_output") == 0)
382 ws = workspace_next_on_output();
383 else if (strcmp(which, "prev_on_output") == 0)
384 ws = workspace_prev_on_output();
385 else if (strcmp(which, "current") == 0)
386 ws = con_get_workspace(focused);
388 ELOG("BUG: called with which=%s\n", which);
393 TAILQ_FOREACH(current, &owindows, owindows) {
394 DLOG("matching: %p / %s\n", current->con, current->con->name);
395 con_move_to_workspace(current->con, ws, true, false);
398 cmd_output->needs_tree_render = true;
399 // XXX: default reply for now, make this a better reply
404 * Implementation of 'move [window|container] [to] workspace <name>'.
407 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
408 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
409 LOG("You cannot switch to the i3 internal workspaces.\n");
416 /* We have nothing to move:
417 * when criteria was specified but didn't match any window or
418 * when criteria wasn't specified and we don't have any window focused. */
419 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
420 ELOG("No windows match your criteria, cannot move.\n");
425 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
426 ELOG("No window to move, you have focused a workspace.\n");
431 LOG("should move window to workspace %s\n", name);
432 /* get the workspace */
433 Con *ws = workspace_get(name, NULL);
437 TAILQ_FOREACH(current, &owindows, owindows) {
438 DLOG("matching: %p / %s\n", current->con, current->con->name);
439 con_move_to_workspace(current->con, ws, true, false);
442 cmd_output->needs_tree_render = true;
443 // XXX: default reply for now, make this a better reply
448 * Implementation of 'move [window|container] [to] workspace number <number>'.
451 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
454 /* We have nothing to move:
455 * when criteria was specified but didn't match any window or
456 * when criteria wasn't specified and we don't have any window focused. */
457 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
458 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
463 LOG("should move window to workspace %s\n", which);
464 /* get the workspace */
465 Con *output, *workspace = NULL;
468 long parsed_num = strtol(which, &endptr, 10);
469 if (parsed_num == LONG_MIN ||
470 parsed_num == LONG_MAX ||
473 LOG("Could not parse \"%s\" as a number.\n", which);
478 // TODO: better error message
479 ystr("Could not parse number");
484 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
485 GREP_FIRST(workspace, output_get_content(output),
486 child->num == parsed_num);
489 workspace = workspace_get(which, NULL);
494 TAILQ_FOREACH(current, &owindows, owindows) {
495 DLOG("matching: %p / %s\n", current->con, current->con->name);
496 con_move_to_workspace(current->con, workspace, true, false);
499 cmd_output->needs_tree_render = true;
500 // XXX: default reply for now, make this a better reply
504 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
505 LOG("floating resize\n");
506 if (strcmp(direction, "up") == 0) {
507 floating_con->rect.y -= px;
508 floating_con->rect.height += px;
509 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
510 floating_con->rect.height += px;
511 } else if (strcmp(direction, "left") == 0) {
512 floating_con->rect.x -= px;
513 floating_con->rect.width += px;
515 floating_con->rect.width += px;
519 static bool cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
520 LOG("tiling resize\n");
521 /* get the appropriate current container (skip stacked/tabbed cons) */
522 Con *current = focused;
524 double percentage = 0;
525 while (current->parent->layout == L_STACKED ||
526 current->parent->layout == L_TABBED)
527 current = current->parent;
529 /* Then further go up until we find one with the matching orientation. */
530 orientation_t search_orientation =
531 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
534 if (con_orientation(current->parent) != search_orientation) {
535 current = current->parent;
539 /* get the default percentage */
540 int children = con_num_children(current->parent);
541 LOG("ins. %d children\n", children);
542 percentage = 1.0 / children;
543 LOG("default percentage = %f\n", percentage);
545 orientation_t orientation = con_orientation(current->parent);
547 if ((orientation == HORIZ &&
548 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
549 (orientation == VERT &&
550 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
551 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
552 (orientation == HORIZ ? "horizontal" : "vertical"));
557 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
558 other = TAILQ_PREV(current, nodes_head, nodes);
560 other = TAILQ_NEXT(current, nodes);
562 if (other == TAILQ_END(workspaces)) {
563 LOG("No other container in this direction found, trying to look further up in the tree...\n");
564 current = current->parent;
568 } while (current->type != CT_WORKSPACE &&
569 current->type != CT_FLOATING_CON);
572 LOG("No other container in this direction found, trying to look further up in the tree...\n");
577 LOG("other->percent = %f\n", other->percent);
578 LOG("current->percent before = %f\n", current->percent);
579 if (current->percent == 0.0)
580 current->percent = percentage;
581 if (other->percent == 0.0)
582 other->percent = percentage;
583 double new_current_percent = current->percent + ((double)ppt / 100.0);
584 double new_other_percent = other->percent - ((double)ppt / 100.0);
585 LOG("new_current_percent = %f\n", new_current_percent);
586 LOG("new_other_percent = %f\n", new_other_percent);
587 /* Ensure that the new percentages are positive and greater than
588 * 0.05 to have a reasonable minimum size. */
589 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
590 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
591 current->percent += ((double)ppt / 100.0);
592 other->percent -= ((double)ppt / 100.0);
593 LOG("current->percent after = %f\n", current->percent);
594 LOG("other->percent after = %f\n", other->percent);
596 LOG("Not resizing, already at minimum size\n");
602 static bool cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
603 LOG("width/height resize\n");
604 /* get the appropriate current container (skip stacked/tabbed cons) */
605 Con *current = focused;
606 while (current->parent->layout == L_STACKED ||
607 current->parent->layout == L_TABBED)
608 current = current->parent;
610 /* Then further go up until we find one with the matching orientation. */
611 orientation_t search_orientation =
612 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
614 while (current->type != CT_WORKSPACE &&
615 current->type != CT_FLOATING_CON &&
616 con_orientation(current->parent) != search_orientation)
617 current = current->parent;
619 /* get the default percentage */
620 int children = con_num_children(current->parent);
621 LOG("ins. %d children\n", children);
622 double percentage = 1.0 / children;
623 LOG("default percentage = %f\n", percentage);
625 orientation_t orientation = con_orientation(current->parent);
627 if ((orientation == HORIZ &&
628 strcmp(direction, "height") == 0) ||
629 (orientation == VERT &&
630 strcmp(direction, "width") == 0)) {
631 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
632 (orientation == HORIZ ? "horizontal" : "vertical"));
638 LOG("This is the only container, cannot resize.\n");
643 /* Ensure all the other children have a percentage set. */
645 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
646 LOG("child->percent = %f (child %p)\n", child->percent, child);
647 if (child->percent == 0.0)
648 child->percent = percentage;
651 double new_current_percent = current->percent + ((double)ppt / 100.0);
652 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
653 LOG("new_current_percent = %f\n", new_current_percent);
654 LOG("subtract_percent = %f\n", subtract_percent);
655 /* Ensure that the new percentages are positive and greater than
656 * 0.05 to have a reasonable minimum size. */
657 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
658 if (child == current)
660 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
661 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
666 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
667 LOG("Not resizing, already at minimum size\n");
672 current->percent += ((double)ppt / 100.0);
673 LOG("current->percent after = %f\n", current->percent);
675 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
676 if (child == current)
678 child->percent -= subtract_percent;
679 LOG("child->percent after (%p) = %f\n", child, child->percent);
686 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
689 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
690 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
691 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
692 // 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
693 int px = atoi(resize_px);
694 int ppt = atoi(resize_ppt);
695 if (strcmp(way, "shrink") == 0) {
701 if ((floating_con = con_inside_floating(focused))) {
702 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
704 if (strcmp(direction, "width") == 0 ||
705 strcmp(direction, "height") == 0) {
706 if (!cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt))
709 if (!cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt))
714 cmd_output->needs_tree_render = true;
715 // XXX: default reply for now, make this a better reply
720 * Implementation of 'border normal|none|1pixel|toggle'.
723 void cmd_border(I3_CMD, char *border_style_str) {
724 DLOG("border style should be changed to %s\n", border_style_str);
729 TAILQ_FOREACH(current, &owindows, owindows) {
730 DLOG("matching: %p / %s\n", current->con, current->con->name);
731 int border_style = current->con->border_style;
732 if (strcmp(border_style_str, "toggle") == 0) {
736 if (strcmp(border_style_str, "normal") == 0)
737 border_style = BS_NORMAL;
738 else if (strcmp(border_style_str, "none") == 0)
739 border_style = BS_NONE;
740 else if (strcmp(border_style_str, "1pixel") == 0)
741 border_style = BS_1PIXEL;
743 ELOG("BUG: called with border_style=%s\n", border_style_str);
748 con_set_border_style(current->con, border_style);
751 cmd_output->needs_tree_render = true;
752 // XXX: default reply for now, make this a better reply
757 * Implementation of 'nop <comment>'.
760 void cmd_nop(I3_CMD, char *comment) {
761 LOG("-------------------------------------------------\n");
762 LOG(" NOP: %s\n", comment);
763 LOG("-------------------------------------------------\n");
767 * Implementation of 'append_layout <path>'.
770 void cmd_append_layout(I3_CMD, char *path) {
771 LOG("Appending layout \"%s\"\n", path);
772 tree_append_json(path);
774 cmd_output->needs_tree_render = true;
775 // XXX: default reply for now, make this a better reply
780 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
783 void cmd_workspace(I3_CMD, char *which) {
786 DLOG("which=%s\n", which);
788 if (strcmp(which, "next") == 0)
789 ws = workspace_next();
790 else if (strcmp(which, "prev") == 0)
791 ws = workspace_prev();
792 else if (strcmp(which, "next_on_output") == 0)
793 ws = workspace_next_on_output();
794 else if (strcmp(which, "prev_on_output") == 0)
795 ws = workspace_prev_on_output();
797 ELOG("BUG: called with which=%s\n", which);
804 cmd_output->needs_tree_render = true;
805 // XXX: default reply for now, make this a better reply
810 * Implementation of 'workspace number <number>'
813 void cmd_workspace_number(I3_CMD, char *which) {
814 Con *output, *workspace = NULL;
817 long parsed_num = strtol(which, &endptr, 10);
818 if (parsed_num == LONG_MIN ||
819 parsed_num == LONG_MAX ||
822 LOG("Could not parse \"%s\" as a number.\n", which);
827 // TODO: better error message
828 ystr("Could not parse number");
834 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
835 GREP_FIRST(workspace, output_get_content(output),
836 child->num == parsed_num);
839 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
841 /* terminate the which string after the endposition of the number */
843 workspace_show_by_name(which);
844 cmd_output->needs_tree_render = true;
847 if (maybe_back_and_forth(cmd_output, workspace->name))
849 workspace_show(workspace);
851 cmd_output->needs_tree_render = true;
852 // XXX: default reply for now, make this a better reply
857 * Implementation of 'workspace back_and_forth'.
860 void cmd_workspace_back_and_forth(I3_CMD) {
861 workspace_back_and_forth();
863 cmd_output->needs_tree_render = true;
864 // XXX: default reply for now, make this a better reply
869 * Implementation of 'workspace <name>'
872 void cmd_workspace_name(I3_CMD, char *name) {
873 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
874 LOG("You cannot switch to the i3 internal workspaces.\n");
879 DLOG("should switch to workspace %s\n", name);
880 if (maybe_back_and_forth(cmd_output, name))
882 workspace_show_by_name(name);
884 cmd_output->needs_tree_render = true;
885 // XXX: default reply for now, make this a better reply
890 * Implementation of 'mark <mark>'
893 void cmd_mark(I3_CMD, char *mark) {
894 DLOG("Clearing all windows which have that mark first\n");
897 TAILQ_FOREACH(con, &all_cons, all_cons) {
898 if (con->mark && strcmp(con->mark, mark) == 0)
902 DLOG("marking window with str %s\n", mark);
907 TAILQ_FOREACH(current, &owindows, owindows) {
908 DLOG("matching: %p / %s\n", current->con, current->con->name);
909 current->con->mark = sstrdup(mark);
912 cmd_output->needs_tree_render = true;
913 // XXX: default reply for now, make this a better reply
918 * Implementation of 'mode <string>'.
921 void cmd_mode(I3_CMD, char *mode) {
922 DLOG("mode=%s\n", mode);
925 // XXX: default reply for now, make this a better reply
930 * Implementation of 'move [window|container] [to] output <str>'.
933 void cmd_move_con_to_output(I3_CMD, char *name) {
936 DLOG("should move window to output %s\n", name);
941 Output *current_output = NULL;
944 // TODO: fix the handling of criteria
945 TAILQ_FOREACH(current, &owindows, owindows)
946 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
948 assert(current_output != NULL);
950 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
951 if (strcasecmp(name, "up") == 0)
952 output = get_output_next(D_UP, current_output);
953 else if (strcasecmp(name, "down") == 0)
954 output = get_output_next(D_DOWN, current_output);
955 else if (strcasecmp(name, "left") == 0)
956 output = get_output_next(D_LEFT, current_output);
957 else if (strcasecmp(name, "right") == 0)
958 output = get_output_next(D_RIGHT, current_output);
960 output = get_output_by_name(name);
963 LOG("No such output found.\n");
968 /* get visible workspace on output */
970 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
976 TAILQ_FOREACH(current, &owindows, owindows) {
977 DLOG("matching: %p / %s\n", current->con, current->con->name);
978 con_move_to_workspace(current->con, ws, true, false);
981 cmd_output->needs_tree_render = true;
982 // XXX: default reply for now, make this a better reply
987 * Implementation of 'floating enable|disable|toggle'
990 void cmd_floating(I3_CMD, char *floating_mode) {
993 DLOG("floating_mode=%s\n", floating_mode);
997 TAILQ_FOREACH(current, &owindows, owindows) {
998 DLOG("matching: %p / %s\n", current->con, current->con->name);
999 if (strcmp(floating_mode, "toggle") == 0) {
1000 DLOG("should toggle mode\n");
1001 toggle_floating_mode(current->con, false);
1003 DLOG("should switch mode to %s\n", floating_mode);
1004 if (strcmp(floating_mode, "enable") == 0) {
1005 floating_enable(current->con, false);
1007 floating_disable(current->con, false);
1012 cmd_output->needs_tree_render = true;
1013 // XXX: default reply for now, make this a better reply
1018 * Implementation of 'move workspace to [output] <str>'.
1021 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1022 DLOG("should move workspace to output %s\n", name);
1027 TAILQ_FOREACH(current, &owindows, owindows) {
1028 Output *current_output = get_output_containing(current->con->rect.x,
1029 current->con->rect.y);
1030 if (!current_output) {
1031 ELOG("Cannot get current output. This is a bug in i3.\n");
1035 Output *output = get_output_from_string(current_output, name);
1037 ELOG("Could not get output from string \"%s\"\n", name);
1042 Con *content = output_get_content(output->con);
1043 LOG("got output %p with content %p\n", output, content);
1045 Con *ws = con_get_workspace(current->con);
1046 LOG("should move workspace %p / %s\n", ws, ws->name);
1048 if (con_num_children(ws->parent) == 1) {
1049 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1051 /* check if we can find a workspace assigned to this output */
1052 bool used_assignment = false;
1053 struct Workspace_Assignment *assignment;
1054 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1055 if (strcmp(assignment->output, current_output->name) != 0)
1058 /* check if this workspace is already attached to the tree */
1059 Con *workspace = NULL, *out;
1060 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1061 GREP_FIRST(workspace, output_get_content(out),
1062 !strcasecmp(child->name, assignment->name));
1063 if (workspace != NULL)
1066 /* so create the workspace referenced to by this assignment */
1067 LOG("Creating workspace from assignment %s.\n", assignment->name);
1068 workspace_get(assignment->name, NULL);
1069 used_assignment = true;
1073 /* if we couldn't create the workspace using an assignment, create
1074 * it on the output */
1075 if (!used_assignment)
1076 create_workspace_on_output(current_output, ws->parent);
1078 /* notify the IPC listeners */
1079 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1082 /* detach from the old output and attach to the new output */
1083 bool workspace_was_visible = workspace_is_visible(ws);
1084 Con *old_content = ws->parent;
1086 if (workspace_was_visible) {
1087 /* The workspace which we just detached was visible, so focus
1088 * the next one in the focus-stack. */
1089 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1090 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1091 workspace_show(focus_ws);
1093 con_attach(ws, content, false);
1095 /* fix the coordinates of the floating containers */
1097 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1098 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1100 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1101 if (workspace_was_visible) {
1102 /* Focus the moved workspace on the destination output. */
1107 cmd_output->needs_tree_render = true;
1108 // XXX: default reply for now, make this a better reply
1113 * Implementation of 'split v|h|vertical|horizontal'.
1116 void cmd_split(I3_CMD, char *direction) {
1117 /* TODO: use matches */
1118 LOG("splitting in direction %c\n", direction[0]);
1119 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1121 cmd_output->needs_tree_render = true;
1122 // XXX: default reply for now, make this a better reply
1127 * Implementaiton of 'kill [window|client]'.
1130 void cmd_kill(I3_CMD, char *kill_mode_str) {
1131 if (kill_mode_str == NULL)
1132 kill_mode_str = "window";
1135 DLOG("kill_mode=%s\n", kill_mode_str);
1138 if (strcmp(kill_mode_str, "window") == 0)
1139 kill_mode = KILL_WINDOW;
1140 else if (strcmp(kill_mode_str, "client") == 0)
1141 kill_mode = KILL_CLIENT;
1143 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1148 /* check if the match is empty, not if the result is empty */
1149 if (match_is_empty(current_match))
1150 tree_close_con(kill_mode);
1152 TAILQ_FOREACH(current, &owindows, owindows) {
1153 DLOG("matching: %p / %s\n", current->con, current->con->name);
1154 tree_close(current->con, kill_mode, false, false);
1158 cmd_output->needs_tree_render = true;
1159 // XXX: default reply for now, make this a better reply
1164 * Implementation of 'exec [--no-startup-id] <command>'.
1167 void cmd_exec(I3_CMD, char *nosn, char *command) {
1168 bool no_startup_id = (nosn != NULL);
1170 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1171 start_application(command, no_startup_id);
1173 // XXX: default reply for now, make this a better reply
1178 * Implementation of 'focus left|right|up|down'.
1181 void cmd_focus_direction(I3_CMD, char *direction) {
1183 focused->type != CT_WORKSPACE &&
1184 focused->fullscreen_mode != CF_NONE) {
1185 LOG("Cannot change focus while in fullscreen mode.\n");
1190 DLOG("direction = *%s*\n", direction);
1192 if (strcmp(direction, "left") == 0)
1193 tree_next('p', HORIZ);
1194 else if (strcmp(direction, "right") == 0)
1195 tree_next('n', HORIZ);
1196 else if (strcmp(direction, "up") == 0)
1197 tree_next('p', VERT);
1198 else if (strcmp(direction, "down") == 0)
1199 tree_next('n', VERT);
1201 ELOG("Invalid focus direction (%s)\n", direction);
1206 cmd_output->needs_tree_render = true;
1207 // XXX: default reply for now, make this a better reply
1212 * Implementation of 'focus tiling|floating|mode_toggle'.
1215 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1217 focused->type != CT_WORKSPACE &&
1218 focused->fullscreen_mode != CF_NONE) {
1219 LOG("Cannot change focus while in fullscreen mode.\n");
1224 DLOG("window_mode = %s\n", window_mode);
1226 Con *ws = con_get_workspace(focused);
1229 if (strcmp(window_mode, "mode_toggle") == 0) {
1230 current = TAILQ_FIRST(&(ws->focus_head));
1231 if (current != NULL && current->type == CT_FLOATING_CON)
1232 window_mode = "tiling";
1233 else window_mode = "floating";
1235 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1236 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1237 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1240 con_focus(con_descend_focused(current));
1245 cmd_output->needs_tree_render = true;
1246 // XXX: default reply for now, make this a better reply
1251 * Implementation of 'focus parent|child'.
1254 void cmd_focus_level(I3_CMD, char *level) {
1255 DLOG("level = %s\n", level);
1256 bool success = false;
1258 /* Focusing the parent can only be allowed if the newly
1259 * focused container won't escape the fullscreen container. */
1260 if (strcmp(level, "parent") == 0) {
1261 if (focused && focused->parent) {
1262 if (con_fullscreen_permits_focusing(focused->parent))
1263 success = level_up();
1265 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1269 /* Focusing a child should always be allowed. */
1270 else success = level_down();
1272 cmd_output->needs_tree_render = success;
1273 // XXX: default reply for now, make this a better reply
1278 * Implementation of 'focus'.
1281 void cmd_focus(I3_CMD) {
1282 DLOG("current_match = %p\n", current_match);
1284 if (match_is_empty(current_match)) {
1285 ELOG("You have to specify which window/container should be focused.\n");
1286 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1292 ystr("You have to specify which window/container should be focused");
1300 TAILQ_FOREACH(current, &owindows, owindows) {
1301 Con *ws = con_get_workspace(current->con);
1302 /* If no workspace could be found, this was a dock window.
1303 * Just skip it, you cannot focus dock windows. */
1307 /* Check the fullscreen focus constraints. */
1308 if (!con_fullscreen_permits_focusing(current->con)) {
1309 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1314 /* If the container is not on the current workspace,
1315 * workspace_show() will switch to a different workspace and (if
1316 * enabled) trigger a mouse pointer warp to the currently focused
1317 * container (!) on the target workspace.
1319 * Therefore, before calling workspace_show(), we make sure that
1320 * 'current' will be focused on the workspace. However, we cannot
1321 * just con_focus(current) because then the pointer will not be
1322 * warped at all (the code thinks we are already there).
1324 * So we focus 'current' to make it the currently focused window of
1325 * the target workspace, then revert focus. */
1326 Con *currently_focused = focused;
1327 con_focus(current->con);
1328 con_focus(currently_focused);
1330 /* Now switch to the workspace, then focus */
1332 LOG("focusing %p / %s\n", current->con, current->con->name);
1333 con_focus(current->con);
1338 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1339 "while only exactly one container can be focused at a time.\n", count);
1341 cmd_output->needs_tree_render = true;
1342 // XXX: default reply for now, make this a better reply
1347 * Implementation of 'fullscreen [global]'.
1350 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1351 if (fullscreen_mode == NULL)
1352 fullscreen_mode = "output";
1353 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1358 TAILQ_FOREACH(current, &owindows, owindows) {
1359 printf("matching: %p / %s\n", current->con, current->con->name);
1360 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1363 cmd_output->needs_tree_render = true;
1364 // XXX: default reply for now, make this a better reply
1369 * Implementation of 'move <direction> [<pixels> [px]]'.
1372 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1373 // 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
1374 int px = atoi(move_px);
1376 /* TODO: make 'move' work with criteria. */
1377 DLOG("moving in direction %s, px %s\n", direction, move_px);
1378 if (con_is_floating(focused)) {
1379 DLOG("floating move with %d pixels\n", px);
1380 Rect newrect = focused->parent->rect;
1381 if (strcmp(direction, "left") == 0) {
1383 } else if (strcmp(direction, "right") == 0) {
1385 } else if (strcmp(direction, "up") == 0) {
1387 } else if (strcmp(direction, "down") == 0) {
1390 floating_reposition(focused->parent, newrect);
1392 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1393 (strcmp(direction, "left") == 0 ? D_LEFT :
1394 (strcmp(direction, "up") == 0 ? D_UP :
1396 cmd_output->needs_tree_render = true;
1399 // XXX: default reply for now, make this a better reply
1404 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1407 void cmd_layout(I3_CMD, char *layout_str) {
1408 if (strcmp(layout_str, "stacking") == 0)
1409 layout_str = "stacked";
1412 /* default is a special case which will be handled in con_set_layout(). */
1413 if (strcmp(layout_str, "default") == 0)
1415 else if (strcmp(layout_str, "stacked") == 0)
1417 else if (strcmp(layout_str, "tabbed") == 0)
1419 else if (strcmp(layout_str, "splitv") == 0)
1421 else if (strcmp(layout_str, "splith") == 0)
1424 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1428 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1430 /* check if the match is empty, not if the result is empty */
1431 if (match_is_empty(current_match))
1432 con_set_layout(focused->parent, layout);
1434 TAILQ_FOREACH(current, &owindows, owindows) {
1435 DLOG("matching: %p / %s\n", current->con, current->con->name);
1436 con_set_layout(current->con, layout);
1440 cmd_output->needs_tree_render = true;
1441 // XXX: default reply for now, make this a better reply
1446 * Implementation of 'layout toggle [all|split]'.
1449 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1452 if (toggle_mode == NULL)
1453 toggle_mode = "default";
1455 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1457 /* check if the match is empty, not if the result is empty */
1458 if (match_is_empty(current_match))
1459 con_toggle_layout(focused->parent, toggle_mode);
1461 TAILQ_FOREACH(current, &owindows, owindows) {
1462 DLOG("matching: %p / %s\n", current->con, current->con->name);
1463 con_toggle_layout(current->con, toggle_mode);
1467 cmd_output->needs_tree_render = true;
1468 // XXX: default reply for now, make this a better reply
1473 * Implementaiton of 'exit'.
1476 void cmd_exit(I3_CMD) {
1477 LOG("Exiting due to user command.\n");
1478 xcb_disconnect(conn);
1485 * Implementaiton of 'reload'.
1488 void cmd_reload(I3_CMD) {
1490 kill_configerror_nagbar(false);
1491 kill_commanderror_nagbar(false);
1492 load_configuration(conn, NULL, true);
1494 /* Send an IPC event just in case the ws names have changed */
1495 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1497 // XXX: default reply for now, make this a better reply
1502 * Implementaiton of 'restart'.
1505 void cmd_restart(I3_CMD) {
1506 LOG("restarting i3\n");
1509 // XXX: default reply for now, make this a better reply
1514 * Implementaiton of 'open'.
1517 void cmd_open(I3_CMD) {
1518 LOG("opening new container\n");
1519 Con *con = tree_open_con(NULL, NULL);
1520 con->layout = L_SPLITH;
1527 y(integer, (long int)con);
1530 cmd_output->needs_tree_render = true;
1534 * Implementation of 'focus output <output>'.
1537 void cmd_focus_output(I3_CMD, char *name) {
1540 DLOG("name = %s\n", name);
1544 /* get the output */
1545 Output *current_output = NULL;
1548 TAILQ_FOREACH(current, &owindows, owindows)
1549 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1550 assert(current_output != NULL);
1552 output = get_output_from_string(current_output, name);
1555 LOG("No such output found.\n");
1560 /* get visible workspace on output */
1562 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1570 cmd_output->needs_tree_render = true;
1571 // XXX: default reply for now, make this a better reply
1576 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1579 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1584 if (!con_is_floating(focused)) {
1585 ELOG("Cannot change position. The window/container is not floating\n");
1590 ystr("Cannot change position. The window/container is not floating.");
1595 if (strcmp(method, "absolute") == 0) {
1596 focused->parent->rect.x = x;
1597 focused->parent->rect.y = y;
1599 DLOG("moving to absolute position %d %d\n", x, y);
1600 floating_maybe_reassign_ws(focused->parent);
1601 cmd_output->needs_tree_render = true;
1604 if (strcmp(method, "position") == 0) {
1605 Rect newrect = focused->parent->rect;
1607 DLOG("moving to position %d %d\n", x, y);
1611 floating_reposition(focused->parent, newrect);
1614 // XXX: default reply for now, make this a better reply
1619 * Implementation of 'move [window|container] [to] [absolute] position center
1622 void cmd_move_window_to_center(I3_CMD, char *method) {
1624 if (!con_is_floating(focused)) {
1625 ELOG("Cannot change position. The window/container is not floating\n");
1630 ystr("Cannot change position. The window/container is not floating.");
1634 if (strcmp(method, "absolute") == 0) {
1635 Rect *rect = &focused->parent->rect;
1637 DLOG("moving to absolute center\n");
1638 rect->x = croot->rect.width/2 - rect->width/2;
1639 rect->y = croot->rect.height/2 - rect->height/2;
1641 floating_maybe_reassign_ws(focused->parent);
1642 cmd_output->needs_tree_render = true;
1645 if (strcmp(method, "position") == 0) {
1646 Rect *wsrect = &con_get_workspace(focused)->rect;
1647 Rect newrect = focused->parent->rect;
1649 DLOG("moving to center\n");
1650 newrect.x = wsrect->width/2 - newrect.width/2;
1651 newrect.y = wsrect->height/2 - newrect.height/2;
1653 floating_reposition(focused->parent, newrect);
1656 // XXX: default reply for now, make this a better reply
1661 * Implementation of 'move scratchpad'.
1664 void cmd_move_scratchpad(I3_CMD) {
1665 DLOG("should move window to scratchpad\n");
1670 TAILQ_FOREACH(current, &owindows, owindows) {
1671 DLOG("matching: %p / %s\n", current->con, current->con->name);
1672 scratchpad_move(current->con);
1675 cmd_output->needs_tree_render = true;
1676 // XXX: default reply for now, make this a better reply
1681 * Implementation of 'scratchpad show'.
1684 void cmd_scratchpad_show(I3_CMD) {
1685 DLOG("should show scratchpad window\n");
1688 if (match_is_empty(current_match)) {
1689 scratchpad_show(NULL);
1691 TAILQ_FOREACH(current, &owindows, owindows) {
1692 DLOG("matching: %p / %s\n", current->con, current->con->name);
1693 scratchpad_show(current->con);
1697 cmd_output->needs_tree_render = true;
1698 // XXX: default reply for now, make this a better reply
1703 * Implementation of 'rename workspace <name> to <name>'
1706 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1707 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1709 Con *output, *workspace = NULL;
1710 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1711 GREP_FIRST(workspace, output_get_content(output),
1712 !strcasecmp(child->name, old_name));
1715 // TODO: we should include the old workspace name here and use yajl for
1716 // generating the reply.
1721 // TODO: better error message
1722 ystr("Old workspace not found");
1727 Con *check_dest = NULL;
1728 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1729 GREP_FIRST(check_dest, output_get_content(output),
1730 !strcasecmp(child->name, new_name));
1732 if (check_dest != NULL) {
1733 // TODO: we should include the new workspace name here and use yajl for
1734 // generating the reply.
1739 // TODO: better error message
1740 ystr("New workspace already exists");
1745 /* Change the name and try to parse it as a number. */
1746 FREE(workspace->name);
1747 workspace->name = sstrdup(new_name);
1748 char *endptr = NULL;
1749 long parsed_num = strtol(new_name, &endptr, 10);
1750 if (parsed_num == LONG_MIN ||
1751 parsed_num == LONG_MAX ||
1754 workspace->num = -1;
1755 else workspace->num = parsed_num;
1756 LOG("num = %d\n", workspace->num);
1758 /* By re-attaching, the sort order will be correct afterwards. */
1759 Con *previously_focused = focused;
1760 Con *parent = workspace->parent;
1761 con_detach(workspace);
1762 con_attach(workspace, parent, false);
1763 /* Restore the previous focus since con_attach messes with the focus. */
1764 con_focus(previously_focused);
1766 cmd_output->needs_tree_render = true;
1769 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");