2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * commands.c: all command functions (see commands_parser.c)
15 // Macros to make the YAJL API a bit easier to use.
16 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
17 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
18 #define ysuccess(success) do { \
25 /** When the command did not include match criteria (!), we use the currently
26 * focused container. Do not confuse this case with a command which included
27 * criteria but which did not match any windows. This macro has to be called in
30 #define HANDLE_EMPTY_MATCH do { \
31 if (match_is_empty(current_match)) { \
32 owindow *ow = smalloc(sizeof(owindow)); \
34 TAILQ_INIT(&owindows); \
35 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
39 static owindows_head owindows;
42 * Returns true if a is definitely greater than b (using the given epsilon)
45 static bool definitelyGreaterThan(float a, float b, float epsilon) {
46 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
50 * Returns an 'output' corresponding to one of left/right/down/up or a specific
54 static Output *get_output_from_string(Output *current_output, const char *output_str) {
57 if (strcasecmp(output_str, "left") == 0) {
58 output = get_output_next(D_LEFT, current_output);
60 output = get_output_most(D_RIGHT, current_output);
61 } else if (strcasecmp(output_str, "right") == 0) {
62 output = get_output_next(D_RIGHT, current_output);
64 output = get_output_most(D_LEFT, current_output);
65 } else if (strcasecmp(output_str, "up") == 0) {
66 output = get_output_next(D_UP, current_output);
68 output = get_output_most(D_DOWN, current_output);
69 } else if (strcasecmp(output_str, "down") == 0) {
70 output = get_output_next(D_DOWN, current_output);
72 output = get_output_most(D_UP, current_output);
73 } else output = get_output_by_name(output_str);
79 * Checks whether we switched to a new workspace and returns false in that case,
80 * signaling that further workspace switching should be done by the calling function
81 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
82 * and return true, signaling that no further workspace switching should occur in the calling function.
85 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
86 Con *ws = con_get_workspace(focused);
88 /* If we switched to a different workspace, do nothing */
89 if (strcmp(ws->name, name) != 0)
92 DLOG("This workspace is already focused.\n");
93 if (config.workspace_auto_back_and_forth) {
94 workspace_back_and_forth();
95 cmd_output->needs_tree_render = true;
100 // This code is commented out because we might recycle it for popping up error
101 // messages on parser errors.
103 static pid_t migration_pid = -1;
106 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
107 * it exited (or could not be started, depending on the exit code).
110 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
111 ev_child_stop(EV_A_ watcher);
112 if (!WIFEXITED(watcher->rstatus)) {
113 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
117 int exitcode = WEXITSTATUS(watcher->rstatus);
118 printf("i3-nagbar process exited with status %d\n", exitcode);
120 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
126 /* We need ev >= 4 for the following code. Since it is not *that* important (it
127 * only makes sure that there are no i3-nagbar instances left behind) we still
128 * support old systems with libev 3. */
129 #if EV_VERSION_MAJOR >= 4
131 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
132 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
135 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
136 if (migration_pid != -1) {
137 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
138 kill(migration_pid, SIGKILL);
143 void cmd_MIGRATION_start_nagbar(void) {
144 if (migration_pid != -1) {
145 fprintf(stderr, "i3-nagbar already running.\n");
148 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
149 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
150 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
151 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
152 migration_pid = fork();
153 if (migration_pid == -1) {
154 warn("Could not fork()");
159 if (migration_pid == 0) {
161 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
163 NULL, /* will be replaced by the executable path */
167 "You found a parsing error. Please, please, please, report it!",
173 exec_i3_utility("i3-nagbar", argv);
177 /* install a child watcher */
178 ev_child *child = smalloc(sizeof(ev_child));
179 ev_child_init(child, &nagbar_exited, migration_pid, 0);
180 ev_child_start(main_loop, child);
182 /* We need ev >= 4 for the following code. Since it is not *that* important (it
183 * only makes sure that there are no i3-nagbar instances left behind) we still
184 * support old systems with libev 3. */
185 #if EV_VERSION_MAJOR >= 4
186 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
188 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
189 ev_cleanup_init(cleanup, nagbar_cleanup);
190 ev_cleanup_start(main_loop, cleanup);
196 /*******************************************************************************
197 * Criteria functions.
198 ******************************************************************************/
201 * Initializes the specified 'Match' data structure and the initial state of
202 * commands.c for matching target windows of a command.
205 void cmd_criteria_init(I3_CMD) {
209 DLOG("Initializing criteria, current_match = %p\n", current_match);
210 match_init(current_match);
211 while (!TAILQ_EMPTY(&owindows)) {
212 ow = TAILQ_FIRST(&owindows);
213 TAILQ_REMOVE(&owindows, ow, owindows);
216 TAILQ_INIT(&owindows);
218 TAILQ_FOREACH(con, &all_cons, all_cons) {
219 ow = smalloc(sizeof(owindow));
221 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
226 * A match specification just finished (the closing square bracket was found),
227 * so we filter the list of owindows.
230 void cmd_criteria_match_windows(I3_CMD) {
231 owindow *next, *current;
233 DLOG("match specification finished, matching...\n");
234 /* copy the old list head to iterate through it and start with a fresh
235 * list which will contain only matching windows */
236 struct owindows_head old = owindows;
237 TAILQ_INIT(&owindows);
238 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
239 /* make a copy of the next pointer and advance the pointer to the
240 * next element as we are going to invalidate the element’s
241 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
243 next = TAILQ_NEXT(next, owindows);
245 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
246 if (current_match->con_id != NULL) {
247 if (current_match->con_id == current->con) {
248 DLOG("matches container!\n");
249 TAILQ_INSERT_TAIL(&owindows, current, owindows);
251 } else if (current_match->mark != NULL && current->con->mark != NULL &&
252 regex_matches(current_match->mark, current->con->mark)) {
253 DLOG("match by mark\n");
254 TAILQ_INSERT_TAIL(&owindows, current, owindows);
256 if (current->con->window == NULL)
258 if (match_matches_window(current_match, current->con->window)) {
259 DLOG("matches window!\n");
260 TAILQ_INSERT_TAIL(&owindows, current, owindows);
262 DLOG("doesnt match\n");
268 TAILQ_FOREACH(current, &owindows, owindows) {
269 DLOG("matching: %p / %s\n", current->con, current->con->name);
274 * Interprets a ctype=cvalue pair and adds it to the current match
278 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
279 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
281 if (strcmp(ctype, "class") == 0) {
282 current_match->class = regex_new(cvalue);
286 if (strcmp(ctype, "instance") == 0) {
287 current_match->instance = regex_new(cvalue);
291 if (strcmp(ctype, "window_role") == 0) {
292 current_match->role = regex_new(cvalue);
296 if (strcmp(ctype, "con_id") == 0) {
298 long parsed = strtol(cvalue, &end, 10);
299 if (parsed == LONG_MIN ||
300 parsed == LONG_MAX ||
302 (end && *end != '\0')) {
303 ELOG("Could not parse con id \"%s\"\n", cvalue);
305 current_match->con_id = (Con*)parsed;
306 printf("id as int = %p\n", current_match->con_id);
311 if (strcmp(ctype, "id") == 0) {
313 long parsed = strtol(cvalue, &end, 10);
314 if (parsed == LONG_MIN ||
315 parsed == LONG_MAX ||
317 (end && *end != '\0')) {
318 ELOG("Could not parse window id \"%s\"\n", cvalue);
320 current_match->id = parsed;
321 printf("window id as int = %d\n", current_match->id);
326 if (strcmp(ctype, "con_mark") == 0) {
327 current_match->mark = regex_new(cvalue);
331 if (strcmp(ctype, "title") == 0) {
332 current_match->title = regex_new(cvalue);
336 if (strcmp(ctype, "urgent") == 0) {
337 if (strcasecmp(cvalue, "latest") == 0 ||
338 strcasecmp(cvalue, "newest") == 0 ||
339 strcasecmp(cvalue, "recent") == 0 ||
340 strcasecmp(cvalue, "last") == 0) {
341 current_match->urgent = U_LATEST;
342 } else if (strcasecmp(cvalue, "oldest") == 0 ||
343 strcasecmp(cvalue, "first") == 0) {
344 current_match->urgent = U_OLDEST;
349 ELOG("Unknown criterion: %s\n", ctype);
353 * Implementation of 'move [window|container] [to] workspace
354 * next|prev|next_on_output|prev_on_output'.
357 void cmd_move_con_to_workspace(I3_CMD, char *which) {
360 DLOG("which=%s\n", which);
362 /* We have nothing to move:
363 * when criteria was specified but didn't match any window or
364 * when criteria wasn't specified and we don't have any window focused. */
365 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
366 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
373 /* get the workspace */
375 if (strcmp(which, "next") == 0)
376 ws = workspace_next();
377 else if (strcmp(which, "prev") == 0)
378 ws = workspace_prev();
379 else if (strcmp(which, "next_on_output") == 0)
380 ws = workspace_next_on_output();
381 else if (strcmp(which, "prev_on_output") == 0)
382 ws = workspace_prev_on_output();
384 ELOG("BUG: called with which=%s\n", which);
389 TAILQ_FOREACH(current, &owindows, owindows) {
390 DLOG("matching: %p / %s\n", current->con, current->con->name);
391 con_move_to_workspace(current->con, ws, true, false);
394 cmd_output->needs_tree_render = true;
395 // XXX: default reply for now, make this a better reply
400 * Implementation of 'move [window|container] [to] workspace <name>'.
403 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
404 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
405 LOG("You cannot switch to the i3 internal workspaces.\n");
412 /* We have nothing to move:
413 * when criteria was specified but didn't match any window or
414 * when criteria wasn't specified and we don't have any window focused. */
415 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
416 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
421 LOG("should move window to workspace %s\n", name);
422 /* get the workspace */
423 Con *ws = workspace_get(name, NULL);
427 TAILQ_FOREACH(current, &owindows, owindows) {
428 DLOG("matching: %p / %s\n", current->con, current->con->name);
429 con_move_to_workspace(current->con, ws, true, false);
432 cmd_output->needs_tree_render = true;
433 // XXX: default reply for now, make this a better reply
438 * Implementation of 'move [window|container] [to] workspace number <number>'.
441 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
444 /* We have nothing to move:
445 * when criteria was specified but didn't match any window or
446 * when criteria wasn't specified and we don't have any window focused. */
447 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
448 (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
453 LOG("should move window to workspace with number %d\n", which);
454 /* get the workspace */
455 Con *output, *workspace = NULL;
458 long parsed_num = strtol(which, &endptr, 10);
459 if (parsed_num == LONG_MIN ||
460 parsed_num == LONG_MAX ||
463 LOG("Could not parse \"%s\" as a number.\n", which);
468 // TODO: better error message
469 ystr("Could not parse number");
474 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
475 GREP_FIRST(workspace, output_get_content(output),
476 child->num == parsed_num);
483 // TODO: better error message
484 ystr("No such workspace");
491 TAILQ_FOREACH(current, &owindows, owindows) {
492 DLOG("matching: %p / %s\n", current->con, current->con->name);
493 con_move_to_workspace(current->con, workspace, true, false);
496 cmd_output->needs_tree_render = true;
497 // XXX: default reply for now, make this a better reply
501 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
502 LOG("floating resize\n");
503 if (strcmp(direction, "up") == 0) {
504 floating_con->rect.y -= px;
505 floating_con->rect.height += px;
506 } else if (strcmp(direction, "down") == 0) {
507 floating_con->rect.height += px;
508 } else if (strcmp(direction, "left") == 0) {
509 floating_con->rect.x -= px;
510 floating_con->rect.width += px;
512 floating_con->rect.width += px;
516 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
517 LOG("tiling resize\n");
518 /* get the appropriate current container (skip stacked/tabbed cons) */
519 Con *current = focused;
520 while (current->parent->layout == L_STACKED ||
521 current->parent->layout == L_TABBED)
522 current = current->parent;
524 /* Then further go up until we find one with the matching orientation. */
525 orientation_t search_orientation =
526 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
528 while (current->type != CT_WORKSPACE &&
529 current->type != CT_FLOATING_CON &&
530 current->parent->orientation != search_orientation)
531 current = current->parent;
533 /* get the default percentage */
534 int children = con_num_children(current->parent);
536 LOG("ins. %d children\n", children);
537 double percentage = 1.0 / children;
538 LOG("default percentage = %f\n", percentage);
540 orientation_t orientation = current->parent->orientation;
542 if ((orientation == HORIZ &&
543 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
544 (orientation == VERT &&
545 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
546 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
547 (orientation == HORIZ ? "horizontal" : "vertical"));
552 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
553 other = TAILQ_PREV(current, nodes_head, nodes);
555 other = TAILQ_NEXT(current, nodes);
557 if (other == TAILQ_END(workspaces)) {
558 LOG("No other container in this direction found, cannot resize.\n");
562 LOG("other->percent = %f\n", other->percent);
563 LOG("current->percent before = %f\n", current->percent);
564 if (current->percent == 0.0)
565 current->percent = percentage;
566 if (other->percent == 0.0)
567 other->percent = percentage;
568 double new_current_percent = current->percent + ((double)ppt / 100.0);
569 double new_other_percent = other->percent - ((double)ppt / 100.0);
570 LOG("new_current_percent = %f\n", new_current_percent);
571 LOG("new_other_percent = %f\n", new_other_percent);
572 /* Ensure that the new percentages are positive and greater than
573 * 0.05 to have a reasonable minimum size. */
574 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
575 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
576 current->percent += ((double)ppt / 100.0);
577 other->percent -= ((double)ppt / 100.0);
578 LOG("current->percent after = %f\n", current->percent);
579 LOG("other->percent after = %f\n", other->percent);
581 LOG("Not resizing, already at minimum size\n");
585 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
586 LOG("width/height resize\n");
587 /* get the appropriate current container (skip stacked/tabbed cons) */
588 Con *current = focused;
589 while (current->parent->layout == L_STACKED ||
590 current->parent->layout == L_TABBED)
591 current = current->parent;
593 /* Then further go up until we find one with the matching orientation. */
594 orientation_t search_orientation =
595 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
597 while (current->type != CT_WORKSPACE &&
598 current->type != CT_FLOATING_CON &&
599 current->parent->orientation != search_orientation)
600 current = current->parent;
602 /* get the default percentage */
603 int children = con_num_children(current->parent);
604 LOG("ins. %d children\n", children);
605 double percentage = 1.0 / children;
606 LOG("default percentage = %f\n", percentage);
608 orientation_t orientation = current->parent->orientation;
610 if ((orientation == HORIZ &&
611 strcmp(direction, "height") == 0) ||
612 (orientation == VERT &&
613 strcmp(direction, "width") == 0)) {
614 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
615 (orientation == HORIZ ? "horizontal" : "vertical"));
621 LOG("This is the only container, cannot resize.\n");
626 /* Ensure all the other children have a percentage set. */
628 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
629 LOG("child->percent = %f (child %p)\n", child->percent, child);
630 if (child->percent == 0.0)
631 child->percent = percentage;
634 double new_current_percent = current->percent + ((double)ppt / 100.0);
635 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
636 LOG("new_current_percent = %f\n", new_current_percent);
637 LOG("subtract_percent = %f\n", subtract_percent);
638 /* Ensure that the new percentages are positive and greater than
639 * 0.05 to have a reasonable minimum size. */
640 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
641 if (child == current)
643 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
644 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
649 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
650 LOG("Not resizing, already at minimum size\n");
655 current->percent += ((double)ppt / 100.0);
656 LOG("current->percent after = %f\n", current->percent);
658 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
659 if (child == current)
661 child->percent -= subtract_percent;
662 LOG("child->percent after (%p) = %f\n", child, child->percent);
667 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
670 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
671 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
672 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
673 // 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
674 int px = atoi(resize_px);
675 int ppt = atoi(resize_ppt);
676 if (strcmp(way, "shrink") == 0) {
682 if ((floating_con = con_inside_floating(focused))) {
683 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
685 if (strcmp(direction, "width") == 0 ||
686 strcmp(direction, "height") == 0)
687 cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
688 else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
691 cmd_output->needs_tree_render = true;
692 // XXX: default reply for now, make this a better reply
697 * Implementation of 'border normal|none|1pixel|toggle'.
700 void cmd_border(I3_CMD, char *border_style_str) {
701 DLOG("border style should be changed to %s\n", border_style_str);
706 TAILQ_FOREACH(current, &owindows, owindows) {
707 DLOG("matching: %p / %s\n", current->con, current->con->name);
708 int border_style = current->con->border_style;
709 if (strcmp(border_style_str, "toggle") == 0) {
713 if (strcmp(border_style_str, "normal") == 0)
714 border_style = BS_NORMAL;
715 else if (strcmp(border_style_str, "none") == 0)
716 border_style = BS_NONE;
717 else if (strcmp(border_style_str, "1pixel") == 0)
718 border_style = BS_1PIXEL;
720 ELOG("BUG: called with border_style=%s\n", border_style_str);
725 con_set_border_style(current->con, border_style);
728 cmd_output->needs_tree_render = true;
729 // XXX: default reply for now, make this a better reply
734 * Implementation of 'nop <comment>'.
737 void cmd_nop(I3_CMD, char *comment) {
738 LOG("-------------------------------------------------\n");
739 LOG(" NOP: %s\n", comment);
740 LOG("-------------------------------------------------\n");
744 * Implementation of 'append_layout <path>'.
747 void cmd_append_layout(I3_CMD, char *path) {
748 LOG("Appending layout \"%s\"\n", path);
749 tree_append_json(path);
751 cmd_output->needs_tree_render = true;
752 // XXX: default reply for now, make this a better reply
757 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
760 void cmd_workspace(I3_CMD, char *which) {
763 DLOG("which=%s\n", which);
765 if (strcmp(which, "next") == 0)
766 ws = workspace_next();
767 else if (strcmp(which, "prev") == 0)
768 ws = workspace_prev();
769 else if (strcmp(which, "next_on_output") == 0)
770 ws = workspace_next_on_output();
771 else if (strcmp(which, "prev_on_output") == 0)
772 ws = workspace_prev_on_output();
774 ELOG("BUG: called with which=%s\n", which);
781 cmd_output->needs_tree_render = true;
782 // XXX: default reply for now, make this a better reply
787 * Implementation of 'workspace number <number>'
790 void cmd_workspace_number(I3_CMD, char *which) {
791 Con *output, *workspace = NULL;
794 long parsed_num = strtol(which, &endptr, 10);
795 if (parsed_num == LONG_MIN ||
796 parsed_num == LONG_MAX ||
799 LOG("Could not parse \"%s\" as a number.\n", which);
804 // TODO: better error message
805 ystr("Could not parse number");
811 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
812 GREP_FIRST(workspace, output_get_content(output),
813 child->num == parsed_num);
816 LOG("There is no workspace with number %d, creating a new one.\n", parsed_num);
818 /* terminate the which string after the endposition of the number */
820 if (maybe_back_and_forth(cmd_output, which))
822 workspace_show_by_name(which);
823 cmd_output->needs_tree_render = true;
826 if (maybe_back_and_forth(cmd_output, which))
828 workspace_show(workspace);
830 cmd_output->needs_tree_render = true;
831 // XXX: default reply for now, make this a better reply
836 * Implementation of 'workspace back_and_forth'.
839 void cmd_workspace_back_and_forth(I3_CMD) {
840 workspace_back_and_forth();
842 cmd_output->needs_tree_render = true;
843 // XXX: default reply for now, make this a better reply
848 * Implementation of 'workspace <name>'
851 void cmd_workspace_name(I3_CMD, char *name) {
852 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
853 LOG("You cannot switch to the i3 internal workspaces.\n");
858 DLOG("should switch to workspace %s\n", name);
859 if (maybe_back_and_forth(cmd_output, name))
861 workspace_show_by_name(name);
863 cmd_output->needs_tree_render = true;
864 // XXX: default reply for now, make this a better reply
869 * Implementation of 'mark <mark>'
872 void cmd_mark(I3_CMD, char *mark) {
873 DLOG("Clearing all windows which have that mark first\n");
876 TAILQ_FOREACH(con, &all_cons, all_cons) {
877 if (con->mark && strcmp(con->mark, mark) == 0)
881 DLOG("marking window with str %s\n", mark);
886 TAILQ_FOREACH(current, &owindows, owindows) {
887 DLOG("matching: %p / %s\n", current->con, current->con->name);
888 current->con->mark = sstrdup(mark);
891 cmd_output->needs_tree_render = true;
892 // XXX: default reply for now, make this a better reply
897 * Implementation of 'mode <string>'.
900 void cmd_mode(I3_CMD, char *mode) {
901 DLOG("mode=%s\n", mode);
904 // XXX: default reply for now, make this a better reply
909 * Implementation of 'move [window|container] [to] output <str>'.
912 void cmd_move_con_to_output(I3_CMD, char *name) {
915 DLOG("should move window to output %s\n", name);
920 Output *current_output = NULL;
923 // TODO: fix the handling of criteria
924 TAILQ_FOREACH(current, &owindows, owindows)
925 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
927 assert(current_output != NULL);
929 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
930 if (strcasecmp(name, "up") == 0)
931 output = get_output_next(D_UP, current_output);
932 else if (strcasecmp(name, "down") == 0)
933 output = get_output_next(D_DOWN, current_output);
934 else if (strcasecmp(name, "left") == 0)
935 output = get_output_next(D_LEFT, current_output);
936 else if (strcasecmp(name, "right") == 0)
937 output = get_output_next(D_RIGHT, current_output);
939 output = get_output_by_name(name);
942 LOG("No such output found.\n");
947 /* get visible workspace on output */
949 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
955 TAILQ_FOREACH(current, &owindows, owindows) {
956 DLOG("matching: %p / %s\n", current->con, current->con->name);
957 con_move_to_workspace(current->con, ws, true, false);
960 cmd_output->needs_tree_render = true;
961 // XXX: default reply for now, make this a better reply
966 * Implementation of 'floating enable|disable|toggle'
969 void cmd_floating(I3_CMD, char *floating_mode) {
972 DLOG("floating_mode=%s\n", floating_mode);
976 TAILQ_FOREACH(current, &owindows, owindows) {
977 DLOG("matching: %p / %s\n", current->con, current->con->name);
978 if (strcmp(floating_mode, "toggle") == 0) {
979 DLOG("should toggle mode\n");
980 toggle_floating_mode(current->con, false);
982 DLOG("should switch mode to %s\n", floating_mode);
983 if (strcmp(floating_mode, "enable") == 0) {
984 floating_enable(current->con, false);
986 floating_disable(current->con, false);
991 cmd_output->needs_tree_render = true;
992 // XXX: default reply for now, make this a better reply
997 * Implementation of 'move workspace to [output] <str>'.
1000 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1001 DLOG("should move workspace to output %s\n", name);
1006 TAILQ_FOREACH(current, &owindows, owindows) {
1007 Output *current_output = get_output_containing(current->con->rect.x,
1008 current->con->rect.y);
1009 Output *output = get_output_from_string(current_output, name);
1011 LOG("No such output\n");
1016 Con *content = output_get_content(output->con);
1017 LOG("got output %p with content %p\n", output, content);
1019 Con *ws = con_get_workspace(current->con);
1020 LOG("should move workspace %p / %s\n", ws, ws->name);
1022 if (con_num_children(ws->parent) == 1) {
1023 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1025 /* check if we can find a workspace assigned to this output */
1026 bool used_assignment = false;
1027 struct Workspace_Assignment *assignment;
1028 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1029 if (strcmp(assignment->output, current_output->name) != 0)
1032 /* check if this workspace is already attached to the tree */
1033 Con *workspace = NULL, *out;
1034 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1035 GREP_FIRST(workspace, output_get_content(out),
1036 !strcasecmp(child->name, assignment->name));
1037 if (workspace != NULL)
1040 /* so create the workspace referenced to by this assignment */
1041 LOG("Creating workspace from assignment %s.\n", assignment->name);
1042 workspace_get(assignment->name, NULL);
1043 used_assignment = true;
1047 /* if we couldn't create the workspace using an assignment, create
1048 * it on the output */
1049 if (!used_assignment)
1050 create_workspace_on_output(current_output, ws->parent);
1052 /* notify the IPC listeners */
1053 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1056 /* detach from the old output and attach to the new output */
1057 bool workspace_was_visible = workspace_is_visible(ws);
1058 Con *old_content = ws->parent;
1060 if (workspace_was_visible) {
1061 /* The workspace which we just detached was visible, so focus
1062 * the next one in the focus-stack. */
1063 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1064 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1065 workspace_show(focus_ws);
1067 con_attach(ws, content, false);
1069 /* fix the coordinates of the floating containers */
1071 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1072 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1074 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1075 if (workspace_was_visible) {
1076 /* Focus the moved workspace on the destination output. */
1081 cmd_output->needs_tree_render = true;
1082 // XXX: default reply for now, make this a better reply
1087 * Implementation of 'split v|h|vertical|horizontal'.
1090 void cmd_split(I3_CMD, char *direction) {
1091 /* TODO: use matches */
1092 LOG("splitting in direction %c\n", direction[0]);
1093 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1095 cmd_output->needs_tree_render = true;
1096 // XXX: default reply for now, make this a better reply
1101 * Implementaiton of 'kill [window|client]'.
1104 void cmd_kill(I3_CMD, char *kill_mode_str) {
1105 if (kill_mode_str == NULL)
1106 kill_mode_str = "window";
1109 DLOG("kill_mode=%s\n", kill_mode_str);
1112 if (strcmp(kill_mode_str, "window") == 0)
1113 kill_mode = KILL_WINDOW;
1114 else if (strcmp(kill_mode_str, "client") == 0)
1115 kill_mode = KILL_CLIENT;
1117 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1122 /* check if the match is empty, not if the result is empty */
1123 if (match_is_empty(current_match))
1124 tree_close_con(kill_mode);
1126 TAILQ_FOREACH(current, &owindows, owindows) {
1127 DLOG("matching: %p / %s\n", current->con, current->con->name);
1128 tree_close(current->con, kill_mode, false, false);
1132 cmd_output->needs_tree_render = true;
1133 // XXX: default reply for now, make this a better reply
1138 * Implementation of 'exec [--no-startup-id] <command>'.
1141 void cmd_exec(I3_CMD, char *nosn, char *command) {
1142 bool no_startup_id = (nosn != NULL);
1144 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1145 start_application(command, no_startup_id);
1147 // XXX: default reply for now, make this a better reply
1152 * Implementation of 'focus left|right|up|down'.
1155 void cmd_focus_direction(I3_CMD, char *direction) {
1157 focused->type != CT_WORKSPACE &&
1158 focused->fullscreen_mode != CF_NONE) {
1159 LOG("Cannot change focus while in fullscreen mode.\n");
1164 DLOG("direction = *%s*\n", direction);
1166 if (strcmp(direction, "left") == 0)
1167 tree_next('p', HORIZ);
1168 else if (strcmp(direction, "right") == 0)
1169 tree_next('n', HORIZ);
1170 else if (strcmp(direction, "up") == 0)
1171 tree_next('p', VERT);
1172 else if (strcmp(direction, "down") == 0)
1173 tree_next('n', VERT);
1175 ELOG("Invalid focus direction (%s)\n", direction);
1180 cmd_output->needs_tree_render = true;
1181 // XXX: default reply for now, make this a better reply
1186 * Implementation of 'focus tiling|floating|mode_toggle'.
1189 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1191 focused->type != CT_WORKSPACE &&
1192 focused->fullscreen_mode != CF_NONE) {
1193 LOG("Cannot change focus while in fullscreen mode.\n");
1198 DLOG("window_mode = %s\n", window_mode);
1200 Con *ws = con_get_workspace(focused);
1203 if (strcmp(window_mode, "mode_toggle") == 0) {
1204 current = TAILQ_FIRST(&(ws->focus_head));
1205 if (current != NULL && current->type == CT_FLOATING_CON)
1206 window_mode = "tiling";
1207 else window_mode = "floating";
1209 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1210 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1211 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1214 con_focus(con_descend_focused(current));
1219 cmd_output->needs_tree_render = true;
1220 // XXX: default reply for now, make this a better reply
1225 * Implementation of 'focus parent|child'.
1228 void cmd_focus_level(I3_CMD, char *level) {
1229 DLOG("level = %s\n", level);
1230 bool success = false;
1232 /* Focusing the parent can only be allowed if the newly
1233 * focused container won't escape the fullscreen container. */
1234 if (strcmp(level, "parent") == 0) {
1235 if (focused && focused->parent) {
1236 if (con_fullscreen_permits_focusing(focused->parent))
1237 success = level_up();
1239 LOG("Currently in fullscreen, not going up\n");
1243 /* Focusing a child should always be allowed. */
1244 else success = level_down();
1246 cmd_output->needs_tree_render = success;
1247 // XXX: default reply for now, make this a better reply
1252 * Implementation of 'focus'.
1255 void cmd_focus(I3_CMD) {
1256 DLOG("current_match = %p\n", current_match);
1258 if (match_is_empty(current_match)) {
1259 ELOG("You have to specify which window/container should be focused.\n");
1260 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1266 ystr("You have to specify which window/container should be focused");
1274 TAILQ_FOREACH(current, &owindows, owindows) {
1275 Con *ws = con_get_workspace(current->con);
1276 /* If no workspace could be found, this was a dock window.
1277 * Just skip it, you cannot focus dock windows. */
1281 /* Check the fullscreen focus constraints. */
1282 if (!con_fullscreen_permits_focusing(current->con)) {
1283 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1288 /* If the container is not on the current workspace,
1289 * workspace_show() will switch to a different workspace and (if
1290 * enabled) trigger a mouse pointer warp to the currently focused
1291 * container (!) on the target workspace.
1293 * Therefore, before calling workspace_show(), we make sure that
1294 * 'current' will be focused on the workspace. However, we cannot
1295 * just con_focus(current) because then the pointer will not be
1296 * warped at all (the code thinks we are already there).
1298 * So we focus 'current' to make it the currently focused window of
1299 * the target workspace, then revert focus. */
1300 Con *currently_focused = focused;
1301 con_focus(current->con);
1302 con_focus(currently_focused);
1304 /* Now switch to the workspace, then focus */
1306 LOG("focusing %p / %s\n", current->con, current->con->name);
1307 con_focus(current->con);
1312 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1313 "while only exactly one container can be focused at a time.\n", count);
1315 cmd_output->needs_tree_render = true;
1316 // XXX: default reply for now, make this a better reply
1321 * Implementation of 'fullscreen [global]'.
1324 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1325 if (fullscreen_mode == NULL)
1326 fullscreen_mode = "output";
1327 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1332 TAILQ_FOREACH(current, &owindows, owindows) {
1333 printf("matching: %p / %s\n", current->con, current->con->name);
1334 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1337 cmd_output->needs_tree_render = true;
1338 // XXX: default reply for now, make this a better reply
1343 * Implementation of 'move <direction> [<pixels> [px]]'.
1346 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1347 // 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
1348 int px = atoi(move_px);
1350 /* TODO: make 'move' work with criteria. */
1351 DLOG("moving in direction %s, px %s\n", direction, move_px);
1352 if (con_is_floating(focused)) {
1353 DLOG("floating move with %d pixels\n", px);
1354 Rect newrect = focused->parent->rect;
1355 if (strcmp(direction, "left") == 0) {
1357 } else if (strcmp(direction, "right") == 0) {
1359 } else if (strcmp(direction, "up") == 0) {
1361 } else if (strcmp(direction, "down") == 0) {
1364 floating_reposition(focused->parent, newrect);
1366 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1367 (strcmp(direction, "left") == 0 ? D_LEFT :
1368 (strcmp(direction, "up") == 0 ? D_UP :
1370 cmd_output->needs_tree_render = true;
1373 // XXX: default reply for now, make this a better reply
1378 * Implementation of 'layout default|stacked|stacking|tabbed'.
1381 void cmd_layout(I3_CMD, char *layout_str) {
1382 if (strcmp(layout_str, "stacking") == 0)
1383 layout_str = "stacked";
1384 DLOG("changing layout to %s\n", layout_str);
1386 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1387 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1390 /* check if the match is empty, not if the result is empty */
1391 if (match_is_empty(current_match))
1392 con_set_layout(focused->parent, layout);
1394 TAILQ_FOREACH(current, &owindows, owindows) {
1395 DLOG("matching: %p / %s\n", current->con, current->con->name);
1396 con_set_layout(current->con, layout);
1400 cmd_output->needs_tree_render = true;
1401 // XXX: default reply for now, make this a better reply
1406 * Implementaiton of 'exit'.
1409 void cmd_exit(I3_CMD) {
1410 LOG("Exiting due to user command.\n");
1417 * Implementaiton of 'reload'.
1420 void cmd_reload(I3_CMD) {
1422 kill_configerror_nagbar(false);
1423 load_configuration(conn, NULL, true);
1425 /* Send an IPC event just in case the ws names have changed */
1426 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1428 // XXX: default reply for now, make this a better reply
1433 * Implementaiton of 'restart'.
1436 void cmd_restart(I3_CMD) {
1437 LOG("restarting i3\n");
1440 // XXX: default reply for now, make this a better reply
1445 * Implementaiton of 'open'.
1448 void cmd_open(I3_CMD) {
1449 LOG("opening new container\n");
1450 Con *con = tree_open_con(NULL, NULL);
1457 y(integer, (long int)con);
1460 cmd_output->needs_tree_render = true;
1464 * Implementation of 'focus output <output>'.
1467 void cmd_focus_output(I3_CMD, char *name) {
1470 DLOG("name = %s\n", name);
1474 /* get the output */
1475 Output *current_output = NULL;
1478 TAILQ_FOREACH(current, &owindows, owindows)
1479 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1480 assert(current_output != NULL);
1482 output = get_output_from_string(current_output, name);
1485 LOG("No such output found.\n");
1490 /* get visible workspace on output */
1492 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1500 cmd_output->needs_tree_render = true;
1501 // XXX: default reply for now, make this a better reply
1506 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1509 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1514 if (!con_is_floating(focused)) {
1515 ELOG("Cannot change position. The window/container is not floating\n");
1520 ystr("Cannot change position. The window/container is not floating.");
1525 if (strcmp(method, "absolute") == 0) {
1526 focused->parent->rect.x = x;
1527 focused->parent->rect.y = y;
1529 DLOG("moving to absolute position %d %d\n", x, y);
1530 floating_maybe_reassign_ws(focused->parent);
1531 cmd_output->needs_tree_render = true;
1534 if (strcmp(method, "position") == 0) {
1535 Rect newrect = focused->parent->rect;
1537 DLOG("moving to position %d %d\n", x, y);
1541 floating_reposition(focused->parent, newrect);
1544 // XXX: default reply for now, make this a better reply
1549 * Implementation of 'move [window|container] [to] [absolute] position center
1552 void cmd_move_window_to_center(I3_CMD, char *method) {
1554 if (!con_is_floating(focused)) {
1555 ELOG("Cannot change position. The window/container is not floating\n");
1560 ystr("Cannot change position. The window/container is not floating.");
1564 if (strcmp(method, "absolute") == 0) {
1565 Rect *rect = &focused->parent->rect;
1567 DLOG("moving to absolute center\n");
1568 rect->x = croot->rect.width/2 - rect->width/2;
1569 rect->y = croot->rect.height/2 - rect->height/2;
1571 floating_maybe_reassign_ws(focused->parent);
1572 cmd_output->needs_tree_render = true;
1575 if (strcmp(method, "position") == 0) {
1576 Rect *wsrect = &con_get_workspace(focused)->rect;
1577 Rect newrect = focused->parent->rect;
1579 DLOG("moving to center\n");
1580 newrect.x = wsrect->width/2 - newrect.width/2;
1581 newrect.y = wsrect->height/2 - newrect.height/2;
1583 floating_reposition(focused->parent, newrect);
1586 // XXX: default reply for now, make this a better reply
1591 * Implementation of 'move scratchpad'.
1594 void cmd_move_scratchpad(I3_CMD) {
1595 DLOG("should move window to scratchpad\n");
1600 TAILQ_FOREACH(current, &owindows, owindows) {
1601 DLOG("matching: %p / %s\n", current->con, current->con->name);
1602 scratchpad_move(current->con);
1605 cmd_output->needs_tree_render = true;
1606 // XXX: default reply for now, make this a better reply
1611 * Implementation of 'scratchpad show'.
1614 void cmd_scratchpad_show(I3_CMD) {
1615 DLOG("should show scratchpad window\n");
1618 if (match_is_empty(current_match)) {
1619 scratchpad_show(NULL);
1621 TAILQ_FOREACH(current, &owindows, owindows) {
1622 DLOG("matching: %p / %s\n", current->con, current->con->name);
1623 scratchpad_show(current->con);
1627 cmd_output->needs_tree_render = true;
1628 // XXX: default reply for now, make this a better reply
1633 * Implementation of 'rename workspace <name> to <name>'
1636 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1637 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1639 Con *output, *workspace = NULL;
1640 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1641 GREP_FIRST(workspace, output_get_content(output),
1642 !strcasecmp(child->name, old_name));
1645 // TODO: we should include the old workspace name here and use yajl for
1646 // generating the reply.
1651 // TODO: better error message
1652 ystr("Old workspace not found");
1657 Con *check_dest = NULL;
1658 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1659 GREP_FIRST(check_dest, output_get_content(output),
1660 !strcasecmp(child->name, new_name));
1662 if (check_dest != NULL) {
1663 // TODO: we should include the new workspace name here and use yajl for
1664 // generating the reply.
1669 // TODO: better error message
1670 ystr("New workspace already exists");
1675 /* Change the name and try to parse it as a number. */
1676 FREE(workspace->name);
1677 workspace->name = sstrdup(new_name);
1678 char *endptr = NULL;
1679 long parsed_num = strtol(new_name, &endptr, 10);
1680 if (parsed_num == LONG_MIN ||
1681 parsed_num == LONG_MAX ||
1684 workspace->num = -1;
1685 else workspace->num = parsed_num;
1686 LOG("num = %d\n", workspace->num);
1688 /* By re-attaching, the sort order will be correct afterwards. */
1689 Con *previously_focused = focused;
1690 Con *parent = workspace->parent;
1691 con_detach(workspace);
1692 con_attach(workspace, parent, false);
1693 /* Restore the previous focus since con_attach messes with the focus. */
1694 con_focus(previously_focused);
1696 cmd_output->needs_tree_render = true;
1699 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");