2 #define I3__FILE__ "commands.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * commands.c: all command functions (see commands_parser.c)
18 // Macros to make the YAJL API a bit easier to use.
19 #define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
20 #define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
21 #define ysuccess(success) \
23 if (cmd_output->json_gen != NULL) { \
30 #define yerror(message) \
32 if (cmd_output->json_gen != NULL) { \
42 /** When the command did not include match criteria (!), we use the currently
43 * focused container. Do not confuse this case with a command which included
44 * criteria but which did not match any windows. This macro has to be called in
47 #define HANDLE_EMPTY_MATCH \
49 if (match_is_empty(current_match)) { \
50 owindow *ow = smalloc(sizeof(owindow)); \
52 TAILQ_INIT(&owindows); \
53 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
58 * Returns true if a is definitely greater than b (using the given epsilon)
61 static bool definitelyGreaterThan(float a, float b, float epsilon) {
62 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
66 * Returns an 'output' corresponding to one of left/right/down/up or a specific
70 static Output *get_output_from_string(Output *current_output, const char *output_str) {
73 if (strcasecmp(output_str, "left") == 0)
74 output = get_output_next_wrap(D_LEFT, current_output);
75 else if (strcasecmp(output_str, "right") == 0)
76 output = get_output_next_wrap(D_RIGHT, current_output);
77 else if (strcasecmp(output_str, "up") == 0)
78 output = get_output_next_wrap(D_UP, current_output);
79 else if (strcasecmp(output_str, "down") == 0)
80 output = get_output_next_wrap(D_DOWN, current_output);
82 output = get_output_by_name(output_str);
88 * Returns the output containing the given container.
90 static Output *get_output_of_con(Con *con) {
91 Con *output_con = con_get_output(con);
92 Output *output = get_output_by_name(output_con->name);
93 assert(output != NULL);
99 * Checks whether we switched to a new workspace and returns false in that case,
100 * signaling that further workspace switching should be done by the calling function
101 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
102 * and return true, signaling that no further workspace switching should occur in the calling function.
105 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
106 Con *ws = con_get_workspace(focused);
108 /* If we switched to a different workspace, do nothing */
109 if (strcmp(ws->name, name) != 0)
112 DLOG("This workspace is already focused.\n");
113 if (config.workspace_auto_back_and_forth) {
114 workspace_back_and_forth();
115 cmd_output->needs_tree_render = true;
121 * Return the passed workspace unless it is the current one and auto back and
122 * forth is enabled, in which case the back_and_forth workspace is returned.
124 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
127 if (!config.workspace_auto_back_and_forth)
130 current = con_get_workspace(focused);
132 if (current == workspace) {
133 baf = workspace_back_and_forth_get();
135 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
143 // This code is commented out because we might recycle it for popping up error
144 // messages on parser errors.
146 static pid_t migration_pid = -1;
149 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
150 * it exited (or could not be started, depending on the exit code).
153 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
154 ev_child_stop(EV_A_ watcher);
155 if (!WIFEXITED(watcher->rstatus)) {
156 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
160 int exitcode = WEXITSTATUS(watcher->rstatus);
161 printf("i3-nagbar process exited with status %d\n", exitcode);
163 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
169 /* We need ev >= 4 for the following code. Since it is not *that* important (it
170 * only makes sure that there are no i3-nagbar instances left behind) we still
171 * support old systems with libev 3. */
172 #if EV_VERSION_MAJOR >= 4
174 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
175 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
178 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
179 if (migration_pid != -1) {
180 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
181 kill(migration_pid, SIGKILL);
186 void cmd_MIGRATION_start_nagbar(void) {
187 if (migration_pid != -1) {
188 fprintf(stderr, "i3-nagbar already running.\n");
191 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
192 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
193 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
194 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
195 migration_pid = fork();
196 if (migration_pid == -1) {
197 warn("Could not fork()");
202 if (migration_pid == 0) {
204 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
206 NULL, /* will be replaced by the executable path */
210 "You found a parsing error. Please, please, please, report it!",
216 exec_i3_utility("i3-nagbar", argv);
220 /* install a child watcher */
221 ev_child *child = smalloc(sizeof(ev_child));
222 ev_child_init(child, &nagbar_exited, migration_pid, 0);
223 ev_child_start(main_loop, child);
225 /* We need ev >= 4 for the following code. Since it is not *that* important (it
226 * only makes sure that there are no i3-nagbar instances left behind) we still
227 * support old systems with libev 3. */
228 #if EV_VERSION_MAJOR >= 4
229 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
231 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
232 ev_cleanup_init(cleanup, nagbar_cleanup);
233 ev_cleanup_start(main_loop, cleanup);
239 /*******************************************************************************
240 * Criteria functions.
241 ******************************************************************************/
244 * Helper data structure for an operation window (window on which the operation
245 * will be performed). Used to build the TAILQ owindows.
248 typedef struct owindow {
250 TAILQ_ENTRY(owindow) owindows;
253 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
255 static owindows_head owindows;
258 * Initializes the specified 'Match' data structure and the initial state of
259 * commands.c for matching target windows of a command.
262 void cmd_criteria_init(I3_CMD) {
266 DLOG("Initializing criteria, current_match = %p\n", current_match);
267 match_free(current_match);
268 match_init(current_match);
269 while (!TAILQ_EMPTY(&owindows)) {
270 ow = TAILQ_FIRST(&owindows);
271 TAILQ_REMOVE(&owindows, ow, owindows);
274 TAILQ_INIT(&owindows);
276 TAILQ_FOREACH(con, &all_cons, all_cons) {
277 ow = smalloc(sizeof(owindow));
279 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
284 * A match specification just finished (the closing square bracket was found),
285 * so we filter the list of owindows.
288 void cmd_criteria_match_windows(I3_CMD) {
289 owindow *next, *current;
291 DLOG("match specification finished, matching...\n");
292 /* copy the old list head to iterate through it and start with a fresh
293 * list which will contain only matching windows */
294 struct owindows_head old = owindows;
295 TAILQ_INIT(&owindows);
296 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
297 /* make a copy of the next pointer and advance the pointer to the
298 * next element as we are going to invalidate the element’s
299 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
301 next = TAILQ_NEXT(next, owindows);
303 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
304 if (current_match->con_id != NULL) {
305 if (current_match->con_id == current->con) {
306 DLOG("matches container!\n");
307 TAILQ_INSERT_TAIL(&owindows, current, owindows);
309 DLOG("doesnt match\n");
312 } else if (current_match->mark != NULL && current->con->mark != NULL &&
313 regex_matches(current_match->mark, current->con->mark)) {
314 DLOG("match by mark\n");
315 TAILQ_INSERT_TAIL(&owindows, current, owindows);
317 if (current->con->window && match_matches_window(current_match, current->con->window)) {
318 DLOG("matches window!\n");
319 TAILQ_INSERT_TAIL(&owindows, current, owindows);
321 DLOG("doesnt match\n");
327 TAILQ_FOREACH(current, &owindows, owindows) {
328 DLOG("matching: %p / %s\n", current->con, current->con->name);
333 * Interprets a ctype=cvalue pair and adds it to the current match
337 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
338 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
340 if (strcmp(ctype, "class") == 0) {
341 current_match->class = regex_new(cvalue);
345 if (strcmp(ctype, "instance") == 0) {
346 current_match->instance = regex_new(cvalue);
350 if (strcmp(ctype, "window_role") == 0) {
351 current_match->window_role = regex_new(cvalue);
355 if (strcmp(ctype, "con_id") == 0) {
357 long parsed = strtol(cvalue, &end, 10);
358 if (parsed == LONG_MIN ||
359 parsed == LONG_MAX ||
361 (end && *end != '\0')) {
362 ELOG("Could not parse con id \"%s\"\n", cvalue);
364 current_match->con_id = (Con *)parsed;
365 DLOG("id as int = %p\n", current_match->con_id);
370 if (strcmp(ctype, "id") == 0) {
372 long parsed = strtol(cvalue, &end, 10);
373 if (parsed == LONG_MIN ||
374 parsed == LONG_MAX ||
376 (end && *end != '\0')) {
377 ELOG("Could not parse window id \"%s\"\n", cvalue);
379 current_match->id = parsed;
380 DLOG("window id as int = %d\n", current_match->id);
385 if (strcmp(ctype, "con_mark") == 0) {
386 current_match->mark = regex_new(cvalue);
390 if (strcmp(ctype, "title") == 0) {
391 current_match->title = regex_new(cvalue);
395 if (strcmp(ctype, "urgent") == 0) {
396 if (strcasecmp(cvalue, "latest") == 0 ||
397 strcasecmp(cvalue, "newest") == 0 ||
398 strcasecmp(cvalue, "recent") == 0 ||
399 strcasecmp(cvalue, "last") == 0) {
400 current_match->urgent = U_LATEST;
401 } else if (strcasecmp(cvalue, "oldest") == 0 ||
402 strcasecmp(cvalue, "first") == 0) {
403 current_match->urgent = U_OLDEST;
408 ELOG("Unknown criterion: %s\n", ctype);
412 * Implementation of 'move [window|container] [to] workspace
413 * next|prev|next_on_output|prev_on_output|current'.
416 void cmd_move_con_to_workspace(I3_CMD, char *which) {
419 DLOG("which=%s\n", which);
421 /* We have nothing to move:
422 * when criteria was specified but didn't match any window or
423 * when criteria wasn't specified and we don't have any window focused. */
424 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
425 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
426 !con_has_children(focused))) {
433 /* get the workspace */
435 if (strcmp(which, "next") == 0)
436 ws = workspace_next();
437 else if (strcmp(which, "prev") == 0)
438 ws = workspace_prev();
439 else if (strcmp(which, "next_on_output") == 0)
440 ws = workspace_next_on_output();
441 else if (strcmp(which, "prev_on_output") == 0)
442 ws = workspace_prev_on_output();
443 else if (strcmp(which, "current") == 0)
444 ws = con_get_workspace(focused);
446 ELOG("BUG: called with which=%s\n", which);
451 TAILQ_FOREACH(current, &owindows, owindows) {
452 DLOG("matching: %p / %s\n", current->con, current->con->name);
453 con_move_to_workspace(current->con, ws, true, false);
456 cmd_output->needs_tree_render = true;
457 // XXX: default reply for now, make this a better reply
462 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
465 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
469 ws = workspace_back_and_forth_get();
472 yerror("No workspace was previously active.");
478 TAILQ_FOREACH(current, &owindows, owindows) {
479 DLOG("matching: %p / %s\n", current->con, current->con->name);
480 con_move_to_workspace(current->con, ws, true, false);
483 cmd_output->needs_tree_render = true;
484 // XXX: default reply for now, make this a better reply
489 * Implementation of 'move [window|container] [to] workspace <name>'.
492 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
493 if (strncasecmp(name, "__", strlen("__")) == 0) {
494 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
501 /* We have nothing to move:
502 * when criteria was specified but didn't match any window or
503 * when criteria wasn't specified and we don't have any window focused. */
504 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
505 ELOG("No windows match your criteria, cannot move.\n");
508 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
509 !con_has_children(focused)) {
514 LOG("should move window to workspace %s\n", name);
515 /* get the workspace */
519 /* first look for a workspace with this name */
520 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
521 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
524 /* if the name is plain digits, we interpret this as a "workspace number"
526 if (!ws && name_is_digits(name)) {
527 long parsed_num = ws_name_to_number(name);
528 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
529 GREP_FIRST(ws, output_get_content(output),
530 child->num == parsed_num);
534 /* if no workspace was found, make a new one */
536 ws = workspace_get(name, NULL);
538 ws = maybe_auto_back_and_forth_workspace(ws);
542 TAILQ_FOREACH(current, &owindows, owindows) {
543 DLOG("matching: %p / %s\n", current->con, current->con->name);
544 con_move_to_workspace(current->con, ws, true, false);
547 cmd_output->needs_tree_render = true;
548 // XXX: default reply for now, make this a better reply
553 * Implementation of 'move [window|container] [to] workspace number <name>'.
556 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
559 /* We have nothing to move:
560 * when criteria was specified but didn't match any window or
561 * when criteria wasn't specified and we don't have any window focused. */
562 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
563 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
564 !con_has_children(focused))) {
569 LOG("should move window to workspace %s\n", which);
570 /* get the workspace */
571 Con *output, *workspace = NULL;
573 long parsed_num = ws_name_to_number(which);
575 if (parsed_num == -1) {
576 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
577 // TODO: better error message
578 yerror("Could not parse number");
582 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
583 GREP_FIRST(workspace, output_get_content(output),
584 child->num == parsed_num);
587 workspace = workspace_get(which, NULL);
590 workspace = maybe_auto_back_and_forth_workspace(workspace);
594 TAILQ_FOREACH(current, &owindows, owindows) {
595 DLOG("matching: %p / %s\n", current->con, current->con->name);
596 con_move_to_workspace(current->con, workspace, true, false);
599 cmd_output->needs_tree_render = true;
600 // XXX: default reply for now, make this a better reply
604 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
605 LOG("floating resize\n");
606 Rect old_rect = floating_con->rect;
607 Con *focused_con = con_descend_focused(floating_con);
609 /* ensure that resize will take place even if pixel increment is smaller than
610 * height increment or width increment.
612 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
613 strcmp(direction, "height") == 0) {
615 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
617 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
618 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
620 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
622 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
625 if (strcmp(direction, "up") == 0) {
626 floating_con->rect.height += px;
627 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
628 floating_con->rect.height += px;
629 } else if (strcmp(direction, "left") == 0) {
630 floating_con->rect.width += px;
632 floating_con->rect.width += px;
635 floating_check_size(floating_con);
637 /* Did we actually resize anything or did the size constraints prevent us?
638 * If we could not resize, exit now to not move the window. */
639 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
642 if (strcmp(direction, "up") == 0) {
643 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
644 } else if (strcmp(direction, "left") == 0) {
645 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
648 /* If this is a scratchpad window, don't auto center it from now on. */
649 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
650 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
653 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
654 LOG("tiling resize\n");
656 Con *first = current;
657 direction_t search_direction;
658 if (!strcmp(direction, "left"))
659 search_direction = D_LEFT;
660 else if (!strcmp(direction, "right"))
661 search_direction = D_RIGHT;
662 else if (!strcmp(direction, "up"))
663 search_direction = D_UP;
665 search_direction = D_DOWN;
667 bool res = resize_find_tiling_participants(&first, &second, search_direction);
669 LOG("No second container in this direction found.\n");
674 /* get the default percentage */
675 int children = con_num_children(first->parent);
676 LOG("ins. %d children\n", children);
677 double percentage = 1.0 / children;
678 LOG("default percentage = %f\n", percentage);
681 LOG("second->percent = %f\n", second->percent);
682 LOG("first->percent before = %f\n", first->percent);
683 if (first->percent == 0.0)
684 first->percent = percentage;
685 if (second->percent == 0.0)
686 second->percent = percentage;
687 double new_first_percent = first->percent + ((double)ppt / 100.0);
688 double new_second_percent = second->percent - ((double)ppt / 100.0);
689 LOG("new_first_percent = %f\n", new_first_percent);
690 LOG("new_second_percent = %f\n", new_second_percent);
691 /* Ensure that the new percentages are positive and greater than
692 * 0.05 to have a reasonable minimum size. */
693 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
694 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
695 first->percent += ((double)ppt / 100.0);
696 second->percent -= ((double)ppt / 100.0);
697 LOG("first->percent after = %f\n", first->percent);
698 LOG("second->percent after = %f\n", second->percent);
700 LOG("Not resizing, already at minimum size\n");
706 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
707 LOG("width/height resize\n");
708 /* get the appropriate current container (skip stacked/tabbed cons) */
709 while (current->parent->layout == L_STACKED ||
710 current->parent->layout == L_TABBED)
711 current = current->parent;
713 /* Then further go up until we find one with the matching orientation. */
714 orientation_t search_orientation =
715 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
717 while (current->type != CT_WORKSPACE &&
718 current->type != CT_FLOATING_CON &&
719 con_orientation(current->parent) != search_orientation)
720 current = current->parent;
722 /* get the default percentage */
723 int children = con_num_children(current->parent);
724 LOG("ins. %d children\n", children);
725 double percentage = 1.0 / children;
726 LOG("default percentage = %f\n", percentage);
728 orientation_t orientation = con_orientation(current->parent);
730 if ((orientation == HORIZ &&
731 strcmp(direction, "height") == 0) ||
732 (orientation == VERT &&
733 strcmp(direction, "width") == 0)) {
734 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
735 (orientation == HORIZ ? "horizontal" : "vertical"));
741 LOG("This is the only container, cannot resize.\n");
746 /* Ensure all the other children have a percentage set. */
748 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
749 LOG("child->percent = %f (child %p)\n", child->percent, child);
750 if (child->percent == 0.0)
751 child->percent = percentage;
754 double new_current_percent = current->percent + ((double)ppt / 100.0);
755 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
756 LOG("new_current_percent = %f\n", new_current_percent);
757 LOG("subtract_percent = %f\n", subtract_percent);
758 /* Ensure that the new percentages are positive and greater than
759 * 0.05 to have a reasonable minimum size. */
760 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
761 if (child == current)
763 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
764 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
769 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
770 LOG("Not resizing, already at minimum size\n");
775 current->percent += ((double)ppt / 100.0);
776 LOG("current->percent after = %f\n", current->percent);
778 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
779 if (child == current)
781 child->percent -= subtract_percent;
782 LOG("child->percent after (%p) = %f\n", child, child->percent);
789 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
792 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
793 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
794 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
795 // 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
796 int px = atoi(resize_px);
797 int ppt = atoi(resize_ppt);
798 if (strcmp(way, "shrink") == 0) {
806 TAILQ_FOREACH(current, &owindows, owindows) {
807 /* Don't handle dock windows (issue #1201) */
808 if (current->con->window && current->con->window->dock) {
809 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
814 if ((floating_con = con_inside_floating(current->con))) {
815 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
817 if (strcmp(direction, "width") == 0 ||
818 strcmp(direction, "height") == 0) {
819 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
822 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
828 cmd_output->needs_tree_render = true;
829 // XXX: default reply for now, make this a better reply
834 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
837 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
838 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
843 TAILQ_FOREACH(current, &owindows, owindows) {
844 DLOG("matching: %p / %s\n", current->con, current->con->name);
845 int border_style = current->con->border_style;
847 int tmp_border_width = -1;
848 tmp_border_width = strtol(border_width, &end, 10);
849 if (end == border_width) {
850 /* no valid digits found */
851 tmp_border_width = -1;
853 if (strcmp(border_style_str, "toggle") == 0) {
856 if (border_style == BS_NORMAL)
857 tmp_border_width = 2;
858 else if (border_style == BS_NONE)
859 tmp_border_width = 0;
860 else if (border_style == BS_PIXEL)
861 tmp_border_width = 1;
863 if (strcmp(border_style_str, "normal") == 0)
864 border_style = BS_NORMAL;
865 else if (strcmp(border_style_str, "pixel") == 0)
866 border_style = BS_PIXEL;
867 else if (strcmp(border_style_str, "1pixel") == 0) {
868 border_style = BS_PIXEL;
869 tmp_border_width = 1;
870 } else if (strcmp(border_style_str, "none") == 0)
871 border_style = BS_NONE;
873 ELOG("BUG: called with border_style=%s\n", border_style_str);
878 con_set_border_style(current->con, border_style, tmp_border_width);
881 cmd_output->needs_tree_render = true;
882 // XXX: default reply for now, make this a better reply
887 * Implementation of 'nop <comment>'.
890 void cmd_nop(I3_CMD, char *comment) {
891 LOG("-------------------------------------------------\n");
892 LOG(" NOP: %s\n", comment);
893 LOG("-------------------------------------------------\n");
897 * Implementation of 'append_layout <path>'.
900 void cmd_append_layout(I3_CMD, char *path) {
901 LOG("Appending layout \"%s\"\n", path);
903 json_content_t content = json_determine_content(path);
904 LOG("JSON content = %d\n", content);
905 if (content == JSON_CONTENT_UNKNOWN) {
906 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
911 Con *parent = focused;
912 if (content == JSON_CONTENT_WORKSPACE) {
913 parent = output_get_content(con_get_output(parent));
915 /* We need to append the layout to a split container, since a leaf
916 * container must not have any children (by definition).
917 * Note that we explicitly check for workspaces, since they are okay for
918 * this purpose, but con_accepts_window() returns false for workspaces. */
919 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
920 parent = parent->parent;
922 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
923 char *errormsg = NULL;
924 tree_append_json(parent, path, &errormsg);
925 if (errormsg != NULL) {
928 /* Note that we continue executing since tree_append_json() has
929 * side-effects — user-provided layouts can be partly valid, partly
930 * invalid, leading to half of the placeholder containers being
936 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
937 // false); should be enough, but when sending 'workspace 4; append_layout
938 // /tmp/foo.json', the needs_tree_render == true of the workspace command
939 // is not executed yet and will be batched with append_layout’s
940 // needs_tree_render after the parser finished. We should check if that is
942 render_con(croot, false);
944 restore_open_placeholder_windows(parent);
946 if (content == JSON_CONTENT_WORKSPACE)
947 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"restored\"}");
949 cmd_output->needs_tree_render = true;
953 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
956 void cmd_workspace(I3_CMD, char *which) {
959 DLOG("which=%s\n", which);
961 if (strcmp(which, "next") == 0)
962 ws = workspace_next();
963 else if (strcmp(which, "prev") == 0)
964 ws = workspace_prev();
965 else if (strcmp(which, "next_on_output") == 0)
966 ws = workspace_next_on_output();
967 else if (strcmp(which, "prev_on_output") == 0)
968 ws = workspace_prev_on_output();
970 ELOG("BUG: called with which=%s\n", which);
977 cmd_output->needs_tree_render = true;
978 // XXX: default reply for now, make this a better reply
983 * Implementation of 'workspace number <name>'
986 void cmd_workspace_number(I3_CMD, char *which) {
987 Con *output, *workspace = NULL;
989 long parsed_num = ws_name_to_number(which);
991 if (parsed_num == -1) {
992 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
993 // TODO: better error message
994 yerror("Could not parse number");
998 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
999 GREP_FIRST(workspace, output_get_content(output),
1000 child->num == parsed_num);
1003 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1005 workspace_show_by_name(which);
1006 cmd_output->needs_tree_render = true;
1009 if (maybe_back_and_forth(cmd_output, workspace->name))
1011 workspace_show(workspace);
1013 cmd_output->needs_tree_render = true;
1014 // XXX: default reply for now, make this a better reply
1019 * Implementation of 'workspace back_and_forth'.
1022 void cmd_workspace_back_and_forth(I3_CMD) {
1023 workspace_back_and_forth();
1025 cmd_output->needs_tree_render = true;
1026 // XXX: default reply for now, make this a better reply
1031 * Implementation of 'workspace <name>'
1034 void cmd_workspace_name(I3_CMD, char *name) {
1035 if (strncasecmp(name, "__", strlen("__")) == 0) {
1036 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1041 DLOG("should switch to workspace %s\n", name);
1042 if (maybe_back_and_forth(cmd_output, name))
1048 /* first look for a workspace with this name */
1049 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1050 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1053 /* if the name is only digits, we interpret this as a "workspace number"
1055 if (!ws && name_is_digits(name)) {
1056 long parsed_num = ws_name_to_number(name);
1057 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1058 GREP_FIRST(ws, output_get_content(output),
1059 child->num == parsed_num);
1063 /* if no workspace was found, make a new one */
1065 ws = workspace_get(name, NULL);
1069 cmd_output->needs_tree_render = true;
1070 // XXX: default reply for now, make this a better reply
1075 * Implementation of 'mark <mark>'
1078 void cmd_mark(I3_CMD, char *mark) {
1079 DLOG("Clearing all windows which have that mark first\n");
1082 TAILQ_FOREACH(con, &all_cons, all_cons) {
1083 if (con->mark && strcmp(con->mark, mark) == 0)
1087 DLOG("marking window with str %s\n", mark);
1092 TAILQ_FOREACH(current, &owindows, owindows) {
1093 DLOG("matching: %p / %s\n", current->con, current->con->name);
1094 current->con->mark = sstrdup(mark);
1097 cmd_output->needs_tree_render = true;
1098 // XXX: default reply for now, make this a better reply
1103 * Implementation of 'unmark [mark]'
1106 void cmd_unmark(I3_CMD, char *mark) {
1109 TAILQ_FOREACH(con, &all_cons, all_cons) {
1112 DLOG("removed all window marks");
1115 TAILQ_FOREACH(con, &all_cons, all_cons) {
1116 if (con->mark && strcmp(con->mark, mark) == 0)
1119 DLOG("removed window mark %s\n", mark);
1122 cmd_output->needs_tree_render = true;
1123 // XXX: default reply for now, make this a better reply
1128 * Implementation of 'mode <string>'.
1131 void cmd_mode(I3_CMD, char *mode) {
1132 DLOG("mode=%s\n", mode);
1135 // XXX: default reply for now, make this a better reply
1140 * Implementation of 'move [window|container] [to] output <str>'.
1143 void cmd_move_con_to_output(I3_CMD, char *name) {
1146 DLOG("should move window to output %s\n", name);
1150 /* get the output */
1151 Output *current_output = NULL;
1154 // TODO: fix the handling of criteria
1155 TAILQ_FOREACH(current, &owindows, owindows)
1156 current_output = get_output_of_con(current->con);
1158 assert(current_output != NULL);
1160 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1161 if (strcasecmp(name, "up") == 0)
1162 output = get_output_next_wrap(D_UP, current_output);
1163 else if (strcasecmp(name, "down") == 0)
1164 output = get_output_next_wrap(D_DOWN, current_output);
1165 else if (strcasecmp(name, "left") == 0)
1166 output = get_output_next_wrap(D_LEFT, current_output);
1167 else if (strcasecmp(name, "right") == 0)
1168 output = get_output_next_wrap(D_RIGHT, current_output);
1170 output = get_output_by_name(name);
1173 LOG("No such output found.\n");
1178 /* get visible workspace on output */
1180 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1186 TAILQ_FOREACH(current, &owindows, owindows) {
1187 DLOG("matching: %p / %s\n", current->con, current->con->name);
1188 con_move_to_workspace(current->con, ws, true, false);
1191 cmd_output->needs_tree_render = true;
1192 // XXX: default reply for now, make this a better reply
1197 * Implementation of 'floating enable|disable|toggle'
1200 void cmd_floating(I3_CMD, char *floating_mode) {
1203 DLOG("floating_mode=%s\n", floating_mode);
1207 TAILQ_FOREACH(current, &owindows, owindows) {
1208 DLOG("matching: %p / %s\n", current->con, current->con->name);
1209 if (strcmp(floating_mode, "toggle") == 0) {
1210 DLOG("should toggle mode\n");
1211 toggle_floating_mode(current->con, false);
1213 DLOG("should switch mode to %s\n", floating_mode);
1214 if (strcmp(floating_mode, "enable") == 0) {
1215 floating_enable(current->con, false);
1217 floating_disable(current->con, false);
1222 cmd_output->needs_tree_render = true;
1223 // XXX: default reply for now, make this a better reply
1228 * Implementation of 'move workspace to [output] <str>'.
1231 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1232 DLOG("should move workspace to output %s\n", name);
1237 TAILQ_FOREACH(current, &owindows, owindows) {
1238 Output *current_output = get_output_of_con(current->con);
1239 if (!current_output) {
1240 ELOG("Cannot get current output. This is a bug in i3.\n");
1244 Output *output = get_output_from_string(current_output, name);
1246 ELOG("Could not get output from string \"%s\"\n", name);
1251 Con *content = output_get_content(output->con);
1252 LOG("got output %p with content %p\n", output, content);
1254 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1255 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1257 Con *ws = con_get_workspace(current->con);
1258 LOG("should move workspace %p / %s\n", ws, ws->name);
1259 bool workspace_was_visible = workspace_is_visible(ws);
1261 if (con_num_children(ws->parent) == 1) {
1262 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1264 /* check if we can find a workspace assigned to this output */
1265 bool used_assignment = false;
1266 struct Workspace_Assignment *assignment;
1267 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1268 if (strcmp(assignment->output, current_output->name) != 0)
1271 /* check if this workspace is already attached to the tree */
1272 Con *workspace = NULL, *out;
1273 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1274 GREP_FIRST(workspace, output_get_content(out),
1275 !strcasecmp(child->name, assignment->name));
1276 if (workspace != NULL)
1279 /* so create the workspace referenced to by this assignment */
1280 LOG("Creating workspace from assignment %s.\n", assignment->name);
1281 workspace_get(assignment->name, NULL);
1282 used_assignment = true;
1286 /* if we couldn't create the workspace using an assignment, create
1287 * it on the output */
1288 if (!used_assignment)
1289 create_workspace_on_output(current_output, ws->parent);
1291 /* notify the IPC listeners */
1292 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1294 DLOG("Detaching\n");
1296 /* detach from the old output and attach to the new output */
1297 Con *old_content = ws->parent;
1299 if (workspace_was_visible) {
1300 /* The workspace which we just detached was visible, so focus
1301 * the next one in the focus-stack. */
1302 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1303 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1304 workspace_show(focus_ws);
1306 con_attach(ws, content, false);
1308 /* fix the coordinates of the floating containers */
1310 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1311 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1313 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1314 if (workspace_was_visible) {
1315 /* Focus the moved workspace on the destination output. */
1319 /* NB: We cannot simply work with previously_visible_ws since it might
1320 * have been cleaned up by workspace_show() already, depending on the
1321 * focus order/number of other workspaces on the output.
1322 * Instead, we loop through the available workspaces and only work with
1323 * previously_visible_ws if we still find it. */
1324 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1325 if (ws != previously_visible_ws)
1328 /* Call the on_remove_child callback of the workspace which previously
1329 * was visible on the destination output. Since it is no longer
1330 * visible, it might need to get cleaned up. */
1331 CALL(previously_visible_ws, on_remove_child);
1336 cmd_output->needs_tree_render = true;
1337 // XXX: default reply for now, make this a better reply
1342 * Implementation of 'split v|h|vertical|horizontal'.
1345 void cmd_split(I3_CMD, char *direction) {
1347 /* TODO: use matches */
1348 LOG("splitting in direction %c\n", direction[0]);
1349 if (match_is_empty(current_match))
1350 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1352 TAILQ_FOREACH(current, &owindows, owindows) {
1353 DLOG("matching: %p / %s\n", current->con, current->con->name);
1354 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1358 cmd_output->needs_tree_render = true;
1359 // XXX: default reply for now, make this a better reply
1364 * Implementation of 'kill [window|client]'.
1367 void cmd_kill(I3_CMD, char *kill_mode_str) {
1368 if (kill_mode_str == NULL)
1369 kill_mode_str = "window";
1372 DLOG("kill_mode=%s\n", kill_mode_str);
1375 if (strcmp(kill_mode_str, "window") == 0)
1376 kill_mode = KILL_WINDOW;
1377 else if (strcmp(kill_mode_str, "client") == 0)
1378 kill_mode = KILL_CLIENT;
1380 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1385 /* check if the match is empty, not if the result is empty */
1386 if (match_is_empty(current_match))
1387 tree_close_con(kill_mode);
1389 TAILQ_FOREACH(current, &owindows, owindows) {
1390 DLOG("matching: %p / %s\n", current->con, current->con->name);
1391 tree_close(current->con, kill_mode, false, false);
1395 cmd_output->needs_tree_render = true;
1396 // XXX: default reply for now, make this a better reply
1401 * Implementation of 'exec [--no-startup-id] <command>'.
1404 void cmd_exec(I3_CMD, char *nosn, char *command) {
1405 bool no_startup_id = (nosn != NULL);
1407 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1408 start_application(command, no_startup_id);
1410 // XXX: default reply for now, make this a better reply
1415 * Implementation of 'focus left|right|up|down'.
1418 void cmd_focus_direction(I3_CMD, char *direction) {
1419 DLOG("direction = *%s*\n", direction);
1421 if (strcmp(direction, "left") == 0)
1422 tree_next('p', HORIZ);
1423 else if (strcmp(direction, "right") == 0)
1424 tree_next('n', HORIZ);
1425 else if (strcmp(direction, "up") == 0)
1426 tree_next('p', VERT);
1427 else if (strcmp(direction, "down") == 0)
1428 tree_next('n', VERT);
1430 ELOG("Invalid focus direction (%s)\n", direction);
1435 cmd_output->needs_tree_render = true;
1436 // XXX: default reply for now, make this a better reply
1441 * Implementation of 'focus tiling|floating|mode_toggle'.
1444 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1445 DLOG("window_mode = %s\n", window_mode);
1447 Con *ws = con_get_workspace(focused);
1450 if (strcmp(window_mode, "mode_toggle") == 0) {
1451 current = TAILQ_FIRST(&(ws->focus_head));
1452 if (current != NULL && current->type == CT_FLOATING_CON)
1453 window_mode = "tiling";
1455 window_mode = "floating";
1457 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1458 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1459 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1462 con_focus(con_descend_focused(current));
1467 cmd_output->needs_tree_render = true;
1468 // XXX: default reply for now, make this a better reply
1473 * Implementation of 'focus parent|child'.
1476 void cmd_focus_level(I3_CMD, char *level) {
1477 DLOG("level = %s\n", level);
1478 bool success = false;
1480 /* Focusing the parent can only be allowed if the newly
1481 * focused container won't escape the fullscreen container. */
1482 if (strcmp(level, "parent") == 0) {
1483 if (focused && focused->parent) {
1484 if (con_fullscreen_permits_focusing(focused->parent))
1485 success = level_up();
1487 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1491 /* Focusing a child should always be allowed. */
1493 success = level_down();
1495 cmd_output->needs_tree_render = success;
1496 // XXX: default reply for now, make this a better reply
1501 * Implementation of 'focus'.
1504 void cmd_focus(I3_CMD) {
1505 DLOG("current_match = %p\n", current_match);
1507 if (match_is_empty(current_match)) {
1508 ELOG("You have to specify which window/container should be focused.\n");
1509 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1511 yerror("You have to specify which window/container should be focused");
1516 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1519 TAILQ_FOREACH(current, &owindows, owindows) {
1520 Con *ws = con_get_workspace(current->con);
1521 /* If no workspace could be found, this was a dock window.
1522 * Just skip it, you cannot focus dock windows. */
1526 /* Check the fullscreen focus constraints. */
1527 if (!con_fullscreen_permits_focusing(current->con)) {
1528 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1533 /* In case this is a scratchpad window, call scratchpad_show(). */
1534 if (ws == __i3_scratch) {
1535 scratchpad_show(current->con);
1537 /* While for the normal focus case we can change focus multiple
1538 * times and only a single window ends up focused, we could show
1539 * multiple scratchpad windows. So, rather break here. */
1543 /* If the container is not on the current workspace,
1544 * workspace_show() will switch to a different workspace and (if
1545 * enabled) trigger a mouse pointer warp to the currently focused
1546 * container (!) on the target workspace.
1548 * Therefore, before calling workspace_show(), we make sure that
1549 * 'current' will be focused on the workspace. However, we cannot
1550 * just con_focus(current) because then the pointer will not be
1551 * warped at all (the code thinks we are already there).
1553 * So we focus 'current' to make it the currently focused window of
1554 * the target workspace, then revert focus. */
1555 Con *currently_focused = focused;
1556 con_focus(current->con);
1557 con_focus(currently_focused);
1559 /* Now switch to the workspace, then focus */
1561 LOG("focusing %p / %s\n", current->con, current->con->name);
1562 con_focus(current->con);
1567 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1568 "while only exactly one container can be focused at a time.\n",
1571 cmd_output->needs_tree_render = true;
1572 // XXX: default reply for now, make this a better reply
1577 * Implementation of 'fullscreen [global]'.
1580 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1581 if (fullscreen_mode == NULL)
1582 fullscreen_mode = "output";
1583 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1588 TAILQ_FOREACH(current, &owindows, owindows) {
1589 DLOG("matching: %p / %s\n", current->con, current->con->name);
1590 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1593 cmd_output->needs_tree_render = true;
1594 // XXX: default reply for now, make this a better reply
1599 * Implementation of 'move <direction> [<pixels> [px]]'.
1602 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1603 // 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
1604 int px = atoi(move_px);
1609 Con *initially_focused = focused;
1611 TAILQ_FOREACH(current, &owindows, owindows) {
1612 DLOG("moving in direction %s, px %s\n", direction, move_px);
1613 if (con_is_floating(current->con)) {
1614 DLOG("floating move with %d pixels\n", px);
1615 Rect newrect = current->con->parent->rect;
1616 if (strcmp(direction, "left") == 0) {
1618 } else if (strcmp(direction, "right") == 0) {
1620 } else if (strcmp(direction, "up") == 0) {
1622 } else if (strcmp(direction, "down") == 0) {
1625 floating_reposition(current->con->parent, newrect);
1627 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1628 cmd_output->needs_tree_render = true;
1632 /* the move command should not disturb focus */
1633 if (focused != initially_focused)
1634 con_focus(initially_focused);
1636 // XXX: default reply for now, make this a better reply
1641 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1644 void cmd_layout(I3_CMD, char *layout_str) {
1645 if (strcmp(layout_str, "stacking") == 0)
1646 layout_str = "stacked";
1649 /* default is a special case which will be handled in con_set_layout(). */
1650 if (strcmp(layout_str, "default") == 0)
1652 else if (strcmp(layout_str, "stacked") == 0)
1654 else if (strcmp(layout_str, "tabbed") == 0)
1656 else if (strcmp(layout_str, "splitv") == 0)
1658 else if (strcmp(layout_str, "splith") == 0)
1661 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1665 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1667 /* check if the match is empty, not if the result is empty */
1668 if (match_is_empty(current_match))
1669 con_set_layout(focused, layout);
1671 TAILQ_FOREACH(current, &owindows, owindows) {
1672 DLOG("matching: %p / %s\n", current->con, current->con->name);
1673 con_set_layout(current->con, layout);
1677 cmd_output->needs_tree_render = true;
1678 // XXX: default reply for now, make this a better reply
1683 * Implementation of 'layout toggle [all|split]'.
1686 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1689 if (toggle_mode == NULL)
1690 toggle_mode = "default";
1692 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1694 /* check if the match is empty, not if the result is empty */
1695 if (match_is_empty(current_match))
1696 con_toggle_layout(focused, toggle_mode);
1698 TAILQ_FOREACH(current, &owindows, owindows) {
1699 DLOG("matching: %p / %s\n", current->con, current->con->name);
1700 con_toggle_layout(current->con, toggle_mode);
1704 cmd_output->needs_tree_render = true;
1705 // XXX: default reply for now, make this a better reply
1710 * Implementation of 'exit'.
1713 void cmd_exit(I3_CMD) {
1714 LOG("Exiting due to user command.\n");
1716 unlink(config.ipc_socket_path);
1717 xcb_disconnect(conn);
1724 * Implementation of 'reload'.
1727 void cmd_reload(I3_CMD) {
1729 kill_nagbar(&config_error_nagbar_pid, false);
1730 kill_nagbar(&command_error_nagbar_pid, false);
1731 load_configuration(conn, NULL, true);
1733 /* Send an IPC event just in case the ws names have changed */
1734 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1735 /* Send an update event for the barconfig just in case it has changed */
1738 // XXX: default reply for now, make this a better reply
1743 * Implementation of 'restart'.
1746 void cmd_restart(I3_CMD) {
1747 LOG("restarting i3\n");
1749 unlink(config.ipc_socket_path);
1750 /* We need to call this manually since atexit handlers don’t get called
1752 purge_zerobyte_logfile();
1755 // XXX: default reply for now, make this a better reply
1760 * Implementation of 'open'.
1763 void cmd_open(I3_CMD) {
1764 LOG("opening new container\n");
1765 Con *con = tree_open_con(NULL, NULL);
1766 con->layout = L_SPLITH;
1773 y(integer, (long int)con);
1776 cmd_output->needs_tree_render = true;
1780 * Implementation of 'focus output <output>'.
1783 void cmd_focus_output(I3_CMD, char *name) {
1786 DLOG("name = %s\n", name);
1790 /* get the output */
1791 Output *current_output = NULL;
1794 TAILQ_FOREACH(current, &owindows, owindows)
1795 current_output = get_output_of_con(current->con);
1796 assert(current_output != NULL);
1798 output = get_output_from_string(current_output, name);
1801 LOG("No such output found.\n");
1806 /* get visible workspace on output */
1808 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1816 cmd_output->needs_tree_render = true;
1817 // XXX: default reply for now, make this a better reply
1822 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1825 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1828 bool has_error = false;
1833 TAILQ_FOREACH(current, &owindows, owindows) {
1834 if (!con_is_floating(current->con)) {
1835 ELOG("Cannot change position. The window/container is not floating\n");
1838 yerror("Cannot change position of a window/container because it is not floating.");
1845 if (strcmp(method, "absolute") == 0) {
1846 current->con->parent->rect.x = x;
1847 current->con->parent->rect.y = y;
1849 DLOG("moving to absolute position %d %d\n", x, y);
1850 floating_maybe_reassign_ws(current->con->parent);
1851 cmd_output->needs_tree_render = true;
1854 if (strcmp(method, "position") == 0) {
1855 Rect newrect = current->con->parent->rect;
1857 DLOG("moving to position %d %d\n", x, y);
1861 floating_reposition(current->con->parent, newrect);
1865 // XXX: default reply for now, make this a better reply
1871 * Implementation of 'move [window|container] [to] [absolute] position center
1874 void cmd_move_window_to_center(I3_CMD, char *method) {
1875 if (!con_is_floating(focused)) {
1876 ELOG("Cannot change position. The window/container is not floating\n");
1877 yerror("Cannot change position. The window/container is not floating.");
1881 if (strcmp(method, "absolute") == 0) {
1882 Rect *rect = &focused->parent->rect;
1884 DLOG("moving to absolute center\n");
1885 rect->x = croot->rect.width / 2 - rect->width / 2;
1886 rect->y = croot->rect.height / 2 - rect->height / 2;
1888 floating_maybe_reassign_ws(focused->parent);
1889 cmd_output->needs_tree_render = true;
1892 if (strcmp(method, "position") == 0) {
1893 Rect *wsrect = &con_get_workspace(focused)->rect;
1894 Rect newrect = focused->parent->rect;
1896 DLOG("moving to center\n");
1897 newrect.x = wsrect->width / 2 - newrect.width / 2;
1898 newrect.y = wsrect->height / 2 - newrect.height / 2;
1900 floating_reposition(focused->parent, newrect);
1903 // XXX: default reply for now, make this a better reply
1908 * Implementation of 'move scratchpad'.
1911 void cmd_move_scratchpad(I3_CMD) {
1912 DLOG("should move window to scratchpad\n");
1917 TAILQ_FOREACH(current, &owindows, owindows) {
1918 DLOG("matching: %p / %s\n", current->con, current->con->name);
1919 scratchpad_move(current->con);
1922 cmd_output->needs_tree_render = true;
1923 // XXX: default reply for now, make this a better reply
1928 * Implementation of 'scratchpad show'.
1931 void cmd_scratchpad_show(I3_CMD) {
1932 DLOG("should show scratchpad window\n");
1935 if (match_is_empty(current_match)) {
1936 scratchpad_show(NULL);
1938 TAILQ_FOREACH(current, &owindows, owindows) {
1939 DLOG("matching: %p / %s\n", current->con, current->con->name);
1940 scratchpad_show(current->con);
1944 cmd_output->needs_tree_render = true;
1945 // XXX: default reply for now, make this a better reply
1950 * Implementation of 'rename workspace [<name>] to <name>'
1953 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1954 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1955 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1960 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1962 LOG("Renaming current workspace to \"%s\"\n", new_name);
1965 Con *output, *workspace = NULL;
1967 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1968 GREP_FIRST(workspace, output_get_content(output),
1969 !strcasecmp(child->name, old_name));
1971 workspace = con_get_workspace(focused);
1975 // TODO: we should include the old workspace name here and use yajl for
1976 // generating the reply.
1977 // TODO: better error message
1978 yerror("Old workspace not found");
1982 Con *check_dest = NULL;
1983 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1984 GREP_FIRST(check_dest, output_get_content(output),
1985 !strcasecmp(child->name, new_name));
1987 if (check_dest != NULL) {
1988 // TODO: we should include the new workspace name here and use yajl for
1989 // generating the reply.
1990 // TODO: better error message
1991 yerror("New workspace already exists");
1995 /* Change the name and try to parse it as a number. */
1996 FREE(workspace->name);
1997 workspace->name = sstrdup(new_name);
1999 workspace->num = ws_name_to_number(new_name);
2000 LOG("num = %d\n", workspace->num);
2002 /* By re-attaching, the sort order will be correct afterwards. */
2003 Con *previously_focused = focused;
2004 Con *parent = workspace->parent;
2005 con_detach(workspace);
2006 con_attach(workspace, parent, false);
2007 /* Restore the previous focus since con_attach messes with the focus. */
2008 con_focus(previously_focused);
2010 cmd_output->needs_tree_render = true;
2013 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
2014 ewmh_update_desktop_names();
2015 ewmh_update_desktop_viewport();
2016 ewmh_update_current_desktop();
2020 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2023 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2025 bool toggle = false;
2026 if (strcmp(bar_mode, "dock") == 0)
2028 else if (strcmp(bar_mode, "hide") == 0)
2030 else if (strcmp(bar_mode, "invisible") == 0)
2032 else if (strcmp(bar_mode, "toggle") == 0)
2035 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2039 bool changed_sth = false;
2040 Barconfig *current = NULL;
2041 TAILQ_FOREACH(current, &barconfigs, configs) {
2042 if (bar_id && strcmp(current->id, bar_id) != 0)
2046 mode = (current->mode + 1) % 2;
2048 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2049 current->mode = mode;
2056 if (bar_id && !changed_sth) {
2057 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2065 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2068 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2069 int hidden_state = S_SHOW;
2070 bool toggle = false;
2071 if (strcmp(bar_hidden_state, "hide") == 0)
2072 hidden_state = S_HIDE;
2073 else if (strcmp(bar_hidden_state, "show") == 0)
2074 hidden_state = S_SHOW;
2075 else if (strcmp(bar_hidden_state, "toggle") == 0)
2078 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2082 bool changed_sth = false;
2083 Barconfig *current = NULL;
2084 TAILQ_FOREACH(current, &barconfigs, configs) {
2085 if (bar_id && strcmp(current->id, bar_id) != 0)
2089 hidden_state = (current->hidden_state + 1) % 2;
2091 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2092 current->hidden_state = hidden_state;
2099 if (bar_id && !changed_sth) {
2100 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2108 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2111 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2113 if (strcmp(bar_type, "mode") == 0)
2114 ret = cmd_bar_mode(bar_value, bar_id);
2115 else if (strcmp(bar_type, "hidden_state") == 0)
2116 ret = cmd_bar_hidden_state(bar_value, bar_id);
2118 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2130 * Implementation of 'shmlog <size>|toggle|on|off'
2133 void cmd_shmlog(I3_CMD, char *argument) {
2134 if (!strcmp(argument, "toggle"))
2135 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2136 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2137 else if (!strcmp(argument, "on"))
2138 shmlog_size = default_shmlog_size;
2139 else if (!strcmp(argument, "off"))
2142 /* If shm logging now, restart logging with the new size. */
2143 if (shmlog_size > 0) {
2145 LOG("Restarting shm logging...\n");
2148 shmlog_size = atoi(argument);
2149 /* Make a weakly attempt at ensuring the argument is valid. */
2150 if (shmlog_size <= 0)
2151 shmlog_size = default_shmlog_size;
2153 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2155 update_shmlog_atom();
2156 // XXX: default reply for now, make this a better reply
2161 * Implementation of 'debuglog toggle|on|off'
2164 void cmd_debuglog(I3_CMD, char *argument) {
2165 bool logging = get_debug_logging();
2166 if (!strcmp(argument, "toggle")) {
2167 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2168 set_debug_logging(!logging);
2169 } else if (!strcmp(argument, "on") && !logging) {
2170 LOG("Enabling debug logging\n");
2171 set_debug_logging(true);
2172 } else if (!strcmp(argument, "off") && logging) {
2173 LOG("Disabling debug logging\n");
2174 set_debug_logging(false);
2176 // XXX: default reply for now, make this a better reply