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 command. 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);
364 /* get the workspace */
366 if (strcmp(which, "next") == 0)
367 ws = workspace_next();
368 else if (strcmp(which, "prev") == 0)
369 ws = workspace_prev();
370 else if (strcmp(which, "next_on_output") == 0)
371 ws = workspace_next_on_output();
372 else if (strcmp(which, "prev_on_output") == 0)
373 ws = workspace_prev_on_output();
375 ELOG("BUG: called with which=%s\n", which);
380 TAILQ_FOREACH(current, &owindows, owindows) {
381 DLOG("matching: %p / %s\n", current->con, current->con->name);
382 con_move_to_workspace(current->con, ws, true, false);
385 cmd_output->needs_tree_render = true;
386 // XXX: default reply for now, make this a better reply
391 * Implementation of 'move [window|container] [to] workspace <name>'.
394 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
395 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
396 LOG("You cannot switch to the i3 internal workspaces.\n");
403 /* Error out early to not create a non-existing workspace (in
404 * workspace_get()) if we are not actually able to move anything. */
405 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
410 LOG("should move window to workspace %s\n", name);
411 /* get the workspace */
412 Con *ws = workspace_get(name, NULL);
416 TAILQ_FOREACH(current, &owindows, owindows) {
417 DLOG("matching: %p / %s\n", current->con, current->con->name);
418 con_move_to_workspace(current->con, ws, true, false);
421 cmd_output->needs_tree_render = true;
422 // XXX: default reply for now, make this a better reply
427 * Implementation of 'move [window|container] [to] workspace number <number>'.
430 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
433 /* Error out early to not create a non-existing workspace (in
434 * workspace_get()) if we are not actually able to move anything. */
435 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
440 LOG("should move window to workspace with number %d\n", which);
441 /* get the workspace */
442 Con *output, *workspace = NULL;
445 long parsed_num = strtol(which, &endptr, 10);
446 if (parsed_num == LONG_MIN ||
447 parsed_num == LONG_MAX ||
450 LOG("Could not parse \"%s\" as a number.\n", which);
455 // TODO: better error message
456 ystr("Could not parse number");
461 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
462 GREP_FIRST(workspace, output_get_content(output),
463 child->num == parsed_num);
470 // TODO: better error message
471 ystr("No such workspace");
478 TAILQ_FOREACH(current, &owindows, owindows) {
479 DLOG("matching: %p / %s\n", current->con, current->con->name);
480 con_move_to_workspace(current->con, workspace, true, false);
483 cmd_output->needs_tree_render = true;
484 // XXX: default reply for now, make this a better reply
488 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
489 LOG("floating resize\n");
490 if (strcmp(direction, "up") == 0) {
491 floating_con->rect.y -= px;
492 floating_con->rect.height += px;
493 } else if (strcmp(direction, "down") == 0) {
494 floating_con->rect.height += px;
495 } else if (strcmp(direction, "left") == 0) {
496 floating_con->rect.x -= px;
497 floating_con->rect.width += px;
499 floating_con->rect.width += px;
503 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
504 LOG("tiling resize\n");
505 /* get the appropriate current container (skip stacked/tabbed cons) */
506 Con *current = focused;
507 while (current->parent->layout == L_STACKED ||
508 current->parent->layout == L_TABBED)
509 current = current->parent;
511 /* Then further go up until we find one with the matching orientation. */
512 orientation_t search_orientation =
513 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
515 while (current->type != CT_WORKSPACE &&
516 current->type != CT_FLOATING_CON &&
517 current->parent->orientation != search_orientation)
518 current = current->parent;
520 /* get the default percentage */
521 int children = con_num_children(current->parent);
523 LOG("ins. %d children\n", children);
524 double percentage = 1.0 / children;
525 LOG("default percentage = %f\n", percentage);
527 orientation_t orientation = current->parent->orientation;
529 if ((orientation == HORIZ &&
530 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
531 (orientation == VERT &&
532 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
533 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
534 (orientation == HORIZ ? "horizontal" : "vertical"));
539 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
540 other = TAILQ_PREV(current, nodes_head, nodes);
542 other = TAILQ_NEXT(current, nodes);
544 if (other == TAILQ_END(workspaces)) {
545 LOG("No other container in this direction found, cannot resize.\n");
549 LOG("other->percent = %f\n", other->percent);
550 LOG("current->percent before = %f\n", current->percent);
551 if (current->percent == 0.0)
552 current->percent = percentage;
553 if (other->percent == 0.0)
554 other->percent = percentage;
555 double new_current_percent = current->percent + ((double)ppt / 100.0);
556 double new_other_percent = other->percent - ((double)ppt / 100.0);
557 LOG("new_current_percent = %f\n", new_current_percent);
558 LOG("new_other_percent = %f\n", new_other_percent);
559 /* Ensure that the new percentages are positive and greater than
560 * 0.05 to have a reasonable minimum size. */
561 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
562 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
563 current->percent += ((double)ppt / 100.0);
564 other->percent -= ((double)ppt / 100.0);
565 LOG("current->percent after = %f\n", current->percent);
566 LOG("other->percent after = %f\n", other->percent);
568 LOG("Not resizing, already at minimum size\n");
572 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
573 LOG("width/height resize\n");
574 /* get the appropriate current container (skip stacked/tabbed cons) */
575 Con *current = focused;
576 while (current->parent->layout == L_STACKED ||
577 current->parent->layout == L_TABBED)
578 current = current->parent;
580 /* Then further go up until we find one with the matching orientation. */
581 orientation_t search_orientation =
582 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
584 while (current->type != CT_WORKSPACE &&
585 current->type != CT_FLOATING_CON &&
586 current->parent->orientation != search_orientation)
587 current = current->parent;
589 /* get the default percentage */
590 int children = con_num_children(current->parent);
591 LOG("ins. %d children\n", children);
592 double percentage = 1.0 / children;
593 LOG("default percentage = %f\n", percentage);
595 orientation_t orientation = current->parent->orientation;
597 if ((orientation == HORIZ &&
598 strcmp(direction, "height") == 0) ||
599 (orientation == VERT &&
600 strcmp(direction, "width") == 0)) {
601 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
602 (orientation == HORIZ ? "horizontal" : "vertical"));
608 LOG("This is the only container, cannot resize.\n");
613 /* Ensure all the other children have a percentage set. */
615 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
616 LOG("child->percent = %f (child %p)\n", child->percent, child);
617 if (child->percent == 0.0)
618 child->percent = percentage;
621 double new_current_percent = current->percent + ((double)ppt / 100.0);
622 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
623 LOG("new_current_percent = %f\n", new_current_percent);
624 LOG("subtract_percent = %f\n", subtract_percent);
625 /* Ensure that the new percentages are positive and greater than
626 * 0.05 to have a reasonable minimum size. */
627 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
628 if (child == current)
630 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
631 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
636 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
637 LOG("Not resizing, already at minimum size\n");
642 current->percent += ((double)ppt / 100.0);
643 LOG("current->percent after = %f\n", current->percent);
645 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
646 if (child == current)
648 child->percent -= subtract_percent;
649 LOG("child->percent after (%p) = %f\n", child, child->percent);
654 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
657 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
658 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
659 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
660 // 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
661 int px = atoi(resize_px);
662 int ppt = atoi(resize_ppt);
663 if (strcmp(way, "shrink") == 0) {
669 if ((floating_con = con_inside_floating(focused))) {
670 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
672 if (strcmp(direction, "width") == 0 ||
673 strcmp(direction, "height") == 0)
674 cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
675 else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
678 cmd_output->needs_tree_render = true;
679 // XXX: default reply for now, make this a better reply
684 * Implementation of 'border normal|none|1pixel|toggle'.
687 void cmd_border(I3_CMD, char *border_style_str) {
688 DLOG("border style should be changed to %s\n", border_style_str);
693 TAILQ_FOREACH(current, &owindows, owindows) {
694 DLOG("matching: %p / %s\n", current->con, current->con->name);
695 int border_style = current->con->border_style;
696 if (strcmp(border_style_str, "toggle") == 0) {
700 if (strcmp(border_style_str, "normal") == 0)
701 border_style = BS_NORMAL;
702 else if (strcmp(border_style_str, "none") == 0)
703 border_style = BS_NONE;
704 else if (strcmp(border_style_str, "1pixel") == 0)
705 border_style = BS_1PIXEL;
707 ELOG("BUG: called with border_style=%s\n", border_style_str);
712 con_set_border_style(current->con, border_style);
715 cmd_output->needs_tree_render = true;
716 // XXX: default reply for now, make this a better reply
721 * Implementation of 'nop <comment>'.
724 void cmd_nop(I3_CMD, char *comment) {
725 LOG("-------------------------------------------------\n");
726 LOG(" NOP: %s\n", comment);
727 LOG("-------------------------------------------------\n");
731 * Implementation of 'append_layout <path>'.
734 void cmd_append_layout(I3_CMD, char *path) {
735 LOG("Appending layout \"%s\"\n", path);
736 tree_append_json(path);
738 cmd_output->needs_tree_render = true;
739 // XXX: default reply for now, make this a better reply
744 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
747 void cmd_workspace(I3_CMD, char *which) {
750 DLOG("which=%s\n", which);
752 if (strcmp(which, "next") == 0)
753 ws = workspace_next();
754 else if (strcmp(which, "prev") == 0)
755 ws = workspace_prev();
756 else if (strcmp(which, "next_on_output") == 0)
757 ws = workspace_next_on_output();
758 else if (strcmp(which, "prev_on_output") == 0)
759 ws = workspace_prev_on_output();
761 ELOG("BUG: called with which=%s\n", which);
768 cmd_output->needs_tree_render = true;
769 // XXX: default reply for now, make this a better reply
774 * Implementation of 'workspace number <number>'
777 void cmd_workspace_number(I3_CMD, char *which) {
778 Con *output, *workspace = NULL;
781 long parsed_num = strtol(which, &endptr, 10);
782 if (parsed_num == LONG_MIN ||
783 parsed_num == LONG_MAX ||
786 LOG("Could not parse \"%s\" as a number.\n", which);
791 // TODO: better error message
792 ystr("Could not parse number");
798 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
799 GREP_FIRST(workspace, output_get_content(output),
800 child->num == parsed_num);
803 LOG("There is no workspace with number %d, creating a new one.\n", parsed_num);
805 /* terminate the which string after the endposition of the number */
807 if (maybe_back_and_forth(cmd_output, which))
809 workspace_show_by_name(which);
810 cmd_output->needs_tree_render = true;
813 if (maybe_back_and_forth(cmd_output, which))
815 workspace_show(workspace);
817 cmd_output->needs_tree_render = true;
818 // XXX: default reply for now, make this a better reply
823 * Implementation of 'workspace back_and_forth'.
826 void cmd_workspace_back_and_forth(I3_CMD) {
827 workspace_back_and_forth();
829 cmd_output->needs_tree_render = true;
830 // XXX: default reply for now, make this a better reply
835 * Implementation of 'workspace <name>'
838 void cmd_workspace_name(I3_CMD, char *name) {
839 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
840 LOG("You cannot switch to the i3 internal workspaces.\n");
845 DLOG("should switch to workspace %s\n", name);
846 if (maybe_back_and_forth(cmd_output, name))
848 workspace_show_by_name(name);
850 cmd_output->needs_tree_render = true;
851 // XXX: default reply for now, make this a better reply
856 * Implementation of 'mark <mark>'
859 void cmd_mark(I3_CMD, char *mark) {
860 DLOG("Clearing all windows which have that mark first\n");
863 TAILQ_FOREACH(con, &all_cons, all_cons) {
864 if (con->mark && strcmp(con->mark, mark) == 0)
868 DLOG("marking window with str %s\n", mark);
873 TAILQ_FOREACH(current, &owindows, owindows) {
874 DLOG("matching: %p / %s\n", current->con, current->con->name);
875 current->con->mark = sstrdup(mark);
878 cmd_output->needs_tree_render = true;
879 // XXX: default reply for now, make this a better reply
884 * Implementation of 'mode <string>'.
887 void cmd_mode(I3_CMD, char *mode) {
888 DLOG("mode=%s\n", mode);
891 // XXX: default reply for now, make this a better reply
896 * Implementation of 'move [window|container] [to] output <str>'.
899 void cmd_move_con_to_output(I3_CMD, char *name) {
902 DLOG("should move window to output %s\n", name);
907 Output *current_output = NULL;
910 // TODO: fix the handling of criteria
911 TAILQ_FOREACH(current, &owindows, owindows)
912 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
914 assert(current_output != NULL);
916 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
917 if (strcasecmp(name, "up") == 0)
918 output = get_output_next(D_UP, current_output);
919 else if (strcasecmp(name, "down") == 0)
920 output = get_output_next(D_DOWN, current_output);
921 else if (strcasecmp(name, "left") == 0)
922 output = get_output_next(D_LEFT, current_output);
923 else if (strcasecmp(name, "right") == 0)
924 output = get_output_next(D_RIGHT, current_output);
926 output = get_output_by_name(name);
929 LOG("No such output found.\n");
934 /* get visible workspace on output */
936 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
942 TAILQ_FOREACH(current, &owindows, owindows) {
943 DLOG("matching: %p / %s\n", current->con, current->con->name);
944 con_move_to_workspace(current->con, ws, true, false);
947 cmd_output->needs_tree_render = true;
948 // XXX: default reply for now, make this a better reply
953 * Implementation of 'floating enable|disable|toggle'
956 void cmd_floating(I3_CMD, char *floating_mode) {
959 DLOG("floating_mode=%s\n", floating_mode);
963 TAILQ_FOREACH(current, &owindows, owindows) {
964 DLOG("matching: %p / %s\n", current->con, current->con->name);
965 if (strcmp(floating_mode, "toggle") == 0) {
966 DLOG("should toggle mode\n");
967 toggle_floating_mode(current->con, false);
969 DLOG("should switch mode to %s\n", floating_mode);
970 if (strcmp(floating_mode, "enable") == 0) {
971 floating_enable(current->con, false);
973 floating_disable(current->con, false);
978 cmd_output->needs_tree_render = true;
979 // XXX: default reply for now, make this a better reply
984 * Implementation of 'move workspace to [output] <str>'.
987 void cmd_move_workspace_to_output(I3_CMD, char *name) {
988 DLOG("should move workspace to output %s\n", name);
993 TAILQ_FOREACH(current, &owindows, owindows) {
994 Output *current_output = get_output_containing(current->con->rect.x,
995 current->con->rect.y);
996 Output *output = get_output_from_string(current_output, name);
998 LOG("No such output\n");
1003 Con *content = output_get_content(output->con);
1004 LOG("got output %p with content %p\n", output, content);
1006 Con *ws = con_get_workspace(current->con);
1007 LOG("should move workspace %p / %s\n", ws, ws->name);
1009 if (con_num_children(ws->parent) == 1) {
1010 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1012 /* check if we can find a workspace assigned to this output */
1013 bool used_assignment = false;
1014 struct Workspace_Assignment *assignment;
1015 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1016 if (strcmp(assignment->output, current_output->name) != 0)
1019 /* check if this workspace is already attached to the tree */
1020 Con *workspace = NULL, *out;
1021 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1022 GREP_FIRST(workspace, output_get_content(out),
1023 !strcasecmp(child->name, assignment->name));
1024 if (workspace != NULL)
1027 /* so create the workspace referenced to by this assignment */
1028 LOG("Creating workspace from assignment %s.\n", assignment->name);
1029 workspace_get(assignment->name, NULL);
1030 used_assignment = true;
1034 /* if we couldn't create the workspace using an assignment, create
1035 * it on the output */
1036 if (!used_assignment)
1037 create_workspace_on_output(current_output, ws->parent);
1039 /* notify the IPC listeners */
1040 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1043 /* detach from the old output and attach to the new output */
1044 bool workspace_was_visible = workspace_is_visible(ws);
1045 Con *old_content = ws->parent;
1047 if (workspace_was_visible) {
1048 /* The workspace which we just detached was visible, so focus
1049 * the next one in the focus-stack. */
1050 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1051 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1052 workspace_show(focus_ws);
1054 con_attach(ws, content, false);
1056 /* fix the coordinates of the floating containers */
1058 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1059 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1061 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1062 if (workspace_was_visible) {
1063 /* Focus the moved workspace on the destination output. */
1068 cmd_output->needs_tree_render = true;
1069 // XXX: default reply for now, make this a better reply
1074 * Implementation of 'split v|h|vertical|horizontal'.
1077 void cmd_split(I3_CMD, char *direction) {
1078 /* TODO: use matches */
1079 LOG("splitting in direction %c\n", direction[0]);
1080 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1082 cmd_output->needs_tree_render = true;
1083 // XXX: default reply for now, make this a better reply
1088 * Implementaiton of 'kill [window|client]'.
1091 void cmd_kill(I3_CMD, char *kill_mode_str) {
1092 if (kill_mode_str == NULL)
1093 kill_mode_str = "window";
1096 DLOG("kill_mode=%s\n", kill_mode_str);
1099 if (strcmp(kill_mode_str, "window") == 0)
1100 kill_mode = KILL_WINDOW;
1101 else if (strcmp(kill_mode_str, "client") == 0)
1102 kill_mode = KILL_CLIENT;
1104 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1109 /* check if the match is empty, not if the result is empty */
1110 if (match_is_empty(current_match))
1111 tree_close_con(kill_mode);
1113 TAILQ_FOREACH(current, &owindows, owindows) {
1114 DLOG("matching: %p / %s\n", current->con, current->con->name);
1115 tree_close(current->con, kill_mode, false, false);
1119 cmd_output->needs_tree_render = true;
1120 // XXX: default reply for now, make this a better reply
1125 * Implementation of 'exec [--no-startup-id] <command>'.
1128 void cmd_exec(I3_CMD, char *nosn, char *command) {
1129 bool no_startup_id = (nosn != NULL);
1131 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1132 start_application(command, no_startup_id);
1134 // XXX: default reply for now, make this a better reply
1139 * Implementation of 'focus left|right|up|down'.
1142 void cmd_focus_direction(I3_CMD, char *direction) {
1144 focused->type != CT_WORKSPACE &&
1145 focused->fullscreen_mode != CF_NONE) {
1146 LOG("Cannot change focus while in fullscreen mode.\n");
1151 DLOG("direction = *%s*\n", direction);
1153 if (strcmp(direction, "left") == 0)
1154 tree_next('p', HORIZ);
1155 else if (strcmp(direction, "right") == 0)
1156 tree_next('n', HORIZ);
1157 else if (strcmp(direction, "up") == 0)
1158 tree_next('p', VERT);
1159 else if (strcmp(direction, "down") == 0)
1160 tree_next('n', VERT);
1162 ELOG("Invalid focus direction (%s)\n", direction);
1167 cmd_output->needs_tree_render = true;
1168 // XXX: default reply for now, make this a better reply
1173 * Implementation of 'focus tiling|floating|mode_toggle'.
1176 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1178 focused->type != CT_WORKSPACE &&
1179 focused->fullscreen_mode != CF_NONE) {
1180 LOG("Cannot change focus while in fullscreen mode.\n");
1185 DLOG("window_mode = %s\n", window_mode);
1187 Con *ws = con_get_workspace(focused);
1190 if (strcmp(window_mode, "mode_toggle") == 0) {
1191 current = TAILQ_FIRST(&(ws->focus_head));
1192 if (current != NULL && current->type == CT_FLOATING_CON)
1193 window_mode = "tiling";
1194 else window_mode = "floating";
1196 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1197 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1198 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1201 con_focus(con_descend_focused(current));
1206 cmd_output->needs_tree_render = true;
1207 // XXX: default reply for now, make this a better reply
1212 * Implementation of 'focus parent|child'.
1215 void cmd_focus_level(I3_CMD, char *level) {
1217 focused->type != CT_WORKSPACE &&
1218 focused->fullscreen_mode != CF_NONE) {
1219 LOG("Cannot change focus while in fullscreen mode.\n");
1224 DLOG("level = %s\n", level);
1226 if (strcmp(level, "parent") == 0)
1230 cmd_output->needs_tree_render = true;
1231 // XXX: default reply for now, make this a better reply
1236 * Implementation of 'focus'.
1239 void cmd_focus(I3_CMD) {
1240 DLOG("current_match = %p\n", current_match);
1242 focused->type != CT_WORKSPACE &&
1243 focused->fullscreen_mode != CF_NONE) {
1244 LOG("Cannot change focus while in fullscreen mode.\n");
1251 if (match_is_empty(current_match)) {
1252 ELOG("You have to specify which window/container should be focused.\n");
1253 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1259 ystr("You have to specify which window/container should be focused");
1266 TAILQ_FOREACH(current, &owindows, owindows) {
1267 Con *ws = con_get_workspace(current->con);
1268 /* If no workspace could be found, this was a dock window.
1269 * Just skip it, you cannot focus dock windows. */
1273 /* If the container is not on the current workspace,
1274 * workspace_show() will switch to a different workspace and (if
1275 * enabled) trigger a mouse pointer warp to the currently focused
1276 * container (!) on the target workspace.
1278 * Therefore, before calling workspace_show(), we make sure that
1279 * 'current' will be focused on the workspace. However, we cannot
1280 * just con_focus(current) because then the pointer will not be
1281 * warped at all (the code thinks we are already there).
1283 * So we focus 'current' to make it the currently focused window of
1284 * the target workspace, then revert focus. */
1285 Con *currently_focused = focused;
1286 con_focus(current->con);
1287 con_focus(currently_focused);
1289 /* Now switch to the workspace, then focus */
1291 LOG("focusing %p / %s\n", current->con, current->con->name);
1292 con_focus(current->con);
1297 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1298 "while only exactly one container can be focused at a time.\n", count);
1300 cmd_output->needs_tree_render = true;
1301 // XXX: default reply for now, make this a better reply
1306 * Implementation of 'fullscreen [global]'.
1309 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1310 if (fullscreen_mode == NULL)
1311 fullscreen_mode = "output";
1312 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1317 TAILQ_FOREACH(current, &owindows, owindows) {
1318 printf("matching: %p / %s\n", current->con, current->con->name);
1319 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1322 cmd_output->needs_tree_render = true;
1323 // XXX: default reply for now, make this a better reply
1328 * Implementation of 'move <direction> [<pixels> [px]]'.
1331 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1332 // 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
1333 int px = atoi(move_px);
1335 /* TODO: make 'move' work with criteria. */
1336 DLOG("moving in direction %s, px %s\n", direction, move_px);
1337 if (con_is_floating(focused)) {
1338 DLOG("floating move with %d pixels\n", px);
1339 Rect newrect = focused->parent->rect;
1340 if (strcmp(direction, "left") == 0) {
1342 } else if (strcmp(direction, "right") == 0) {
1344 } else if (strcmp(direction, "up") == 0) {
1346 } else if (strcmp(direction, "down") == 0) {
1349 floating_reposition(focused->parent, newrect);
1351 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1352 (strcmp(direction, "left") == 0 ? D_LEFT :
1353 (strcmp(direction, "up") == 0 ? D_UP :
1355 cmd_output->needs_tree_render = true;
1358 // XXX: default reply for now, make this a better reply
1363 * Implementation of 'layout default|stacked|stacking|tabbed'.
1366 void cmd_layout(I3_CMD, char *layout_str) {
1367 if (strcmp(layout_str, "stacking") == 0)
1368 layout_str = "stacked";
1369 DLOG("changing layout to %s\n", layout_str);
1371 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1372 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1375 /* check if the match is empty, not if the result is empty */
1376 if (match_is_empty(current_match))
1377 con_set_layout(focused->parent, layout);
1379 TAILQ_FOREACH(current, &owindows, owindows) {
1380 DLOG("matching: %p / %s\n", current->con, current->con->name);
1381 con_set_layout(current->con, layout);
1385 cmd_output->needs_tree_render = true;
1386 // XXX: default reply for now, make this a better reply
1391 * Implementaiton of 'exit'.
1394 void cmd_exit(I3_CMD) {
1395 LOG("Exiting due to user command.\n");
1402 * Implementaiton of 'reload'.
1405 void cmd_reload(I3_CMD) {
1407 kill_configerror_nagbar(false);
1408 load_configuration(conn, NULL, true);
1410 /* Send an IPC event just in case the ws names have changed */
1411 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1413 // XXX: default reply for now, make this a better reply
1418 * Implementaiton of 'restart'.
1421 void cmd_restart(I3_CMD) {
1422 LOG("restarting i3\n");
1425 // XXX: default reply for now, make this a better reply
1430 * Implementaiton of 'open'.
1433 void cmd_open(I3_CMD) {
1434 LOG("opening new container\n");
1435 Con *con = tree_open_con(NULL, NULL);
1442 y(integer, (long int)con);
1445 cmd_output->needs_tree_render = true;
1449 * Implementation of 'focus output <output>'.
1452 void cmd_focus_output(I3_CMD, char *name) {
1455 DLOG("name = %s\n", name);
1459 /* get the output */
1460 Output *current_output = NULL;
1463 TAILQ_FOREACH(current, &owindows, owindows)
1464 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1465 assert(current_output != NULL);
1467 output = get_output_from_string(current_output, name);
1470 LOG("No such output found.\n");
1475 /* get visible workspace on output */
1477 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1485 cmd_output->needs_tree_render = true;
1486 // XXX: default reply for now, make this a better reply
1491 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1494 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1499 if (!con_is_floating(focused)) {
1500 ELOG("Cannot change position. The window/container is not floating\n");
1505 ystr("Cannot change position. The window/container is not floating.");
1510 if (strcmp(method, "absolute") == 0) {
1511 focused->parent->rect.x = x;
1512 focused->parent->rect.y = y;
1514 DLOG("moving to absolute position %d %d\n", x, y);
1515 floating_maybe_reassign_ws(focused->parent);
1516 cmd_output->needs_tree_render = true;
1519 if (strcmp(method, "position") == 0) {
1520 Rect newrect = focused->parent->rect;
1522 DLOG("moving to position %d %d\n", x, y);
1526 floating_reposition(focused->parent, newrect);
1529 // XXX: default reply for now, make this a better reply
1534 * Implementation of 'move [window|container] [to] [absolute] position center
1537 void cmd_move_window_to_center(I3_CMD, char *method) {
1539 if (!con_is_floating(focused)) {
1540 ELOG("Cannot change position. The window/container is not floating\n");
1545 ystr("Cannot change position. The window/container is not floating.");
1549 if (strcmp(method, "absolute") == 0) {
1550 Rect *rect = &focused->parent->rect;
1552 DLOG("moving to absolute center\n");
1553 rect->x = croot->rect.width/2 - rect->width/2;
1554 rect->y = croot->rect.height/2 - rect->height/2;
1556 floating_maybe_reassign_ws(focused->parent);
1557 cmd_output->needs_tree_render = true;
1560 if (strcmp(method, "position") == 0) {
1561 Rect *wsrect = &con_get_workspace(focused)->rect;
1562 Rect newrect = focused->parent->rect;
1564 DLOG("moving to center\n");
1565 newrect.x = wsrect->width/2 - newrect.width/2;
1566 newrect.y = wsrect->height/2 - newrect.height/2;
1568 floating_reposition(focused->parent, newrect);
1571 // XXX: default reply for now, make this a better reply
1576 * Implementation of 'move scratchpad'.
1579 void cmd_move_scratchpad(I3_CMD) {
1580 DLOG("should move window to scratchpad\n");
1585 TAILQ_FOREACH(current, &owindows, owindows) {
1586 DLOG("matching: %p / %s\n", current->con, current->con->name);
1587 scratchpad_move(current->con);
1590 cmd_output->needs_tree_render = true;
1591 // XXX: default reply for now, make this a better reply
1596 * Implementation of 'scratchpad show'.
1599 void cmd_scratchpad_show(I3_CMD) {
1600 DLOG("should show scratchpad window\n");
1603 if (match_is_empty(current_match)) {
1604 scratchpad_show(NULL);
1606 TAILQ_FOREACH(current, &owindows, owindows) {
1607 DLOG("matching: %p / %s\n", current->con, current->con->name);
1608 scratchpad_show(current->con);
1612 cmd_output->needs_tree_render = true;
1613 // XXX: default reply for now, make this a better reply
1618 * Implementation of 'rename workspace <name> to <name>'
1621 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1622 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1624 Con *output, *workspace = NULL;
1625 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1626 GREP_FIRST(workspace, output_get_content(output),
1627 !strcasecmp(child->name, old_name));
1630 // TODO: we should include the old workspace name here and use yajl for
1631 // generating the reply.
1636 // TODO: better error message
1637 ystr("Old workspace not found");
1642 Con *check_dest = NULL;
1643 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1644 GREP_FIRST(check_dest, output_get_content(output),
1645 !strcasecmp(child->name, new_name));
1647 if (check_dest != NULL) {
1648 // TODO: we should include the new workspace name here and use yajl for
1649 // generating the reply.
1654 // TODO: better error message
1655 ystr("New workspace already exists");
1660 /* Change the name and try to parse it as a number. */
1661 FREE(workspace->name);
1662 workspace->name = sstrdup(new_name);
1663 char *endptr = NULL;
1664 long parsed_num = strtol(new_name, &endptr, 10);
1665 if (parsed_num == LONG_MIN ||
1666 parsed_num == LONG_MAX ||
1669 workspace->num = -1;
1670 else workspace->num = parsed_num;
1671 LOG("num = %d\n", workspace->num);
1673 /* By re-attaching, the sort order will be correct afterwards. */
1674 Con *previously_focused = focused;
1675 Con *parent = workspace->parent;
1676 con_detach(workspace);
1677 con_attach(workspace, parent, false);
1678 /* Restore the previous focus since con_attach messes with the focus. */
1679 con_focus(previously_focused);
1681 cmd_output->needs_tree_render = true;
1684 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");