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(format, ...) \
32 if (cmd_output->json_gen != NULL) { \
34 sasprintf(&message, format, ##__VA_ARGS__); \
45 /** When the command did not include match criteria (!), we use the currently
46 * focused container. Do not confuse this case with a command which included
47 * criteria but which did not match any windows. This macro has to be called in
50 #define HANDLE_EMPTY_MATCH \
52 if (match_is_empty(current_match)) { \
53 owindow *ow = smalloc(sizeof(owindow)); \
55 TAILQ_INIT(&owindows); \
56 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
61 * Returns true if a is definitely greater than b (using the given epsilon)
64 static bool definitelyGreaterThan(float a, float b, float epsilon) {
65 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
69 * Returns an 'output' corresponding to one of left/right/down/up or a specific
73 static Output *get_output_from_string(Output *current_output, const char *output_str) {
76 if (strcasecmp(output_str, "left") == 0)
77 output = get_output_next_wrap(D_LEFT, current_output);
78 else if (strcasecmp(output_str, "right") == 0)
79 output = get_output_next_wrap(D_RIGHT, current_output);
80 else if (strcasecmp(output_str, "up") == 0)
81 output = get_output_next_wrap(D_UP, current_output);
82 else if (strcasecmp(output_str, "down") == 0)
83 output = get_output_next_wrap(D_DOWN, current_output);
85 output = get_output_by_name(output_str);
91 * Returns the output containing the given container.
93 static Output *get_output_of_con(Con *con) {
94 Con *output_con = con_get_output(con);
95 Output *output = get_output_by_name(output_con->name);
96 assert(output != NULL);
102 * Checks whether we switched to a new workspace and returns false in that case,
103 * signaling that further workspace switching should be done by the calling function
104 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
105 * and return true, signaling that no further workspace switching should occur in the calling function.
108 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
109 Con *ws = con_get_workspace(focused);
111 /* If we switched to a different workspace, do nothing */
112 if (strcmp(ws->name, name) != 0)
115 DLOG("This workspace is already focused.\n");
116 if (config.workspace_auto_back_and_forth) {
117 workspace_back_and_forth();
118 cmd_output->needs_tree_render = true;
124 * Return the passed workspace unless it is the current one and auto back and
125 * forth is enabled, in which case the back_and_forth workspace is returned.
127 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
130 if (!config.workspace_auto_back_and_forth)
133 current = con_get_workspace(focused);
135 if (current == workspace) {
136 baf = workspace_back_and_forth_get();
138 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
146 // This code is commented out because we might recycle it for popping up error
147 // messages on parser errors.
149 static pid_t migration_pid = -1;
152 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
153 * it exited (or could not be started, depending on the exit code).
156 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
157 ev_child_stop(EV_A_ watcher);
158 if (!WIFEXITED(watcher->rstatus)) {
159 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
163 int exitcode = WEXITSTATUS(watcher->rstatus);
164 printf("i3-nagbar process exited with status %d\n", exitcode);
166 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
172 /* We need ev >= 4 for the following code. Since it is not *that* important (it
173 * only makes sure that there are no i3-nagbar instances left behind) we still
174 * support old systems with libev 3. */
175 #if EV_VERSION_MAJOR >= 4
177 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
178 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
181 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
182 if (migration_pid != -1) {
183 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
184 kill(migration_pid, SIGKILL);
189 void cmd_MIGRATION_start_nagbar(void) {
190 if (migration_pid != -1) {
191 fprintf(stderr, "i3-nagbar already running.\n");
194 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
195 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
196 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
197 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
198 migration_pid = fork();
199 if (migration_pid == -1) {
200 warn("Could not fork()");
205 if (migration_pid == 0) {
207 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
209 NULL, /* will be replaced by the executable path */
213 "You found a parsing error. Please, please, please, report it!",
219 exec_i3_utility("i3-nagbar", argv);
223 /* install a child watcher */
224 ev_child *child = smalloc(sizeof(ev_child));
225 ev_child_init(child, &nagbar_exited, migration_pid, 0);
226 ev_child_start(main_loop, child);
228 /* We need ev >= 4 for the following code. Since it is not *that* important (it
229 * only makes sure that there are no i3-nagbar instances left behind) we still
230 * support old systems with libev 3. */
231 #if EV_VERSION_MAJOR >= 4
232 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
234 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
235 ev_cleanup_init(cleanup, nagbar_cleanup);
236 ev_cleanup_start(main_loop, cleanup);
242 /*******************************************************************************
243 * Criteria functions.
244 ******************************************************************************/
247 * Helper data structure for an operation window (window on which the operation
248 * will be performed). Used to build the TAILQ owindows.
251 typedef struct owindow {
253 TAILQ_ENTRY(owindow) owindows;
256 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
258 static owindows_head owindows;
261 * Initializes the specified 'Match' data structure and the initial state of
262 * commands.c for matching target windows of a command.
265 void cmd_criteria_init(I3_CMD) {
269 DLOG("Initializing criteria, current_match = %p\n", current_match);
270 match_free(current_match);
271 match_init(current_match);
272 while (!TAILQ_EMPTY(&owindows)) {
273 ow = TAILQ_FIRST(&owindows);
274 TAILQ_REMOVE(&owindows, ow, owindows);
277 TAILQ_INIT(&owindows);
279 TAILQ_FOREACH(con, &all_cons, all_cons) {
280 ow = smalloc(sizeof(owindow));
282 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
287 * A match specification just finished (the closing square bracket was found),
288 * so we filter the list of owindows.
291 void cmd_criteria_match_windows(I3_CMD) {
292 owindow *next, *current;
294 DLOG("match specification finished, matching...\n");
295 /* copy the old list head to iterate through it and start with a fresh
296 * list which will contain only matching windows */
297 struct owindows_head old = owindows;
298 TAILQ_INIT(&owindows);
299 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
300 /* make a copy of the next pointer and advance the pointer to the
301 * next element as we are going to invalidate the element’s
302 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
304 next = TAILQ_NEXT(next, owindows);
306 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
307 if (current_match->con_id != NULL) {
308 if (current_match->con_id == current->con) {
309 DLOG("matches container!\n");
310 TAILQ_INSERT_TAIL(&owindows, current, owindows);
312 DLOG("doesnt match\n");
315 } else if (current_match->mark != NULL && current->con->mark != NULL &&
316 regex_matches(current_match->mark, current->con->mark)) {
317 DLOG("match by mark\n");
318 TAILQ_INSERT_TAIL(&owindows, current, owindows);
320 if (current->con->window && match_matches_window(current_match, current->con->window)) {
321 DLOG("matches window!\n");
322 TAILQ_INSERT_TAIL(&owindows, current, owindows);
324 DLOG("doesnt match\n");
330 TAILQ_FOREACH(current, &owindows, owindows) {
331 DLOG("matching: %p / %s\n", current->con, current->con->name);
336 * Interprets a ctype=cvalue pair and adds it to the current match
340 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
341 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
343 if (strcmp(ctype, "class") == 0) {
344 current_match->class = regex_new(cvalue);
348 if (strcmp(ctype, "instance") == 0) {
349 current_match->instance = regex_new(cvalue);
353 if (strcmp(ctype, "window_role") == 0) {
354 current_match->window_role = regex_new(cvalue);
358 if (strcmp(ctype, "con_id") == 0) {
360 long parsed = strtol(cvalue, &end, 10);
361 if (parsed == LONG_MIN ||
362 parsed == LONG_MAX ||
364 (end && *end != '\0')) {
365 ELOG("Could not parse con id \"%s\"\n", cvalue);
367 current_match->con_id = (Con *)parsed;
368 DLOG("id as int = %p\n", current_match->con_id);
373 if (strcmp(ctype, "id") == 0) {
375 long parsed = strtol(cvalue, &end, 10);
376 if (parsed == LONG_MIN ||
377 parsed == LONG_MAX ||
379 (end && *end != '\0')) {
380 ELOG("Could not parse window id \"%s\"\n", cvalue);
382 current_match->id = parsed;
383 DLOG("window id as int = %d\n", current_match->id);
388 if (strcmp(ctype, "con_mark") == 0) {
389 current_match->mark = regex_new(cvalue);
393 if (strcmp(ctype, "title") == 0) {
394 current_match->title = regex_new(cvalue);
398 if (strcmp(ctype, "urgent") == 0) {
399 if (strcasecmp(cvalue, "latest") == 0 ||
400 strcasecmp(cvalue, "newest") == 0 ||
401 strcasecmp(cvalue, "recent") == 0 ||
402 strcasecmp(cvalue, "last") == 0) {
403 current_match->urgent = U_LATEST;
404 } else if (strcasecmp(cvalue, "oldest") == 0 ||
405 strcasecmp(cvalue, "first") == 0) {
406 current_match->urgent = U_OLDEST;
411 ELOG("Unknown criterion: %s\n", ctype);
415 * Implementation of 'move [window|container] [to] workspace
416 * next|prev|next_on_output|prev_on_output|current'.
419 void cmd_move_con_to_workspace(I3_CMD, char *which) {
422 DLOG("which=%s\n", which);
424 /* We have nothing to move:
425 * when criteria was specified but didn't match any window or
426 * when criteria wasn't specified and we don't have any window focused. */
427 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
428 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
429 !con_has_children(focused))) {
436 /* get the workspace */
438 if (strcmp(which, "next") == 0)
439 ws = workspace_next();
440 else if (strcmp(which, "prev") == 0)
441 ws = workspace_prev();
442 else if (strcmp(which, "next_on_output") == 0)
443 ws = workspace_next_on_output();
444 else if (strcmp(which, "prev_on_output") == 0)
445 ws = workspace_prev_on_output();
446 else if (strcmp(which, "current") == 0)
447 ws = con_get_workspace(focused);
449 ELOG("BUG: called with which=%s\n", which);
454 TAILQ_FOREACH(current, &owindows, owindows) {
455 DLOG("matching: %p / %s\n", current->con, current->con->name);
456 con_move_to_workspace(current->con, ws, true, false);
459 cmd_output->needs_tree_render = true;
460 // XXX: default reply for now, make this a better reply
465 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
468 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
472 ws = workspace_back_and_forth_get();
475 yerror("No workspace was previously active.");
481 TAILQ_FOREACH(current, &owindows, owindows) {
482 DLOG("matching: %p / %s\n", current->con, current->con->name);
483 con_move_to_workspace(current->con, ws, true, false);
486 cmd_output->needs_tree_render = true;
487 // XXX: default reply for now, make this a better reply
492 * Implementation of 'move [window|container] [to] workspace <name>'.
495 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
496 if (strncasecmp(name, "__", strlen("__")) == 0) {
497 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
504 /* We have nothing to move:
505 * when criteria was specified but didn't match any window or
506 * when criteria wasn't specified and we don't have any window focused. */
507 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
508 ELOG("No windows match your criteria, cannot move.\n");
511 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
512 !con_has_children(focused)) {
517 LOG("should move window to workspace %s\n", name);
518 /* get the workspace */
522 /* first look for a workspace with this name */
523 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
524 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
527 /* if the name is plain digits, we interpret this as a "workspace number"
529 if (!ws && name_is_digits(name)) {
530 long parsed_num = ws_name_to_number(name);
531 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
532 GREP_FIRST(ws, output_get_content(output),
533 child->num == parsed_num);
537 /* if no workspace was found, make a new one */
539 ws = workspace_get(name, NULL);
541 ws = maybe_auto_back_and_forth_workspace(ws);
545 TAILQ_FOREACH(current, &owindows, owindows) {
546 DLOG("matching: %p / %s\n", current->con, current->con->name);
547 con_move_to_workspace(current->con, ws, true, false);
550 cmd_output->needs_tree_render = true;
551 // XXX: default reply for now, make this a better reply
556 * Implementation of 'move [window|container] [to] workspace number <name>'.
559 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
562 /* We have nothing to move:
563 * when criteria was specified but didn't match any window or
564 * when criteria wasn't specified and we don't have any window focused. */
565 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
566 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
567 !con_has_children(focused))) {
572 LOG("should move window to workspace %s\n", which);
573 /* get the workspace */
574 Con *output, *workspace = NULL;
576 long parsed_num = ws_name_to_number(which);
578 if (parsed_num == -1) {
579 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
580 yerror("Could not parse number \"%s\"", which);
584 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
585 GREP_FIRST(workspace, output_get_content(output),
586 child->num == parsed_num);
589 workspace = workspace_get(which, NULL);
592 workspace = maybe_auto_back_and_forth_workspace(workspace);
596 TAILQ_FOREACH(current, &owindows, owindows) {
597 DLOG("matching: %p / %s\n", current->con, current->con->name);
598 con_move_to_workspace(current->con, workspace, true, false);
601 cmd_output->needs_tree_render = true;
602 // XXX: default reply for now, make this a better reply
606 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
607 LOG("floating resize\n");
608 Rect old_rect = floating_con->rect;
609 Con *focused_con = con_descend_focused(floating_con);
611 /* ensure that resize will take place even if pixel increment is smaller than
612 * height increment or width increment.
614 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
615 strcmp(direction, "height") == 0) {
617 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
619 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
620 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
622 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
624 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
627 if (strcmp(direction, "up") == 0) {
628 floating_con->rect.height += px;
629 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
630 floating_con->rect.height += px;
631 } else if (strcmp(direction, "left") == 0) {
632 floating_con->rect.width += px;
634 floating_con->rect.width += px;
637 floating_check_size(floating_con);
639 /* Did we actually resize anything or did the size constraints prevent us?
640 * If we could not resize, exit now to not move the window. */
641 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
644 if (strcmp(direction, "up") == 0) {
645 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
646 } else if (strcmp(direction, "left") == 0) {
647 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
650 /* If this is a scratchpad window, don't auto center it from now on. */
651 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
652 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
655 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
656 LOG("tiling resize\n");
658 Con *first = current;
659 direction_t search_direction;
660 if (!strcmp(direction, "left"))
661 search_direction = D_LEFT;
662 else if (!strcmp(direction, "right"))
663 search_direction = D_RIGHT;
664 else if (!strcmp(direction, "up"))
665 search_direction = D_UP;
667 search_direction = D_DOWN;
669 bool res = resize_find_tiling_participants(&first, &second, search_direction);
671 LOG("No second container in this direction found.\n");
676 /* get the default percentage */
677 int children = con_num_children(first->parent);
678 LOG("ins. %d children\n", children);
679 double percentage = 1.0 / children;
680 LOG("default percentage = %f\n", percentage);
683 LOG("second->percent = %f\n", second->percent);
684 LOG("first->percent before = %f\n", first->percent);
685 if (first->percent == 0.0)
686 first->percent = percentage;
687 if (second->percent == 0.0)
688 second->percent = percentage;
689 double new_first_percent = first->percent + ((double)ppt / 100.0);
690 double new_second_percent = second->percent - ((double)ppt / 100.0);
691 LOG("new_first_percent = %f\n", new_first_percent);
692 LOG("new_second_percent = %f\n", new_second_percent);
693 /* Ensure that the new percentages are positive and greater than
694 * 0.05 to have a reasonable minimum size. */
695 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
696 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
697 first->percent += ((double)ppt / 100.0);
698 second->percent -= ((double)ppt / 100.0);
699 LOG("first->percent after = %f\n", first->percent);
700 LOG("second->percent after = %f\n", second->percent);
702 LOG("Not resizing, already at minimum size\n");
708 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
709 LOG("width/height resize\n");
710 /* get the appropriate current container (skip stacked/tabbed cons) */
711 while (current->parent->layout == L_STACKED ||
712 current->parent->layout == L_TABBED)
713 current = current->parent;
715 /* Then further go up until we find one with the matching orientation. */
716 orientation_t search_orientation =
717 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
719 while (current->type != CT_WORKSPACE &&
720 current->type != CT_FLOATING_CON &&
721 con_orientation(current->parent) != search_orientation)
722 current = current->parent;
724 /* get the default percentage */
725 int children = con_num_children(current->parent);
726 LOG("ins. %d children\n", children);
727 double percentage = 1.0 / children;
728 LOG("default percentage = %f\n", percentage);
730 orientation_t orientation = con_orientation(current->parent);
732 if ((orientation == HORIZ &&
733 strcmp(direction, "height") == 0) ||
734 (orientation == VERT &&
735 strcmp(direction, "width") == 0)) {
736 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
737 (orientation == HORIZ ? "horizontal" : "vertical"));
743 LOG("This is the only container, cannot resize.\n");
748 /* Ensure all the other children have a percentage set. */
750 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
751 LOG("child->percent = %f (child %p)\n", child->percent, child);
752 if (child->percent == 0.0)
753 child->percent = percentage;
756 double new_current_percent = current->percent + ((double)ppt / 100.0);
757 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
758 LOG("new_current_percent = %f\n", new_current_percent);
759 LOG("subtract_percent = %f\n", subtract_percent);
760 /* Ensure that the new percentages are positive and greater than
761 * 0.05 to have a reasonable minimum size. */
762 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
763 if (child == current)
765 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
766 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
771 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
772 LOG("Not resizing, already at minimum size\n");
777 current->percent += ((double)ppt / 100.0);
778 LOG("current->percent after = %f\n", current->percent);
780 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
781 if (child == current)
783 child->percent -= subtract_percent;
784 LOG("child->percent after (%p) = %f\n", child, child->percent);
791 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
794 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
795 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
796 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
797 // 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
798 int px = atoi(resize_px);
799 int ppt = atoi(resize_ppt);
800 if (strcmp(way, "shrink") == 0) {
808 TAILQ_FOREACH(current, &owindows, owindows) {
809 /* Don't handle dock windows (issue #1201) */
810 if (current->con->window && current->con->window->dock) {
811 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
816 if ((floating_con = con_inside_floating(current->con))) {
817 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
819 if (strcmp(direction, "width") == 0 ||
820 strcmp(direction, "height") == 0) {
821 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
824 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
830 cmd_output->needs_tree_render = true;
831 // XXX: default reply for now, make this a better reply
836 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
839 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
840 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
845 TAILQ_FOREACH(current, &owindows, owindows) {
846 DLOG("matching: %p / %s\n", current->con, current->con->name);
847 int border_style = current->con->border_style;
849 int tmp_border_width = -1;
850 tmp_border_width = strtol(border_width, &end, 10);
851 if (end == border_width) {
852 /* no valid digits found */
853 tmp_border_width = -1;
855 if (strcmp(border_style_str, "toggle") == 0) {
858 if (border_style == BS_NORMAL)
859 tmp_border_width = 2;
860 else if (border_style == BS_NONE)
861 tmp_border_width = 0;
862 else if (border_style == BS_PIXEL)
863 tmp_border_width = 1;
865 if (strcmp(border_style_str, "normal") == 0)
866 border_style = BS_NORMAL;
867 else if (strcmp(border_style_str, "pixel") == 0)
868 border_style = BS_PIXEL;
869 else if (strcmp(border_style_str, "1pixel") == 0) {
870 border_style = BS_PIXEL;
871 tmp_border_width = 1;
872 } else if (strcmp(border_style_str, "none") == 0)
873 border_style = BS_NONE;
875 ELOG("BUG: called with border_style=%s\n", border_style_str);
880 con_set_border_style(current->con, border_style, tmp_border_width);
883 cmd_output->needs_tree_render = true;
884 // XXX: default reply for now, make this a better reply
889 * Implementation of 'nop <comment>'.
892 void cmd_nop(I3_CMD, char *comment) {
893 LOG("-------------------------------------------------\n");
894 LOG(" NOP: %s\n", comment);
895 LOG("-------------------------------------------------\n");
899 * Implementation of 'append_layout <path>'.
902 void cmd_append_layout(I3_CMD, char *path) {
903 LOG("Appending layout \"%s\"\n", path);
905 /* Make sure we allow paths like '~/.i3/layout.json' */
906 path = resolve_tilde(path);
908 json_content_t content = json_determine_content(path);
909 LOG("JSON content = %d\n", content);
910 if (content == JSON_CONTENT_UNKNOWN) {
911 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
912 yerror("Could not determine the contents of \"%s\".", path);
917 Con *parent = focused;
918 if (content == JSON_CONTENT_WORKSPACE) {
919 parent = output_get_content(con_get_output(parent));
921 /* We need to append the layout to a split container, since a leaf
922 * container must not have any children (by definition).
923 * Note that we explicitly check for workspaces, since they are okay for
924 * this purpose, but con_accepts_window() returns false for workspaces. */
925 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
926 parent = parent->parent;
928 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
929 char *errormsg = NULL;
930 tree_append_json(parent, path, &errormsg);
931 if (errormsg != NULL) {
934 /* Note that we continue executing since tree_append_json() has
935 * side-effects — user-provided layouts can be partly valid, partly
936 * invalid, leading to half of the placeholder containers being
942 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
943 // false); should be enough, but when sending 'workspace 4; append_layout
944 // /tmp/foo.json', the needs_tree_render == true of the workspace command
945 // is not executed yet and will be batched with append_layout’s
946 // needs_tree_render after the parser finished. We should check if that is
948 render_con(croot, false);
950 restore_open_placeholder_windows(parent);
952 if (content == JSON_CONTENT_WORKSPACE)
953 ipc_send_workspace_event("restored", parent, NULL);
956 cmd_output->needs_tree_render = true;
960 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
963 void cmd_workspace(I3_CMD, char *which) {
966 DLOG("which=%s\n", which);
968 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
969 LOG("Cannot switch workspace while in global fullscreen\n");
974 if (strcmp(which, "next") == 0)
975 ws = workspace_next();
976 else if (strcmp(which, "prev") == 0)
977 ws = workspace_prev();
978 else if (strcmp(which, "next_on_output") == 0)
979 ws = workspace_next_on_output();
980 else if (strcmp(which, "prev_on_output") == 0)
981 ws = workspace_prev_on_output();
983 ELOG("BUG: called with which=%s\n", which);
990 cmd_output->needs_tree_render = true;
991 // XXX: default reply for now, make this a better reply
996 * Implementation of 'workspace number <name>'
999 void cmd_workspace_number(I3_CMD, char *which) {
1000 Con *output, *workspace = NULL;
1002 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1003 LOG("Cannot switch workspace while in global fullscreen\n");
1008 long parsed_num = ws_name_to_number(which);
1010 if (parsed_num == -1) {
1011 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1012 yerror("Could not parse number \"%s\"", which);
1016 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1017 GREP_FIRST(workspace, output_get_content(output),
1018 child->num == parsed_num);
1021 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1023 workspace_show_by_name(which);
1024 cmd_output->needs_tree_render = true;
1027 if (maybe_back_and_forth(cmd_output, workspace->name))
1029 workspace_show(workspace);
1031 cmd_output->needs_tree_render = true;
1032 // XXX: default reply for now, make this a better reply
1037 * Implementation of 'workspace back_and_forth'.
1040 void cmd_workspace_back_and_forth(I3_CMD) {
1041 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1042 LOG("Cannot switch workspace while in global fullscreen\n");
1047 workspace_back_and_forth();
1049 cmd_output->needs_tree_render = true;
1050 // XXX: default reply for now, make this a better reply
1055 * Implementation of 'workspace <name>'
1058 void cmd_workspace_name(I3_CMD, char *name) {
1059 if (strncasecmp(name, "__", strlen("__")) == 0) {
1060 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1065 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1066 LOG("Cannot switch workspace while in global fullscreen\n");
1071 DLOG("should switch to workspace %s\n", name);
1072 if (maybe_back_and_forth(cmd_output, name))
1078 /* first look for a workspace with this name */
1079 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1080 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1083 /* if the name is only digits, we interpret this as a "workspace number"
1085 if (!ws && name_is_digits(name)) {
1086 long parsed_num = ws_name_to_number(name);
1087 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1088 GREP_FIRST(ws, output_get_content(output),
1089 child->num == parsed_num);
1093 /* if no workspace was found, make a new one */
1095 ws = workspace_get(name, NULL);
1099 cmd_output->needs_tree_render = true;
1100 // XXX: default reply for now, make this a better reply
1105 * Implementation of 'mark <mark>'
1108 void cmd_mark(I3_CMD, char *mark) {
1109 DLOG("Clearing all windows which have that mark first\n");
1112 TAILQ_FOREACH(con, &all_cons, all_cons) {
1113 if (con->mark && strcmp(con->mark, mark) == 0)
1117 DLOG("marking window with str %s\n", mark);
1122 TAILQ_FOREACH(current, &owindows, owindows) {
1123 DLOG("matching: %p / %s\n", current->con, current->con->name);
1124 current->con->mark = sstrdup(mark);
1127 cmd_output->needs_tree_render = true;
1128 // XXX: default reply for now, make this a better reply
1133 * Implementation of 'unmark [mark]'
1136 void cmd_unmark(I3_CMD, char *mark) {
1139 TAILQ_FOREACH(con, &all_cons, all_cons) {
1142 DLOG("removed all window marks");
1145 TAILQ_FOREACH(con, &all_cons, all_cons) {
1146 if (con->mark && strcmp(con->mark, mark) == 0)
1149 DLOG("removed window mark %s\n", mark);
1152 cmd_output->needs_tree_render = true;
1153 // XXX: default reply for now, make this a better reply
1158 * Implementation of 'mode <string>'.
1161 void cmd_mode(I3_CMD, char *mode) {
1162 DLOG("mode=%s\n", mode);
1165 // XXX: default reply for now, make this a better reply
1170 * Implementation of 'move [window|container] [to] output <str>'.
1173 void cmd_move_con_to_output(I3_CMD, char *name) {
1176 DLOG("should move window to output %s\n", name);
1180 /* get the output */
1181 Output *current_output = NULL;
1184 // TODO: fix the handling of criteria
1185 TAILQ_FOREACH(current, &owindows, owindows)
1186 current_output = get_output_of_con(current->con);
1188 assert(current_output != NULL);
1190 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1191 if (strcasecmp(name, "up") == 0)
1192 output = get_output_next_wrap(D_UP, current_output);
1193 else if (strcasecmp(name, "down") == 0)
1194 output = get_output_next_wrap(D_DOWN, current_output);
1195 else if (strcasecmp(name, "left") == 0)
1196 output = get_output_next_wrap(D_LEFT, current_output);
1197 else if (strcasecmp(name, "right") == 0)
1198 output = get_output_next_wrap(D_RIGHT, current_output);
1200 output = get_output_by_name(name);
1203 LOG("No such output found.\n");
1208 /* get visible workspace on output */
1210 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1216 TAILQ_FOREACH(current, &owindows, owindows) {
1217 DLOG("matching: %p / %s\n", current->con, current->con->name);
1218 con_move_to_workspace(current->con, ws, true, false);
1221 cmd_output->needs_tree_render = true;
1222 // XXX: default reply for now, make this a better reply
1227 * Implementation of 'floating enable|disable|toggle'
1230 void cmd_floating(I3_CMD, char *floating_mode) {
1233 DLOG("floating_mode=%s\n", floating_mode);
1237 TAILQ_FOREACH(current, &owindows, owindows) {
1238 DLOG("matching: %p / %s\n", current->con, current->con->name);
1239 if (strcmp(floating_mode, "toggle") == 0) {
1240 DLOG("should toggle mode\n");
1241 toggle_floating_mode(current->con, false);
1243 DLOG("should switch mode to %s\n", floating_mode);
1244 if (strcmp(floating_mode, "enable") == 0) {
1245 floating_enable(current->con, false);
1247 floating_disable(current->con, false);
1252 cmd_output->needs_tree_render = true;
1253 // XXX: default reply for now, make this a better reply
1258 * Implementation of 'move workspace to [output] <str>'.
1261 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1262 DLOG("should move workspace to output %s\n", name);
1267 TAILQ_FOREACH(current, &owindows, owindows) {
1268 Output *current_output = get_output_of_con(current->con);
1269 if (!current_output) {
1270 ELOG("Cannot get current output. This is a bug in i3.\n");
1274 Output *output = get_output_from_string(current_output, name);
1276 ELOG("Could not get output from string \"%s\"\n", name);
1281 Con *content = output_get_content(output->con);
1282 LOG("got output %p with content %p\n", output, content);
1284 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1285 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1287 Con *ws = con_get_workspace(current->con);
1288 LOG("should move workspace %p / %s\n", ws, ws->name);
1289 bool workspace_was_visible = workspace_is_visible(ws);
1291 if (con_num_children(ws->parent) == 1) {
1292 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1294 /* check if we can find a workspace assigned to this output */
1295 bool used_assignment = false;
1296 struct Workspace_Assignment *assignment;
1297 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1298 if (strcmp(assignment->output, current_output->name) != 0)
1301 /* check if this workspace is already attached to the tree */
1302 Con *workspace = NULL, *out;
1303 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1304 GREP_FIRST(workspace, output_get_content(out),
1305 !strcasecmp(child->name, assignment->name));
1306 if (workspace != NULL)
1309 /* so create the workspace referenced to by this assignment */
1310 LOG("Creating workspace from assignment %s.\n", assignment->name);
1311 workspace_get(assignment->name, NULL);
1312 used_assignment = true;
1316 /* if we couldn't create the workspace using an assignment, create
1317 * it on the output */
1318 if (!used_assignment)
1319 create_workspace_on_output(current_output, ws->parent);
1321 /* notify the IPC listeners */
1322 ipc_send_workspace_event("init", ws, NULL);
1324 DLOG("Detaching\n");
1326 /* detach from the old output and attach to the new output */
1327 Con *old_content = ws->parent;
1329 if (workspace_was_visible) {
1330 /* The workspace which we just detached was visible, so focus
1331 * the next one in the focus-stack. */
1332 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1333 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1334 workspace_show(focus_ws);
1336 con_attach(ws, content, false);
1338 /* fix the coordinates of the floating containers */
1340 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1341 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1343 ipc_send_workspace_event("move", ws, NULL);
1344 if (workspace_was_visible) {
1345 /* Focus the moved workspace on the destination output. */
1349 /* NB: We cannot simply work with previously_visible_ws since it might
1350 * have been cleaned up by workspace_show() already, depending on the
1351 * focus order/number of other workspaces on the output.
1352 * Instead, we loop through the available workspaces and only work with
1353 * previously_visible_ws if we still find it. */
1354 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1355 if (ws != previously_visible_ws)
1358 /* Call the on_remove_child callback of the workspace which previously
1359 * was visible on the destination output. Since it is no longer
1360 * visible, it might need to get cleaned up. */
1361 CALL(previously_visible_ws, on_remove_child);
1366 cmd_output->needs_tree_render = true;
1367 // XXX: default reply for now, make this a better reply
1372 * Implementation of 'split v|h|vertical|horizontal'.
1375 void cmd_split(I3_CMD, char *direction) {
1377 /* TODO: use matches */
1378 LOG("splitting in direction %c\n", direction[0]);
1379 if (match_is_empty(current_match))
1380 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1382 TAILQ_FOREACH(current, &owindows, owindows) {
1383 DLOG("matching: %p / %s\n", current->con, current->con->name);
1384 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1388 cmd_output->needs_tree_render = true;
1389 // XXX: default reply for now, make this a better reply
1394 * Implementation of 'kill [window|client]'.
1397 void cmd_kill(I3_CMD, char *kill_mode_str) {
1398 if (kill_mode_str == NULL)
1399 kill_mode_str = "window";
1402 DLOG("kill_mode=%s\n", kill_mode_str);
1405 if (strcmp(kill_mode_str, "window") == 0)
1406 kill_mode = KILL_WINDOW;
1407 else if (strcmp(kill_mode_str, "client") == 0)
1408 kill_mode = KILL_CLIENT;
1410 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1415 /* check if the match is empty, not if the result is empty */
1416 if (match_is_empty(current_match))
1417 tree_close_con(kill_mode);
1419 TAILQ_FOREACH(current, &owindows, owindows) {
1420 DLOG("matching: %p / %s\n", current->con, current->con->name);
1421 tree_close(current->con, kill_mode, false, false);
1425 cmd_output->needs_tree_render = true;
1426 // XXX: default reply for now, make this a better reply
1431 * Implementation of 'exec [--no-startup-id] <command>'.
1434 void cmd_exec(I3_CMD, char *nosn, char *command) {
1435 bool no_startup_id = (nosn != NULL);
1437 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1438 start_application(command, no_startup_id);
1440 // XXX: default reply for now, make this a better reply
1445 * Implementation of 'focus left|right|up|down'.
1448 void cmd_focus_direction(I3_CMD, char *direction) {
1449 DLOG("direction = *%s*\n", direction);
1451 if (strcmp(direction, "left") == 0)
1452 tree_next('p', HORIZ);
1453 else if (strcmp(direction, "right") == 0)
1454 tree_next('n', HORIZ);
1455 else if (strcmp(direction, "up") == 0)
1456 tree_next('p', VERT);
1457 else if (strcmp(direction, "down") == 0)
1458 tree_next('n', VERT);
1460 ELOG("Invalid focus direction (%s)\n", direction);
1465 cmd_output->needs_tree_render = true;
1466 // XXX: default reply for now, make this a better reply
1471 * Implementation of 'focus tiling|floating|mode_toggle'.
1474 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1475 DLOG("window_mode = %s\n", window_mode);
1477 Con *ws = con_get_workspace(focused);
1480 if (strcmp(window_mode, "mode_toggle") == 0) {
1481 current = TAILQ_FIRST(&(ws->focus_head));
1482 if (current != NULL && current->type == CT_FLOATING_CON)
1483 window_mode = "tiling";
1485 window_mode = "floating";
1487 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1488 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1489 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1492 con_focus(con_descend_focused(current));
1497 cmd_output->needs_tree_render = true;
1498 // XXX: default reply for now, make this a better reply
1503 * Implementation of 'focus parent|child'.
1506 void cmd_focus_level(I3_CMD, char *level) {
1507 DLOG("level = %s\n", level);
1508 bool success = false;
1510 /* Focusing the parent can only be allowed if the newly
1511 * focused container won't escape the fullscreen container. */
1512 if (strcmp(level, "parent") == 0) {
1513 if (focused && focused->parent) {
1514 if (con_fullscreen_permits_focusing(focused->parent))
1515 success = level_up();
1517 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1521 /* Focusing a child should always be allowed. */
1523 success = level_down();
1525 cmd_output->needs_tree_render = success;
1526 // XXX: default reply for now, make this a better reply
1531 * Implementation of 'focus'.
1534 void cmd_focus(I3_CMD) {
1535 DLOG("current_match = %p\n", current_match);
1537 if (match_is_empty(current_match)) {
1538 ELOG("You have to specify which window/container should be focused.\n");
1539 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1541 yerror("You have to specify which window/container should be focused");
1546 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1549 TAILQ_FOREACH(current, &owindows, owindows) {
1550 Con *ws = con_get_workspace(current->con);
1551 /* If no workspace could be found, this was a dock window.
1552 * Just skip it, you cannot focus dock windows. */
1556 /* Check the fullscreen focus constraints. */
1557 if (!con_fullscreen_permits_focusing(current->con)) {
1558 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1563 /* In case this is a scratchpad window, call scratchpad_show(). */
1564 if (ws == __i3_scratch) {
1565 scratchpad_show(current->con);
1567 /* While for the normal focus case we can change focus multiple
1568 * times and only a single window ends up focused, we could show
1569 * multiple scratchpad windows. So, rather break here. */
1573 /* If the container is not on the current workspace,
1574 * workspace_show() will switch to a different workspace and (if
1575 * enabled) trigger a mouse pointer warp to the currently focused
1576 * container (!) on the target workspace.
1578 * Therefore, before calling workspace_show(), we make sure that
1579 * 'current' will be focused on the workspace. However, we cannot
1580 * just con_focus(current) because then the pointer will not be
1581 * warped at all (the code thinks we are already there).
1583 * So we focus 'current' to make it the currently focused window of
1584 * the target workspace, then revert focus. */
1585 Con *currently_focused = focused;
1586 con_focus(current->con);
1587 con_focus(currently_focused);
1589 /* Now switch to the workspace, then focus */
1591 LOG("focusing %p / %s\n", current->con, current->con->name);
1592 con_focus(current->con);
1597 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1598 "while only exactly one container can be focused at a time.\n",
1601 cmd_output->needs_tree_render = true;
1602 // XXX: default reply for now, make this a better reply
1607 * Implementation of 'fullscreen enable|toggle [global]' and
1608 * 'fullscreen disable'
1611 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1612 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1613 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1618 TAILQ_FOREACH(current, &owindows, owindows) {
1619 DLOG("matching: %p / %s\n", current->con, current->con->name);
1620 if (strcmp(action, "toggle") == 0) {
1621 con_toggle_fullscreen(current->con, mode);
1622 } else if (strcmp(action, "enable") == 0) {
1623 con_enable_fullscreen(current->con, mode);
1624 } else if (strcmp(action, "disable") == 0) {
1625 con_disable_fullscreen(current->con);
1629 cmd_output->needs_tree_render = true;
1630 // XXX: default reply for now, make this a better reply
1635 * Implementation of 'move <direction> [<pixels> [px]]'.
1638 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1639 // 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
1640 int px = atoi(move_px);
1645 Con *initially_focused = focused;
1647 TAILQ_FOREACH(current, &owindows, owindows) {
1648 DLOG("moving in direction %s, px %s\n", direction, move_px);
1649 if (con_is_floating(current->con)) {
1650 DLOG("floating move with %d pixels\n", px);
1651 Rect newrect = current->con->parent->rect;
1652 if (strcmp(direction, "left") == 0) {
1654 } else if (strcmp(direction, "right") == 0) {
1656 } else if (strcmp(direction, "up") == 0) {
1658 } else if (strcmp(direction, "down") == 0) {
1661 floating_reposition(current->con->parent, newrect);
1663 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1664 cmd_output->needs_tree_render = true;
1668 /* the move command should not disturb focus */
1669 if (focused != initially_focused)
1670 con_focus(initially_focused);
1672 // XXX: default reply for now, make this a better reply
1677 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1680 void cmd_layout(I3_CMD, char *layout_str) {
1681 if (strcmp(layout_str, "stacking") == 0)
1682 layout_str = "stacked";
1685 /* default is a special case which will be handled in con_set_layout(). */
1686 if (strcmp(layout_str, "default") == 0)
1688 else if (strcmp(layout_str, "stacked") == 0)
1690 else if (strcmp(layout_str, "tabbed") == 0)
1692 else if (strcmp(layout_str, "splitv") == 0)
1694 else if (strcmp(layout_str, "splith") == 0)
1697 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1701 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1703 /* check if the match is empty, not if the result is empty */
1704 if (match_is_empty(current_match))
1705 con_set_layout(focused, layout);
1707 TAILQ_FOREACH(current, &owindows, owindows) {
1708 DLOG("matching: %p / %s\n", current->con, current->con->name);
1709 con_set_layout(current->con, layout);
1713 cmd_output->needs_tree_render = true;
1714 // XXX: default reply for now, make this a better reply
1719 * Implementation of 'layout toggle [all|split]'.
1722 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1725 if (toggle_mode == NULL)
1726 toggle_mode = "default";
1728 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1730 /* check if the match is empty, not if the result is empty */
1731 if (match_is_empty(current_match))
1732 con_toggle_layout(focused, toggle_mode);
1734 TAILQ_FOREACH(current, &owindows, owindows) {
1735 DLOG("matching: %p / %s\n", current->con, current->con->name);
1736 con_toggle_layout(current->con, toggle_mode);
1740 cmd_output->needs_tree_render = true;
1741 // XXX: default reply for now, make this a better reply
1746 * Implementation of 'exit'.
1749 void cmd_exit(I3_CMD) {
1750 LOG("Exiting due to user command.\n");
1752 unlink(config.ipc_socket_path);
1753 xcb_disconnect(conn);
1760 * Implementation of 'reload'.
1763 void cmd_reload(I3_CMD) {
1765 kill_nagbar(&config_error_nagbar_pid, false);
1766 kill_nagbar(&command_error_nagbar_pid, false);
1767 load_configuration(conn, NULL, true);
1769 /* Send an IPC event just in case the ws names have changed */
1770 ipc_send_workspace_event("reload", NULL, NULL);
1771 /* Send an update event for the barconfig just in case it has changed */
1774 // XXX: default reply for now, make this a better reply
1779 * Implementation of 'restart'.
1782 void cmd_restart(I3_CMD) {
1783 LOG("restarting i3\n");
1785 unlink(config.ipc_socket_path);
1786 /* We need to call this manually since atexit handlers don’t get called
1788 purge_zerobyte_logfile();
1791 // XXX: default reply for now, make this a better reply
1796 * Implementation of 'open'.
1799 void cmd_open(I3_CMD) {
1800 LOG("opening new container\n");
1801 Con *con = tree_open_con(NULL, NULL);
1802 con->layout = L_SPLITH;
1809 y(integer, (long int)con);
1812 cmd_output->needs_tree_render = true;
1816 * Implementation of 'focus output <output>'.
1819 void cmd_focus_output(I3_CMD, char *name) {
1822 DLOG("name = %s\n", name);
1826 /* get the output */
1827 Output *current_output = NULL;
1830 TAILQ_FOREACH(current, &owindows, owindows)
1831 current_output = get_output_of_con(current->con);
1832 assert(current_output != NULL);
1834 output = get_output_from_string(current_output, name);
1837 LOG("No such output found.\n");
1842 /* get visible workspace on output */
1844 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1852 cmd_output->needs_tree_render = true;
1853 // XXX: default reply for now, make this a better reply
1858 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1861 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1864 bool has_error = false;
1869 TAILQ_FOREACH(current, &owindows, owindows) {
1870 if (!con_is_floating(current->con)) {
1871 ELOG("Cannot change position. The window/container is not floating\n");
1874 yerror("Cannot change position of a window/container because it is not floating.");
1881 if (strcmp(method, "absolute") == 0) {
1882 current->con->parent->rect.x = x;
1883 current->con->parent->rect.y = y;
1885 DLOG("moving to absolute position %d %d\n", x, y);
1886 floating_maybe_reassign_ws(current->con->parent);
1887 cmd_output->needs_tree_render = true;
1890 if (strcmp(method, "position") == 0) {
1891 Rect newrect = current->con->parent->rect;
1893 DLOG("moving to position %d %d\n", x, y);
1897 floating_reposition(current->con->parent, newrect);
1901 // XXX: default reply for now, make this a better reply
1907 * Implementation of 'move [window|container] [to] [absolute] position center
1910 void cmd_move_window_to_center(I3_CMD, char *method) {
1911 if (!con_is_floating(focused)) {
1912 ELOG("Cannot change position. The window/container is not floating\n");
1913 yerror("Cannot change position. The window/container is not floating.");
1917 if (strcmp(method, "absolute") == 0) {
1918 Rect *rect = &focused->parent->rect;
1920 DLOG("moving to absolute center\n");
1921 rect->x = croot->rect.width / 2 - rect->width / 2;
1922 rect->y = croot->rect.height / 2 - rect->height / 2;
1924 floating_maybe_reassign_ws(focused->parent);
1925 cmd_output->needs_tree_render = true;
1928 if (strcmp(method, "position") == 0) {
1929 Rect *wsrect = &con_get_workspace(focused)->rect;
1930 Rect newrect = focused->parent->rect;
1932 DLOG("moving to center\n");
1933 newrect.x = wsrect->width / 2 - newrect.width / 2;
1934 newrect.y = wsrect->height / 2 - newrect.height / 2;
1936 floating_reposition(focused->parent, newrect);
1939 // XXX: default reply for now, make this a better reply
1944 * Implementation of 'move scratchpad'.
1947 void cmd_move_scratchpad(I3_CMD) {
1948 DLOG("should move window to scratchpad\n");
1953 TAILQ_FOREACH(current, &owindows, owindows) {
1954 DLOG("matching: %p / %s\n", current->con, current->con->name);
1955 scratchpad_move(current->con);
1958 cmd_output->needs_tree_render = true;
1959 // XXX: default reply for now, make this a better reply
1964 * Implementation of 'scratchpad show'.
1967 void cmd_scratchpad_show(I3_CMD) {
1968 DLOG("should show scratchpad window\n");
1971 if (match_is_empty(current_match)) {
1972 scratchpad_show(NULL);
1974 TAILQ_FOREACH(current, &owindows, owindows) {
1975 DLOG("matching: %p / %s\n", current->con, current->con->name);
1976 scratchpad_show(current->con);
1980 cmd_output->needs_tree_render = true;
1981 // XXX: default reply for now, make this a better reply
1986 * Implementation of 'rename workspace [<name>] to <name>'
1989 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1990 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1991 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1996 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1998 LOG("Renaming current workspace to \"%s\"\n", new_name);
2001 Con *output, *workspace = NULL;
2003 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2004 GREP_FIRST(workspace, output_get_content(output),
2005 !strcasecmp(child->name, old_name));
2007 workspace = con_get_workspace(focused);
2011 yerror("Old workspace \"%s\" not found", old_name);
2015 Con *check_dest = NULL;
2016 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2017 GREP_FIRST(check_dest, output_get_content(output),
2018 !strcasecmp(child->name, new_name));
2020 if (check_dest != NULL) {
2021 yerror("New workspace \"%s\" already exists", new_name);
2025 /* Change the name and try to parse it as a number. */
2026 FREE(workspace->name);
2027 workspace->name = sstrdup(new_name);
2029 workspace->num = ws_name_to_number(new_name);
2030 LOG("num = %d\n", workspace->num);
2032 /* By re-attaching, the sort order will be correct afterwards. */
2033 Con *previously_focused = focused;
2034 Con *parent = workspace->parent;
2035 con_detach(workspace);
2036 con_attach(workspace, parent, false);
2037 /* Restore the previous focus since con_attach messes with the focus. */
2038 con_focus(previously_focused);
2040 cmd_output->needs_tree_render = true;
2043 ipc_send_workspace_event("rename", workspace, NULL);
2044 ewmh_update_desktop_names();
2045 ewmh_update_desktop_viewport();
2046 ewmh_update_current_desktop();
2050 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2053 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2055 bool toggle = false;
2056 if (strcmp(bar_mode, "dock") == 0)
2058 else if (strcmp(bar_mode, "hide") == 0)
2060 else if (strcmp(bar_mode, "invisible") == 0)
2062 else if (strcmp(bar_mode, "toggle") == 0)
2065 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2069 bool changed_sth = false;
2070 Barconfig *current = NULL;
2071 TAILQ_FOREACH(current, &barconfigs, configs) {
2072 if (bar_id && strcmp(current->id, bar_id) != 0)
2076 mode = (current->mode + 1) % 2;
2078 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2079 current->mode = mode;
2086 if (bar_id && !changed_sth) {
2087 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2095 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2098 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2099 int hidden_state = S_SHOW;
2100 bool toggle = false;
2101 if (strcmp(bar_hidden_state, "hide") == 0)
2102 hidden_state = S_HIDE;
2103 else if (strcmp(bar_hidden_state, "show") == 0)
2104 hidden_state = S_SHOW;
2105 else if (strcmp(bar_hidden_state, "toggle") == 0)
2108 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2112 bool changed_sth = false;
2113 Barconfig *current = NULL;
2114 TAILQ_FOREACH(current, &barconfigs, configs) {
2115 if (bar_id && strcmp(current->id, bar_id) != 0)
2119 hidden_state = (current->hidden_state + 1) % 2;
2121 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2122 current->hidden_state = hidden_state;
2129 if (bar_id && !changed_sth) {
2130 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2138 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2141 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2143 if (strcmp(bar_type, "mode") == 0)
2144 ret = cmd_bar_mode(bar_value, bar_id);
2145 else if (strcmp(bar_type, "hidden_state") == 0)
2146 ret = cmd_bar_hidden_state(bar_value, bar_id);
2148 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2160 * Implementation of 'shmlog <size>|toggle|on|off'
2163 void cmd_shmlog(I3_CMD, char *argument) {
2164 if (!strcmp(argument, "toggle"))
2165 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2166 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2167 else if (!strcmp(argument, "on"))
2168 shmlog_size = default_shmlog_size;
2169 else if (!strcmp(argument, "off"))
2172 /* If shm logging now, restart logging with the new size. */
2173 if (shmlog_size > 0) {
2175 LOG("Restarting shm logging...\n");
2178 shmlog_size = atoi(argument);
2179 /* Make a weakly attempt at ensuring the argument is valid. */
2180 if (shmlog_size <= 0)
2181 shmlog_size = default_shmlog_size;
2183 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2185 update_shmlog_atom();
2186 // XXX: default reply for now, make this a better reply
2191 * Implementation of 'debuglog toggle|on|off'
2194 void cmd_debuglog(I3_CMD, char *argument) {
2195 bool logging = get_debug_logging();
2196 if (!strcmp(argument, "toggle")) {
2197 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2198 set_debug_logging(!logging);
2199 } else if (!strcmp(argument, "on") && !logging) {
2200 LOG("Enabling debug logging\n");
2201 set_debug_logging(true);
2202 } else if (!strcmp(argument, "off") && logging) {
2203 LOG("Disabling debug logging\n");
2204 set_debug_logging(false);
2206 // XXX: default reply for now, make this a better reply