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 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, 10);
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, 10);
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 ELOG("Unknown criterion: %s\n", ctype);
418 * Implementation of 'move [window|container] [to] workspace
419 * next|prev|next_on_output|prev_on_output|current'.
422 void cmd_move_con_to_workspace(I3_CMD, char *which) {
425 DLOG("which=%s\n", which);
427 /* We have nothing to move:
428 * when criteria was specified but didn't match any window or
429 * when criteria wasn't specified and we don't have any window focused. */
430 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
431 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
432 !con_has_children(focused))) {
439 /* get the workspace */
441 if (strcmp(which, "next") == 0)
442 ws = workspace_next();
443 else if (strcmp(which, "prev") == 0)
444 ws = workspace_prev();
445 else if (strcmp(which, "next_on_output") == 0)
446 ws = workspace_next_on_output();
447 else if (strcmp(which, "prev_on_output") == 0)
448 ws = workspace_prev_on_output();
449 else if (strcmp(which, "current") == 0)
450 ws = con_get_workspace(focused);
452 ELOG("BUG: called with which=%s\n", which);
457 TAILQ_FOREACH(current, &owindows, owindows) {
458 DLOG("matching: %p / %s\n", current->con, current->con->name);
459 con_move_to_workspace(current->con, ws, true, false);
462 cmd_output->needs_tree_render = true;
463 // XXX: default reply for now, make this a better reply
468 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
471 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
475 ws = workspace_back_and_forth_get();
478 yerror("No workspace was previously active.");
484 TAILQ_FOREACH(current, &owindows, owindows) {
485 DLOG("matching: %p / %s\n", current->con, current->con->name);
486 con_move_to_workspace(current->con, ws, true, false);
489 cmd_output->needs_tree_render = true;
490 // XXX: default reply for now, make this a better reply
495 * Implementation of 'move [window|container] [to] workspace <name>'.
498 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
499 if (strncasecmp(name, "__", strlen("__")) == 0) {
500 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
507 /* We have nothing to move:
508 * when criteria was specified but didn't match any window or
509 * when criteria wasn't specified and we don't have any window focused. */
510 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
511 ELOG("No windows match your criteria, cannot move.\n");
514 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
515 !con_has_children(focused)) {
520 LOG("should move window to workspace %s\n", name);
521 /* get the workspace */
522 Con *ws = workspace_get(name, NULL);
524 ws = maybe_auto_back_and_forth_workspace(ws);
528 TAILQ_FOREACH(current, &owindows, owindows) {
529 DLOG("matching: %p / %s\n", current->con, current->con->name);
530 con_move_to_workspace(current->con, ws, true, false);
533 cmd_output->needs_tree_render = true;
534 // XXX: default reply for now, make this a better reply
539 * Implementation of 'move [window|container] [to] workspace number <name>'.
542 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
545 /* We have nothing to move:
546 * when criteria was specified but didn't match any window or
547 * when criteria wasn't specified and we don't have any window focused. */
548 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
549 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
550 !con_has_children(focused))) {
555 LOG("should move window to workspace %s\n", which);
556 /* get the workspace */
557 Con *output, *workspace = NULL;
559 long parsed_num = ws_name_to_number(which);
561 if (parsed_num == -1) {
562 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
563 yerror("Could not parse number \"%s\"", which);
567 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
568 GREP_FIRST(workspace, output_get_content(output),
569 child->num == parsed_num);
572 workspace = workspace_get(which, NULL);
575 workspace = maybe_auto_back_and_forth_workspace(workspace);
579 TAILQ_FOREACH(current, &owindows, owindows) {
580 DLOG("matching: %p / %s\n", current->con, current->con->name);
581 con_move_to_workspace(current->con, workspace, true, false);
584 cmd_output->needs_tree_render = true;
585 // XXX: default reply for now, make this a better reply
589 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
590 LOG("floating resize\n");
591 Rect old_rect = floating_con->rect;
592 Con *focused_con = con_descend_focused(floating_con);
594 /* ensure that resize will take place even if pixel increment is smaller than
595 * height increment or width increment.
597 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
598 strcmp(direction, "height") == 0) {
600 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
602 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
603 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
605 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
607 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
610 if (strcmp(direction, "up") == 0) {
611 floating_con->rect.height += px;
612 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
613 floating_con->rect.height += px;
614 } else if (strcmp(direction, "left") == 0) {
615 floating_con->rect.width += px;
617 floating_con->rect.width += px;
620 floating_check_size(floating_con);
622 /* Did we actually resize anything or did the size constraints prevent us?
623 * If we could not resize, exit now to not move the window. */
624 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
627 if (strcmp(direction, "up") == 0) {
628 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
629 } else if (strcmp(direction, "left") == 0) {
630 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
633 /* If this is a scratchpad window, don't auto center it from now on. */
634 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
635 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
638 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
639 LOG("tiling resize\n");
641 Con *first = current;
642 direction_t search_direction;
643 if (!strcmp(direction, "left"))
644 search_direction = D_LEFT;
645 else if (!strcmp(direction, "right"))
646 search_direction = D_RIGHT;
647 else if (!strcmp(direction, "up"))
648 search_direction = D_UP;
650 search_direction = D_DOWN;
652 bool res = resize_find_tiling_participants(&first, &second, search_direction);
654 LOG("No second container in this direction found.\n");
659 /* get the default percentage */
660 int children = con_num_children(first->parent);
661 LOG("ins. %d children\n", children);
662 double percentage = 1.0 / children;
663 LOG("default percentage = %f\n", percentage);
666 LOG("second->percent = %f\n", second->percent);
667 LOG("first->percent before = %f\n", first->percent);
668 if (first->percent == 0.0)
669 first->percent = percentage;
670 if (second->percent == 0.0)
671 second->percent = percentage;
672 double new_first_percent = first->percent + ((double)ppt / 100.0);
673 double new_second_percent = second->percent - ((double)ppt / 100.0);
674 LOG("new_first_percent = %f\n", new_first_percent);
675 LOG("new_second_percent = %f\n", new_second_percent);
676 /* Ensure that the new percentages are positive and greater than
677 * 0.05 to have a reasonable minimum size. */
678 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
679 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
680 first->percent += ((double)ppt / 100.0);
681 second->percent -= ((double)ppt / 100.0);
682 LOG("first->percent after = %f\n", first->percent);
683 LOG("second->percent after = %f\n", second->percent);
685 LOG("Not resizing, already at minimum size\n");
691 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
692 LOG("width/height resize\n");
693 /* get the appropriate current container (skip stacked/tabbed cons) */
694 while (current->parent->layout == L_STACKED ||
695 current->parent->layout == L_TABBED)
696 current = current->parent;
698 /* Then further go up until we find one with the matching orientation. */
699 orientation_t search_orientation =
700 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
702 while (current->type != CT_WORKSPACE &&
703 current->type != CT_FLOATING_CON &&
704 con_orientation(current->parent) != search_orientation)
705 current = current->parent;
707 /* get the default percentage */
708 int children = con_num_children(current->parent);
709 LOG("ins. %d children\n", children);
710 double percentage = 1.0 / children;
711 LOG("default percentage = %f\n", percentage);
713 orientation_t orientation = con_orientation(current->parent);
715 if ((orientation == HORIZ &&
716 strcmp(direction, "height") == 0) ||
717 (orientation == VERT &&
718 strcmp(direction, "width") == 0)) {
719 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
720 (orientation == HORIZ ? "horizontal" : "vertical"));
726 LOG("This is the only container, cannot resize.\n");
731 /* Ensure all the other children have a percentage set. */
733 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
734 LOG("child->percent = %f (child %p)\n", child->percent, child);
735 if (child->percent == 0.0)
736 child->percent = percentage;
739 double new_current_percent = current->percent + ((double)ppt / 100.0);
740 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
741 LOG("new_current_percent = %f\n", new_current_percent);
742 LOG("subtract_percent = %f\n", subtract_percent);
743 /* Ensure that the new percentages are positive and greater than
744 * 0.05 to have a reasonable minimum size. */
745 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
746 if (child == current)
748 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
749 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
754 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
755 LOG("Not resizing, already at minimum size\n");
760 current->percent += ((double)ppt / 100.0);
761 LOG("current->percent after = %f\n", current->percent);
763 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
764 if (child == current)
766 child->percent -= subtract_percent;
767 LOG("child->percent after (%p) = %f\n", child, child->percent);
774 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
777 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
778 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
779 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
780 // 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
781 int px = atoi(resize_px);
782 int ppt = atoi(resize_ppt);
783 if (strcmp(way, "shrink") == 0) {
791 TAILQ_FOREACH(current, &owindows, owindows) {
792 /* Don't handle dock windows (issue #1201) */
793 if (current->con->window && current->con->window->dock) {
794 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
799 if ((floating_con = con_inside_floating(current->con))) {
800 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
802 if (strcmp(direction, "width") == 0 ||
803 strcmp(direction, "height") == 0) {
804 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
807 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
813 cmd_output->needs_tree_render = true;
814 // XXX: default reply for now, make this a better reply
819 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
822 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
823 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
828 TAILQ_FOREACH(current, &owindows, owindows) {
829 DLOG("matching: %p / %s\n", current->con, current->con->name);
830 int border_style = current->con->border_style;
832 int tmp_border_width = -1;
833 tmp_border_width = strtol(border_width, &end, 10);
834 if (end == border_width) {
835 /* no valid digits found */
836 tmp_border_width = -1;
838 if (strcmp(border_style_str, "toggle") == 0) {
841 if (border_style == BS_NORMAL)
842 tmp_border_width = 2;
843 else if (border_style == BS_NONE)
844 tmp_border_width = 0;
845 else if (border_style == BS_PIXEL)
846 tmp_border_width = 1;
848 if (strcmp(border_style_str, "normal") == 0)
849 border_style = BS_NORMAL;
850 else if (strcmp(border_style_str, "pixel") == 0)
851 border_style = BS_PIXEL;
852 else if (strcmp(border_style_str, "1pixel") == 0) {
853 border_style = BS_PIXEL;
854 tmp_border_width = 1;
855 } else if (strcmp(border_style_str, "none") == 0)
856 border_style = BS_NONE;
858 ELOG("BUG: called with border_style=%s\n", border_style_str);
863 con_set_border_style(current->con, border_style, tmp_border_width);
866 cmd_output->needs_tree_render = true;
867 // XXX: default reply for now, make this a better reply
872 * Implementation of 'nop <comment>'.
875 void cmd_nop(I3_CMD, char *comment) {
876 LOG("-------------------------------------------------\n");
877 LOG(" NOP: %s\n", comment);
878 LOG("-------------------------------------------------\n");
882 * Implementation of 'append_layout <path>'.
885 void cmd_append_layout(I3_CMD, char *path) {
886 LOG("Appending layout \"%s\"\n", path);
888 /* Make sure we allow paths like '~/.i3/layout.json' */
889 path = resolve_tilde(path);
891 json_content_t content = json_determine_content(path);
892 LOG("JSON content = %d\n", content);
893 if (content == JSON_CONTENT_UNKNOWN) {
894 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
895 yerror("Could not determine the contents of \"%s\".", path);
900 Con *parent = focused;
901 if (content == JSON_CONTENT_WORKSPACE) {
902 parent = output_get_content(con_get_output(parent));
904 /* We need to append the layout to a split container, since a leaf
905 * container must not have any children (by definition).
906 * Note that we explicitly check for workspaces, since they are okay for
907 * this purpose, but con_accepts_window() returns false for workspaces. */
908 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
909 parent = parent->parent;
911 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
912 char *errormsg = NULL;
913 tree_append_json(parent, path, &errormsg);
914 if (errormsg != NULL) {
917 /* Note that we continue executing since tree_append_json() has
918 * side-effects — user-provided layouts can be partly valid, partly
919 * invalid, leading to half of the placeholder containers being
925 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
926 // false); should be enough, but when sending 'workspace 4; append_layout
927 // /tmp/foo.json', the needs_tree_render == true of the workspace command
928 // is not executed yet and will be batched with append_layout’s
929 // needs_tree_render after the parser finished. We should check if that is
931 render_con(croot, false);
933 restore_open_placeholder_windows(parent);
935 if (content == JSON_CONTENT_WORKSPACE)
936 ipc_send_workspace_event("restored", parent, NULL);
939 cmd_output->needs_tree_render = true;
943 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
946 void cmd_workspace(I3_CMD, char *which) {
949 DLOG("which=%s\n", which);
951 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
952 LOG("Cannot switch workspace while in global fullscreen\n");
957 if (strcmp(which, "next") == 0)
958 ws = workspace_next();
959 else if (strcmp(which, "prev") == 0)
960 ws = workspace_prev();
961 else if (strcmp(which, "next_on_output") == 0)
962 ws = workspace_next_on_output();
963 else if (strcmp(which, "prev_on_output") == 0)
964 ws = workspace_prev_on_output();
966 ELOG("BUG: called with which=%s\n", which);
973 cmd_output->needs_tree_render = true;
974 // XXX: default reply for now, make this a better reply
979 * Implementation of 'workspace number <name>'
982 void cmd_workspace_number(I3_CMD, char *which) {
983 Con *output, *workspace = NULL;
985 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
986 LOG("Cannot switch workspace while in global fullscreen\n");
991 long parsed_num = ws_name_to_number(which);
993 if (parsed_num == -1) {
994 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
995 yerror("Could not parse number \"%s\"", which);
999 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1000 GREP_FIRST(workspace, output_get_content(output),
1001 child->num == parsed_num);
1004 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1006 workspace_show_by_name(which);
1007 cmd_output->needs_tree_render = true;
1010 if (maybe_back_and_forth(cmd_output, workspace->name))
1012 workspace_show(workspace);
1014 cmd_output->needs_tree_render = true;
1015 // XXX: default reply for now, make this a better reply
1020 * Implementation of 'workspace back_and_forth'.
1023 void cmd_workspace_back_and_forth(I3_CMD) {
1024 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1025 LOG("Cannot switch workspace while in global fullscreen\n");
1030 workspace_back_and_forth();
1032 cmd_output->needs_tree_render = true;
1033 // XXX: default reply for now, make this a better reply
1038 * Implementation of 'workspace <name>'
1041 void cmd_workspace_name(I3_CMD, char *name) {
1042 if (strncasecmp(name, "__", strlen("__")) == 0) {
1043 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1048 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1049 LOG("Cannot switch workspace while in global fullscreen\n");
1054 DLOG("should switch to workspace %s\n", name);
1055 if (maybe_back_and_forth(cmd_output, name))
1057 workspace_show_by_name(name);
1059 cmd_output->needs_tree_render = true;
1060 // XXX: default reply for now, make this a better reply
1065 * Implementation of 'mark [--toggle] <mark>'
1068 void cmd_mark(I3_CMD, char *mark, char *toggle) {
1071 owindow *current = TAILQ_FIRST(&owindows);
1072 if (current == NULL) {
1077 /* Marks must be unique, i.e., no two windows must have the same mark. */
1078 if (current != TAILQ_LAST(&owindows, owindows_head)) {
1079 yerror("A mark must not be put onto more than one window");
1083 DLOG("matching: %p / %s\n", current->con, current->con->name);
1084 current->con->mark_changed = true;
1085 if (toggle != NULL && current->con->mark && strcmp(current->con->mark, mark) == 0) {
1086 DLOG("removing window mark %s\n", mark);
1087 FREE(current->con->mark);
1089 DLOG("marking window with str %s\n", mark);
1090 FREE(current->con->mark);
1091 current->con->mark = sstrdup(mark);
1094 DLOG("Clearing all non-matched windows with this mark\n");
1096 TAILQ_FOREACH(con, &all_cons, all_cons) {
1097 /* Skip matched window, we took care of it already. */
1098 if (current->con == con)
1101 if (con->mark && strcmp(con->mark, mark) == 0) {
1103 con->mark_changed = true;
1107 cmd_output->needs_tree_render = true;
1108 // XXX: default reply for now, make this a better reply
1113 * Implementation of 'unmark [mark]'
1116 void cmd_unmark(I3_CMD, char *mark) {
1119 TAILQ_FOREACH(con, &all_cons, all_cons) {
1120 if (con->mark == NULL)
1124 con->mark_changed = true;
1126 DLOG("removed all window marks");
1129 TAILQ_FOREACH(con, &all_cons, all_cons) {
1130 if (con->mark && strcmp(con->mark, mark) == 0) {
1132 con->mark_changed = true;
1135 DLOG("removed window mark %s\n", mark);
1138 cmd_output->needs_tree_render = true;
1139 // XXX: default reply for now, make this a better reply
1144 * Implementation of 'mode <string>'.
1147 void cmd_mode(I3_CMD, char *mode) {
1148 DLOG("mode=%s\n", mode);
1151 // XXX: default reply for now, make this a better reply
1156 * Implementation of 'move [window|container] [to] output <str>'.
1159 void cmd_move_con_to_output(I3_CMD, char *name) {
1162 DLOG("should move window to output %s\n", name);
1166 Output *current_output = NULL;
1167 // TODO: fix the handling of criteria
1168 TAILQ_FOREACH(current, &owindows, owindows)
1169 current_output = get_output_of_con(current->con);
1170 assert(current_output != NULL);
1172 Output *output = get_output_from_string(current_output, name);
1174 LOG("No such output found.\n");
1179 /* get visible workspace on output */
1181 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1187 TAILQ_FOREACH(current, &owindows, owindows) {
1188 DLOG("matching: %p / %s\n", current->con, current->con->name);
1189 con_move_to_workspace(current->con, ws, true, false);
1192 cmd_output->needs_tree_render = true;
1193 // XXX: default reply for now, make this a better reply
1198 * Implementation of 'floating enable|disable|toggle'
1201 void cmd_floating(I3_CMD, char *floating_mode) {
1204 DLOG("floating_mode=%s\n", floating_mode);
1208 TAILQ_FOREACH(current, &owindows, owindows) {
1209 DLOG("matching: %p / %s\n", current->con, current->con->name);
1210 if (strcmp(floating_mode, "toggle") == 0) {
1211 DLOG("should toggle mode\n");
1212 toggle_floating_mode(current->con, false);
1214 DLOG("should switch mode to %s\n", floating_mode);
1215 if (strcmp(floating_mode, "enable") == 0) {
1216 floating_enable(current->con, false);
1218 floating_disable(current->con, false);
1223 cmd_output->needs_tree_render = true;
1224 // XXX: default reply for now, make this a better reply
1229 * Implementation of 'move workspace to [output] <str>'.
1232 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1233 DLOG("should move workspace to output %s\n", name);
1238 TAILQ_FOREACH(current, &owindows, owindows) {
1239 Con *ws = con_get_workspace(current->con);
1240 bool success = workspace_move_to_output(ws, name);
1242 ELOG("Failed to move workspace to output.\n");
1248 cmd_output->needs_tree_render = true;
1249 // XXX: default reply for now, make this a better reply
1254 * Implementation of 'split v|h|vertical|horizontal'.
1257 void cmd_split(I3_CMD, char *direction) {
1259 /* TODO: use matches */
1260 LOG("splitting in direction %c\n", direction[0]);
1261 if (match_is_empty(current_match))
1262 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1264 TAILQ_FOREACH(current, &owindows, owindows) {
1265 DLOG("matching: %p / %s\n", current->con, current->con->name);
1266 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1270 cmd_output->needs_tree_render = true;
1271 // XXX: default reply for now, make this a better reply
1276 * Implementation of 'kill [window|client]'.
1279 void cmd_kill(I3_CMD, char *kill_mode_str) {
1280 if (kill_mode_str == NULL)
1281 kill_mode_str = "window";
1284 DLOG("kill_mode=%s\n", kill_mode_str);
1287 if (strcmp(kill_mode_str, "window") == 0)
1288 kill_mode = KILL_WINDOW;
1289 else if (strcmp(kill_mode_str, "client") == 0)
1290 kill_mode = KILL_CLIENT;
1292 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1297 /* check if the match is empty, not if the result is empty */
1298 if (match_is_empty(current_match))
1299 tree_close_con(kill_mode);
1301 TAILQ_FOREACH(current, &owindows, owindows) {
1302 DLOG("matching: %p / %s\n", current->con, current->con->name);
1303 tree_close(current->con, kill_mode, false, false);
1307 cmd_output->needs_tree_render = true;
1308 // XXX: default reply for now, make this a better reply
1313 * Implementation of 'exec [--no-startup-id] <command>'.
1316 void cmd_exec(I3_CMD, char *nosn, char *command) {
1317 bool no_startup_id = (nosn != NULL);
1319 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1320 start_application(command, no_startup_id);
1322 // XXX: default reply for now, make this a better reply
1327 * Implementation of 'focus left|right|up|down'.
1330 void cmd_focus_direction(I3_CMD, char *direction) {
1331 DLOG("direction = *%s*\n", direction);
1333 if (strcmp(direction, "left") == 0)
1334 tree_next('p', HORIZ);
1335 else if (strcmp(direction, "right") == 0)
1336 tree_next('n', HORIZ);
1337 else if (strcmp(direction, "up") == 0)
1338 tree_next('p', VERT);
1339 else if (strcmp(direction, "down") == 0)
1340 tree_next('n', VERT);
1342 ELOG("Invalid focus direction (%s)\n", direction);
1347 cmd_output->needs_tree_render = true;
1348 // XXX: default reply for now, make this a better reply
1353 * Implementation of 'focus tiling|floating|mode_toggle'.
1356 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1357 DLOG("window_mode = %s\n", window_mode);
1359 Con *ws = con_get_workspace(focused);
1362 if (strcmp(window_mode, "mode_toggle") == 0) {
1363 current = TAILQ_FIRST(&(ws->focus_head));
1364 if (current != NULL && current->type == CT_FLOATING_CON)
1365 window_mode = "tiling";
1367 window_mode = "floating";
1369 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1370 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1371 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1374 con_focus(con_descend_focused(current));
1379 cmd_output->needs_tree_render = true;
1380 // XXX: default reply for now, make this a better reply
1385 * Implementation of 'focus parent|child'.
1388 void cmd_focus_level(I3_CMD, char *level) {
1389 DLOG("level = %s\n", level);
1390 bool success = false;
1392 /* Focusing the parent can only be allowed if the newly
1393 * focused container won't escape the fullscreen container. */
1394 if (strcmp(level, "parent") == 0) {
1395 if (focused && focused->parent) {
1396 if (con_fullscreen_permits_focusing(focused->parent))
1397 success = level_up();
1399 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1403 /* Focusing a child should always be allowed. */
1405 success = level_down();
1407 cmd_output->needs_tree_render = success;
1408 // XXX: default reply for now, make this a better reply
1413 * Implementation of 'focus'.
1416 void cmd_focus(I3_CMD) {
1417 DLOG("current_match = %p\n", current_match);
1419 if (match_is_empty(current_match)) {
1420 ELOG("You have to specify which window/container should be focused.\n");
1421 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1423 yerror("You have to specify which window/container should be focused");
1428 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1431 TAILQ_FOREACH(current, &owindows, owindows) {
1432 Con *ws = con_get_workspace(current->con);
1433 /* If no workspace could be found, this was a dock window.
1434 * Just skip it, you cannot focus dock windows. */
1438 /* Check the fullscreen focus constraints. */
1439 if (!con_fullscreen_permits_focusing(current->con)) {
1440 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1445 /* In case this is a scratchpad window, call scratchpad_show(). */
1446 if (ws == __i3_scratch) {
1447 scratchpad_show(current->con);
1449 /* While for the normal focus case we can change focus multiple
1450 * times and only a single window ends up focused, we could show
1451 * multiple scratchpad windows. So, rather break here. */
1455 /* If the container is not on the current workspace,
1456 * workspace_show() will switch to a different workspace and (if
1457 * enabled) trigger a mouse pointer warp to the currently focused
1458 * container (!) on the target workspace.
1460 * Therefore, before calling workspace_show(), we make sure that
1461 * 'current' will be focused on the workspace. However, we cannot
1462 * just con_focus(current) because then the pointer will not be
1463 * warped at all (the code thinks we are already there).
1465 * So we focus 'current' to make it the currently focused window of
1466 * the target workspace, then revert focus. */
1467 Con *currently_focused = focused;
1468 con_focus(current->con);
1469 con_focus(currently_focused);
1471 /* Now switch to the workspace, then focus */
1473 LOG("focusing %p / %s\n", current->con, current->con->name);
1474 con_focus(current->con);
1479 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1480 "while only exactly one container can be focused at a time.\n",
1483 cmd_output->needs_tree_render = true;
1484 // XXX: default reply for now, make this a better reply
1489 * Implementation of 'fullscreen enable|toggle [global]' and
1490 * 'fullscreen disable'
1493 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1494 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1495 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1500 TAILQ_FOREACH(current, &owindows, owindows) {
1501 DLOG("matching: %p / %s\n", current->con, current->con->name);
1502 if (strcmp(action, "toggle") == 0) {
1503 con_toggle_fullscreen(current->con, mode);
1504 } else if (strcmp(action, "enable") == 0) {
1505 con_enable_fullscreen(current->con, mode);
1506 } else if (strcmp(action, "disable") == 0) {
1507 con_disable_fullscreen(current->con);
1511 cmd_output->needs_tree_render = true;
1512 // XXX: default reply for now, make this a better reply
1517 * Implementation of 'move <direction> [<pixels> [px]]'.
1520 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1521 // 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
1522 int px = atoi(move_px);
1527 Con *initially_focused = focused;
1529 TAILQ_FOREACH(current, &owindows, owindows) {
1530 DLOG("moving in direction %s, px %s\n", direction, move_px);
1531 if (con_is_floating(current->con)) {
1532 DLOG("floating move with %d pixels\n", px);
1533 Rect newrect = current->con->parent->rect;
1534 if (strcmp(direction, "left") == 0) {
1536 } else if (strcmp(direction, "right") == 0) {
1538 } else if (strcmp(direction, "up") == 0) {
1540 } else if (strcmp(direction, "down") == 0) {
1543 floating_reposition(current->con->parent, newrect);
1545 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1546 cmd_output->needs_tree_render = true;
1550 /* the move command should not disturb focus */
1551 if (focused != initially_focused)
1552 con_focus(initially_focused);
1554 // XXX: default reply for now, make this a better reply
1559 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1562 void cmd_layout(I3_CMD, char *layout_str) {
1563 if (strcmp(layout_str, "stacking") == 0)
1564 layout_str = "stacked";
1567 /* default is a special case which will be handled in con_set_layout(). */
1568 if (strcmp(layout_str, "default") == 0)
1570 else if (strcmp(layout_str, "stacked") == 0)
1572 else if (strcmp(layout_str, "tabbed") == 0)
1574 else if (strcmp(layout_str, "splitv") == 0)
1576 else if (strcmp(layout_str, "splith") == 0)
1579 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1583 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1585 /* check if the match is empty, not if the result is empty */
1586 if (match_is_empty(current_match))
1587 con_set_layout(focused, layout);
1589 TAILQ_FOREACH(current, &owindows, owindows) {
1590 DLOG("matching: %p / %s\n", current->con, current->con->name);
1591 con_set_layout(current->con, layout);
1595 cmd_output->needs_tree_render = true;
1596 // XXX: default reply for now, make this a better reply
1601 * Implementation of 'layout toggle [all|split]'.
1604 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1607 if (toggle_mode == NULL)
1608 toggle_mode = "default";
1610 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1612 /* check if the match is empty, not if the result is empty */
1613 if (match_is_empty(current_match))
1614 con_toggle_layout(focused, toggle_mode);
1616 TAILQ_FOREACH(current, &owindows, owindows) {
1617 DLOG("matching: %p / %s\n", current->con, current->con->name);
1618 con_toggle_layout(current->con, toggle_mode);
1622 cmd_output->needs_tree_render = true;
1623 // XXX: default reply for now, make this a better reply
1628 * Implementation of 'exit'.
1631 void cmd_exit(I3_CMD) {
1632 LOG("Exiting due to user command.\n");
1634 unlink(config.ipc_socket_path);
1635 xcb_disconnect(conn);
1642 * Implementation of 'reload'.
1645 void cmd_reload(I3_CMD) {
1647 kill_nagbar(&config_error_nagbar_pid, false);
1648 kill_nagbar(&command_error_nagbar_pid, false);
1649 load_configuration(conn, NULL, true);
1651 /* Send an IPC event just in case the ws names have changed */
1652 ipc_send_workspace_event("reload", NULL, NULL);
1653 /* Send an update event for the barconfig just in case it has changed */
1656 // XXX: default reply for now, make this a better reply
1661 * Implementation of 'restart'.
1664 void cmd_restart(I3_CMD) {
1665 LOG("restarting i3\n");
1667 unlink(config.ipc_socket_path);
1668 /* We need to call this manually since atexit handlers don’t get called
1670 purge_zerobyte_logfile();
1673 // XXX: default reply for now, make this a better reply
1678 * Implementation of 'open'.
1681 void cmd_open(I3_CMD) {
1682 LOG("opening new container\n");
1683 Con *con = tree_open_con(NULL, NULL);
1684 con->layout = L_SPLITH;
1691 y(integer, (long int)con);
1694 cmd_output->needs_tree_render = true;
1698 * Implementation of 'focus output <output>'.
1701 void cmd_focus_output(I3_CMD, char *name) {
1704 DLOG("name = %s\n", name);
1708 /* get the output */
1709 Output *current_output = NULL;
1712 TAILQ_FOREACH(current, &owindows, owindows)
1713 current_output = get_output_of_con(current->con);
1714 assert(current_output != NULL);
1716 output = get_output_from_string(current_output, name);
1719 LOG("No such output found.\n");
1724 /* get visible workspace on output */
1726 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1734 cmd_output->needs_tree_render = true;
1735 // XXX: default reply for now, make this a better reply
1740 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1743 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1746 bool has_error = false;
1751 TAILQ_FOREACH(current, &owindows, owindows) {
1752 if (!con_is_floating(current->con)) {
1753 ELOG("Cannot change position. The window/container is not floating\n");
1756 yerror("Cannot change position of a window/container because it is not floating.");
1763 if (strcmp(method, "absolute") == 0) {
1764 current->con->parent->rect.x = x;
1765 current->con->parent->rect.y = y;
1767 DLOG("moving to absolute position %d %d\n", x, y);
1768 floating_maybe_reassign_ws(current->con->parent);
1769 cmd_output->needs_tree_render = true;
1772 if (strcmp(method, "position") == 0) {
1773 Rect newrect = current->con->parent->rect;
1775 DLOG("moving to position %d %d\n", x, y);
1779 floating_reposition(current->con->parent, newrect);
1783 // XXX: default reply for now, make this a better reply
1789 * Implementation of 'move [window|container] [to] [absolute] position center
1792 void cmd_move_window_to_center(I3_CMD, char *method) {
1793 if (!con_is_floating(focused)) {
1794 ELOG("Cannot change position. The window/container is not floating\n");
1795 yerror("Cannot change position. The window/container is not floating.");
1799 if (strcmp(method, "absolute") == 0) {
1800 DLOG("moving to absolute center\n");
1801 floating_center(focused->parent, croot->rect);
1803 floating_maybe_reassign_ws(focused->parent);
1804 cmd_output->needs_tree_render = true;
1807 if (strcmp(method, "position") == 0) {
1808 DLOG("moving to center\n");
1809 floating_center(focused->parent, con_get_workspace(focused)->rect);
1811 cmd_output->needs_tree_render = true;
1814 // XXX: default reply for now, make this a better reply
1819 * Implementation of 'move scratchpad'.
1822 void cmd_move_scratchpad(I3_CMD) {
1823 DLOG("should move window to scratchpad\n");
1828 TAILQ_FOREACH(current, &owindows, owindows) {
1829 DLOG("matching: %p / %s\n", current->con, current->con->name);
1830 scratchpad_move(current->con);
1833 cmd_output->needs_tree_render = true;
1834 // XXX: default reply for now, make this a better reply
1839 * Implementation of 'scratchpad show'.
1842 void cmd_scratchpad_show(I3_CMD) {
1843 DLOG("should show scratchpad window\n");
1846 if (match_is_empty(current_match)) {
1847 scratchpad_show(NULL);
1849 TAILQ_FOREACH(current, &owindows, owindows) {
1850 DLOG("matching: %p / %s\n", current->con, current->con->name);
1851 scratchpad_show(current->con);
1855 cmd_output->needs_tree_render = true;
1856 // XXX: default reply for now, make this a better reply
1861 * Implementation of 'rename workspace [<name>] to <name>'
1864 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1865 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1866 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1871 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1873 LOG("Renaming current workspace to \"%s\"\n", new_name);
1876 Con *output, *workspace = NULL;
1878 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1879 GREP_FIRST(workspace, output_get_content(output),
1880 !strcasecmp(child->name, old_name));
1882 workspace = con_get_workspace(focused);
1883 old_name = workspace->name;
1887 yerror("Old workspace \"%s\" not found", old_name);
1891 Con *check_dest = NULL;
1892 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1893 GREP_FIRST(check_dest, output_get_content(output),
1894 !strcasecmp(child->name, new_name));
1896 if (check_dest != NULL) {
1897 yerror("New workspace \"%s\" already exists", new_name);
1901 /* Change the name and try to parse it as a number. */
1902 FREE(workspace->name);
1903 workspace->name = sstrdup(new_name);
1905 workspace->num = ws_name_to_number(new_name);
1906 LOG("num = %d\n", workspace->num);
1908 /* By re-attaching, the sort order will be correct afterwards. */
1909 Con *previously_focused = focused;
1910 Con *parent = workspace->parent;
1911 con_detach(workspace);
1912 con_attach(workspace, parent, false);
1914 /* Move the workspace to the correct output if it has an assignment */
1915 struct Workspace_Assignment *assignment = NULL;
1916 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1917 if (assignment->output == NULL)
1919 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1923 workspace_move_to_output(workspace, assignment->output);
1925 if (previously_focused)
1926 workspace_show(con_get_workspace(previously_focused));
1931 /* Restore the previous focus since con_attach messes with the focus. */
1932 con_focus(previously_focused);
1934 cmd_output->needs_tree_render = true;
1937 ipc_send_workspace_event("rename", workspace, NULL);
1938 ewmh_update_desktop_names();
1939 ewmh_update_desktop_viewport();
1940 ewmh_update_current_desktop();
1942 startup_sequence_rename_workspace(old_name, new_name);
1946 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1949 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1951 bool toggle = false;
1952 if (strcmp(bar_mode, "dock") == 0)
1954 else if (strcmp(bar_mode, "hide") == 0)
1956 else if (strcmp(bar_mode, "invisible") == 0)
1958 else if (strcmp(bar_mode, "toggle") == 0)
1961 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1965 bool changed_sth = false;
1966 Barconfig *current = NULL;
1967 TAILQ_FOREACH(current, &barconfigs, configs) {
1968 if (bar_id && strcmp(current->id, bar_id) != 0)
1972 mode = (current->mode + 1) % 2;
1974 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1975 current->mode = mode;
1982 if (bar_id && !changed_sth) {
1983 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1991 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
1994 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
1995 int hidden_state = S_SHOW;
1996 bool toggle = false;
1997 if (strcmp(bar_hidden_state, "hide") == 0)
1998 hidden_state = S_HIDE;
1999 else if (strcmp(bar_hidden_state, "show") == 0)
2000 hidden_state = S_SHOW;
2001 else if (strcmp(bar_hidden_state, "toggle") == 0)
2004 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2008 bool changed_sth = false;
2009 Barconfig *current = NULL;
2010 TAILQ_FOREACH(current, &barconfigs, configs) {
2011 if (bar_id && strcmp(current->id, bar_id) != 0)
2015 hidden_state = (current->hidden_state + 1) % 2;
2017 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2018 current->hidden_state = hidden_state;
2025 if (bar_id && !changed_sth) {
2026 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2034 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2037 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2039 if (strcmp(bar_type, "mode") == 0)
2040 ret = cmd_bar_mode(bar_value, bar_id);
2041 else if (strcmp(bar_type, "hidden_state") == 0)
2042 ret = cmd_bar_hidden_state(bar_value, bar_id);
2044 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2056 * Implementation of 'shmlog <size>|toggle|on|off'
2059 void cmd_shmlog(I3_CMD, char *argument) {
2060 if (!strcmp(argument, "toggle"))
2061 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2062 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2063 else if (!strcmp(argument, "on"))
2064 shmlog_size = default_shmlog_size;
2065 else if (!strcmp(argument, "off"))
2068 /* If shm logging now, restart logging with the new size. */
2069 if (shmlog_size > 0) {
2071 LOG("Restarting shm logging...\n");
2074 shmlog_size = atoi(argument);
2075 /* Make a weakly attempt at ensuring the argument is valid. */
2076 if (shmlog_size <= 0)
2077 shmlog_size = default_shmlog_size;
2079 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2081 update_shmlog_atom();
2082 // XXX: default reply for now, make this a better reply
2087 * Implementation of 'debuglog toggle|on|off'
2090 void cmd_debuglog(I3_CMD, char *argument) {
2091 bool logging = get_debug_logging();
2092 if (!strcmp(argument, "toggle")) {
2093 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2094 set_debug_logging(!logging);
2095 } else if (!strcmp(argument, "on") && !logging) {
2096 LOG("Enabling debug logging\n");
2097 set_debug_logging(true);
2098 } else if (!strcmp(argument, "off") && logging) {
2099 LOG("Disabling debug logging\n");
2100 set_debug_logging(false);
2102 // XXX: default reply for now, make this a better reply