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 /** When the command did not include match criteria (!), we use the currently
16 * focused command. Do not confuse this case with a command which included
17 * criteria but which did not match any windows. This macro has to be called in
20 #define HANDLE_EMPTY_MATCH do { \
21 if (match_is_empty(current_match)) { \
22 owindow *ow = smalloc(sizeof(owindow)); \
24 TAILQ_INIT(&owindows); \
25 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
29 static owindows_head owindows;
32 * Returns true if a is definitely greater than b (using the given epsilon)
35 static bool definitelyGreaterThan(float a, float b, float epsilon) {
36 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
40 * Returns an 'output' corresponding to one of left/right/down/up or a specific
44 static Output *get_output_from_string(Output *current_output, const char *output_str) {
47 if (strcasecmp(output_str, "left") == 0) {
48 output = get_output_next(D_LEFT, current_output);
50 output = get_output_most(D_RIGHT, current_output);
51 } else if (strcasecmp(output_str, "right") == 0) {
52 output = get_output_next(D_RIGHT, current_output);
54 output = get_output_most(D_LEFT, current_output);
55 } else if (strcasecmp(output_str, "up") == 0) {
56 output = get_output_next(D_UP, current_output);
58 output = get_output_most(D_DOWN, current_output);
59 } else if (strcasecmp(output_str, "down") == 0) {
60 output = get_output_next(D_DOWN, current_output);
62 output = get_output_most(D_UP, current_output);
63 } else output = get_output_by_name(output_str);
68 // This code is commented out because we might recycle it for popping up error
69 // messages on parser errors.
71 static pid_t migration_pid = -1;
74 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
75 * it exited (or could not be started, depending on the exit code).
78 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
79 ev_child_stop(EV_A_ watcher);
80 if (!WIFEXITED(watcher->rstatus)) {
81 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
85 int exitcode = WEXITSTATUS(watcher->rstatus);
86 printf("i3-nagbar process exited with status %d\n", exitcode);
88 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
94 /* We need ev >= 4 for the following code. Since it is not *that* important (it
95 * only makes sure that there are no i3-nagbar instances left behind) we still
96 * support old systems with libev 3. */
97 #if EV_VERSION_MAJOR >= 4
99 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
100 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
103 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
104 if (migration_pid != -1) {
105 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
106 kill(migration_pid, SIGKILL);
111 void cmd_MIGRATION_start_nagbar(void) {
112 if (migration_pid != -1) {
113 fprintf(stderr, "i3-nagbar already running.\n");
116 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
117 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
118 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
119 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
120 migration_pid = fork();
121 if (migration_pid == -1) {
122 warn("Could not fork()");
127 if (migration_pid == 0) {
129 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
131 NULL, /* will be replaced by the executable path */
135 "You found a parsing error. Please, please, please, report it!",
141 exec_i3_utility("i3-nagbar", argv);
145 /* install a child watcher */
146 ev_child *child = smalloc(sizeof(ev_child));
147 ev_child_init(child, &nagbar_exited, migration_pid, 0);
148 ev_child_start(main_loop, child);
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151 * only makes sure that there are no i3-nagbar instances left behind) we still
152 * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
154 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
156 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
157 ev_cleanup_init(cleanup, nagbar_cleanup);
158 ev_cleanup_start(main_loop, cleanup);
164 /*******************************************************************************
165 * Criteria functions.
166 ******************************************************************************/
169 * Initializes the specified 'Match' data structure and the initial state of
170 * commands.c for matching target windows of a command.
173 void cmd_criteria_init(I3_CMD) {
177 DLOG("Initializing criteria, current_match = %p\n", current_match);
178 match_init(current_match);
179 while (!TAILQ_EMPTY(&owindows)) {
180 ow = TAILQ_FIRST(&owindows);
181 TAILQ_REMOVE(&owindows, ow, owindows);
184 TAILQ_INIT(&owindows);
186 TAILQ_FOREACH(con, &all_cons, all_cons) {
187 ow = smalloc(sizeof(owindow));
189 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
194 * A match specification just finished (the closing square bracket was found),
195 * so we filter the list of owindows.
198 void cmd_criteria_match_windows(I3_CMD) {
199 owindow *next, *current;
201 DLOG("match specification finished, matching...\n");
202 /* copy the old list head to iterate through it and start with a fresh
203 * list which will contain only matching windows */
204 struct owindows_head old = owindows;
205 TAILQ_INIT(&owindows);
206 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
207 /* make a copy of the next pointer and advance the pointer to the
208 * next element as we are going to invalidate the element’s
209 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
211 next = TAILQ_NEXT(next, owindows);
213 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
214 if (current_match->con_id != NULL) {
215 if (current_match->con_id == current->con) {
216 DLOG("matches container!\n");
217 TAILQ_INSERT_TAIL(&owindows, current, owindows);
219 } else if (current_match->mark != NULL && current->con->mark != NULL &&
220 regex_matches(current_match->mark, current->con->mark)) {
221 DLOG("match by mark\n");
222 TAILQ_INSERT_TAIL(&owindows, current, owindows);
224 if (current->con->window == NULL)
226 if (match_matches_window(current_match, current->con->window)) {
227 DLOG("matches window!\n");
228 TAILQ_INSERT_TAIL(&owindows, current, owindows);
230 DLOG("doesnt match\n");
236 TAILQ_FOREACH(current, &owindows, owindows) {
237 DLOG("matching: %p / %s\n", current->con, current->con->name);
242 * Interprets a ctype=cvalue pair and adds it to the current match
246 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
247 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
249 if (strcmp(ctype, "class") == 0) {
250 current_match->class = regex_new(cvalue);
254 if (strcmp(ctype, "instance") == 0) {
255 current_match->instance = regex_new(cvalue);
259 if (strcmp(ctype, "window_role") == 0) {
260 current_match->role = regex_new(cvalue);
264 if (strcmp(ctype, "con_id") == 0) {
266 long parsed = strtol(cvalue, &end, 10);
267 if (parsed == LONG_MIN ||
268 parsed == LONG_MAX ||
270 (end && *end != '\0')) {
271 ELOG("Could not parse con id \"%s\"\n", cvalue);
273 current_match->con_id = (Con*)parsed;
274 printf("id as int = %p\n", current_match->con_id);
279 if (strcmp(ctype, "id") == 0) {
281 long parsed = strtol(cvalue, &end, 10);
282 if (parsed == LONG_MIN ||
283 parsed == LONG_MAX ||
285 (end && *end != '\0')) {
286 ELOG("Could not parse window id \"%s\"\n", cvalue);
288 current_match->id = parsed;
289 printf("window id as int = %d\n", current_match->id);
294 if (strcmp(ctype, "con_mark") == 0) {
295 current_match->mark = regex_new(cvalue);
299 if (strcmp(ctype, "title") == 0) {
300 current_match->title = regex_new(cvalue);
304 if (strcmp(ctype, "urgent") == 0) {
305 if (strcasecmp(cvalue, "latest") == 0 ||
306 strcasecmp(cvalue, "newest") == 0 ||
307 strcasecmp(cvalue, "recent") == 0 ||
308 strcasecmp(cvalue, "last") == 0) {
309 current_match->urgent = U_LATEST;
310 } else if (strcasecmp(cvalue, "oldest") == 0 ||
311 strcasecmp(cvalue, "first") == 0) {
312 current_match->urgent = U_OLDEST;
317 ELOG("Unknown criterion: %s\n", ctype);
321 * Implementation of 'move [window|container] [to] workspace
322 * next|prev|next_on_output|prev_on_output'.
325 void cmd_move_con_to_workspace(I3_CMD, char *which) {
328 DLOG("which=%s\n", which);
332 /* get the workspace */
334 if (strcmp(which, "next") == 0)
335 ws = workspace_next();
336 else if (strcmp(which, "prev") == 0)
337 ws = workspace_prev();
338 else if (strcmp(which, "next_on_output") == 0)
339 ws = workspace_next_on_output();
340 else if (strcmp(which, "prev_on_output") == 0)
341 ws = workspace_prev_on_output();
343 ELOG("BUG: called with which=%s\n", which);
344 cmd_output->json_output = sstrdup("{\"sucess\": false}");
348 TAILQ_FOREACH(current, &owindows, owindows) {
349 DLOG("matching: %p / %s\n", current->con, current->con->name);
350 con_move_to_workspace(current->con, ws, true, false);
353 cmd_output->needs_tree_render = true;
354 // XXX: default reply for now, make this a better reply
355 cmd_output->json_output = sstrdup("{\"success\": true}");
359 * Implementation of 'move [window|container] [to] workspace <name>'.
362 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
363 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
364 LOG("You cannot switch to the i3 internal workspaces.\n");
365 cmd_output->json_output = sstrdup("{\"sucess\": false}");
371 /* Error out early to not create a non-existing workspace (in
372 * workspace_get()) if we are not actually able to move anything. */
373 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
374 cmd_output->json_output = sstrdup("{\"sucess\": false}");
378 LOG("should move window to workspace %s\n", name);
379 /* get the workspace */
380 Con *ws = workspace_get(name, NULL);
384 TAILQ_FOREACH(current, &owindows, owindows) {
385 DLOG("matching: %p / %s\n", current->con, current->con->name);
386 con_move_to_workspace(current->con, ws, true, false);
389 cmd_output->needs_tree_render = true;
390 // XXX: default reply for now, make this a better reply
391 cmd_output->json_output = sstrdup("{\"success\": true}");
394 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
395 LOG("floating resize\n");
396 if (strcmp(direction, "up") == 0) {
397 floating_con->rect.y -= px;
398 floating_con->rect.height += px;
399 } else if (strcmp(direction, "down") == 0) {
400 floating_con->rect.height += px;
401 } else if (strcmp(direction, "left") == 0) {
402 floating_con->rect.x -= px;
403 floating_con->rect.width += px;
405 floating_con->rect.width += px;
409 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
410 LOG("tiling resize\n");
411 /* get the appropriate current container (skip stacked/tabbed cons) */
412 Con *current = focused;
413 while (current->parent->layout == L_STACKED ||
414 current->parent->layout == L_TABBED)
415 current = current->parent;
417 /* Then further go up until we find one with the matching orientation. */
418 orientation_t search_orientation =
419 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
421 while (current->type != CT_WORKSPACE &&
422 current->type != CT_FLOATING_CON &&
423 current->parent->orientation != search_orientation)
424 current = current->parent;
426 /* get the default percentage */
427 int children = con_num_children(current->parent);
429 LOG("ins. %d children\n", children);
430 double percentage = 1.0 / children;
431 LOG("default percentage = %f\n", percentage);
433 orientation_t orientation = current->parent->orientation;
435 if ((orientation == HORIZ &&
436 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
437 (orientation == VERT &&
438 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
439 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
440 (orientation == HORIZ ? "horizontal" : "vertical"));
441 cmd_output->json_output = sstrdup("{\"sucess\": false}");
445 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
446 other = TAILQ_PREV(current, nodes_head, nodes);
448 other = TAILQ_NEXT(current, nodes);
450 if (other == TAILQ_END(workspaces)) {
451 LOG("No other container in this direction found, cannot resize.\n");
452 cmd_output->json_output = sstrdup("{\"sucess\": false}");
455 LOG("other->percent = %f\n", other->percent);
456 LOG("current->percent before = %f\n", current->percent);
457 if (current->percent == 0.0)
458 current->percent = percentage;
459 if (other->percent == 0.0)
460 other->percent = percentage;
461 double new_current_percent = current->percent + ((double)ppt / 100.0);
462 double new_other_percent = other->percent - ((double)ppt / 100.0);
463 LOG("new_current_percent = %f\n", new_current_percent);
464 LOG("new_other_percent = %f\n", new_other_percent);
465 /* Ensure that the new percentages are positive and greater than
466 * 0.05 to have a reasonable minimum size. */
467 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
468 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
469 current->percent += ((double)ppt / 100.0);
470 other->percent -= ((double)ppt / 100.0);
471 LOG("current->percent after = %f\n", current->percent);
472 LOG("other->percent after = %f\n", other->percent);
474 LOG("Not resizing, already at minimum size\n");
478 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
479 LOG("width/height resize\n");
480 /* get the appropriate current container (skip stacked/tabbed cons) */
481 Con *current = focused;
482 while (current->parent->layout == L_STACKED ||
483 current->parent->layout == L_TABBED)
484 current = current->parent;
486 /* Then further go up until we find one with the matching orientation. */
487 orientation_t search_orientation =
488 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
490 while (current->type != CT_WORKSPACE &&
491 current->type != CT_FLOATING_CON &&
492 current->parent->orientation != search_orientation)
493 current = current->parent;
495 /* get the default percentage */
496 int children = con_num_children(current->parent);
497 LOG("ins. %d children\n", children);
498 double percentage = 1.0 / children;
499 LOG("default percentage = %f\n", percentage);
501 orientation_t orientation = current->parent->orientation;
503 if ((orientation == HORIZ &&
504 strcmp(direction, "height") == 0) ||
505 (orientation == VERT &&
506 strcmp(direction, "width") == 0)) {
507 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
508 (orientation == HORIZ ? "horizontal" : "vertical"));
509 cmd_output->json_output = sstrdup("{\"sucess\": false}");
514 LOG("This is the only container, cannot resize.\n");
515 cmd_output->json_output = sstrdup("{\"sucess\": false}");
519 /* Ensure all the other children have a percentage set. */
521 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
522 LOG("child->percent = %f (child %p)\n", child->percent, child);
523 if (child->percent == 0.0)
524 child->percent = percentage;
527 double new_current_percent = current->percent + ((double)ppt / 100.0);
528 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
529 LOG("new_current_percent = %f\n", new_current_percent);
530 LOG("subtract_percent = %f\n", subtract_percent);
531 /* Ensure that the new percentages are positive and greater than
532 * 0.05 to have a reasonable minimum size. */
533 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
534 if (child == current)
536 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
537 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
538 cmd_output->json_output = sstrdup("{\"sucess\": false}");
542 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
543 LOG("Not resizing, already at minimum size\n");
544 cmd_output->json_output = sstrdup("{\"sucess\": false}");
548 current->percent += ((double)ppt / 100.0);
549 LOG("current->percent after = %f\n", current->percent);
551 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
552 if (child == current)
554 child->percent -= subtract_percent;
555 LOG("child->percent after (%p) = %f\n", child, child->percent);
560 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
563 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
564 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
565 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
566 // 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
567 int px = atoi(resize_px);
568 int ppt = atoi(resize_ppt);
569 if (strcmp(way, "shrink") == 0) {
575 if ((floating_con = con_inside_floating(focused))) {
576 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
578 if (strcmp(direction, "width") == 0 ||
579 strcmp(direction, "height") == 0)
580 cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
581 else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
584 cmd_output->needs_tree_render = true;
585 // XXX: default reply for now, make this a better reply
586 cmd_output->json_output = sstrdup("{\"success\": true}");
590 * Implementation of 'border normal|none|1pixel|toggle'.
593 void cmd_border(I3_CMD, char *border_style_str) {
594 DLOG("border style should be changed to %s\n", border_style_str);
599 TAILQ_FOREACH(current, &owindows, owindows) {
600 DLOG("matching: %p / %s\n", current->con, current->con->name);
601 int border_style = current->con->border_style;
602 if (strcmp(border_style_str, "toggle") == 0) {
606 if (strcmp(border_style_str, "normal") == 0)
607 border_style = BS_NORMAL;
608 else if (strcmp(border_style_str, "none") == 0)
609 border_style = BS_NONE;
610 else if (strcmp(border_style_str, "1pixel") == 0)
611 border_style = BS_1PIXEL;
613 ELOG("BUG: called with border_style=%s\n", border_style_str);
614 cmd_output->json_output = sstrdup("{\"sucess\": false}");
618 con_set_border_style(current->con, border_style);
621 cmd_output->needs_tree_render = true;
622 // XXX: default reply for now, make this a better reply
623 cmd_output->json_output = sstrdup("{\"success\": true}");
627 * Implementation of 'nop <comment>'.
630 void cmd_nop(I3_CMD, char *comment) {
631 LOG("-------------------------------------------------\n");
632 LOG(" NOP: %s\n", comment);
633 LOG("-------------------------------------------------\n");
637 * Implementation of 'append_layout <path>'.
640 void cmd_append_layout(I3_CMD, char *path) {
641 LOG("Appending layout \"%s\"\n", path);
642 tree_append_json(path);
644 cmd_output->needs_tree_render = true;
645 // XXX: default reply for now, make this a better reply
646 cmd_output->json_output = sstrdup("{\"success\": true}");
650 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
653 void cmd_workspace(I3_CMD, char *which) {
656 DLOG("which=%s\n", which);
658 if (strcmp(which, "next") == 0)
659 ws = workspace_next();
660 else if (strcmp(which, "prev") == 0)
661 ws = workspace_prev();
662 else if (strcmp(which, "next_on_output") == 0)
663 ws = workspace_next_on_output();
664 else if (strcmp(which, "prev_on_output") == 0)
665 ws = workspace_prev_on_output();
667 ELOG("BUG: called with which=%s\n", which);
668 cmd_output->json_output = sstrdup("{\"sucess\": false}");
674 cmd_output->needs_tree_render = true;
675 // XXX: default reply for now, make this a better reply
676 cmd_output->json_output = sstrdup("{\"success\": true}");
680 * Implementation of 'workspace number <number>'
683 void cmd_workspace_number(I3_CMD, char *which) {
684 Con *output, *workspace;
687 long parsed_num = strtol(which, &endptr, 10);
688 if (parsed_num == LONG_MIN ||
689 parsed_num == LONG_MAX ||
692 LOG("Could not parse \"%s\" as a number.\n", which);
693 cmd_output->json_output = sstrdup("{\"success\": false, "
694 "\"error\": \"Could not parse number\"}");
698 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
699 GREP_FIRST(workspace, output_get_content(output),
700 child->num == parsed_num);
703 LOG("There is no workspace with number %d.\n", parsed_num);
704 cmd_output->json_output = sstrdup("{\"success\": false, "
705 "\"error\": \"No such workspace\"}");
709 workspace_show(workspace);
711 cmd_output->needs_tree_render = true;
712 // XXX: default reply for now, make this a better reply
713 cmd_output->json_output = sstrdup("{\"success\": true}");
717 * Implementation of 'workspace back_and_forth'.
720 void cmd_workspace_back_and_forth(I3_CMD) {
721 workspace_back_and_forth();
723 cmd_output->needs_tree_render = true;
724 // XXX: default reply for now, make this a better reply
725 cmd_output->json_output = sstrdup("{\"success\": true}");
729 * Implementation of 'workspace <name>'
732 void cmd_workspace_name(I3_CMD, char *name) {
733 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
734 LOG("You cannot switch to the i3 internal workspaces.\n");
735 cmd_output->json_output = sstrdup("{\"sucess\": false}");
739 DLOG("should switch to workspace %s\n", name);
741 Con *ws = con_get_workspace(focused);
743 /* Check if the command wants to switch to the current workspace */
744 if (strcmp(ws->name, name) == 0) {
745 DLOG("This workspace is already focused.\n");
746 if (config.workspace_auto_back_and_forth) {
747 workspace_back_and_forth();
750 cmd_output->json_output = sstrdup("{\"sucess\": false}");
754 workspace_show_by_name(name);
756 cmd_output->needs_tree_render = true;
757 // XXX: default reply for now, make this a better reply
758 cmd_output->json_output = sstrdup("{\"success\": true}");
762 * Implementation of 'mark <mark>'
765 void cmd_mark(I3_CMD, char *mark) {
766 DLOG("Clearing all windows which have that mark first\n");
769 TAILQ_FOREACH(con, &all_cons, all_cons) {
770 if (con->mark && strcmp(con->mark, mark) == 0)
774 DLOG("marking window with str %s\n", mark);
779 TAILQ_FOREACH(current, &owindows, owindows) {
780 DLOG("matching: %p / %s\n", current->con, current->con->name);
781 current->con->mark = sstrdup(mark);
784 cmd_output->needs_tree_render = true;
785 // XXX: default reply for now, make this a better reply
786 cmd_output->json_output = sstrdup("{\"success\": true}");
790 * Implementation of 'mode <string>'.
793 void cmd_mode(I3_CMD, char *mode) {
794 DLOG("mode=%s\n", mode);
797 // XXX: default reply for now, make this a better reply
798 cmd_output->json_output = sstrdup("{\"success\": true}");
802 * Implementation of 'move [window|container] [to] output <str>'.
805 void cmd_move_con_to_output(I3_CMD, char *name) {
808 DLOG("should move window to output %s\n", name);
813 Output *current_output = NULL;
816 // TODO: fix the handling of criteria
817 TAILQ_FOREACH(current, &owindows, owindows)
818 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
820 assert(current_output != NULL);
822 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
823 if (strcasecmp(name, "up") == 0)
824 output = get_output_next(D_UP, current_output);
825 else if (strcasecmp(name, "down") == 0)
826 output = get_output_next(D_DOWN, current_output);
827 else if (strcasecmp(name, "left") == 0)
828 output = get_output_next(D_LEFT, current_output);
829 else if (strcasecmp(name, "right") == 0)
830 output = get_output_next(D_RIGHT, current_output);
832 output = get_output_by_name(name);
835 LOG("No such output found.\n");
836 cmd_output->json_output = sstrdup("{\"sucess\": false}");
840 /* get visible workspace on output */
842 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
844 cmd_output->json_output = sstrdup("{\"sucess\": false}");
848 TAILQ_FOREACH(current, &owindows, owindows) {
849 DLOG("matching: %p / %s\n", current->con, current->con->name);
850 con_move_to_workspace(current->con, ws, true, false);
853 cmd_output->needs_tree_render = true;
854 // XXX: default reply for now, make this a better reply
855 cmd_output->json_output = sstrdup("{\"success\": true}");
859 * Implementation of 'floating enable|disable|toggle'
862 void cmd_floating(I3_CMD, char *floating_mode) {
865 DLOG("floating_mode=%s\n", floating_mode);
869 TAILQ_FOREACH(current, &owindows, owindows) {
870 DLOG("matching: %p / %s\n", current->con, current->con->name);
871 if (strcmp(floating_mode, "toggle") == 0) {
872 DLOG("should toggle mode\n");
873 toggle_floating_mode(current->con, false);
875 DLOG("should switch mode to %s\n", floating_mode);
876 if (strcmp(floating_mode, "enable") == 0) {
877 floating_enable(current->con, false);
879 floating_disable(current->con, false);
884 cmd_output->needs_tree_render = true;
885 // XXX: default reply for now, make this a better reply
886 cmd_output->json_output = sstrdup("{\"success\": true}");
890 * Implementation of 'move workspace to [output] <str>'.
893 void cmd_move_workspace_to_output(I3_CMD, char *name) {
894 DLOG("should move workspace to output %s\n", name);
899 TAILQ_FOREACH(current, &owindows, owindows) {
900 Output *current_output = get_output_containing(current->con->rect.x,
901 current->con->rect.y);
902 Output *output = get_output_from_string(current_output, name);
904 LOG("No such output\n");
905 cmd_output->json_output = sstrdup("{\"sucess\": false}");
909 Con *content = output_get_content(output->con);
910 LOG("got output %p with content %p\n", output, content);
912 Con *ws = con_get_workspace(current->con);
913 LOG("should move workspace %p / %s\n", ws, ws->name);
915 if (con_num_children(ws->parent) == 1) {
916 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
918 /* check if we can find a workspace assigned to this output */
919 bool used_assignment = false;
920 struct Workspace_Assignment *assignment;
921 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
922 if (strcmp(assignment->output, current_output->name) != 0)
925 /* check if this workspace is already attached to the tree */
926 Con *workspace = NULL, *out;
927 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
928 GREP_FIRST(workspace, output_get_content(out),
929 !strcasecmp(child->name, assignment->name));
930 if (workspace != NULL)
933 /* so create the workspace referenced to by this assignment */
934 LOG("Creating workspace from assignment %s.\n", assignment->name);
935 workspace_get(assignment->name, NULL);
936 used_assignment = true;
940 /* if we couldn't create the workspace using an assignment, create
941 * it on the output */
942 if (!used_assignment)
943 create_workspace_on_output(current_output, ws->parent);
945 /* notify the IPC listeners */
946 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
949 /* detach from the old output and attach to the new output */
950 bool workspace_was_visible = workspace_is_visible(ws);
951 Con *old_content = ws->parent;
953 if (workspace_was_visible) {
954 /* The workspace which we just detached was visible, so focus
955 * the next one in the focus-stack. */
956 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
957 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
958 workspace_show(focus_ws);
960 con_attach(ws, content, false);
962 /* fix the coordinates of the floating containers */
964 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
965 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
967 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
968 if (workspace_was_visible) {
969 /* Focus the moved workspace on the destination output. */
974 cmd_output->needs_tree_render = true;
975 // XXX: default reply for now, make this a better reply
976 cmd_output->json_output = sstrdup("{\"success\": true}");
980 * Implementation of 'split v|h|vertical|horizontal'.
983 void cmd_split(I3_CMD, char *direction) {
984 /* TODO: use matches */
985 LOG("splitting in direction %c\n", direction[0]);
986 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
988 cmd_output->needs_tree_render = true;
989 // XXX: default reply for now, make this a better reply
990 cmd_output->json_output = sstrdup("{\"success\": true}");
994 * Implementaiton of 'kill [window|client]'.
997 void cmd_kill(I3_CMD, char *kill_mode_str) {
998 if (kill_mode_str == NULL)
999 kill_mode_str = "window";
1002 DLOG("kill_mode=%s\n", kill_mode_str);
1005 if (strcmp(kill_mode_str, "window") == 0)
1006 kill_mode = KILL_WINDOW;
1007 else if (strcmp(kill_mode_str, "client") == 0)
1008 kill_mode = KILL_CLIENT;
1010 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1011 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1015 /* check if the match is empty, not if the result is empty */
1016 if (match_is_empty(current_match))
1017 tree_close_con(kill_mode);
1019 TAILQ_FOREACH(current, &owindows, owindows) {
1020 DLOG("matching: %p / %s\n", current->con, current->con->name);
1021 tree_close(current->con, kill_mode, false, false);
1025 cmd_output->needs_tree_render = true;
1026 // XXX: default reply for now, make this a better reply
1027 cmd_output->json_output = sstrdup("{\"success\": true}");
1031 * Implementation of 'exec [--no-startup-id] <command>'.
1034 void cmd_exec(I3_CMD, char *nosn, char *command) {
1035 bool no_startup_id = (nosn != NULL);
1037 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1038 start_application(command, no_startup_id);
1040 // XXX: default reply for now, make this a better reply
1041 cmd_output->json_output = sstrdup("{\"success\": true}");
1045 * Implementation of 'focus left|right|up|down'.
1048 void cmd_focus_direction(I3_CMD, char *direction) {
1050 focused->type != CT_WORKSPACE &&
1051 focused->fullscreen_mode != CF_NONE) {
1052 LOG("Cannot change focus while in fullscreen mode.\n");
1053 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1057 DLOG("direction = *%s*\n", direction);
1059 if (strcmp(direction, "left") == 0)
1060 tree_next('p', HORIZ);
1061 else if (strcmp(direction, "right") == 0)
1062 tree_next('n', HORIZ);
1063 else if (strcmp(direction, "up") == 0)
1064 tree_next('p', VERT);
1065 else if (strcmp(direction, "down") == 0)
1066 tree_next('n', VERT);
1068 ELOG("Invalid focus direction (%s)\n", direction);
1069 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1073 cmd_output->needs_tree_render = true;
1074 // XXX: default reply for now, make this a better reply
1075 cmd_output->json_output = sstrdup("{\"success\": true}");
1079 * Implementation of 'focus tiling|floating|mode_toggle'.
1082 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1084 focused->type != CT_WORKSPACE &&
1085 focused->fullscreen_mode != CF_NONE) {
1086 LOG("Cannot change focus while in fullscreen mode.\n");
1087 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1091 DLOG("window_mode = %s\n", window_mode);
1093 Con *ws = con_get_workspace(focused);
1096 if (strcmp(window_mode, "mode_toggle") == 0) {
1097 current = TAILQ_FIRST(&(ws->focus_head));
1098 if (current != NULL && current->type == CT_FLOATING_CON)
1099 window_mode = "tiling";
1100 else window_mode = "floating";
1102 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1103 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1104 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1107 con_focus(con_descend_focused(current));
1112 cmd_output->needs_tree_render = true;
1113 // XXX: default reply for now, make this a better reply
1114 cmd_output->json_output = sstrdup("{\"success\": true}");
1118 * Implementation of 'focus parent|child'.
1121 void cmd_focus_level(I3_CMD, char *level) {
1123 focused->type != CT_WORKSPACE &&
1124 focused->fullscreen_mode != CF_NONE) {
1125 LOG("Cannot change focus while in fullscreen mode.\n");
1126 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1130 DLOG("level = %s\n", level);
1132 if (strcmp(level, "parent") == 0)
1136 cmd_output->needs_tree_render = true;
1137 // XXX: default reply for now, make this a better reply
1138 cmd_output->json_output = sstrdup("{\"success\": true}");
1142 * Implementation of 'focus'.
1145 void cmd_focus(I3_CMD) {
1146 DLOG("current_match = %p\n", current_match);
1148 focused->type != CT_WORKSPACE &&
1149 focused->fullscreen_mode != CF_NONE) {
1150 LOG("Cannot change focus while in fullscreen mode.\n");
1151 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1157 if (match_is_empty(current_match)) {
1158 ELOG("You have to specify which window/container should be focused.\n");
1159 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1161 sasprintf(&(cmd_output->json_output),
1162 "{\"success\":false, \"error\":\"You have to "
1163 "specify which window/container should be focused\"}");
1168 TAILQ_FOREACH(current, &owindows, owindows) {
1169 Con *ws = con_get_workspace(current->con);
1170 /* If no workspace could be found, this was a dock window.
1171 * Just skip it, you cannot focus dock windows. */
1175 /* If the container is not on the current workspace,
1176 * workspace_show() will switch to a different workspace and (if
1177 * enabled) trigger a mouse pointer warp to the currently focused
1178 * container (!) on the target workspace.
1180 * Therefore, before calling workspace_show(), we make sure that
1181 * 'current' will be focused on the workspace. However, we cannot
1182 * just con_focus(current) because then the pointer will not be
1183 * warped at all (the code thinks we are already there).
1185 * So we focus 'current' to make it the currently focused window of
1186 * the target workspace, then revert focus. */
1187 Con *currently_focused = focused;
1188 con_focus(current->con);
1189 con_focus(currently_focused);
1191 /* Now switch to the workspace, then focus */
1193 LOG("focusing %p / %s\n", current->con, current->con->name);
1194 con_focus(current->con);
1199 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1200 "while only exactly one container can be focused at a time.\n", count);
1202 cmd_output->needs_tree_render = true;
1203 // XXX: default reply for now, make this a better reply
1204 cmd_output->json_output = sstrdup("{\"success\": true}");
1208 * Implementation of 'fullscreen [global]'.
1211 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1212 if (fullscreen_mode == NULL)
1213 fullscreen_mode = "output";
1214 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1219 TAILQ_FOREACH(current, &owindows, owindows) {
1220 printf("matching: %p / %s\n", current->con, current->con->name);
1221 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1224 cmd_output->needs_tree_render = true;
1225 // XXX: default reply for now, make this a better reply
1226 cmd_output->json_output = sstrdup("{\"success\": true}");
1230 * Implementation of 'move <direction> [<pixels> [px]]'.
1233 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1234 // 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
1235 int px = atoi(move_px);
1237 /* TODO: make 'move' work with criteria. */
1238 DLOG("moving in direction %s, px %s\n", direction, move_px);
1239 if (con_is_floating(focused)) {
1240 DLOG("floating move with %d pixels\n", px);
1241 Rect newrect = focused->parent->rect;
1242 if (strcmp(direction, "left") == 0) {
1244 } else if (strcmp(direction, "right") == 0) {
1246 } else if (strcmp(direction, "up") == 0) {
1248 } else if (strcmp(direction, "down") == 0) {
1251 floating_reposition(focused->parent, newrect);
1253 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1254 (strcmp(direction, "left") == 0 ? D_LEFT :
1255 (strcmp(direction, "up") == 0 ? D_UP :
1257 cmd_output->needs_tree_render = true;
1260 // XXX: default reply for now, make this a better reply
1261 cmd_output->json_output = sstrdup("{\"success\": true}");
1265 * Implementation of 'layout default|stacked|stacking|tabbed'.
1268 void cmd_layout(I3_CMD, char *layout_str) {
1269 if (strcmp(layout_str, "stacking") == 0)
1270 layout_str = "stacked";
1271 DLOG("changing layout to %s\n", layout_str);
1273 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1274 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1277 /* check if the match is empty, not if the result is empty */
1278 if (match_is_empty(current_match))
1279 con_set_layout(focused->parent, layout);
1281 TAILQ_FOREACH(current, &owindows, owindows) {
1282 DLOG("matching: %p / %s\n", current->con, current->con->name);
1283 con_set_layout(current->con, layout);
1287 cmd_output->needs_tree_render = true;
1288 // XXX: default reply for now, make this a better reply
1289 cmd_output->json_output = sstrdup("{\"success\": true}");
1293 * Implementaiton of 'exit'.
1296 void cmd_exit(I3_CMD) {
1297 LOG("Exiting due to user command.\n");
1304 * Implementaiton of 'reload'.
1307 void cmd_reload(I3_CMD) {
1309 kill_configerror_nagbar(false);
1310 load_configuration(conn, NULL, true);
1312 /* Send an IPC event just in case the ws names have changed */
1313 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1315 // XXX: default reply for now, make this a better reply
1316 cmd_output->json_output = sstrdup("{\"success\": true}");
1320 * Implementaiton of 'restart'.
1323 void cmd_restart(I3_CMD) {
1324 LOG("restarting i3\n");
1327 // XXX: default reply for now, make this a better reply
1328 cmd_output->json_output = sstrdup("{\"success\": true}");
1332 * Implementaiton of 'open'.
1335 void cmd_open(I3_CMD) {
1336 LOG("opening new container\n");
1337 Con *con = tree_open_con(NULL, NULL);
1339 sasprintf(&(cmd_output->json_output),
1340 "{\"success\":true, \"id\":%ld}", (long int)con);
1342 cmd_output->needs_tree_render = true;
1346 * Implementation of 'focus output <output>'.
1349 void cmd_focus_output(I3_CMD, char *name) {
1352 DLOG("name = %s\n", name);
1356 /* get the output */
1357 Output *current_output = NULL;
1360 TAILQ_FOREACH(current, &owindows, owindows)
1361 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1362 assert(current_output != NULL);
1364 output = get_output_from_string(current_output, name);
1367 LOG("No such output found.\n");
1368 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1372 /* get visible workspace on output */
1374 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1376 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1382 cmd_output->needs_tree_render = true;
1383 // XXX: default reply for now, make this a better reply
1384 cmd_output->json_output = sstrdup("{\"success\": true}");
1388 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1391 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1396 if (!con_is_floating(focused)) {
1397 ELOG("Cannot change position. The window/container is not floating\n");
1398 sasprintf(&(cmd_output->json_output),
1399 "{\"success\":false, \"error\":\"Cannot change position. "
1400 "The window/container is not floating.\"}");
1404 if (strcmp(method, "absolute") == 0) {
1405 focused->parent->rect.x = x;
1406 focused->parent->rect.y = y;
1408 DLOG("moving to absolute position %d %d\n", x, y);
1409 floating_maybe_reassign_ws(focused->parent);
1410 cmd_output->needs_tree_render = true;
1413 if (strcmp(method, "position") == 0) {
1414 Rect newrect = focused->parent->rect;
1416 DLOG("moving to position %d %d\n", x, y);
1420 floating_reposition(focused->parent, newrect);
1423 // XXX: default reply for now, make this a better reply
1424 cmd_output->json_output = sstrdup("{\"success\": true}");
1428 * Implementation of 'move [window|container] [to] [absolute] position center
1431 void cmd_move_window_to_center(I3_CMD, char *method) {
1433 if (!con_is_floating(focused)) {
1434 ELOG("Cannot change position. The window/container is not floating\n");
1435 sasprintf(&(cmd_output->json_output),
1436 "{\"success\":false, \"error\":\"Cannot change position. "
1437 "The window/container is not floating.\"}");
1440 if (strcmp(method, "absolute") == 0) {
1441 Rect *rect = &focused->parent->rect;
1443 DLOG("moving to absolute center\n");
1444 rect->x = croot->rect.width/2 - rect->width/2;
1445 rect->y = croot->rect.height/2 - rect->height/2;
1447 floating_maybe_reassign_ws(focused->parent);
1448 cmd_output->needs_tree_render = true;
1451 if (strcmp(method, "position") == 0) {
1452 Rect *wsrect = &con_get_workspace(focused)->rect;
1453 Rect newrect = focused->parent->rect;
1455 DLOG("moving to center\n");
1456 newrect.x = wsrect->width/2 - newrect.width/2;
1457 newrect.y = wsrect->height/2 - newrect.height/2;
1459 floating_reposition(focused->parent, newrect);
1462 // XXX: default reply for now, make this a better reply
1463 cmd_output->json_output = sstrdup("{\"success\": true}");
1467 * Implementation of 'move scratchpad'.
1470 void cmd_move_scratchpad(I3_CMD) {
1471 DLOG("should move window to scratchpad\n");
1476 TAILQ_FOREACH(current, &owindows, owindows) {
1477 DLOG("matching: %p / %s\n", current->con, current->con->name);
1478 scratchpad_move(current->con);
1481 cmd_output->needs_tree_render = true;
1482 // XXX: default reply for now, make this a better reply
1483 cmd_output->json_output = sstrdup("{\"success\": true}");
1487 * Implementation of 'scratchpad show'.
1490 void cmd_scratchpad_show(I3_CMD) {
1491 DLOG("should show scratchpad window\n");
1494 if (match_is_empty(current_match)) {
1495 scratchpad_show(NULL);
1497 TAILQ_FOREACH(current, &owindows, owindows) {
1498 DLOG("matching: %p / %s\n", current->con, current->con->name);
1499 scratchpad_show(current->con);
1503 cmd_output->needs_tree_render = true;
1504 // XXX: default reply for now, make this a better reply
1505 cmd_output->json_output = sstrdup("{\"success\": true}");