2 #define I3__FILE__ "commands.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 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 the output containing the given container.
71 static Output *get_output_of_con(Con *con) {
72 Con *output_con = con_get_output(con);
73 Output *output = get_output_by_name(output_con->name);
74 assert(output != NULL);
80 * Checks whether we switched to a new workspace and returns false in that case,
81 * signaling that further workspace switching should be done by the calling function
82 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
83 * and return true, signaling that no further workspace switching should occur in the calling function.
86 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
87 Con *ws = con_get_workspace(focused);
89 /* If we switched to a different workspace, do nothing */
90 if (strcmp(ws->name, name) != 0)
93 DLOG("This workspace is already focused.\n");
94 if (config.workspace_auto_back_and_forth) {
95 workspace_back_and_forth();
96 cmd_output->needs_tree_render = true;
102 * Return the passed workspace unless it is the current one and auto back and
103 * forth is enabled, in which case the back_and_forth workspace is returned.
105 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
108 if (!config.workspace_auto_back_and_forth)
111 current = con_get_workspace(focused);
113 if (current == workspace) {
114 baf = workspace_back_and_forth_get();
116 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
124 // This code is commented out because we might recycle it for popping up error
125 // messages on parser errors.
127 static pid_t migration_pid = -1;
130 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
131 * it exited (or could not be started, depending on the exit code).
134 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
135 ev_child_stop(EV_A_ watcher);
136 if (!WIFEXITED(watcher->rstatus)) {
137 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
141 int exitcode = WEXITSTATUS(watcher->rstatus);
142 printf("i3-nagbar process exited with status %d\n", exitcode);
144 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151 * only makes sure that there are no i3-nagbar instances left behind) we still
152 * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
155 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
156 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
159 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
160 if (migration_pid != -1) {
161 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
162 kill(migration_pid, SIGKILL);
167 void cmd_MIGRATION_start_nagbar(void) {
168 if (migration_pid != -1) {
169 fprintf(stderr, "i3-nagbar already running.\n");
172 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
173 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
174 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
175 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
176 migration_pid = fork();
177 if (migration_pid == -1) {
178 warn("Could not fork()");
183 if (migration_pid == 0) {
185 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
187 NULL, /* will be replaced by the executable path */
191 "You found a parsing error. Please, please, please, report it!",
197 exec_i3_utility("i3-nagbar", argv);
201 /* install a child watcher */
202 ev_child *child = smalloc(sizeof(ev_child));
203 ev_child_init(child, &nagbar_exited, migration_pid, 0);
204 ev_child_start(main_loop, child);
206 /* We need ev >= 4 for the following code. Since it is not *that* important (it
207 * only makes sure that there are no i3-nagbar instances left behind) we still
208 * support old systems with libev 3. */
209 #if EV_VERSION_MAJOR >= 4
210 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
212 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
213 ev_cleanup_init(cleanup, nagbar_cleanup);
214 ev_cleanup_start(main_loop, cleanup);
220 /*******************************************************************************
221 * Criteria functions.
222 ******************************************************************************/
225 * Helper data structure for an operation window (window on which the operation
226 * will be performed). Used to build the TAILQ owindows.
229 typedef struct owindow {
231 TAILQ_ENTRY(owindow) owindows;
234 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
236 static owindows_head owindows;
239 * Initializes the specified 'Match' data structure and the initial state of
240 * commands.c for matching target windows of a command.
243 void cmd_criteria_init(I3_CMD) {
247 DLOG("Initializing criteria, current_match = %p\n", current_match);
248 match_free(current_match);
249 match_init(current_match);
250 while (!TAILQ_EMPTY(&owindows)) {
251 ow = TAILQ_FIRST(&owindows);
252 TAILQ_REMOVE(&owindows, ow, owindows);
255 TAILQ_INIT(&owindows);
257 TAILQ_FOREACH(con, &all_cons, all_cons) {
258 ow = smalloc(sizeof(owindow));
260 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
265 * A match specification just finished (the closing square bracket was found),
266 * so we filter the list of owindows.
269 void cmd_criteria_match_windows(I3_CMD) {
270 owindow *next, *current;
272 DLOG("match specification finished, matching...\n");
273 /* copy the old list head to iterate through it and start with a fresh
274 * list which will contain only matching windows */
275 struct owindows_head old = owindows;
276 TAILQ_INIT(&owindows);
277 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
278 /* make a copy of the next pointer and advance the pointer to the
279 * next element as we are going to invalidate the element’s
280 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
282 next = TAILQ_NEXT(next, owindows);
284 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
285 if (current_match->con_id != NULL) {
286 if (current_match->con_id == current->con) {
287 DLOG("matches container!\n");
288 TAILQ_INSERT_TAIL(&owindows, current, owindows);
290 DLOG("doesnt match\n");
293 } else if (current_match->mark != NULL && current->con->mark != NULL &&
294 regex_matches(current_match->mark, current->con->mark)) {
295 DLOG("match by mark\n");
296 TAILQ_INSERT_TAIL(&owindows, current, owindows);
298 if (current->con->window && match_matches_window(current_match, current->con->window)) {
299 DLOG("matches window!\n");
300 TAILQ_INSERT_TAIL(&owindows, current, owindows);
302 DLOG("doesnt match\n");
308 TAILQ_FOREACH(current, &owindows, owindows) {
309 DLOG("matching: %p / %s\n", current->con, current->con->name);
314 * Interprets a ctype=cvalue pair and adds it to the current match
318 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
319 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
321 if (strcmp(ctype, "class") == 0) {
322 current_match->class = regex_new(cvalue);
326 if (strcmp(ctype, "instance") == 0) {
327 current_match->instance = regex_new(cvalue);
331 if (strcmp(ctype, "window_role") == 0) {
332 current_match->window_role = regex_new(cvalue);
336 if (strcmp(ctype, "con_id") == 0) {
338 long parsed = strtol(cvalue, &end, 0);
339 if (parsed == LONG_MIN ||
340 parsed == LONG_MAX ||
342 (end && *end != '\0')) {
343 ELOG("Could not parse con id \"%s\"\n", cvalue);
345 current_match->con_id = (Con *)parsed;
346 DLOG("id as int = %p\n", current_match->con_id);
351 if (strcmp(ctype, "id") == 0) {
353 long parsed = strtol(cvalue, &end, 0);
354 if (parsed == LONG_MIN ||
355 parsed == LONG_MAX ||
357 (end && *end != '\0')) {
358 ELOG("Could not parse window id \"%s\"\n", cvalue);
360 current_match->id = parsed;
361 DLOG("window id as int = %d\n", current_match->id);
366 if (strcmp(ctype, "window_type") == 0) {
367 if (strcasecmp(cvalue, "normal") == 0)
368 current_match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
369 else if (strcasecmp(cvalue, "dialog") == 0)
370 current_match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
371 else if (strcasecmp(cvalue, "utility") == 0)
372 current_match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
373 else if (strcasecmp(cvalue, "toolbar") == 0)
374 current_match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
375 else if (strcasecmp(cvalue, "splash") == 0)
376 current_match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
377 else if (strcasecmp(cvalue, "menu") == 0)
378 current_match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
379 else if (strcasecmp(cvalue, "dropdown_menu") == 0)
380 current_match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
381 else if (strcasecmp(cvalue, "popup_menu") == 0)
382 current_match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
383 else if (strcasecmp(cvalue, "tooltip") == 0)
384 current_match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
386 ELOG("unknown window_type value \"%s\"\n", cvalue);
391 if (strcmp(ctype, "con_mark") == 0) {
392 current_match->mark = regex_new(cvalue);
396 if (strcmp(ctype, "title") == 0) {
397 current_match->title = regex_new(cvalue);
401 if (strcmp(ctype, "urgent") == 0) {
402 if (strcasecmp(cvalue, "latest") == 0 ||
403 strcasecmp(cvalue, "newest") == 0 ||
404 strcasecmp(cvalue, "recent") == 0 ||
405 strcasecmp(cvalue, "last") == 0) {
406 current_match->urgent = U_LATEST;
407 } else if (strcasecmp(cvalue, "oldest") == 0 ||
408 strcasecmp(cvalue, "first") == 0) {
409 current_match->urgent = U_OLDEST;
414 if (strcmp(ctype, "workspace") == 0) {
415 current_match->workspace = regex_new(cvalue);
419 ELOG("Unknown criterion: %s\n", ctype);
423 * Implementation of 'move [window|container] [to] workspace
424 * next|prev|next_on_output|prev_on_output|current'.
427 void cmd_move_con_to_workspace(I3_CMD, char *which) {
430 DLOG("which=%s\n", which);
432 /* We have nothing to move:
433 * when criteria was specified but didn't match any window or
434 * when criteria wasn't specified and we don't have any window focused. */
435 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
436 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
437 !con_has_children(focused))) {
444 /* get the workspace */
446 if (strcmp(which, "next") == 0)
447 ws = workspace_next();
448 else if (strcmp(which, "prev") == 0)
449 ws = workspace_prev();
450 else if (strcmp(which, "next_on_output") == 0)
451 ws = workspace_next_on_output();
452 else if (strcmp(which, "prev_on_output") == 0)
453 ws = workspace_prev_on_output();
454 else if (strcmp(which, "current") == 0)
455 ws = con_get_workspace(focused);
457 ELOG("BUG: called with which=%s\n", which);
462 TAILQ_FOREACH(current, &owindows, owindows) {
463 DLOG("matching: %p / %s\n", current->con, current->con->name);
464 con_move_to_workspace(current->con, ws, true, false, false);
467 cmd_output->needs_tree_render = true;
468 // XXX: default reply for now, make this a better reply
473 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
476 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
480 ws = workspace_back_and_forth_get();
483 yerror("No workspace was previously active.");
489 TAILQ_FOREACH(current, &owindows, owindows) {
490 DLOG("matching: %p / %s\n", current->con, current->con->name);
491 con_move_to_workspace(current->con, ws, true, false, false);
494 cmd_output->needs_tree_render = true;
495 // XXX: default reply for now, make this a better reply
500 * Implementation of 'move [window|container] [to] workspace <name>'.
503 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
504 if (strncasecmp(name, "__", strlen("__")) == 0) {
505 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
512 /* We have nothing to move:
513 * when criteria was specified but didn't match any window or
514 * when criteria wasn't specified and we don't have any window focused. */
515 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
516 ELOG("No windows match your criteria, cannot move.\n");
519 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
520 !con_has_children(focused)) {
525 LOG("should move window to workspace %s\n", name);
526 /* get the workspace */
527 Con *ws = workspace_get(name, NULL);
529 ws = maybe_auto_back_and_forth_workspace(ws);
533 TAILQ_FOREACH(current, &owindows, owindows) {
534 DLOG("matching: %p / %s\n", current->con, current->con->name);
535 con_move_to_workspace(current->con, ws, true, false, false);
538 cmd_output->needs_tree_render = true;
539 // XXX: default reply for now, make this a better reply
544 * Implementation of 'move [window|container] [to] workspace number <name>'.
547 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
550 /* We have nothing to move:
551 * when criteria was specified but didn't match any window or
552 * when criteria wasn't specified and we don't have any window focused. */
553 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
554 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
555 !con_has_children(focused))) {
560 LOG("should move window to workspace %s\n", which);
561 /* get the workspace */
562 Con *output, *workspace = NULL;
564 long parsed_num = ws_name_to_number(which);
566 if (parsed_num == -1) {
567 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
568 yerror("Could not parse number \"%s\"", which);
572 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
573 GREP_FIRST(workspace, output_get_content(output),
574 child->num == parsed_num);
577 workspace = workspace_get(which, NULL);
580 workspace = maybe_auto_back_and_forth_workspace(workspace);
584 TAILQ_FOREACH(current, &owindows, owindows) {
585 DLOG("matching: %p / %s\n", current->con, current->con->name);
586 con_move_to_workspace(current->con, workspace, true, false, false);
589 cmd_output->needs_tree_render = true;
590 // XXX: default reply for now, make this a better reply
594 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
595 LOG("floating resize\n");
596 Rect old_rect = floating_con->rect;
597 Con *focused_con = con_descend_focused(floating_con);
599 /* ensure that resize will take place even if pixel increment is smaller than
600 * height increment or width increment.
602 const i3Window *window = focused_con->window;
603 if (window != NULL) {
604 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
605 strcmp(direction, "height") == 0) {
607 px = (-px < window->height_increment) ? -window->height_increment : px;
609 px = (px < window->height_increment) ? window->height_increment : px;
610 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
612 px = (-px < window->width_increment) ? -window->width_increment : px;
614 px = (px < window->width_increment) ? window->width_increment : px;
618 if (strcmp(direction, "up") == 0) {
619 floating_con->rect.height += px;
620 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
621 floating_con->rect.height += px;
622 } else if (strcmp(direction, "left") == 0) {
623 floating_con->rect.width += px;
625 floating_con->rect.width += px;
628 floating_check_size(floating_con);
630 /* Did we actually resize anything or did the size constraints prevent us?
631 * If we could not resize, exit now to not move the window. */
632 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
635 if (strcmp(direction, "up") == 0) {
636 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
637 } else if (strcmp(direction, "left") == 0) {
638 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
641 /* If this is a scratchpad window, don't auto center it from now on. */
642 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
643 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
646 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
647 LOG("tiling resize\n");
649 Con *first = current;
650 direction_t search_direction;
651 if (!strcmp(direction, "left"))
652 search_direction = D_LEFT;
653 else if (!strcmp(direction, "right"))
654 search_direction = D_RIGHT;
655 else if (!strcmp(direction, "up"))
656 search_direction = D_UP;
658 search_direction = D_DOWN;
660 bool res = resize_find_tiling_participants(&first, &second, search_direction);
662 LOG("No second container in this direction found.\n");
667 /* get the default percentage */
668 int children = con_num_children(first->parent);
669 LOG("ins. %d children\n", children);
670 double percentage = 1.0 / children;
671 LOG("default percentage = %f\n", percentage);
674 LOG("second->percent = %f\n", second->percent);
675 LOG("first->percent before = %f\n", first->percent);
676 if (first->percent == 0.0)
677 first->percent = percentage;
678 if (second->percent == 0.0)
679 second->percent = percentage;
680 double new_first_percent = first->percent + ((double)ppt / 100.0);
681 double new_second_percent = second->percent - ((double)ppt / 100.0);
682 LOG("new_first_percent = %f\n", new_first_percent);
683 LOG("new_second_percent = %f\n", new_second_percent);
684 /* Ensure that the new percentages are positive and greater than
685 * 0.05 to have a reasonable minimum size. */
686 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
687 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
688 first->percent += ((double)ppt / 100.0);
689 second->percent -= ((double)ppt / 100.0);
690 LOG("first->percent after = %f\n", first->percent);
691 LOG("second->percent after = %f\n", second->percent);
693 LOG("Not resizing, already at minimum size\n");
699 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
700 LOG("width/height resize\n");
701 /* get the appropriate current container (skip stacked/tabbed cons) */
702 while (current->parent->layout == L_STACKED ||
703 current->parent->layout == L_TABBED)
704 current = current->parent;
706 /* Then further go up until we find one with the matching orientation. */
707 orientation_t search_orientation =
708 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
710 while (current->type != CT_WORKSPACE &&
711 current->type != CT_FLOATING_CON &&
712 con_orientation(current->parent) != search_orientation)
713 current = current->parent;
715 /* get the default percentage */
716 int children = con_num_children(current->parent);
717 LOG("ins. %d children\n", children);
718 double percentage = 1.0 / children;
719 LOG("default percentage = %f\n", percentage);
721 orientation_t orientation = con_orientation(current->parent);
723 if ((orientation == HORIZ &&
724 strcmp(direction, "height") == 0) ||
725 (orientation == VERT &&
726 strcmp(direction, "width") == 0)) {
727 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
728 (orientation == HORIZ ? "horizontal" : "vertical"));
734 LOG("This is the only container, cannot resize.\n");
739 /* Ensure all the other children have a percentage set. */
741 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
742 LOG("child->percent = %f (child %p)\n", child->percent, child);
743 if (child->percent == 0.0)
744 child->percent = percentage;
747 double new_current_percent = current->percent + ((double)ppt / 100.0);
748 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
749 LOG("new_current_percent = %f\n", new_current_percent);
750 LOG("subtract_percent = %f\n", subtract_percent);
751 /* Ensure that the new percentages are positive and greater than
752 * 0.05 to have a reasonable minimum size. */
753 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
754 if (child == current)
756 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
757 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
762 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
763 LOG("Not resizing, already at minimum size\n");
768 current->percent += ((double)ppt / 100.0);
769 LOG("current->percent after = %f\n", current->percent);
771 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
772 if (child == current)
774 child->percent -= subtract_percent;
775 LOG("child->percent after (%p) = %f\n", child, child->percent);
782 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
785 void cmd_resize(I3_CMD, char *way, char *direction, long resize_px, long resize_ppt) {
786 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
787 if (strcmp(way, "shrink") == 0) {
795 TAILQ_FOREACH(current, &owindows, owindows) {
796 /* Don't handle dock windows (issue #1201) */
797 if (current->con->window && current->con->window->dock) {
798 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
803 if ((floating_con = con_inside_floating(current->con))) {
804 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
806 if (strcmp(direction, "width") == 0 ||
807 strcmp(direction, "height") == 0) {
808 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
809 current->con, way, direction, resize_ppt))
812 if (!cmd_resize_tiling_direction(current_match, cmd_output,
813 current->con, way, direction, resize_ppt))
819 cmd_output->needs_tree_render = true;
820 // XXX: default reply for now, make this a better reply
825 * Implementation of 'resize set <px> [px] <px> [px]'.
828 void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
829 DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
830 if (cwidth <= 0 || cheight <= 0) {
831 ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
838 TAILQ_FOREACH(current, &owindows, owindows) {
840 if ((floating_con = con_inside_floating(current->con))) {
841 floating_resize(floating_con, cwidth, cheight);
843 ELOG("Resize failed: %p not a floating container\n", current->con);
847 cmd_output->needs_tree_render = true;
848 // XXX: default reply for now, make this a better reply
853 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
856 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
857 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
862 TAILQ_FOREACH(current, &owindows, owindows) {
863 DLOG("matching: %p / %s\n", current->con, current->con->name);
864 int border_style = current->con->border_style;
866 int tmp_border_width = -1;
867 tmp_border_width = strtol(border_width, &end, 10);
868 if (end == border_width) {
869 /* no valid digits found */
870 tmp_border_width = -1;
872 if (strcmp(border_style_str, "toggle") == 0) {
875 if (border_style == BS_NORMAL)
876 tmp_border_width = 2;
877 else if (border_style == BS_NONE)
878 tmp_border_width = 0;
879 else if (border_style == BS_PIXEL)
880 tmp_border_width = 1;
882 if (strcmp(border_style_str, "normal") == 0)
883 border_style = BS_NORMAL;
884 else if (strcmp(border_style_str, "pixel") == 0)
885 border_style = BS_PIXEL;
886 else if (strcmp(border_style_str, "1pixel") == 0) {
887 border_style = BS_PIXEL;
888 tmp_border_width = 1;
889 } else if (strcmp(border_style_str, "none") == 0)
890 border_style = BS_NONE;
892 ELOG("BUG: called with border_style=%s\n", border_style_str);
897 con_set_border_style(current->con, border_style, tmp_border_width);
900 cmd_output->needs_tree_render = true;
901 // XXX: default reply for now, make this a better reply
906 * Implementation of 'nop <comment>'.
909 void cmd_nop(I3_CMD, char *comment) {
910 LOG("-------------------------------------------------\n");
911 LOG(" NOP: %s\n", comment);
912 LOG("-------------------------------------------------\n");
916 * Implementation of 'append_layout <path>'.
919 void cmd_append_layout(I3_CMD, char *path) {
920 LOG("Appending layout \"%s\"\n", path);
922 /* Make sure we allow paths like '~/.i3/layout.json' */
923 path = resolve_tilde(path);
925 json_content_t content = json_determine_content(path);
926 LOG("JSON content = %d\n", content);
927 if (content == JSON_CONTENT_UNKNOWN) {
928 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
929 yerror("Could not determine the contents of \"%s\".", path);
934 Con *parent = focused;
935 if (content == JSON_CONTENT_WORKSPACE) {
936 parent = output_get_content(con_get_output(parent));
938 /* We need to append the layout to a split container, since a leaf
939 * container must not have any children (by definition).
940 * Note that we explicitly check for workspaces, since they are okay for
941 * this purpose, but con_accepts_window() returns false for workspaces. */
942 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
943 parent = parent->parent;
945 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
946 char *errormsg = NULL;
947 tree_append_json(parent, path, &errormsg);
948 if (errormsg != NULL) {
951 /* Note that we continue executing since tree_append_json() has
952 * side-effects — user-provided layouts can be partly valid, partly
953 * invalid, leading to half of the placeholder containers being
959 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
960 // false); should be enough, but when sending 'workspace 4; append_layout
961 // /tmp/foo.json', the needs_tree_render == true of the workspace command
962 // is not executed yet and will be batched with append_layout’s
963 // needs_tree_render after the parser finished. We should check if that is
965 render_con(croot, false);
967 restore_open_placeholder_windows(parent);
969 if (content == JSON_CONTENT_WORKSPACE)
970 ipc_send_workspace_event("restored", parent, NULL);
973 cmd_output->needs_tree_render = true;
977 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
980 void cmd_workspace(I3_CMD, char *which) {
983 DLOG("which=%s\n", which);
985 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
986 LOG("Cannot switch workspace while in global fullscreen\n");
991 if (strcmp(which, "next") == 0)
992 ws = workspace_next();
993 else if (strcmp(which, "prev") == 0)
994 ws = workspace_prev();
995 else if (strcmp(which, "next_on_output") == 0)
996 ws = workspace_next_on_output();
997 else if (strcmp(which, "prev_on_output") == 0)
998 ws = workspace_prev_on_output();
1000 ELOG("BUG: called with which=%s\n", which);
1007 cmd_output->needs_tree_render = true;
1008 // XXX: default reply for now, make this a better reply
1013 * Implementation of 'workspace number <name>'
1016 void cmd_workspace_number(I3_CMD, char *which) {
1017 Con *output, *workspace = NULL;
1019 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1020 LOG("Cannot switch workspace while in global fullscreen\n");
1025 long parsed_num = ws_name_to_number(which);
1027 if (parsed_num == -1) {
1028 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1029 yerror("Could not parse number \"%s\"", which);
1033 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1034 GREP_FIRST(workspace, output_get_content(output),
1035 child->num == parsed_num);
1038 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1040 workspace_show_by_name(which);
1041 cmd_output->needs_tree_render = true;
1044 if (maybe_back_and_forth(cmd_output, workspace->name))
1046 workspace_show(workspace);
1048 cmd_output->needs_tree_render = true;
1049 // XXX: default reply for now, make this a better reply
1054 * Implementation of 'workspace back_and_forth'.
1057 void cmd_workspace_back_and_forth(I3_CMD) {
1058 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1059 LOG("Cannot switch workspace while in global fullscreen\n");
1064 workspace_back_and_forth();
1066 cmd_output->needs_tree_render = true;
1067 // XXX: default reply for now, make this a better reply
1072 * Implementation of 'workspace <name>'
1075 void cmd_workspace_name(I3_CMD, char *name) {
1076 if (strncasecmp(name, "__", strlen("__")) == 0) {
1077 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1082 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1083 LOG("Cannot switch workspace while in global fullscreen\n");
1088 DLOG("should switch to workspace %s\n", name);
1089 if (maybe_back_and_forth(cmd_output, name))
1091 workspace_show_by_name(name);
1093 cmd_output->needs_tree_render = true;
1094 // XXX: default reply for now, make this a better reply
1099 * Implementation of 'mark [--toggle] <mark>'
1102 void cmd_mark(I3_CMD, char *mark, char *toggle) {
1105 owindow *current = TAILQ_FIRST(&owindows);
1106 if (current == NULL) {
1111 /* Marks must be unique, i.e., no two windows must have the same mark. */
1112 if (current != TAILQ_LAST(&owindows, owindows_head)) {
1113 yerror("A mark must not be put onto more than one window");
1117 DLOG("matching: %p / %s\n", current->con, current->con->name);
1118 if (toggle != NULL) {
1119 con_mark_toggle(current->con, mark);
1121 con_mark(current->con, mark);
1124 cmd_output->needs_tree_render = true;
1125 // XXX: default reply for now, make this a better reply
1130 * Implementation of 'unmark [mark]'
1133 void cmd_unmark(I3_CMD, char *mark) {
1136 cmd_output->needs_tree_render = true;
1137 // XXX: default reply for now, make this a better reply
1142 * Implementation of 'mode <string>'.
1145 void cmd_mode(I3_CMD, char *mode) {
1146 DLOG("mode=%s\n", mode);
1149 // XXX: default reply for now, make this a better reply
1154 * Implementation of 'move [window|container] [to] output <str>'.
1157 void cmd_move_con_to_output(I3_CMD, char *name) {
1158 DLOG("Should move window to output \"%s\".\n", name);
1162 bool had_error = false;
1163 TAILQ_FOREACH(current, &owindows, owindows) {
1164 DLOG("matching: %p / %s\n", current->con, current->con->name);
1166 Output *current_output = get_output_of_con(current->con);
1167 assert(current_output != NULL);
1169 Output *output = get_output_from_string(current_output, name);
1170 if (output == NULL) {
1171 ELOG("Could not find output \"%s\", skipping.\n", name);
1177 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1179 ELOG("Could not find a visible workspace on output %p.\n", output);
1184 con_move_to_workspace(current->con, ws, true, false, false);
1187 cmd_output->needs_tree_render = true;
1188 ysuccess(!had_error);
1192 * Implementation of 'move [container|window] [to] mark <str>'.
1195 void cmd_move_con_to_mark(I3_CMD, char *mark) {
1196 DLOG("moving window to mark \"%s\"\n", mark);
1202 TAILQ_FOREACH(current, &owindows, owindows) {
1203 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1204 result &= con_move_to_mark(current->con, mark);
1207 cmd_output->needs_tree_render = true;
1212 * Implementation of 'floating enable|disable|toggle'
1215 void cmd_floating(I3_CMD, char *floating_mode) {
1218 DLOG("floating_mode=%s\n", floating_mode);
1222 TAILQ_FOREACH(current, &owindows, owindows) {
1223 DLOG("matching: %p / %s\n", current->con, current->con->name);
1224 if (strcmp(floating_mode, "toggle") == 0) {
1225 DLOG("should toggle mode\n");
1226 toggle_floating_mode(current->con, false);
1228 DLOG("should switch mode to %s\n", floating_mode);
1229 if (strcmp(floating_mode, "enable") == 0) {
1230 floating_enable(current->con, false);
1232 floating_disable(current->con, false);
1237 cmd_output->needs_tree_render = true;
1238 // XXX: default reply for now, make this a better reply
1243 * Implementation of 'move workspace to [output] <str>'.
1246 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1247 DLOG("should move workspace to output %s\n", name);
1252 TAILQ_FOREACH(current, &owindows, owindows) {
1253 Con *ws = con_get_workspace(current->con);
1254 bool success = workspace_move_to_output(ws, name);
1256 ELOG("Failed to move workspace to output.\n");
1262 cmd_output->needs_tree_render = true;
1263 // XXX: default reply for now, make this a better reply
1268 * Implementation of 'split v|h|vertical|horizontal'.
1271 void cmd_split(I3_CMD, char *direction) {
1273 /* TODO: use matches */
1274 LOG("splitting in direction %c\n", direction[0]);
1275 if (match_is_empty(current_match))
1276 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1278 TAILQ_FOREACH(current, &owindows, owindows) {
1279 DLOG("matching: %p / %s\n", current->con, current->con->name);
1280 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1284 cmd_output->needs_tree_render = true;
1285 // XXX: default reply for now, make this a better reply
1290 * Implementation of 'kill [window|client]'.
1293 void cmd_kill(I3_CMD, char *kill_mode_str) {
1294 if (kill_mode_str == NULL)
1295 kill_mode_str = "window";
1298 DLOG("kill_mode=%s\n", kill_mode_str);
1301 if (strcmp(kill_mode_str, "window") == 0)
1302 kill_mode = KILL_WINDOW;
1303 else if (strcmp(kill_mode_str, "client") == 0)
1304 kill_mode = KILL_CLIENT;
1306 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1311 /* check if the match is empty, not if the result is empty */
1312 if (match_is_empty(current_match))
1313 tree_close_con(kill_mode);
1315 TAILQ_FOREACH(current, &owindows, owindows) {
1316 DLOG("matching: %p / %s\n", current->con, current->con->name);
1317 tree_close(current->con, kill_mode, false, false);
1321 cmd_output->needs_tree_render = true;
1322 // XXX: default reply for now, make this a better reply
1327 * Implementation of 'exec [--no-startup-id] <command>'.
1330 void cmd_exec(I3_CMD, char *nosn, char *command) {
1331 bool no_startup_id = (nosn != NULL);
1333 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1334 start_application(command, no_startup_id);
1336 // XXX: default reply for now, make this a better reply
1341 * Implementation of 'focus left|right|up|down'.
1344 void cmd_focus_direction(I3_CMD, char *direction) {
1345 DLOG("direction = *%s*\n", direction);
1347 if (strcmp(direction, "left") == 0)
1348 tree_next('p', HORIZ);
1349 else if (strcmp(direction, "right") == 0)
1350 tree_next('n', HORIZ);
1351 else if (strcmp(direction, "up") == 0)
1352 tree_next('p', VERT);
1353 else if (strcmp(direction, "down") == 0)
1354 tree_next('n', VERT);
1356 ELOG("Invalid focus direction (%s)\n", direction);
1361 cmd_output->needs_tree_render = true;
1362 // XXX: default reply for now, make this a better reply
1367 * Implementation of 'focus tiling|floating|mode_toggle'.
1370 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1371 DLOG("window_mode = %s\n", window_mode);
1373 Con *ws = con_get_workspace(focused);
1375 if (strcmp(window_mode, "mode_toggle") == 0) {
1376 if (con_inside_floating(focused))
1377 window_mode = "tiling";
1379 window_mode = "floating";
1382 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1383 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1384 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1387 con_focus(con_descend_focused(current));
1392 cmd_output->needs_tree_render = true;
1393 // XXX: default reply for now, make this a better reply
1398 * Implementation of 'focus parent|child'.
1401 void cmd_focus_level(I3_CMD, char *level) {
1402 DLOG("level = %s\n", level);
1403 bool success = false;
1405 /* Focusing the parent can only be allowed if the newly
1406 * focused container won't escape the fullscreen container. */
1407 if (strcmp(level, "parent") == 0) {
1408 if (focused && focused->parent) {
1409 if (con_fullscreen_permits_focusing(focused->parent))
1410 success = level_up();
1412 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1416 /* Focusing a child should always be allowed. */
1418 success = level_down();
1420 cmd_output->needs_tree_render = success;
1421 // XXX: default reply for now, make this a better reply
1426 * Implementation of 'focus'.
1429 void cmd_focus(I3_CMD) {
1430 DLOG("current_match = %p\n", current_match);
1432 if (match_is_empty(current_match)) {
1433 ELOG("You have to specify which window/container should be focused.\n");
1434 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1436 yerror("You have to specify which window/container should be focused");
1441 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1444 TAILQ_FOREACH(current, &owindows, owindows) {
1445 Con *ws = con_get_workspace(current->con);
1446 /* If no workspace could be found, this was a dock window.
1447 * Just skip it, you cannot focus dock windows. */
1451 /* Check the fullscreen focus constraints. */
1452 if (!con_fullscreen_permits_focusing(current->con)) {
1453 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1458 /* In case this is a scratchpad window, call scratchpad_show(). */
1459 if (ws == __i3_scratch) {
1460 scratchpad_show(current->con);
1462 /* While for the normal focus case we can change focus multiple
1463 * times and only a single window ends up focused, we could show
1464 * multiple scratchpad windows. So, rather break here. */
1468 /* If the container is not on the current workspace,
1469 * workspace_show() will switch to a different workspace and (if
1470 * enabled) trigger a mouse pointer warp to the currently focused
1471 * container (!) on the target workspace.
1473 * Therefore, before calling workspace_show(), we make sure that
1474 * 'current' will be focused on the workspace. However, we cannot
1475 * just con_focus(current) because then the pointer will not be
1476 * warped at all (the code thinks we are already there).
1478 * So we focus 'current' to make it the currently focused window of
1479 * the target workspace, then revert focus. */
1480 Con *currently_focused = focused;
1481 con_focus(current->con);
1482 con_focus(currently_focused);
1484 /* Now switch to the workspace, then focus */
1486 LOG("focusing %p / %s\n", current->con, current->con->name);
1487 con_focus(current->con);
1492 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1493 "while only exactly one container can be focused at a time.\n",
1496 cmd_output->needs_tree_render = true;
1497 ysuccess(count > 0);
1501 * Implementation of 'fullscreen enable|toggle [global]' and
1502 * 'fullscreen disable'
1505 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1506 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1507 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1512 TAILQ_FOREACH(current, &owindows, owindows) {
1513 DLOG("matching: %p / %s\n", current->con, current->con->name);
1514 if (strcmp(action, "toggle") == 0) {
1515 con_toggle_fullscreen(current->con, mode);
1516 } else if (strcmp(action, "enable") == 0) {
1517 con_enable_fullscreen(current->con, mode);
1518 } else if (strcmp(action, "disable") == 0) {
1519 con_disable_fullscreen(current->con);
1523 cmd_output->needs_tree_render = true;
1524 // XXX: default reply for now, make this a better reply
1529 * Implementation of 'sticky enable|disable|toggle'.
1532 void cmd_sticky(I3_CMD, char *action) {
1533 DLOG("%s sticky on window\n", action);
1537 TAILQ_FOREACH(current, &owindows, owindows) {
1538 if (current->con->window == NULL) {
1539 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1542 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1544 bool sticky = false;
1545 if (strcmp(action, "enable") == 0)
1547 else if (strcmp(action, "disable") == 0)
1549 else if (strcmp(action, "toggle") == 0)
1550 sticky = !current->con->sticky;
1552 current->con->sticky = sticky;
1553 ewmh_update_sticky(current->con->window->id, sticky);
1556 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1557 * sure it gets pushed to the front now. */
1558 output_push_sticky_windows(focused);
1560 cmd_output->needs_tree_render = true;
1565 * Implementation of 'move <direction> [<pixels> [px]]'.
1568 void cmd_move_direction(I3_CMD, char *direction, long move_px) {
1572 Con *initially_focused = focused;
1574 TAILQ_FOREACH(current, &owindows, owindows) {
1575 DLOG("moving in direction %s, px %ld\n", direction, move_px);
1576 if (con_is_floating(current->con)) {
1577 DLOG("floating move with %ld pixels\n", move_px);
1578 Rect newrect = current->con->parent->rect;
1579 if (strcmp(direction, "left") == 0) {
1580 newrect.x -= move_px;
1581 } else if (strcmp(direction, "right") == 0) {
1582 newrect.x += move_px;
1583 } else if (strcmp(direction, "up") == 0) {
1584 newrect.y -= move_px;
1585 } else if (strcmp(direction, "down") == 0) {
1586 newrect.y += move_px;
1588 floating_reposition(current->con->parent, newrect);
1590 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1591 cmd_output->needs_tree_render = true;
1595 /* the move command should not disturb focus */
1596 if (focused != initially_focused)
1597 con_focus(initially_focused);
1599 // XXX: default reply for now, make this a better reply
1604 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1607 void cmd_layout(I3_CMD, char *layout_str) {
1608 if (strcmp(layout_str, "stacking") == 0)
1609 layout_str = "stacked";
1612 /* default is a special case which will be handled in con_set_layout(). */
1613 if (strcmp(layout_str, "default") == 0)
1615 else if (strcmp(layout_str, "stacked") == 0)
1617 else if (strcmp(layout_str, "tabbed") == 0)
1619 else if (strcmp(layout_str, "splitv") == 0)
1621 else if (strcmp(layout_str, "splith") == 0)
1624 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1628 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1630 /* check if the match is empty, not if the result is empty */
1631 if (match_is_empty(current_match))
1632 con_set_layout(focused, layout);
1634 TAILQ_FOREACH(current, &owindows, owindows) {
1635 DLOG("matching: %p / %s\n", current->con, current->con->name);
1636 con_set_layout(current->con, layout);
1640 cmd_output->needs_tree_render = true;
1641 // XXX: default reply for now, make this a better reply
1646 * Implementation of 'layout toggle [all|split]'.
1649 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1652 if (toggle_mode == NULL)
1653 toggle_mode = "default";
1655 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1657 /* check if the match is empty, not if the result is empty */
1658 if (match_is_empty(current_match))
1659 con_toggle_layout(focused, toggle_mode);
1661 TAILQ_FOREACH(current, &owindows, owindows) {
1662 DLOG("matching: %p / %s\n", current->con, current->con->name);
1663 con_toggle_layout(current->con, toggle_mode);
1667 cmd_output->needs_tree_render = true;
1668 // XXX: default reply for now, make this a better reply
1673 * Implementation of 'exit'.
1676 void cmd_exit(I3_CMD) {
1677 LOG("Exiting due to user command.\n");
1679 unlink(config.ipc_socket_path);
1680 xcb_disconnect(conn);
1687 * Implementation of 'reload'.
1690 void cmd_reload(I3_CMD) {
1692 kill_nagbar(&config_error_nagbar_pid, false);
1693 kill_nagbar(&command_error_nagbar_pid, false);
1694 load_configuration(conn, NULL, true);
1696 /* Send an IPC event just in case the ws names have changed */
1697 ipc_send_workspace_event("reload", NULL, NULL);
1698 /* Send an update event for the barconfig just in case it has changed */
1701 // XXX: default reply for now, make this a better reply
1706 * Implementation of 'restart'.
1709 void cmd_restart(I3_CMD) {
1710 LOG("restarting i3\n");
1712 unlink(config.ipc_socket_path);
1713 /* We need to call this manually since atexit handlers don’t get called
1715 purge_zerobyte_logfile();
1718 // XXX: default reply for now, make this a better reply
1723 * Implementation of 'open'.
1726 void cmd_open(I3_CMD) {
1727 LOG("opening new container\n");
1728 Con *con = tree_open_con(NULL, NULL);
1729 con->layout = L_SPLITH;
1736 y(integer, (long int)con);
1739 cmd_output->needs_tree_render = true;
1743 * Implementation of 'focus output <output>'.
1746 void cmd_focus_output(I3_CMD, char *name) {
1749 DLOG("name = %s\n", name);
1753 /* get the output */
1754 Output *current_output = NULL;
1757 TAILQ_FOREACH(current, &owindows, owindows)
1758 current_output = get_output_of_con(current->con);
1759 assert(current_output != NULL);
1761 output = get_output_from_string(current_output, name);
1764 LOG("No such output found.\n");
1769 /* get visible workspace on output */
1771 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1779 cmd_output->needs_tree_render = true;
1780 // XXX: default reply for now, make this a better reply
1785 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1788 void cmd_move_window_to_position(I3_CMD, char *method, long x, long y) {
1789 bool has_error = false;
1794 TAILQ_FOREACH(current, &owindows, owindows) {
1795 if (!con_is_floating(current->con)) {
1796 ELOG("Cannot change position. The window/container is not floating\n");
1799 yerror("Cannot change position of a window/container because it is not floating.");
1806 if (strcmp(method, "absolute") == 0) {
1807 current->con->parent->rect.x = x;
1808 current->con->parent->rect.y = y;
1810 DLOG("moving to absolute position %ld %ld\n", x, y);
1811 floating_maybe_reassign_ws(current->con->parent);
1812 cmd_output->needs_tree_render = true;
1815 if (strcmp(method, "position") == 0) {
1816 Rect newrect = current->con->parent->rect;
1818 DLOG("moving to position %ld %ld\n", x, y);
1822 floating_reposition(current->con->parent, newrect);
1826 // XXX: default reply for now, make this a better reply
1832 * Implementation of 'move [window|container] [to] [absolute] position center
1835 void cmd_move_window_to_center(I3_CMD, char *method) {
1836 if (!con_is_floating(focused)) {
1837 ELOG("Cannot change position. The window/container is not floating\n");
1838 yerror("Cannot change position. The window/container is not floating.");
1842 if (strcmp(method, "absolute") == 0) {
1843 DLOG("moving to absolute center\n");
1844 floating_center(focused->parent, croot->rect);
1846 floating_maybe_reassign_ws(focused->parent);
1847 cmd_output->needs_tree_render = true;
1850 if (strcmp(method, "position") == 0) {
1851 DLOG("moving to center\n");
1852 floating_center(focused->parent, con_get_workspace(focused)->rect);
1854 cmd_output->needs_tree_render = true;
1857 // XXX: default reply for now, make this a better reply
1862 * Implementation of 'move [window|container] [to] position mouse'
1865 void cmd_move_window_to_mouse(I3_CMD) {
1869 TAILQ_FOREACH(current, &owindows, owindows) {
1870 Con *floating_con = con_inside_floating(current->con);
1871 if (floating_con == NULL) {
1872 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1873 current->con, current->con->name);
1877 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1878 floating_move_to_pointer(floating_con);
1881 cmd_output->needs_tree_render = true;
1886 * Implementation of 'move scratchpad'.
1889 void cmd_move_scratchpad(I3_CMD) {
1890 DLOG("should move window to scratchpad\n");
1895 TAILQ_FOREACH(current, &owindows, owindows) {
1896 DLOG("matching: %p / %s\n", current->con, current->con->name);
1897 scratchpad_move(current->con);
1900 cmd_output->needs_tree_render = true;
1901 // XXX: default reply for now, make this a better reply
1906 * Implementation of 'scratchpad show'.
1909 void cmd_scratchpad_show(I3_CMD) {
1910 DLOG("should show scratchpad window\n");
1913 if (match_is_empty(current_match)) {
1914 scratchpad_show(NULL);
1916 TAILQ_FOREACH(current, &owindows, owindows) {
1917 DLOG("matching: %p / %s\n", current->con, current->con->name);
1918 scratchpad_show(current->con);
1922 cmd_output->needs_tree_render = true;
1923 // XXX: default reply for now, make this a better reply
1928 * Implementation of 'title_format <format>'
1931 void cmd_title_format(I3_CMD, char *format) {
1932 DLOG("setting title_format to \"%s\"\n", format);
1936 TAILQ_FOREACH(current, &owindows, owindows) {
1937 if (current->con->window == NULL)
1940 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1941 FREE(current->con->window->title_format);
1943 /* If we only display the title without anything else, we can skip the parsing step,
1944 * so we remove the title format altogether. */
1945 if (strcasecmp(format, "%title") != 0) {
1946 current->con->window->title_format = sstrdup(format);
1948 i3String *formatted_title = window_parse_title_format(current->con->window);
1949 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1950 I3STRING_FREE(formatted_title);
1952 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1953 ewmh_update_visible_name(current->con->window->id, NULL);
1956 /* Make sure the window title is redrawn immediately. */
1957 current->con->window->name_x_changed = true;
1960 cmd_output->needs_tree_render = true;
1965 * Implementation of 'rename workspace [<name>] to <name>'
1968 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1969 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1970 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1975 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1977 LOG("Renaming current workspace to \"%s\"\n", new_name);
1980 Con *output, *workspace = NULL;
1982 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1983 GREP_FIRST(workspace, output_get_content(output),
1984 !strcasecmp(child->name, old_name));
1986 workspace = con_get_workspace(focused);
1987 old_name = workspace->name;
1991 yerror("Old workspace \"%s\" not found", old_name);
1995 Con *check_dest = NULL;
1996 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1997 GREP_FIRST(check_dest, output_get_content(output),
1998 !strcasecmp(child->name, new_name));
2000 if (check_dest != NULL) {
2001 yerror("New workspace \"%s\" already exists", new_name);
2005 /* Change the name and try to parse it as a number. */
2006 FREE(workspace->name);
2007 workspace->name = sstrdup(new_name);
2009 workspace->num = ws_name_to_number(new_name);
2010 LOG("num = %d\n", workspace->num);
2012 /* By re-attaching, the sort order will be correct afterwards. */
2013 Con *previously_focused = focused;
2014 Con *parent = workspace->parent;
2015 con_detach(workspace);
2016 con_attach(workspace, parent, false);
2018 /* Move the workspace to the correct output if it has an assignment */
2019 struct Workspace_Assignment *assignment = NULL;
2020 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2021 if (assignment->output == NULL)
2023 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2027 workspace_move_to_output(workspace, assignment->output);
2029 if (previously_focused)
2030 workspace_show(con_get_workspace(previously_focused));
2035 /* Restore the previous focus since con_attach messes with the focus. */
2036 con_focus(previously_focused);
2038 cmd_output->needs_tree_render = true;
2041 ipc_send_workspace_event("rename", workspace, NULL);
2042 ewmh_update_desktop_names();
2043 ewmh_update_desktop_viewport();
2044 ewmh_update_current_desktop();
2046 startup_sequence_rename_workspace(old_name, new_name);
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