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 bool maybe_back_and_forth(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();
98 // This code is commented out because we might recycle it for popping up error
99 // messages on parser errors.
101 static pid_t migration_pid = -1;
104 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
105 * it exited (or could not be started, depending on the exit code).
108 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
109 ev_child_stop(EV_A_ watcher);
110 if (!WIFEXITED(watcher->rstatus)) {
111 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
115 int exitcode = WEXITSTATUS(watcher->rstatus);
116 printf("i3-nagbar process exited with status %d\n", exitcode);
118 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
124 /* We need ev >= 4 for the following code. Since it is not *that* important (it
125 * only makes sure that there are no i3-nagbar instances left behind) we still
126 * support old systems with libev 3. */
127 #if EV_VERSION_MAJOR >= 4
129 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
130 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
133 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
134 if (migration_pid != -1) {
135 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
136 kill(migration_pid, SIGKILL);
141 void cmd_MIGRATION_start_nagbar(void) {
142 if (migration_pid != -1) {
143 fprintf(stderr, "i3-nagbar already running.\n");
146 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
147 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
148 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
149 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
150 migration_pid = fork();
151 if (migration_pid == -1) {
152 warn("Could not fork()");
157 if (migration_pid == 0) {
159 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
161 NULL, /* will be replaced by the executable path */
165 "You found a parsing error. Please, please, please, report it!",
171 exec_i3_utility("i3-nagbar", argv);
175 /* install a child watcher */
176 ev_child *child = smalloc(sizeof(ev_child));
177 ev_child_init(child, &nagbar_exited, migration_pid, 0);
178 ev_child_start(main_loop, child);
180 /* We need ev >= 4 for the following code. Since it is not *that* important (it
181 * only makes sure that there are no i3-nagbar instances left behind) we still
182 * support old systems with libev 3. */
183 #if EV_VERSION_MAJOR >= 4
184 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
186 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
187 ev_cleanup_init(cleanup, nagbar_cleanup);
188 ev_cleanup_start(main_loop, cleanup);
194 /*******************************************************************************
195 * Criteria functions.
196 ******************************************************************************/
199 * Initializes the specified 'Match' data structure and the initial state of
200 * commands.c for matching target windows of a command.
203 void cmd_criteria_init(I3_CMD) {
207 DLOG("Initializing criteria, current_match = %p\n", current_match);
208 match_init(current_match);
209 while (!TAILQ_EMPTY(&owindows)) {
210 ow = TAILQ_FIRST(&owindows);
211 TAILQ_REMOVE(&owindows, ow, owindows);
214 TAILQ_INIT(&owindows);
216 TAILQ_FOREACH(con, &all_cons, all_cons) {
217 ow = smalloc(sizeof(owindow));
219 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
224 * A match specification just finished (the closing square bracket was found),
225 * so we filter the list of owindows.
228 void cmd_criteria_match_windows(I3_CMD) {
229 owindow *next, *current;
231 DLOG("match specification finished, matching...\n");
232 /* copy the old list head to iterate through it and start with a fresh
233 * list which will contain only matching windows */
234 struct owindows_head old = owindows;
235 TAILQ_INIT(&owindows);
236 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
237 /* make a copy of the next pointer and advance the pointer to the
238 * next element as we are going to invalidate the element’s
239 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
241 next = TAILQ_NEXT(next, owindows);
243 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
244 if (current_match->con_id != NULL) {
245 if (current_match->con_id == current->con) {
246 DLOG("matches container!\n");
247 TAILQ_INSERT_TAIL(&owindows, current, owindows);
249 } else if (current_match->mark != NULL && current->con->mark != NULL &&
250 regex_matches(current_match->mark, current->con->mark)) {
251 DLOG("match by mark\n");
252 TAILQ_INSERT_TAIL(&owindows, current, owindows);
254 if (current->con->window == NULL)
256 if (match_matches_window(current_match, current->con->window)) {
257 DLOG("matches window!\n");
258 TAILQ_INSERT_TAIL(&owindows, current, owindows);
260 DLOG("doesnt match\n");
266 TAILQ_FOREACH(current, &owindows, owindows) {
267 DLOG("matching: %p / %s\n", current->con, current->con->name);
272 * Interprets a ctype=cvalue pair and adds it to the current match
276 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
277 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
279 if (strcmp(ctype, "class") == 0) {
280 current_match->class = regex_new(cvalue);
284 if (strcmp(ctype, "instance") == 0) {
285 current_match->instance = regex_new(cvalue);
289 if (strcmp(ctype, "window_role") == 0) {
290 current_match->role = regex_new(cvalue);
294 if (strcmp(ctype, "con_id") == 0) {
296 long parsed = strtol(cvalue, &end, 10);
297 if (parsed == LONG_MIN ||
298 parsed == LONG_MAX ||
300 (end && *end != '\0')) {
301 ELOG("Could not parse con id \"%s\"\n", cvalue);
303 current_match->con_id = (Con*)parsed;
304 printf("id as int = %p\n", current_match->con_id);
309 if (strcmp(ctype, "id") == 0) {
311 long parsed = strtol(cvalue, &end, 10);
312 if (parsed == LONG_MIN ||
313 parsed == LONG_MAX ||
315 (end && *end != '\0')) {
316 ELOG("Could not parse window id \"%s\"\n", cvalue);
318 current_match->id = parsed;
319 printf("window id as int = %d\n", current_match->id);
324 if (strcmp(ctype, "con_mark") == 0) {
325 current_match->mark = regex_new(cvalue);
329 if (strcmp(ctype, "title") == 0) {
330 current_match->title = regex_new(cvalue);
334 if (strcmp(ctype, "urgent") == 0) {
335 if (strcasecmp(cvalue, "latest") == 0 ||
336 strcasecmp(cvalue, "newest") == 0 ||
337 strcasecmp(cvalue, "recent") == 0 ||
338 strcasecmp(cvalue, "last") == 0) {
339 current_match->urgent = U_LATEST;
340 } else if (strcasecmp(cvalue, "oldest") == 0 ||
341 strcasecmp(cvalue, "first") == 0) {
342 current_match->urgent = U_OLDEST;
347 ELOG("Unknown criterion: %s\n", ctype);
351 * Implementation of 'move [window|container] [to] workspace
352 * next|prev|next_on_output|prev_on_output'.
355 void cmd_move_con_to_workspace(I3_CMD, char *which) {
358 DLOG("which=%s\n", which);
362 /* get the workspace */
364 if (strcmp(which, "next") == 0)
365 ws = workspace_next();
366 else if (strcmp(which, "prev") == 0)
367 ws = workspace_prev();
368 else if (strcmp(which, "next_on_output") == 0)
369 ws = workspace_next_on_output();
370 else if (strcmp(which, "prev_on_output") == 0)
371 ws = workspace_prev_on_output();
373 ELOG("BUG: called with which=%s\n", which);
378 TAILQ_FOREACH(current, &owindows, owindows) {
379 DLOG("matching: %p / %s\n", current->con, current->con->name);
380 con_move_to_workspace(current->con, ws, true, false);
383 cmd_output->needs_tree_render = true;
384 // XXX: default reply for now, make this a better reply
389 * Implementation of 'move [window|container] [to] workspace <name>'.
392 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
393 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
394 LOG("You cannot switch to the i3 internal workspaces.\n");
401 /* Error out early to not create a non-existing workspace (in
402 * workspace_get()) if we are not actually able to move anything. */
403 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
408 LOG("should move window to workspace %s\n", name);
409 /* get the workspace */
410 Con *ws = workspace_get(name, NULL);
414 TAILQ_FOREACH(current, &owindows, owindows) {
415 DLOG("matching: %p / %s\n", current->con, current->con->name);
416 con_move_to_workspace(current->con, ws, true, false);
419 cmd_output->needs_tree_render = true;
420 // XXX: default reply for now, make this a better reply
425 * Implementation of 'move [window|container] [to] workspace number <number>'.
428 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
431 /* Error out early to not create a non-existing workspace (in
432 * workspace_get()) if we are not actually able to move anything. */
433 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
438 LOG("should move window to workspace with number %d\n", which);
439 /* get the workspace */
440 Con *output, *workspace = NULL;
443 long parsed_num = strtol(which, &endptr, 10);
444 if (parsed_num == LONG_MIN ||
445 parsed_num == LONG_MAX ||
448 LOG("Could not parse \"%s\" as a number.\n", which);
453 // TODO: better error message
454 ystr("Could not parse number");
459 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
460 GREP_FIRST(workspace, output_get_content(output),
461 child->num == parsed_num);
468 // TODO: better error message
469 ystr("No such workspace");
476 TAILQ_FOREACH(current, &owindows, owindows) {
477 DLOG("matching: %p / %s\n", current->con, current->con->name);
478 con_move_to_workspace(current->con, workspace, true, false);
481 cmd_output->needs_tree_render = true;
482 // XXX: default reply for now, make this a better reply
486 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
487 LOG("floating resize\n");
488 if (strcmp(direction, "up") == 0) {
489 floating_con->rect.y -= px;
490 floating_con->rect.height += px;
491 } else if (strcmp(direction, "down") == 0) {
492 floating_con->rect.height += px;
493 } else if (strcmp(direction, "left") == 0) {
494 floating_con->rect.x -= px;
495 floating_con->rect.width += px;
497 floating_con->rect.width += px;
501 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
502 LOG("tiling resize\n");
503 /* get the appropriate current container (skip stacked/tabbed cons) */
504 Con *current = focused;
505 while (current->parent->layout == L_STACKED ||
506 current->parent->layout == L_TABBED)
507 current = current->parent;
509 /* Then further go up until we find one with the matching orientation. */
510 orientation_t search_orientation =
511 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
513 while (current->type != CT_WORKSPACE &&
514 current->type != CT_FLOATING_CON &&
515 current->parent->orientation != search_orientation)
516 current = current->parent;
518 /* get the default percentage */
519 int children = con_num_children(current->parent);
521 LOG("ins. %d children\n", children);
522 double percentage = 1.0 / children;
523 LOG("default percentage = %f\n", percentage);
525 orientation_t orientation = current->parent->orientation;
527 if ((orientation == HORIZ &&
528 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
529 (orientation == VERT &&
530 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
531 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
532 (orientation == HORIZ ? "horizontal" : "vertical"));
537 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
538 other = TAILQ_PREV(current, nodes_head, nodes);
540 other = TAILQ_NEXT(current, nodes);
542 if (other == TAILQ_END(workspaces)) {
543 LOG("No other container in this direction found, cannot resize.\n");
547 LOG("other->percent = %f\n", other->percent);
548 LOG("current->percent before = %f\n", current->percent);
549 if (current->percent == 0.0)
550 current->percent = percentage;
551 if (other->percent == 0.0)
552 other->percent = percentage;
553 double new_current_percent = current->percent + ((double)ppt / 100.0);
554 double new_other_percent = other->percent - ((double)ppt / 100.0);
555 LOG("new_current_percent = %f\n", new_current_percent);
556 LOG("new_other_percent = %f\n", new_other_percent);
557 /* Ensure that the new percentages are positive and greater than
558 * 0.05 to have a reasonable minimum size. */
559 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
560 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
561 current->percent += ((double)ppt / 100.0);
562 other->percent -= ((double)ppt / 100.0);
563 LOG("current->percent after = %f\n", current->percent);
564 LOG("other->percent after = %f\n", other->percent);
566 LOG("Not resizing, already at minimum size\n");
570 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
571 LOG("width/height resize\n");
572 /* get the appropriate current container (skip stacked/tabbed cons) */
573 Con *current = focused;
574 while (current->parent->layout == L_STACKED ||
575 current->parent->layout == L_TABBED)
576 current = current->parent;
578 /* Then further go up until we find one with the matching orientation. */
579 orientation_t search_orientation =
580 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
582 while (current->type != CT_WORKSPACE &&
583 current->type != CT_FLOATING_CON &&
584 current->parent->orientation != search_orientation)
585 current = current->parent;
587 /* get the default percentage */
588 int children = con_num_children(current->parent);
589 LOG("ins. %d children\n", children);
590 double percentage = 1.0 / children;
591 LOG("default percentage = %f\n", percentage);
593 orientation_t orientation = current->parent->orientation;
595 if ((orientation == HORIZ &&
596 strcmp(direction, "height") == 0) ||
597 (orientation == VERT &&
598 strcmp(direction, "width") == 0)) {
599 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
600 (orientation == HORIZ ? "horizontal" : "vertical"));
606 LOG("This is the only container, cannot resize.\n");
611 /* Ensure all the other children have a percentage set. */
613 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
614 LOG("child->percent = %f (child %p)\n", child->percent, child);
615 if (child->percent == 0.0)
616 child->percent = percentage;
619 double new_current_percent = current->percent + ((double)ppt / 100.0);
620 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
621 LOG("new_current_percent = %f\n", new_current_percent);
622 LOG("subtract_percent = %f\n", subtract_percent);
623 /* Ensure that the new percentages are positive and greater than
624 * 0.05 to have a reasonable minimum size. */
625 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
626 if (child == current)
628 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
629 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
634 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
635 LOG("Not resizing, already at minimum size\n");
640 current->percent += ((double)ppt / 100.0);
641 LOG("current->percent after = %f\n", current->percent);
643 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
644 if (child == current)
646 child->percent -= subtract_percent;
647 LOG("child->percent after (%p) = %f\n", child, child->percent);
652 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
655 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
656 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
657 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
658 // 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
659 int px = atoi(resize_px);
660 int ppt = atoi(resize_ppt);
661 if (strcmp(way, "shrink") == 0) {
667 if ((floating_con = con_inside_floating(focused))) {
668 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
670 if (strcmp(direction, "width") == 0 ||
671 strcmp(direction, "height") == 0)
672 cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
673 else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
676 cmd_output->needs_tree_render = true;
677 // XXX: default reply for now, make this a better reply
682 * Implementation of 'border normal|none|1pixel|toggle'.
685 void cmd_border(I3_CMD, char *border_style_str) {
686 DLOG("border style should be changed to %s\n", border_style_str);
691 TAILQ_FOREACH(current, &owindows, owindows) {
692 DLOG("matching: %p / %s\n", current->con, current->con->name);
693 int border_style = current->con->border_style;
694 if (strcmp(border_style_str, "toggle") == 0) {
698 if (strcmp(border_style_str, "normal") == 0)
699 border_style = BS_NORMAL;
700 else if (strcmp(border_style_str, "none") == 0)
701 border_style = BS_NONE;
702 else if (strcmp(border_style_str, "1pixel") == 0)
703 border_style = BS_1PIXEL;
705 ELOG("BUG: called with border_style=%s\n", border_style_str);
710 con_set_border_style(current->con, border_style);
713 cmd_output->needs_tree_render = true;
714 // XXX: default reply for now, make this a better reply
719 * Implementation of 'nop <comment>'.
722 void cmd_nop(I3_CMD, char *comment) {
723 LOG("-------------------------------------------------\n");
724 LOG(" NOP: %s\n", comment);
725 LOG("-------------------------------------------------\n");
729 * Implementation of 'append_layout <path>'.
732 void cmd_append_layout(I3_CMD, char *path) {
733 LOG("Appending layout \"%s\"\n", path);
734 tree_append_json(path);
736 cmd_output->needs_tree_render = true;
737 // XXX: default reply for now, make this a better reply
742 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
745 void cmd_workspace(I3_CMD, char *which) {
748 DLOG("which=%s\n", which);
750 if (strcmp(which, "next") == 0)
751 ws = workspace_next();
752 else if (strcmp(which, "prev") == 0)
753 ws = workspace_prev();
754 else if (strcmp(which, "next_on_output") == 0)
755 ws = workspace_next_on_output();
756 else if (strcmp(which, "prev_on_output") == 0)
757 ws = workspace_prev_on_output();
759 ELOG("BUG: called with which=%s\n", which);
766 cmd_output->needs_tree_render = true;
767 // XXX: default reply for now, make this a better reply
772 * Implementation of 'workspace number <number>'
775 void cmd_workspace_number(I3_CMD, char *which) {
776 Con *output, *workspace = NULL;
779 long parsed_num = strtol(which, &endptr, 10);
780 if (parsed_num == LONG_MIN ||
781 parsed_num == LONG_MAX ||
784 LOG("Could not parse \"%s\" as a number.\n", which);
789 // TODO: better error message
790 ystr("Could not parse number");
796 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
797 GREP_FIRST(workspace, output_get_content(output),
798 child->num == parsed_num);
801 LOG("There is no workspace with number %d, creating a new one.\n", parsed_num);
803 /* terminate the which string after the endposition of the number */
805 if (maybe_back_and_forth(which))
807 workspace_show_by_name(which);
808 cmd_output->needs_tree_render = true;
811 if (maybe_back_and_forth(which))
813 workspace_show(workspace);
815 cmd_output->needs_tree_render = true;
816 // XXX: default reply for now, make this a better reply
821 * Implementation of 'workspace back_and_forth'.
824 void cmd_workspace_back_and_forth(I3_CMD) {
825 workspace_back_and_forth();
827 cmd_output->needs_tree_render = true;
828 // XXX: default reply for now, make this a better reply
833 * Implementation of 'workspace <name>'
836 void cmd_workspace_name(I3_CMD, char *name) {
837 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
838 LOG("You cannot switch to the i3 internal workspaces.\n");
843 DLOG("should switch to workspace %s\n", name);
844 if (maybe_back_and_forth(name))
846 workspace_show_by_name(name);
848 cmd_output->needs_tree_render = true;
849 // XXX: default reply for now, make this a better reply
854 * Implementation of 'mark <mark>'
857 void cmd_mark(I3_CMD, char *mark) {
858 DLOG("Clearing all windows which have that mark first\n");
861 TAILQ_FOREACH(con, &all_cons, all_cons) {
862 if (con->mark && strcmp(con->mark, mark) == 0)
866 DLOG("marking window with str %s\n", mark);
871 TAILQ_FOREACH(current, &owindows, owindows) {
872 DLOG("matching: %p / %s\n", current->con, current->con->name);
873 current->con->mark = sstrdup(mark);
876 cmd_output->needs_tree_render = true;
877 // XXX: default reply for now, make this a better reply
882 * Implementation of 'mode <string>'.
885 void cmd_mode(I3_CMD, char *mode) {
886 DLOG("mode=%s\n", mode);
889 // XXX: default reply for now, make this a better reply
894 * Implementation of 'move [window|container] [to] output <str>'.
897 void cmd_move_con_to_output(I3_CMD, char *name) {
900 DLOG("should move window to output %s\n", name);
905 Output *current_output = NULL;
908 // TODO: fix the handling of criteria
909 TAILQ_FOREACH(current, &owindows, owindows)
910 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
912 assert(current_output != NULL);
914 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
915 if (strcasecmp(name, "up") == 0)
916 output = get_output_next(D_UP, current_output);
917 else if (strcasecmp(name, "down") == 0)
918 output = get_output_next(D_DOWN, current_output);
919 else if (strcasecmp(name, "left") == 0)
920 output = get_output_next(D_LEFT, current_output);
921 else if (strcasecmp(name, "right") == 0)
922 output = get_output_next(D_RIGHT, current_output);
924 output = get_output_by_name(name);
927 LOG("No such output found.\n");
932 /* get visible workspace on output */
934 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
940 TAILQ_FOREACH(current, &owindows, owindows) {
941 DLOG("matching: %p / %s\n", current->con, current->con->name);
942 con_move_to_workspace(current->con, ws, true, false);
945 cmd_output->needs_tree_render = true;
946 // XXX: default reply for now, make this a better reply
951 * Implementation of 'floating enable|disable|toggle'
954 void cmd_floating(I3_CMD, char *floating_mode) {
957 DLOG("floating_mode=%s\n", floating_mode);
961 TAILQ_FOREACH(current, &owindows, owindows) {
962 DLOG("matching: %p / %s\n", current->con, current->con->name);
963 if (strcmp(floating_mode, "toggle") == 0) {
964 DLOG("should toggle mode\n");
965 toggle_floating_mode(current->con, false);
967 DLOG("should switch mode to %s\n", floating_mode);
968 if (strcmp(floating_mode, "enable") == 0) {
969 floating_enable(current->con, false);
971 floating_disable(current->con, false);
976 cmd_output->needs_tree_render = true;
977 // XXX: default reply for now, make this a better reply
982 * Implementation of 'move workspace to [output] <str>'.
985 void cmd_move_workspace_to_output(I3_CMD, char *name) {
986 DLOG("should move workspace to output %s\n", name);
991 TAILQ_FOREACH(current, &owindows, owindows) {
992 Output *current_output = get_output_containing(current->con->rect.x,
993 current->con->rect.y);
994 Output *output = get_output_from_string(current_output, name);
996 LOG("No such output\n");
1001 Con *content = output_get_content(output->con);
1002 LOG("got output %p with content %p\n", output, content);
1004 Con *ws = con_get_workspace(current->con);
1005 LOG("should move workspace %p / %s\n", ws, ws->name);
1007 if (con_num_children(ws->parent) == 1) {
1008 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1010 /* check if we can find a workspace assigned to this output */
1011 bool used_assignment = false;
1012 struct Workspace_Assignment *assignment;
1013 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1014 if (strcmp(assignment->output, current_output->name) != 0)
1017 /* check if this workspace is already attached to the tree */
1018 Con *workspace = NULL, *out;
1019 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1020 GREP_FIRST(workspace, output_get_content(out),
1021 !strcasecmp(child->name, assignment->name));
1022 if (workspace != NULL)
1025 /* so create the workspace referenced to by this assignment */
1026 LOG("Creating workspace from assignment %s.\n", assignment->name);
1027 workspace_get(assignment->name, NULL);
1028 used_assignment = true;
1032 /* if we couldn't create the workspace using an assignment, create
1033 * it on the output */
1034 if (!used_assignment)
1035 create_workspace_on_output(current_output, ws->parent);
1037 /* notify the IPC listeners */
1038 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1041 /* detach from the old output and attach to the new output */
1042 bool workspace_was_visible = workspace_is_visible(ws);
1043 Con *old_content = ws->parent;
1045 if (workspace_was_visible) {
1046 /* The workspace which we just detached was visible, so focus
1047 * the next one in the focus-stack. */
1048 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1049 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1050 workspace_show(focus_ws);
1052 con_attach(ws, content, false);
1054 /* fix the coordinates of the floating containers */
1056 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1057 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1059 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1060 if (workspace_was_visible) {
1061 /* Focus the moved workspace on the destination output. */
1066 cmd_output->needs_tree_render = true;
1067 // XXX: default reply for now, make this a better reply
1072 * Implementation of 'split v|h|vertical|horizontal'.
1075 void cmd_split(I3_CMD, char *direction) {
1076 /* TODO: use matches */
1077 LOG("splitting in direction %c\n", direction[0]);
1078 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1080 cmd_output->needs_tree_render = true;
1081 // XXX: default reply for now, make this a better reply
1086 * Implementaiton of 'kill [window|client]'.
1089 void cmd_kill(I3_CMD, char *kill_mode_str) {
1090 if (kill_mode_str == NULL)
1091 kill_mode_str = "window";
1094 DLOG("kill_mode=%s\n", kill_mode_str);
1097 if (strcmp(kill_mode_str, "window") == 0)
1098 kill_mode = KILL_WINDOW;
1099 else if (strcmp(kill_mode_str, "client") == 0)
1100 kill_mode = KILL_CLIENT;
1102 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1107 /* check if the match is empty, not if the result is empty */
1108 if (match_is_empty(current_match))
1109 tree_close_con(kill_mode);
1111 TAILQ_FOREACH(current, &owindows, owindows) {
1112 DLOG("matching: %p / %s\n", current->con, current->con->name);
1113 tree_close(current->con, kill_mode, false, false);
1117 cmd_output->needs_tree_render = true;
1118 // XXX: default reply for now, make this a better reply
1123 * Implementation of 'exec [--no-startup-id] <command>'.
1126 void cmd_exec(I3_CMD, char *nosn, char *command) {
1127 bool no_startup_id = (nosn != NULL);
1129 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1130 start_application(command, no_startup_id);
1132 // XXX: default reply for now, make this a better reply
1137 * Implementation of 'focus left|right|up|down'.
1140 void cmd_focus_direction(I3_CMD, char *direction) {
1142 focused->type != CT_WORKSPACE &&
1143 focused->fullscreen_mode != CF_NONE) {
1144 LOG("Cannot change focus while in fullscreen mode.\n");
1149 DLOG("direction = *%s*\n", direction);
1151 if (strcmp(direction, "left") == 0)
1152 tree_next('p', HORIZ);
1153 else if (strcmp(direction, "right") == 0)
1154 tree_next('n', HORIZ);
1155 else if (strcmp(direction, "up") == 0)
1156 tree_next('p', VERT);
1157 else if (strcmp(direction, "down") == 0)
1158 tree_next('n', VERT);
1160 ELOG("Invalid focus direction (%s)\n", direction);
1165 cmd_output->needs_tree_render = true;
1166 // XXX: default reply for now, make this a better reply
1171 * Implementation of 'focus tiling|floating|mode_toggle'.
1174 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1176 focused->type != CT_WORKSPACE &&
1177 focused->fullscreen_mode != CF_NONE) {
1178 LOG("Cannot change focus while in fullscreen mode.\n");
1183 DLOG("window_mode = %s\n", window_mode);
1185 Con *ws = con_get_workspace(focused);
1188 if (strcmp(window_mode, "mode_toggle") == 0) {
1189 current = TAILQ_FIRST(&(ws->focus_head));
1190 if (current != NULL && current->type == CT_FLOATING_CON)
1191 window_mode = "tiling";
1192 else window_mode = "floating";
1194 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1195 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1196 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1199 con_focus(con_descend_focused(current));
1204 cmd_output->needs_tree_render = true;
1205 // XXX: default reply for now, make this a better reply
1210 * Implementation of 'focus parent|child'.
1213 void cmd_focus_level(I3_CMD, char *level) {
1215 focused->type != CT_WORKSPACE &&
1216 focused->fullscreen_mode != CF_NONE) {
1217 LOG("Cannot change focus while in fullscreen mode.\n");
1222 DLOG("level = %s\n", level);
1224 if (strcmp(level, "parent") == 0)
1228 cmd_output->needs_tree_render = true;
1229 // XXX: default reply for now, make this a better reply
1234 * Implementation of 'focus'.
1237 void cmd_focus(I3_CMD) {
1238 DLOG("current_match = %p\n", current_match);
1240 focused->type != CT_WORKSPACE &&
1241 focused->fullscreen_mode != CF_NONE) {
1242 LOG("Cannot change focus while in fullscreen mode.\n");
1249 if (match_is_empty(current_match)) {
1250 ELOG("You have to specify which window/container should be focused.\n");
1251 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1257 ystr("You have to specify which window/container should be focused");
1264 TAILQ_FOREACH(current, &owindows, owindows) {
1265 Con *ws = con_get_workspace(current->con);
1266 /* If no workspace could be found, this was a dock window.
1267 * Just skip it, you cannot focus dock windows. */
1271 /* If the container is not on the current workspace,
1272 * workspace_show() will switch to a different workspace and (if
1273 * enabled) trigger a mouse pointer warp to the currently focused
1274 * container (!) on the target workspace.
1276 * Therefore, before calling workspace_show(), we make sure that
1277 * 'current' will be focused on the workspace. However, we cannot
1278 * just con_focus(current) because then the pointer will not be
1279 * warped at all (the code thinks we are already there).
1281 * So we focus 'current' to make it the currently focused window of
1282 * the target workspace, then revert focus. */
1283 Con *currently_focused = focused;
1284 con_focus(current->con);
1285 con_focus(currently_focused);
1287 /* Now switch to the workspace, then focus */
1289 LOG("focusing %p / %s\n", current->con, current->con->name);
1290 con_focus(current->con);
1295 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1296 "while only exactly one container can be focused at a time.\n", count);
1298 cmd_output->needs_tree_render = true;
1299 // XXX: default reply for now, make this a better reply
1304 * Implementation of 'fullscreen [global]'.
1307 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1308 if (fullscreen_mode == NULL)
1309 fullscreen_mode = "output";
1310 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1315 TAILQ_FOREACH(current, &owindows, owindows) {
1316 printf("matching: %p / %s\n", current->con, current->con->name);
1317 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1320 cmd_output->needs_tree_render = true;
1321 // XXX: default reply for now, make this a better reply
1326 * Implementation of 'move <direction> [<pixels> [px]]'.
1329 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1330 // 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
1331 int px = atoi(move_px);
1333 /* TODO: make 'move' work with criteria. */
1334 DLOG("moving in direction %s, px %s\n", direction, move_px);
1335 if (con_is_floating(focused)) {
1336 DLOG("floating move with %d pixels\n", px);
1337 Rect newrect = focused->parent->rect;
1338 if (strcmp(direction, "left") == 0) {
1340 } else if (strcmp(direction, "right") == 0) {
1342 } else if (strcmp(direction, "up") == 0) {
1344 } else if (strcmp(direction, "down") == 0) {
1347 floating_reposition(focused->parent, newrect);
1349 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1350 (strcmp(direction, "left") == 0 ? D_LEFT :
1351 (strcmp(direction, "up") == 0 ? D_UP :
1353 cmd_output->needs_tree_render = true;
1356 // XXX: default reply for now, make this a better reply
1361 * Implementation of 'layout default|stacked|stacking|tabbed'.
1364 void cmd_layout(I3_CMD, char *layout_str) {
1365 if (strcmp(layout_str, "stacking") == 0)
1366 layout_str = "stacked";
1367 DLOG("changing layout to %s\n", layout_str);
1369 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1370 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1373 /* check if the match is empty, not if the result is empty */
1374 if (match_is_empty(current_match))
1375 con_set_layout(focused->parent, layout);
1377 TAILQ_FOREACH(current, &owindows, owindows) {
1378 DLOG("matching: %p / %s\n", current->con, current->con->name);
1379 con_set_layout(current->con, layout);
1383 cmd_output->needs_tree_render = true;
1384 // XXX: default reply for now, make this a better reply
1389 * Implementaiton of 'exit'.
1392 void cmd_exit(I3_CMD) {
1393 LOG("Exiting due to user command.\n");
1400 * Implementaiton of 'reload'.
1403 void cmd_reload(I3_CMD) {
1405 kill_configerror_nagbar(false);
1406 load_configuration(conn, NULL, true);
1408 /* Send an IPC event just in case the ws names have changed */
1409 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1411 // XXX: default reply for now, make this a better reply
1416 * Implementaiton of 'restart'.
1419 void cmd_restart(I3_CMD) {
1420 LOG("restarting i3\n");
1423 // XXX: default reply for now, make this a better reply
1428 * Implementaiton of 'open'.
1431 void cmd_open(I3_CMD) {
1432 LOG("opening new container\n");
1433 Con *con = tree_open_con(NULL, NULL);
1440 y(integer, (long int)con);
1443 cmd_output->needs_tree_render = true;
1447 * Implementation of 'focus output <output>'.
1450 void cmd_focus_output(I3_CMD, char *name) {
1453 DLOG("name = %s\n", name);
1457 /* get the output */
1458 Output *current_output = NULL;
1461 TAILQ_FOREACH(current, &owindows, owindows)
1462 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1463 assert(current_output != NULL);
1465 output = get_output_from_string(current_output, name);
1468 LOG("No such output found.\n");
1473 /* get visible workspace on output */
1475 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1483 cmd_output->needs_tree_render = true;
1484 // XXX: default reply for now, make this a better reply
1489 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1492 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1497 if (!con_is_floating(focused)) {
1498 ELOG("Cannot change position. The window/container is not floating\n");
1503 ystr("Cannot change position. The window/container is not floating.");
1508 if (strcmp(method, "absolute") == 0) {
1509 focused->parent->rect.x = x;
1510 focused->parent->rect.y = y;
1512 DLOG("moving to absolute position %d %d\n", x, y);
1513 floating_maybe_reassign_ws(focused->parent);
1514 cmd_output->needs_tree_render = true;
1517 if (strcmp(method, "position") == 0) {
1518 Rect newrect = focused->parent->rect;
1520 DLOG("moving to position %d %d\n", x, y);
1524 floating_reposition(focused->parent, newrect);
1527 // XXX: default reply for now, make this a better reply
1532 * Implementation of 'move [window|container] [to] [absolute] position center
1535 void cmd_move_window_to_center(I3_CMD, char *method) {
1537 if (!con_is_floating(focused)) {
1538 ELOG("Cannot change position. The window/container is not floating\n");
1543 ystr("Cannot change position. The window/container is not floating.");
1547 if (strcmp(method, "absolute") == 0) {
1548 Rect *rect = &focused->parent->rect;
1550 DLOG("moving to absolute center\n");
1551 rect->x = croot->rect.width/2 - rect->width/2;
1552 rect->y = croot->rect.height/2 - rect->height/2;
1554 floating_maybe_reassign_ws(focused->parent);
1555 cmd_output->needs_tree_render = true;
1558 if (strcmp(method, "position") == 0) {
1559 Rect *wsrect = &con_get_workspace(focused)->rect;
1560 Rect newrect = focused->parent->rect;
1562 DLOG("moving to center\n");
1563 newrect.x = wsrect->width/2 - newrect.width/2;
1564 newrect.y = wsrect->height/2 - newrect.height/2;
1566 floating_reposition(focused->parent, newrect);
1569 // XXX: default reply for now, make this a better reply
1574 * Implementation of 'move scratchpad'.
1577 void cmd_move_scratchpad(I3_CMD) {
1578 DLOG("should move window to scratchpad\n");
1583 TAILQ_FOREACH(current, &owindows, owindows) {
1584 DLOG("matching: %p / %s\n", current->con, current->con->name);
1585 scratchpad_move(current->con);
1588 cmd_output->needs_tree_render = true;
1589 // XXX: default reply for now, make this a better reply
1594 * Implementation of 'scratchpad show'.
1597 void cmd_scratchpad_show(I3_CMD) {
1598 DLOG("should show scratchpad window\n");
1601 if (match_is_empty(current_match)) {
1602 scratchpad_show(NULL);
1604 TAILQ_FOREACH(current, &owindows, owindows) {
1605 DLOG("matching: %p / %s\n", current->con, current->con->name);
1606 scratchpad_show(current->con);
1610 cmd_output->needs_tree_render = true;
1611 // XXX: default reply for now, make this a better reply
1616 * Implementation of 'rename workspace <name> to <name>'
1619 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1620 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1622 Con *output, *workspace = NULL;
1623 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1624 GREP_FIRST(workspace, output_get_content(output),
1625 !strcasecmp(child->name, old_name));
1628 // TODO: we should include the old workspace name here and use yajl for
1629 // generating the reply.
1634 // TODO: better error message
1635 ystr("Old workspace not found");
1640 Con *check_dest = NULL;
1641 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1642 GREP_FIRST(check_dest, output_get_content(output),
1643 !strcasecmp(child->name, new_name));
1645 if (check_dest != NULL) {
1646 // TODO: we should include the new workspace name here and use yajl for
1647 // generating the reply.
1652 // TODO: better error message
1653 ystr("New workspace already exists");
1658 /* Change the name and try to parse it as a number. */
1659 FREE(workspace->name);
1660 workspace->name = sstrdup(new_name);
1661 char *endptr = NULL;
1662 long parsed_num = strtol(new_name, &endptr, 10);
1663 if (parsed_num == LONG_MIN ||
1664 parsed_num == LONG_MAX ||
1667 workspace->num = -1;
1668 else workspace->num = parsed_num;
1669 LOG("num = %d\n", workspace->num);
1671 /* By re-attaching, the sort order will be correct afterwards. */
1672 Con *previously_focused = focused;
1673 Con *parent = workspace->parent;
1674 con_detach(workspace);
1675 con_attach(workspace, parent, false);
1676 /* Restore the previous focus since con_attach messes with the focus. */
1677 con_focus(previously_focused);
1679 cmd_output->needs_tree_render = true;
1682 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");