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 json_content_t content = json_determine_content(path);
906 LOG("JSON content = %d\n", content);
907 if (content == JSON_CONTENT_UNKNOWN) {
908 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
913 Con *parent = focused;
914 if (content == JSON_CONTENT_WORKSPACE) {
915 parent = output_get_content(con_get_output(parent));
917 /* We need to append the layout to a split container, since a leaf
918 * container must not have any children (by definition).
919 * Note that we explicitly check for workspaces, since they are okay for
920 * this purpose, but con_accepts_window() returns false for workspaces. */
921 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
922 parent = parent->parent;
924 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
925 char *errormsg = NULL;
926 tree_append_json(parent, path, &errormsg);
927 if (errormsg != NULL) {
930 /* Note that we continue executing since tree_append_json() has
931 * side-effects — user-provided layouts can be partly valid, partly
932 * invalid, leading to half of the placeholder containers being
938 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
939 // false); should be enough, but when sending 'workspace 4; append_layout
940 // /tmp/foo.json', the needs_tree_render == true of the workspace command
941 // is not executed yet and will be batched with append_layout’s
942 // needs_tree_render after the parser finished. We should check if that is
944 render_con(croot, false);
946 restore_open_placeholder_windows(parent);
948 if (content == JSON_CONTENT_WORKSPACE)
949 ipc_send_workspace_event("restored", parent, NULL);
951 cmd_output->needs_tree_render = true;
955 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
958 void cmd_workspace(I3_CMD, char *which) {
961 DLOG("which=%s\n", which);
963 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
964 LOG("Cannot switch workspace while in global fullscreen\n");
969 if (strcmp(which, "next") == 0)
970 ws = workspace_next();
971 else if (strcmp(which, "prev") == 0)
972 ws = workspace_prev();
973 else if (strcmp(which, "next_on_output") == 0)
974 ws = workspace_next_on_output();
975 else if (strcmp(which, "prev_on_output") == 0)
976 ws = workspace_prev_on_output();
978 ELOG("BUG: called with which=%s\n", which);
985 cmd_output->needs_tree_render = true;
986 // XXX: default reply for now, make this a better reply
991 * Implementation of 'workspace number <name>'
994 void cmd_workspace_number(I3_CMD, char *which) {
995 Con *output, *workspace = NULL;
997 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
998 LOG("Cannot switch workspace while in global fullscreen\n");
1003 long parsed_num = ws_name_to_number(which);
1005 if (parsed_num == -1) {
1006 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1007 yerror("Could not parse number \"%s\"", which);
1011 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1012 GREP_FIRST(workspace, output_get_content(output),
1013 child->num == parsed_num);
1016 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1018 workspace_show_by_name(which);
1019 cmd_output->needs_tree_render = true;
1022 if (maybe_back_and_forth(cmd_output, workspace->name))
1024 workspace_show(workspace);
1026 cmd_output->needs_tree_render = true;
1027 // XXX: default reply for now, make this a better reply
1032 * Implementation of 'workspace back_and_forth'.
1035 void cmd_workspace_back_and_forth(I3_CMD) {
1036 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1037 LOG("Cannot switch workspace while in global fullscreen\n");
1042 workspace_back_and_forth();
1044 cmd_output->needs_tree_render = true;
1045 // XXX: default reply for now, make this a better reply
1050 * Implementation of 'workspace <name>'
1053 void cmd_workspace_name(I3_CMD, char *name) {
1054 if (strncasecmp(name, "__", strlen("__")) == 0) {
1055 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1060 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1061 LOG("Cannot switch workspace while in global fullscreen\n");
1066 DLOG("should switch to workspace %s\n", name);
1067 if (maybe_back_and_forth(cmd_output, name))
1073 /* first look for a workspace with this name */
1074 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1075 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1078 /* if the name is only digits, we interpret this as a "workspace number"
1080 if (!ws && name_is_digits(name)) {
1081 long parsed_num = ws_name_to_number(name);
1082 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1083 GREP_FIRST(ws, output_get_content(output),
1084 child->num == parsed_num);
1088 /* if no workspace was found, make a new one */
1090 ws = workspace_get(name, NULL);
1094 cmd_output->needs_tree_render = true;
1095 // XXX: default reply for now, make this a better reply
1100 * Implementation of 'mark <mark>'
1103 void cmd_mark(I3_CMD, char *mark) {
1104 DLOG("Clearing all windows which have that mark first\n");
1107 TAILQ_FOREACH(con, &all_cons, all_cons) {
1108 if (con->mark && strcmp(con->mark, mark) == 0)
1112 DLOG("marking window with str %s\n", mark);
1117 TAILQ_FOREACH(current, &owindows, owindows) {
1118 DLOG("matching: %p / %s\n", current->con, current->con->name);
1119 current->con->mark = sstrdup(mark);
1122 cmd_output->needs_tree_render = true;
1123 // XXX: default reply for now, make this a better reply
1128 * Implementation of 'unmark [mark]'
1131 void cmd_unmark(I3_CMD, char *mark) {
1134 TAILQ_FOREACH(con, &all_cons, all_cons) {
1137 DLOG("removed all window marks");
1140 TAILQ_FOREACH(con, &all_cons, all_cons) {
1141 if (con->mark && strcmp(con->mark, mark) == 0)
1144 DLOG("removed window mark %s\n", mark);
1147 cmd_output->needs_tree_render = true;
1148 // XXX: default reply for now, make this a better reply
1153 * Implementation of 'mode <string>'.
1156 void cmd_mode(I3_CMD, char *mode) {
1157 DLOG("mode=%s\n", mode);
1160 // XXX: default reply for now, make this a better reply
1165 * Implementation of 'move [window|container] [to] output <str>'.
1168 void cmd_move_con_to_output(I3_CMD, char *name) {
1171 DLOG("should move window to output %s\n", name);
1175 /* get the output */
1176 Output *current_output = NULL;
1179 // TODO: fix the handling of criteria
1180 TAILQ_FOREACH(current, &owindows, owindows)
1181 current_output = get_output_of_con(current->con);
1183 assert(current_output != NULL);
1185 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1186 if (strcasecmp(name, "up") == 0)
1187 output = get_output_next_wrap(D_UP, current_output);
1188 else if (strcasecmp(name, "down") == 0)
1189 output = get_output_next_wrap(D_DOWN, current_output);
1190 else if (strcasecmp(name, "left") == 0)
1191 output = get_output_next_wrap(D_LEFT, current_output);
1192 else if (strcasecmp(name, "right") == 0)
1193 output = get_output_next_wrap(D_RIGHT, current_output);
1195 output = get_output_by_name(name);
1198 LOG("No such output found.\n");
1203 /* get visible workspace on output */
1205 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1211 TAILQ_FOREACH(current, &owindows, owindows) {
1212 DLOG("matching: %p / %s\n", current->con, current->con->name);
1213 con_move_to_workspace(current->con, ws, true, false);
1216 cmd_output->needs_tree_render = true;
1217 // XXX: default reply for now, make this a better reply
1222 * Implementation of 'floating enable|disable|toggle'
1225 void cmd_floating(I3_CMD, char *floating_mode) {
1228 DLOG("floating_mode=%s\n", floating_mode);
1232 TAILQ_FOREACH(current, &owindows, owindows) {
1233 DLOG("matching: %p / %s\n", current->con, current->con->name);
1234 if (strcmp(floating_mode, "toggle") == 0) {
1235 DLOG("should toggle mode\n");
1236 toggle_floating_mode(current->con, false);
1238 DLOG("should switch mode to %s\n", floating_mode);
1239 if (strcmp(floating_mode, "enable") == 0) {
1240 floating_enable(current->con, false);
1242 floating_disable(current->con, false);
1247 cmd_output->needs_tree_render = true;
1248 // XXX: default reply for now, make this a better reply
1253 * Implementation of 'move workspace to [output] <str>'.
1256 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1257 DLOG("should move workspace to output %s\n", name);
1262 TAILQ_FOREACH(current, &owindows, owindows) {
1263 Output *current_output = get_output_of_con(current->con);
1264 if (!current_output) {
1265 ELOG("Cannot get current output. This is a bug in i3.\n");
1269 Output *output = get_output_from_string(current_output, name);
1271 ELOG("Could not get output from string \"%s\"\n", name);
1276 Con *content = output_get_content(output->con);
1277 LOG("got output %p with content %p\n", output, content);
1279 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1280 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1282 Con *ws = con_get_workspace(current->con);
1283 LOG("should move workspace %p / %s\n", ws, ws->name);
1284 bool workspace_was_visible = workspace_is_visible(ws);
1286 if (con_num_children(ws->parent) == 1) {
1287 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1289 /* check if we can find a workspace assigned to this output */
1290 bool used_assignment = false;
1291 struct Workspace_Assignment *assignment;
1292 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1293 if (strcmp(assignment->output, current_output->name) != 0)
1296 /* check if this workspace is already attached to the tree */
1297 Con *workspace = NULL, *out;
1298 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1299 GREP_FIRST(workspace, output_get_content(out),
1300 !strcasecmp(child->name, assignment->name));
1301 if (workspace != NULL)
1304 /* so create the workspace referenced to by this assignment */
1305 LOG("Creating workspace from assignment %s.\n", assignment->name);
1306 workspace_get(assignment->name, NULL);
1307 used_assignment = true;
1311 /* if we couldn't create the workspace using an assignment, create
1312 * it on the output */
1313 if (!used_assignment)
1314 create_workspace_on_output(current_output, ws->parent);
1316 /* notify the IPC listeners */
1317 ipc_send_workspace_event("init", ws, NULL);
1319 DLOG("Detaching\n");
1321 /* detach from the old output and attach to the new output */
1322 Con *old_content = ws->parent;
1324 if (workspace_was_visible) {
1325 /* The workspace which we just detached was visible, so focus
1326 * the next one in the focus-stack. */
1327 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1328 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1329 workspace_show(focus_ws);
1331 con_attach(ws, content, false);
1333 /* fix the coordinates of the floating containers */
1335 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1336 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1338 ipc_send_workspace_event("move", ws, NULL);
1339 if (workspace_was_visible) {
1340 /* Focus the moved workspace on the destination output. */
1344 /* NB: We cannot simply work with previously_visible_ws since it might
1345 * have been cleaned up by workspace_show() already, depending on the
1346 * focus order/number of other workspaces on the output.
1347 * Instead, we loop through the available workspaces and only work with
1348 * previously_visible_ws if we still find it. */
1349 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1350 if (ws != previously_visible_ws)
1353 /* Call the on_remove_child callback of the workspace which previously
1354 * was visible on the destination output. Since it is no longer
1355 * visible, it might need to get cleaned up. */
1356 CALL(previously_visible_ws, on_remove_child);
1361 cmd_output->needs_tree_render = true;
1362 // XXX: default reply for now, make this a better reply
1367 * Implementation of 'split v|h|vertical|horizontal'.
1370 void cmd_split(I3_CMD, char *direction) {
1372 /* TODO: use matches */
1373 LOG("splitting in direction %c\n", direction[0]);
1374 if (match_is_empty(current_match))
1375 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1377 TAILQ_FOREACH(current, &owindows, owindows) {
1378 DLOG("matching: %p / %s\n", current->con, current->con->name);
1379 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1383 cmd_output->needs_tree_render = true;
1384 // XXX: default reply for now, make this a better reply
1389 * Implementation of 'kill [window|client]'.
1392 void cmd_kill(I3_CMD, char *kill_mode_str) {
1393 if (kill_mode_str == NULL)
1394 kill_mode_str = "window";
1397 DLOG("kill_mode=%s\n", kill_mode_str);
1400 if (strcmp(kill_mode_str, "window") == 0)
1401 kill_mode = KILL_WINDOW;
1402 else if (strcmp(kill_mode_str, "client") == 0)
1403 kill_mode = KILL_CLIENT;
1405 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1410 /* check if the match is empty, not if the result is empty */
1411 if (match_is_empty(current_match))
1412 tree_close_con(kill_mode);
1414 TAILQ_FOREACH(current, &owindows, owindows) {
1415 DLOG("matching: %p / %s\n", current->con, current->con->name);
1416 tree_close(current->con, kill_mode, false, false);
1420 cmd_output->needs_tree_render = true;
1421 // XXX: default reply for now, make this a better reply
1426 * Implementation of 'exec [--no-startup-id] <command>'.
1429 void cmd_exec(I3_CMD, char *nosn, char *command) {
1430 bool no_startup_id = (nosn != NULL);
1432 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1433 start_application(command, no_startup_id);
1435 // XXX: default reply for now, make this a better reply
1440 * Implementation of 'focus left|right|up|down'.
1443 void cmd_focus_direction(I3_CMD, char *direction) {
1444 DLOG("direction = *%s*\n", direction);
1446 if (strcmp(direction, "left") == 0)
1447 tree_next('p', HORIZ);
1448 else if (strcmp(direction, "right") == 0)
1449 tree_next('n', HORIZ);
1450 else if (strcmp(direction, "up") == 0)
1451 tree_next('p', VERT);
1452 else if (strcmp(direction, "down") == 0)
1453 tree_next('n', VERT);
1455 ELOG("Invalid focus direction (%s)\n", direction);
1460 cmd_output->needs_tree_render = true;
1461 // XXX: default reply for now, make this a better reply
1466 * Implementation of 'focus tiling|floating|mode_toggle'.
1469 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1470 DLOG("window_mode = %s\n", window_mode);
1472 Con *ws = con_get_workspace(focused);
1475 if (strcmp(window_mode, "mode_toggle") == 0) {
1476 current = TAILQ_FIRST(&(ws->focus_head));
1477 if (current != NULL && current->type == CT_FLOATING_CON)
1478 window_mode = "tiling";
1480 window_mode = "floating";
1482 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1483 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1484 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1487 con_focus(con_descend_focused(current));
1492 cmd_output->needs_tree_render = true;
1493 // XXX: default reply for now, make this a better reply
1498 * Implementation of 'focus parent|child'.
1501 void cmd_focus_level(I3_CMD, char *level) {
1502 DLOG("level = %s\n", level);
1503 bool success = false;
1505 /* Focusing the parent can only be allowed if the newly
1506 * focused container won't escape the fullscreen container. */
1507 if (strcmp(level, "parent") == 0) {
1508 if (focused && focused->parent) {
1509 if (con_fullscreen_permits_focusing(focused->parent))
1510 success = level_up();
1512 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1516 /* Focusing a child should always be allowed. */
1518 success = level_down();
1520 cmd_output->needs_tree_render = success;
1521 // XXX: default reply for now, make this a better reply
1526 * Implementation of 'focus'.
1529 void cmd_focus(I3_CMD) {
1530 DLOG("current_match = %p\n", current_match);
1532 if (match_is_empty(current_match)) {
1533 ELOG("You have to specify which window/container should be focused.\n");
1534 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1536 yerror("You have to specify which window/container should be focused");
1541 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1544 TAILQ_FOREACH(current, &owindows, owindows) {
1545 Con *ws = con_get_workspace(current->con);
1546 /* If no workspace could be found, this was a dock window.
1547 * Just skip it, you cannot focus dock windows. */
1551 /* Check the fullscreen focus constraints. */
1552 if (!con_fullscreen_permits_focusing(current->con)) {
1553 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1558 /* In case this is a scratchpad window, call scratchpad_show(). */
1559 if (ws == __i3_scratch) {
1560 scratchpad_show(current->con);
1562 /* While for the normal focus case we can change focus multiple
1563 * times and only a single window ends up focused, we could show
1564 * multiple scratchpad windows. So, rather break here. */
1568 /* If the container is not on the current workspace,
1569 * workspace_show() will switch to a different workspace and (if
1570 * enabled) trigger a mouse pointer warp to the currently focused
1571 * container (!) on the target workspace.
1573 * Therefore, before calling workspace_show(), we make sure that
1574 * 'current' will be focused on the workspace. However, we cannot
1575 * just con_focus(current) because then the pointer will not be
1576 * warped at all (the code thinks we are already there).
1578 * So we focus 'current' to make it the currently focused window of
1579 * the target workspace, then revert focus. */
1580 Con *currently_focused = focused;
1581 con_focus(current->con);
1582 con_focus(currently_focused);
1584 /* Now switch to the workspace, then focus */
1586 LOG("focusing %p / %s\n", current->con, current->con->name);
1587 con_focus(current->con);
1592 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1593 "while only exactly one container can be focused at a time.\n",
1596 cmd_output->needs_tree_render = true;
1597 // XXX: default reply for now, make this a better reply
1602 * Implementation of 'fullscreen enable|toggle [global]' and
1603 * 'fullscreen disable'
1606 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1607 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1608 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1613 TAILQ_FOREACH(current, &owindows, owindows) {
1614 DLOG("matching: %p / %s\n", current->con, current->con->name);
1615 if (strcmp(action, "toggle") == 0) {
1616 con_toggle_fullscreen(current->con, mode);
1617 } else if (strcmp(action, "enable") == 0) {
1618 con_enable_fullscreen(current->con, mode);
1619 } else if (strcmp(action, "disable") == 0) {
1620 con_disable_fullscreen(current->con);
1624 cmd_output->needs_tree_render = true;
1625 // XXX: default reply for now, make this a better reply
1630 * Implementation of 'move <direction> [<pixels> [px]]'.
1633 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1634 // 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
1635 int px = atoi(move_px);
1640 Con *initially_focused = focused;
1642 TAILQ_FOREACH(current, &owindows, owindows) {
1643 DLOG("moving in direction %s, px %s\n", direction, move_px);
1644 if (con_is_floating(current->con)) {
1645 DLOG("floating move with %d pixels\n", px);
1646 Rect newrect = current->con->parent->rect;
1647 if (strcmp(direction, "left") == 0) {
1649 } else if (strcmp(direction, "right") == 0) {
1651 } else if (strcmp(direction, "up") == 0) {
1653 } else if (strcmp(direction, "down") == 0) {
1656 floating_reposition(current->con->parent, newrect);
1658 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1659 cmd_output->needs_tree_render = true;
1663 /* the move command should not disturb focus */
1664 if (focused != initially_focused)
1665 con_focus(initially_focused);
1667 // XXX: default reply for now, make this a better reply
1672 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1675 void cmd_layout(I3_CMD, char *layout_str) {
1676 if (strcmp(layout_str, "stacking") == 0)
1677 layout_str = "stacked";
1680 /* default is a special case which will be handled in con_set_layout(). */
1681 if (strcmp(layout_str, "default") == 0)
1683 else if (strcmp(layout_str, "stacked") == 0)
1685 else if (strcmp(layout_str, "tabbed") == 0)
1687 else if (strcmp(layout_str, "splitv") == 0)
1689 else if (strcmp(layout_str, "splith") == 0)
1692 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1696 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1698 /* check if the match is empty, not if the result is empty */
1699 if (match_is_empty(current_match))
1700 con_set_layout(focused, layout);
1702 TAILQ_FOREACH(current, &owindows, owindows) {
1703 DLOG("matching: %p / %s\n", current->con, current->con->name);
1704 con_set_layout(current->con, layout);
1708 cmd_output->needs_tree_render = true;
1709 // XXX: default reply for now, make this a better reply
1714 * Implementation of 'layout toggle [all|split]'.
1717 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1720 if (toggle_mode == NULL)
1721 toggle_mode = "default";
1723 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1725 /* check if the match is empty, not if the result is empty */
1726 if (match_is_empty(current_match))
1727 con_toggle_layout(focused, toggle_mode);
1729 TAILQ_FOREACH(current, &owindows, owindows) {
1730 DLOG("matching: %p / %s\n", current->con, current->con->name);
1731 con_toggle_layout(current->con, toggle_mode);
1735 cmd_output->needs_tree_render = true;
1736 // XXX: default reply for now, make this a better reply
1741 * Implementation of 'exit'.
1744 void cmd_exit(I3_CMD) {
1745 LOG("Exiting due to user command.\n");
1747 unlink(config.ipc_socket_path);
1748 xcb_disconnect(conn);
1755 * Implementation of 'reload'.
1758 void cmd_reload(I3_CMD) {
1760 kill_nagbar(&config_error_nagbar_pid, false);
1761 kill_nagbar(&command_error_nagbar_pid, false);
1762 load_configuration(conn, NULL, true);
1764 /* Send an IPC event just in case the ws names have changed */
1765 ipc_send_workspace_event("reload", NULL, NULL);
1766 /* Send an update event for the barconfig just in case it has changed */
1769 // XXX: default reply for now, make this a better reply
1774 * Implementation of 'restart'.
1777 void cmd_restart(I3_CMD) {
1778 LOG("restarting i3\n");
1780 unlink(config.ipc_socket_path);
1781 /* We need to call this manually since atexit handlers don’t get called
1783 purge_zerobyte_logfile();
1786 // XXX: default reply for now, make this a better reply
1791 * Implementation of 'open'.
1794 void cmd_open(I3_CMD) {
1795 LOG("opening new container\n");
1796 Con *con = tree_open_con(NULL, NULL);
1797 con->layout = L_SPLITH;
1804 y(integer, (long int)con);
1807 cmd_output->needs_tree_render = true;
1811 * Implementation of 'focus output <output>'.
1814 void cmd_focus_output(I3_CMD, char *name) {
1817 DLOG("name = %s\n", name);
1821 /* get the output */
1822 Output *current_output = NULL;
1825 TAILQ_FOREACH(current, &owindows, owindows)
1826 current_output = get_output_of_con(current->con);
1827 assert(current_output != NULL);
1829 output = get_output_from_string(current_output, name);
1832 LOG("No such output found.\n");
1837 /* get visible workspace on output */
1839 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1847 cmd_output->needs_tree_render = true;
1848 // XXX: default reply for now, make this a better reply
1853 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1856 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1859 bool has_error = false;
1864 TAILQ_FOREACH(current, &owindows, owindows) {
1865 if (!con_is_floating(current->con)) {
1866 ELOG("Cannot change position. The window/container is not floating\n");
1869 yerror("Cannot change position of a window/container because it is not floating.");
1876 if (strcmp(method, "absolute") == 0) {
1877 current->con->parent->rect.x = x;
1878 current->con->parent->rect.y = y;
1880 DLOG("moving to absolute position %d %d\n", x, y);
1881 floating_maybe_reassign_ws(current->con->parent);
1882 cmd_output->needs_tree_render = true;
1885 if (strcmp(method, "position") == 0) {
1886 Rect newrect = current->con->parent->rect;
1888 DLOG("moving to position %d %d\n", x, y);
1892 floating_reposition(current->con->parent, newrect);
1896 // XXX: default reply for now, make this a better reply
1902 * Implementation of 'move [window|container] [to] [absolute] position center
1905 void cmd_move_window_to_center(I3_CMD, char *method) {
1906 if (!con_is_floating(focused)) {
1907 ELOG("Cannot change position. The window/container is not floating\n");
1908 yerror("Cannot change position. The window/container is not floating.");
1912 if (strcmp(method, "absolute") == 0) {
1913 Rect *rect = &focused->parent->rect;
1915 DLOG("moving to absolute center\n");
1916 rect->x = croot->rect.width / 2 - rect->width / 2;
1917 rect->y = croot->rect.height / 2 - rect->height / 2;
1919 floating_maybe_reassign_ws(focused->parent);
1920 cmd_output->needs_tree_render = true;
1923 if (strcmp(method, "position") == 0) {
1924 Rect *wsrect = &con_get_workspace(focused)->rect;
1925 Rect newrect = focused->parent->rect;
1927 DLOG("moving to center\n");
1928 newrect.x = wsrect->width / 2 - newrect.width / 2;
1929 newrect.y = wsrect->height / 2 - newrect.height / 2;
1931 floating_reposition(focused->parent, newrect);
1934 // XXX: default reply for now, make this a better reply
1939 * Implementation of 'move scratchpad'.
1942 void cmd_move_scratchpad(I3_CMD) {
1943 DLOG("should move window to scratchpad\n");
1948 TAILQ_FOREACH(current, &owindows, owindows) {
1949 DLOG("matching: %p / %s\n", current->con, current->con->name);
1950 scratchpad_move(current->con);
1953 cmd_output->needs_tree_render = true;
1954 // XXX: default reply for now, make this a better reply
1959 * Implementation of 'scratchpad show'.
1962 void cmd_scratchpad_show(I3_CMD) {
1963 DLOG("should show scratchpad window\n");
1966 if (match_is_empty(current_match)) {
1967 scratchpad_show(NULL);
1969 TAILQ_FOREACH(current, &owindows, owindows) {
1970 DLOG("matching: %p / %s\n", current->con, current->con->name);
1971 scratchpad_show(current->con);
1975 cmd_output->needs_tree_render = true;
1976 // XXX: default reply for now, make this a better reply
1981 * Implementation of 'rename workspace [<name>] to <name>'
1984 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1985 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1986 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1991 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1993 LOG("Renaming current workspace to \"%s\"\n", new_name);
1996 Con *output, *workspace = NULL;
1998 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1999 GREP_FIRST(workspace, output_get_content(output),
2000 !strcasecmp(child->name, old_name));
2002 workspace = con_get_workspace(focused);
2006 yerror("Old workspace \"%s\" not found", old_name);
2010 Con *check_dest = NULL;
2011 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2012 GREP_FIRST(check_dest, output_get_content(output),
2013 !strcasecmp(child->name, new_name));
2015 if (check_dest != NULL) {
2016 yerror("New workspace \"%s\" already exists", new_name);
2020 /* Change the name and try to parse it as a number. */
2021 FREE(workspace->name);
2022 workspace->name = sstrdup(new_name);
2024 workspace->num = ws_name_to_number(new_name);
2025 LOG("num = %d\n", workspace->num);
2027 /* By re-attaching, the sort order will be correct afterwards. */
2028 Con *previously_focused = focused;
2029 Con *parent = workspace->parent;
2030 con_detach(workspace);
2031 con_attach(workspace, parent, false);
2032 /* Restore the previous focus since con_attach messes with the focus. */
2033 con_focus(previously_focused);
2035 cmd_output->needs_tree_render = true;
2038 ipc_send_workspace_event("rename", workspace, NULL);
2039 ewmh_update_desktop_names();
2040 ewmh_update_desktop_viewport();
2041 ewmh_update_current_desktop();
2045 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2048 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2050 bool toggle = false;
2051 if (strcmp(bar_mode, "dock") == 0)
2053 else if (strcmp(bar_mode, "hide") == 0)
2055 else if (strcmp(bar_mode, "invisible") == 0)
2057 else if (strcmp(bar_mode, "toggle") == 0)
2060 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2064 bool changed_sth = false;
2065 Barconfig *current = NULL;
2066 TAILQ_FOREACH(current, &barconfigs, configs) {
2067 if (bar_id && strcmp(current->id, bar_id) != 0)
2071 mode = (current->mode + 1) % 2;
2073 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2074 current->mode = mode;
2081 if (bar_id && !changed_sth) {
2082 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2090 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2093 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2094 int hidden_state = S_SHOW;
2095 bool toggle = false;
2096 if (strcmp(bar_hidden_state, "hide") == 0)
2097 hidden_state = S_HIDE;
2098 else if (strcmp(bar_hidden_state, "show") == 0)
2099 hidden_state = S_SHOW;
2100 else if (strcmp(bar_hidden_state, "toggle") == 0)
2103 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2107 bool changed_sth = false;
2108 Barconfig *current = NULL;
2109 TAILQ_FOREACH(current, &barconfigs, configs) {
2110 if (bar_id && strcmp(current->id, bar_id) != 0)
2114 hidden_state = (current->hidden_state + 1) % 2;
2116 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2117 current->hidden_state = hidden_state;
2124 if (bar_id && !changed_sth) {
2125 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2133 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2136 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2138 if (strcmp(bar_type, "mode") == 0)
2139 ret = cmd_bar_mode(bar_value, bar_id);
2140 else if (strcmp(bar_type, "hidden_state") == 0)
2141 ret = cmd_bar_hidden_state(bar_value, bar_id);
2143 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2155 * Implementation of 'shmlog <size>|toggle|on|off'
2158 void cmd_shmlog(I3_CMD, char *argument) {
2159 if (!strcmp(argument, "toggle"))
2160 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2161 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2162 else if (!strcmp(argument, "on"))
2163 shmlog_size = default_shmlog_size;
2164 else if (!strcmp(argument, "off"))
2167 /* If shm logging now, restart logging with the new size. */
2168 if (shmlog_size > 0) {
2170 LOG("Restarting shm logging...\n");
2173 shmlog_size = atoi(argument);
2174 /* Make a weakly attempt at ensuring the argument is valid. */
2175 if (shmlog_size <= 0)
2176 shmlog_size = default_shmlog_size;
2178 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2180 update_shmlog_atom();
2181 // XXX: default reply for now, make this a better reply
2186 * Implementation of 'debuglog toggle|on|off'
2189 void cmd_debuglog(I3_CMD, char *argument) {
2190 bool logging = get_debug_logging();
2191 if (!strcmp(argument, "toggle")) {
2192 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2193 set_debug_logging(!logging);
2194 } else if (!strcmp(argument, "on") && !logging) {
2195 LOG("Enabling debug logging\n");
2196 set_debug_logging(true);
2197 } else if (!strcmp(argument, "off") && logging) {
2198 LOG("Disabling debug logging\n");
2199 set_debug_logging(false);
2201 // XXX: default reply for now, make this a better reply