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 /* Make sure we allow paths like '~/.i3/layout.json' */
904 path = resolve_tilde(path);
906 json_content_t content = json_determine_content(path);
907 LOG("JSON content = %d\n", content);
908 if (content == JSON_CONTENT_UNKNOWN) {
909 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
915 Con *parent = focused;
916 if (content == JSON_CONTENT_WORKSPACE) {
917 parent = output_get_content(con_get_output(parent));
919 /* We need to append the layout to a split container, since a leaf
920 * container must not have any children (by definition).
921 * Note that we explicitly check for workspaces, since they are okay for
922 * this purpose, but con_accepts_window() returns false for workspaces. */
923 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
924 parent = parent->parent;
926 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
927 char *errormsg = NULL;
928 tree_append_json(parent, path, &errormsg);
929 if (errormsg != NULL) {
932 /* Note that we continue executing since tree_append_json() has
933 * side-effects — user-provided layouts can be partly valid, partly
934 * invalid, leading to half of the placeholder containers being
940 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
941 // false); should be enough, but when sending 'workspace 4; append_layout
942 // /tmp/foo.json', the needs_tree_render == true of the workspace command
943 // is not executed yet and will be batched with append_layout’s
944 // needs_tree_render after the parser finished. We should check if that is
946 render_con(croot, false);
948 restore_open_placeholder_windows(parent);
950 if (content == JSON_CONTENT_WORKSPACE)
951 ipc_send_workspace_event("restored", parent, NULL);
954 cmd_output->needs_tree_render = true;
958 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
961 void cmd_workspace(I3_CMD, char *which) {
964 DLOG("which=%s\n", which);
966 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
967 LOG("Cannot switch workspace while in global fullscreen\n");
972 if (strcmp(which, "next") == 0)
973 ws = workspace_next();
974 else if (strcmp(which, "prev") == 0)
975 ws = workspace_prev();
976 else if (strcmp(which, "next_on_output") == 0)
977 ws = workspace_next_on_output();
978 else if (strcmp(which, "prev_on_output") == 0)
979 ws = workspace_prev_on_output();
981 ELOG("BUG: called with which=%s\n", which);
988 cmd_output->needs_tree_render = true;
989 // XXX: default reply for now, make this a better reply
994 * Implementation of 'workspace number <name>'
997 void cmd_workspace_number(I3_CMD, char *which) {
998 Con *output, *workspace = NULL;
1000 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1001 LOG("Cannot switch workspace while in global fullscreen\n");
1006 long parsed_num = ws_name_to_number(which);
1008 if (parsed_num == -1) {
1009 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1010 // TODO: better error message
1011 yerror("Could not parse number");
1015 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1016 GREP_FIRST(workspace, output_get_content(output),
1017 child->num == parsed_num);
1020 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1022 workspace_show_by_name(which);
1023 cmd_output->needs_tree_render = true;
1026 if (maybe_back_and_forth(cmd_output, workspace->name))
1028 workspace_show(workspace);
1030 cmd_output->needs_tree_render = true;
1031 // XXX: default reply for now, make this a better reply
1036 * Implementation of 'workspace back_and_forth'.
1039 void cmd_workspace_back_and_forth(I3_CMD) {
1040 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1041 LOG("Cannot switch workspace while in global fullscreen\n");
1046 workspace_back_and_forth();
1048 cmd_output->needs_tree_render = true;
1049 // XXX: default reply for now, make this a better reply
1054 * Implementation of 'workspace <name>'
1057 void cmd_workspace_name(I3_CMD, char *name) {
1058 if (strncasecmp(name, "__", strlen("__")) == 0) {
1059 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1064 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1065 LOG("Cannot switch workspace while in global fullscreen\n");
1070 DLOG("should switch to workspace %s\n", name);
1071 if (maybe_back_and_forth(cmd_output, name))
1077 /* first look for a workspace with this name */
1078 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1079 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1082 /* if the name is only digits, we interpret this as a "workspace number"
1084 if (!ws && name_is_digits(name)) {
1085 long parsed_num = ws_name_to_number(name);
1086 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1087 GREP_FIRST(ws, output_get_content(output),
1088 child->num == parsed_num);
1092 /* if no workspace was found, make a new one */
1094 ws = workspace_get(name, NULL);
1098 cmd_output->needs_tree_render = true;
1099 // XXX: default reply for now, make this a better reply
1104 * Implementation of 'mark <mark>'
1107 void cmd_mark(I3_CMD, char *mark) {
1108 DLOG("Clearing all windows which have that mark first\n");
1111 TAILQ_FOREACH(con, &all_cons, all_cons) {
1112 if (con->mark && strcmp(con->mark, mark) == 0)
1116 DLOG("marking window with str %s\n", mark);
1121 TAILQ_FOREACH(current, &owindows, owindows) {
1122 DLOG("matching: %p / %s\n", current->con, current->con->name);
1123 current->con->mark = sstrdup(mark);
1126 cmd_output->needs_tree_render = true;
1127 // XXX: default reply for now, make this a better reply
1132 * Implementation of 'unmark [mark]'
1135 void cmd_unmark(I3_CMD, char *mark) {
1138 TAILQ_FOREACH(con, &all_cons, all_cons) {
1141 DLOG("removed all window marks");
1144 TAILQ_FOREACH(con, &all_cons, all_cons) {
1145 if (con->mark && strcmp(con->mark, mark) == 0)
1148 DLOG("removed window mark %s\n", mark);
1151 cmd_output->needs_tree_render = true;
1152 // XXX: default reply for now, make this a better reply
1157 * Implementation of 'mode <string>'.
1160 void cmd_mode(I3_CMD, char *mode) {
1161 DLOG("mode=%s\n", mode);
1164 // XXX: default reply for now, make this a better reply
1169 * Implementation of 'move [window|container] [to] output <str>'.
1172 void cmd_move_con_to_output(I3_CMD, char *name) {
1175 DLOG("should move window to output %s\n", name);
1179 /* get the output */
1180 Output *current_output = NULL;
1183 // TODO: fix the handling of criteria
1184 TAILQ_FOREACH(current, &owindows, owindows)
1185 current_output = get_output_of_con(current->con);
1187 assert(current_output != NULL);
1189 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1190 if (strcasecmp(name, "up") == 0)
1191 output = get_output_next_wrap(D_UP, current_output);
1192 else if (strcasecmp(name, "down") == 0)
1193 output = get_output_next_wrap(D_DOWN, current_output);
1194 else if (strcasecmp(name, "left") == 0)
1195 output = get_output_next_wrap(D_LEFT, current_output);
1196 else if (strcasecmp(name, "right") == 0)
1197 output = get_output_next_wrap(D_RIGHT, current_output);
1199 output = get_output_by_name(name);
1202 LOG("No such output found.\n");
1207 /* get visible workspace on output */
1209 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1215 TAILQ_FOREACH(current, &owindows, owindows) {
1216 DLOG("matching: %p / %s\n", current->con, current->con->name);
1217 con_move_to_workspace(current->con, ws, true, false);
1220 cmd_output->needs_tree_render = true;
1221 // XXX: default reply for now, make this a better reply
1226 * Implementation of 'floating enable|disable|toggle'
1229 void cmd_floating(I3_CMD, char *floating_mode) {
1232 DLOG("floating_mode=%s\n", floating_mode);
1236 TAILQ_FOREACH(current, &owindows, owindows) {
1237 DLOG("matching: %p / %s\n", current->con, current->con->name);
1238 if (strcmp(floating_mode, "toggle") == 0) {
1239 DLOG("should toggle mode\n");
1240 toggle_floating_mode(current->con, false);
1242 DLOG("should switch mode to %s\n", floating_mode);
1243 if (strcmp(floating_mode, "enable") == 0) {
1244 floating_enable(current->con, false);
1246 floating_disable(current->con, false);
1251 cmd_output->needs_tree_render = true;
1252 // XXX: default reply for now, make this a better reply
1257 * Implementation of 'move workspace to [output] <str>'.
1260 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1261 DLOG("should move workspace to output %s\n", name);
1266 TAILQ_FOREACH(current, &owindows, owindows) {
1267 Output *current_output = get_output_of_con(current->con);
1268 if (!current_output) {
1269 ELOG("Cannot get current output. This is a bug in i3.\n");
1273 Output *output = get_output_from_string(current_output, name);
1275 ELOG("Could not get output from string \"%s\"\n", name);
1280 Con *content = output_get_content(output->con);
1281 LOG("got output %p with content %p\n", output, content);
1283 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1284 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1286 Con *ws = con_get_workspace(current->con);
1287 LOG("should move workspace %p / %s\n", ws, ws->name);
1288 bool workspace_was_visible = workspace_is_visible(ws);
1290 if (con_num_children(ws->parent) == 1) {
1291 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1293 /* check if we can find a workspace assigned to this output */
1294 bool used_assignment = false;
1295 struct Workspace_Assignment *assignment;
1296 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1297 if (strcmp(assignment->output, current_output->name) != 0)
1300 /* check if this workspace is already attached to the tree */
1301 Con *workspace = NULL, *out;
1302 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1303 GREP_FIRST(workspace, output_get_content(out),
1304 !strcasecmp(child->name, assignment->name));
1305 if (workspace != NULL)
1308 /* so create the workspace referenced to by this assignment */
1309 LOG("Creating workspace from assignment %s.\n", assignment->name);
1310 workspace_get(assignment->name, NULL);
1311 used_assignment = true;
1315 /* if we couldn't create the workspace using an assignment, create
1316 * it on the output */
1317 if (!used_assignment)
1318 create_workspace_on_output(current_output, ws->parent);
1320 /* notify the IPC listeners */
1321 ipc_send_workspace_event("init", ws, NULL);
1323 DLOG("Detaching\n");
1325 /* detach from the old output and attach to the new output */
1326 Con *old_content = ws->parent;
1328 if (workspace_was_visible) {
1329 /* The workspace which we just detached was visible, so focus
1330 * the next one in the focus-stack. */
1331 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1332 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1333 workspace_show(focus_ws);
1335 con_attach(ws, content, false);
1337 /* fix the coordinates of the floating containers */
1339 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1340 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1342 ipc_send_workspace_event("move", ws, NULL);
1343 if (workspace_was_visible) {
1344 /* Focus the moved workspace on the destination output. */
1348 /* NB: We cannot simply work with previously_visible_ws since it might
1349 * have been cleaned up by workspace_show() already, depending on the
1350 * focus order/number of other workspaces on the output.
1351 * Instead, we loop through the available workspaces and only work with
1352 * previously_visible_ws if we still find it. */
1353 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1354 if (ws != previously_visible_ws)
1357 /* Call the on_remove_child callback of the workspace which previously
1358 * was visible on the destination output. Since it is no longer
1359 * visible, it might need to get cleaned up. */
1360 CALL(previously_visible_ws, on_remove_child);
1365 cmd_output->needs_tree_render = true;
1366 // XXX: default reply for now, make this a better reply
1371 * Implementation of 'split v|h|vertical|horizontal'.
1374 void cmd_split(I3_CMD, char *direction) {
1376 /* TODO: use matches */
1377 LOG("splitting in direction %c\n", direction[0]);
1378 if (match_is_empty(current_match))
1379 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1381 TAILQ_FOREACH(current, &owindows, owindows) {
1382 DLOG("matching: %p / %s\n", current->con, current->con->name);
1383 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1387 cmd_output->needs_tree_render = true;
1388 // XXX: default reply for now, make this a better reply
1393 * Implementation of 'kill [window|client]'.
1396 void cmd_kill(I3_CMD, char *kill_mode_str) {
1397 if (kill_mode_str == NULL)
1398 kill_mode_str = "window";
1401 DLOG("kill_mode=%s\n", kill_mode_str);
1404 if (strcmp(kill_mode_str, "window") == 0)
1405 kill_mode = KILL_WINDOW;
1406 else if (strcmp(kill_mode_str, "client") == 0)
1407 kill_mode = KILL_CLIENT;
1409 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1414 /* check if the match is empty, not if the result is empty */
1415 if (match_is_empty(current_match))
1416 tree_close_con(kill_mode);
1418 TAILQ_FOREACH(current, &owindows, owindows) {
1419 DLOG("matching: %p / %s\n", current->con, current->con->name);
1420 tree_close(current->con, kill_mode, false, false);
1424 cmd_output->needs_tree_render = true;
1425 // XXX: default reply for now, make this a better reply
1430 * Implementation of 'exec [--no-startup-id] <command>'.
1433 void cmd_exec(I3_CMD, char *nosn, char *command) {
1434 bool no_startup_id = (nosn != NULL);
1436 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1437 start_application(command, no_startup_id);
1439 // XXX: default reply for now, make this a better reply
1444 * Implementation of 'focus left|right|up|down'.
1447 void cmd_focus_direction(I3_CMD, char *direction) {
1448 DLOG("direction = *%s*\n", direction);
1450 if (strcmp(direction, "left") == 0)
1451 tree_next('p', HORIZ);
1452 else if (strcmp(direction, "right") == 0)
1453 tree_next('n', HORIZ);
1454 else if (strcmp(direction, "up") == 0)
1455 tree_next('p', VERT);
1456 else if (strcmp(direction, "down") == 0)
1457 tree_next('n', VERT);
1459 ELOG("Invalid focus direction (%s)\n", direction);
1464 cmd_output->needs_tree_render = true;
1465 // XXX: default reply for now, make this a better reply
1470 * Implementation of 'focus tiling|floating|mode_toggle'.
1473 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1474 DLOG("window_mode = %s\n", window_mode);
1476 Con *ws = con_get_workspace(focused);
1479 if (strcmp(window_mode, "mode_toggle") == 0) {
1480 current = TAILQ_FIRST(&(ws->focus_head));
1481 if (current != NULL && current->type == CT_FLOATING_CON)
1482 window_mode = "tiling";
1484 window_mode = "floating";
1486 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1487 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1488 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1491 con_focus(con_descend_focused(current));
1496 cmd_output->needs_tree_render = true;
1497 // XXX: default reply for now, make this a better reply
1502 * Implementation of 'focus parent|child'.
1505 void cmd_focus_level(I3_CMD, char *level) {
1506 DLOG("level = %s\n", level);
1507 bool success = false;
1509 /* Focusing the parent can only be allowed if the newly
1510 * focused container won't escape the fullscreen container. */
1511 if (strcmp(level, "parent") == 0) {
1512 if (focused && focused->parent) {
1513 if (con_fullscreen_permits_focusing(focused->parent))
1514 success = level_up();
1516 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1520 /* Focusing a child should always be allowed. */
1522 success = level_down();
1524 cmd_output->needs_tree_render = success;
1525 // XXX: default reply for now, make this a better reply
1530 * Implementation of 'focus'.
1533 void cmd_focus(I3_CMD) {
1534 DLOG("current_match = %p\n", current_match);
1536 if (match_is_empty(current_match)) {
1537 ELOG("You have to specify which window/container should be focused.\n");
1538 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1540 yerror("You have to specify which window/container should be focused");
1545 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1548 TAILQ_FOREACH(current, &owindows, owindows) {
1549 Con *ws = con_get_workspace(current->con);
1550 /* If no workspace could be found, this was a dock window.
1551 * Just skip it, you cannot focus dock windows. */
1555 /* Check the fullscreen focus constraints. */
1556 if (!con_fullscreen_permits_focusing(current->con)) {
1557 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1562 /* In case this is a scratchpad window, call scratchpad_show(). */
1563 if (ws == __i3_scratch) {
1564 scratchpad_show(current->con);
1566 /* While for the normal focus case we can change focus multiple
1567 * times and only a single window ends up focused, we could show
1568 * multiple scratchpad windows. So, rather break here. */
1572 /* If the container is not on the current workspace,
1573 * workspace_show() will switch to a different workspace and (if
1574 * enabled) trigger a mouse pointer warp to the currently focused
1575 * container (!) on the target workspace.
1577 * Therefore, before calling workspace_show(), we make sure that
1578 * 'current' will be focused on the workspace. However, we cannot
1579 * just con_focus(current) because then the pointer will not be
1580 * warped at all (the code thinks we are already there).
1582 * So we focus 'current' to make it the currently focused window of
1583 * the target workspace, then revert focus. */
1584 Con *currently_focused = focused;
1585 con_focus(current->con);
1586 con_focus(currently_focused);
1588 /* Now switch to the workspace, then focus */
1590 LOG("focusing %p / %s\n", current->con, current->con->name);
1591 con_focus(current->con);
1596 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1597 "while only exactly one container can be focused at a time.\n",
1600 cmd_output->needs_tree_render = true;
1601 // XXX: default reply for now, make this a better reply
1606 * Implementation of 'fullscreen enable|toggle [global]' and
1607 * 'fullscreen disable'
1610 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1611 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1612 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1617 TAILQ_FOREACH(current, &owindows, owindows) {
1618 DLOG("matching: %p / %s\n", current->con, current->con->name);
1619 if (strcmp(action, "toggle") == 0) {
1620 con_toggle_fullscreen(current->con, mode);
1621 } else if (strcmp(action, "enable") == 0) {
1622 con_enable_fullscreen(current->con, mode);
1623 } else if (strcmp(action, "disable") == 0) {
1624 con_disable_fullscreen(current->con);
1628 cmd_output->needs_tree_render = true;
1629 // XXX: default reply for now, make this a better reply
1634 * Implementation of 'move <direction> [<pixels> [px]]'.
1637 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1638 // 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
1639 int px = atoi(move_px);
1644 Con *initially_focused = focused;
1646 TAILQ_FOREACH(current, &owindows, owindows) {
1647 DLOG("moving in direction %s, px %s\n", direction, move_px);
1648 if (con_is_floating(current->con)) {
1649 DLOG("floating move with %d pixels\n", px);
1650 Rect newrect = current->con->parent->rect;
1651 if (strcmp(direction, "left") == 0) {
1653 } else if (strcmp(direction, "right") == 0) {
1655 } else if (strcmp(direction, "up") == 0) {
1657 } else if (strcmp(direction, "down") == 0) {
1660 floating_reposition(current->con->parent, newrect);
1662 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1663 cmd_output->needs_tree_render = true;
1667 /* the move command should not disturb focus */
1668 if (focused != initially_focused)
1669 con_focus(initially_focused);
1671 // XXX: default reply for now, make this a better reply
1676 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1679 void cmd_layout(I3_CMD, char *layout_str) {
1680 if (strcmp(layout_str, "stacking") == 0)
1681 layout_str = "stacked";
1684 /* default is a special case which will be handled in con_set_layout(). */
1685 if (strcmp(layout_str, "default") == 0)
1687 else if (strcmp(layout_str, "stacked") == 0)
1689 else if (strcmp(layout_str, "tabbed") == 0)
1691 else if (strcmp(layout_str, "splitv") == 0)
1693 else if (strcmp(layout_str, "splith") == 0)
1696 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1700 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1702 /* check if the match is empty, not if the result is empty */
1703 if (match_is_empty(current_match))
1704 con_set_layout(focused, layout);
1706 TAILQ_FOREACH(current, &owindows, owindows) {
1707 DLOG("matching: %p / %s\n", current->con, current->con->name);
1708 con_set_layout(current->con, layout);
1712 cmd_output->needs_tree_render = true;
1713 // XXX: default reply for now, make this a better reply
1718 * Implementation of 'layout toggle [all|split]'.
1721 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1724 if (toggle_mode == NULL)
1725 toggle_mode = "default";
1727 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1729 /* check if the match is empty, not if the result is empty */
1730 if (match_is_empty(current_match))
1731 con_toggle_layout(focused, toggle_mode);
1733 TAILQ_FOREACH(current, &owindows, owindows) {
1734 DLOG("matching: %p / %s\n", current->con, current->con->name);
1735 con_toggle_layout(current->con, toggle_mode);
1739 cmd_output->needs_tree_render = true;
1740 // XXX: default reply for now, make this a better reply
1745 * Implementation of 'exit'.
1748 void cmd_exit(I3_CMD) {
1749 LOG("Exiting due to user command.\n");
1751 unlink(config.ipc_socket_path);
1752 xcb_disconnect(conn);
1759 * Implementation of 'reload'.
1762 void cmd_reload(I3_CMD) {
1764 kill_nagbar(&config_error_nagbar_pid, false);
1765 kill_nagbar(&command_error_nagbar_pid, false);
1766 load_configuration(conn, NULL, true);
1768 /* Send an IPC event just in case the ws names have changed */
1769 ipc_send_workspace_event("reload", NULL, NULL);
1770 /* Send an update event for the barconfig just in case it has changed */
1773 // XXX: default reply for now, make this a better reply
1778 * Implementation of 'restart'.
1781 void cmd_restart(I3_CMD) {
1782 LOG("restarting i3\n");
1784 unlink(config.ipc_socket_path);
1785 /* We need to call this manually since atexit handlers don’t get called
1787 purge_zerobyte_logfile();
1790 // XXX: default reply for now, make this a better reply
1795 * Implementation of 'open'.
1798 void cmd_open(I3_CMD) {
1799 LOG("opening new container\n");
1800 Con *con = tree_open_con(NULL, NULL);
1801 con->layout = L_SPLITH;
1808 y(integer, (long int)con);
1811 cmd_output->needs_tree_render = true;
1815 * Implementation of 'focus output <output>'.
1818 void cmd_focus_output(I3_CMD, char *name) {
1821 DLOG("name = %s\n", name);
1825 /* get the output */
1826 Output *current_output = NULL;
1829 TAILQ_FOREACH(current, &owindows, owindows)
1830 current_output = get_output_of_con(current->con);
1831 assert(current_output != NULL);
1833 output = get_output_from_string(current_output, name);
1836 LOG("No such output found.\n");
1841 /* get visible workspace on output */
1843 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1851 cmd_output->needs_tree_render = true;
1852 // XXX: default reply for now, make this a better reply
1857 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1860 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1863 bool has_error = false;
1868 TAILQ_FOREACH(current, &owindows, owindows) {
1869 if (!con_is_floating(current->con)) {
1870 ELOG("Cannot change position. The window/container is not floating\n");
1873 yerror("Cannot change position of a window/container because it is not floating.");
1880 if (strcmp(method, "absolute") == 0) {
1881 current->con->parent->rect.x = x;
1882 current->con->parent->rect.y = y;
1884 DLOG("moving to absolute position %d %d\n", x, y);
1885 floating_maybe_reassign_ws(current->con->parent);
1886 cmd_output->needs_tree_render = true;
1889 if (strcmp(method, "position") == 0) {
1890 Rect newrect = current->con->parent->rect;
1892 DLOG("moving to position %d %d\n", x, y);
1896 floating_reposition(current->con->parent, newrect);
1900 // XXX: default reply for now, make this a better reply
1906 * Implementation of 'move [window|container] [to] [absolute] position center
1909 void cmd_move_window_to_center(I3_CMD, char *method) {
1910 if (!con_is_floating(focused)) {
1911 ELOG("Cannot change position. The window/container is not floating\n");
1912 yerror("Cannot change position. The window/container is not floating.");
1916 if (strcmp(method, "absolute") == 0) {
1917 Rect *rect = &focused->parent->rect;
1919 DLOG("moving to absolute center\n");
1920 rect->x = croot->rect.width / 2 - rect->width / 2;
1921 rect->y = croot->rect.height / 2 - rect->height / 2;
1923 floating_maybe_reassign_ws(focused->parent);
1924 cmd_output->needs_tree_render = true;
1927 if (strcmp(method, "position") == 0) {
1928 Rect *wsrect = &con_get_workspace(focused)->rect;
1929 Rect newrect = focused->parent->rect;
1931 DLOG("moving to center\n");
1932 newrect.x = wsrect->width / 2 - newrect.width / 2;
1933 newrect.y = wsrect->height / 2 - newrect.height / 2;
1935 floating_reposition(focused->parent, newrect);
1938 // XXX: default reply for now, make this a better reply
1943 * Implementation of 'move scratchpad'.
1946 void cmd_move_scratchpad(I3_CMD) {
1947 DLOG("should move window to scratchpad\n");
1952 TAILQ_FOREACH(current, &owindows, owindows) {
1953 DLOG("matching: %p / %s\n", current->con, current->con->name);
1954 scratchpad_move(current->con);
1957 cmd_output->needs_tree_render = true;
1958 // XXX: default reply for now, make this a better reply
1963 * Implementation of 'scratchpad show'.
1966 void cmd_scratchpad_show(I3_CMD) {
1967 DLOG("should show scratchpad window\n");
1970 if (match_is_empty(current_match)) {
1971 scratchpad_show(NULL);
1973 TAILQ_FOREACH(current, &owindows, owindows) {
1974 DLOG("matching: %p / %s\n", current->con, current->con->name);
1975 scratchpad_show(current->con);
1979 cmd_output->needs_tree_render = true;
1980 // XXX: default reply for now, make this a better reply
1985 * Implementation of 'rename workspace [<name>] to <name>'
1988 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1989 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1990 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1995 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1997 LOG("Renaming current workspace to \"%s\"\n", new_name);
2000 Con *output, *workspace = NULL;
2002 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2003 GREP_FIRST(workspace, output_get_content(output),
2004 !strcasecmp(child->name, old_name));
2006 workspace = con_get_workspace(focused);
2010 // TODO: we should include the old workspace name here and use yajl for
2011 // generating the reply.
2012 // TODO: better error message
2013 yerror("Old workspace not found");
2017 Con *check_dest = NULL;
2018 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2019 GREP_FIRST(check_dest, output_get_content(output),
2020 !strcasecmp(child->name, new_name));
2022 if (check_dest != NULL) {
2023 // TODO: we should include the new workspace name here and use yajl for
2024 // generating the reply.
2025 // TODO: better error message
2026 yerror("New workspace already exists");
2030 /* Change the name and try to parse it as a number. */
2031 FREE(workspace->name);
2032 workspace->name = sstrdup(new_name);
2034 workspace->num = ws_name_to_number(new_name);
2035 LOG("num = %d\n", workspace->num);
2037 /* By re-attaching, the sort order will be correct afterwards. */
2038 Con *previously_focused = focused;
2039 Con *parent = workspace->parent;
2040 con_detach(workspace);
2041 con_attach(workspace, parent, false);
2042 /* Restore the previous focus since con_attach messes with the focus. */
2043 con_focus(previously_focused);
2045 cmd_output->needs_tree_render = true;
2048 ipc_send_workspace_event("rename", workspace, NULL);
2049 ewmh_update_desktop_names();
2050 ewmh_update_desktop_viewport();
2051 ewmh_update_current_desktop();
2055 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2058 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2060 bool toggle = false;
2061 if (strcmp(bar_mode, "dock") == 0)
2063 else if (strcmp(bar_mode, "hide") == 0)
2065 else if (strcmp(bar_mode, "invisible") == 0)
2067 else if (strcmp(bar_mode, "toggle") == 0)
2070 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2074 bool changed_sth = false;
2075 Barconfig *current = NULL;
2076 TAILQ_FOREACH(current, &barconfigs, configs) {
2077 if (bar_id && strcmp(current->id, bar_id) != 0)
2081 mode = (current->mode + 1) % 2;
2083 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2084 current->mode = mode;
2091 if (bar_id && !changed_sth) {
2092 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2100 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2103 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2104 int hidden_state = S_SHOW;
2105 bool toggle = false;
2106 if (strcmp(bar_hidden_state, "hide") == 0)
2107 hidden_state = S_HIDE;
2108 else if (strcmp(bar_hidden_state, "show") == 0)
2109 hidden_state = S_SHOW;
2110 else if (strcmp(bar_hidden_state, "toggle") == 0)
2113 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2117 bool changed_sth = false;
2118 Barconfig *current = NULL;
2119 TAILQ_FOREACH(current, &barconfigs, configs) {
2120 if (bar_id && strcmp(current->id, bar_id) != 0)
2124 hidden_state = (current->hidden_state + 1) % 2;
2126 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2127 current->hidden_state = hidden_state;
2134 if (bar_id && !changed_sth) {
2135 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2143 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2146 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2148 if (strcmp(bar_type, "mode") == 0)
2149 ret = cmd_bar_mode(bar_value, bar_id);
2150 else if (strcmp(bar_type, "hidden_state") == 0)
2151 ret = cmd_bar_hidden_state(bar_value, bar_id);
2153 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2165 * Implementation of 'shmlog <size>|toggle|on|off'
2168 void cmd_shmlog(I3_CMD, char *argument) {
2169 if (!strcmp(argument, "toggle"))
2170 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2171 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2172 else if (!strcmp(argument, "on"))
2173 shmlog_size = default_shmlog_size;
2174 else if (!strcmp(argument, "off"))
2177 /* If shm logging now, restart logging with the new size. */
2178 if (shmlog_size > 0) {
2180 LOG("Restarting shm logging...\n");
2183 shmlog_size = atoi(argument);
2184 /* Make a weakly attempt at ensuring the argument is valid. */
2185 if (shmlog_size <= 0)
2186 shmlog_size = default_shmlog_size;
2188 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2190 update_shmlog_atom();
2191 // XXX: default reply for now, make this a better reply
2196 * Implementation of 'debuglog toggle|on|off'
2199 void cmd_debuglog(I3_CMD, char *argument) {
2200 bool logging = get_debug_logging();
2201 if (!strcmp(argument, "toggle")) {
2202 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2203 set_debug_logging(!logging);
2204 } else if (!strcmp(argument, "on") && !logging) {
2205 LOG("Enabling debug logging\n");
2206 set_debug_logging(true);
2207 } else if (!strcmp(argument, "off") && logging) {
2208 LOG("Disabling debug logging\n");
2209 set_debug_logging(false);
2211 // XXX: default reply for now, make this a better reply