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, "con_mark") == 0) {
367 current_match->mark = regex_new(cvalue);
371 if (strcmp(ctype, "title") == 0) {
372 current_match->title = regex_new(cvalue);
376 if (strcmp(ctype, "urgent") == 0) {
377 if (strcasecmp(cvalue, "latest") == 0 ||
378 strcasecmp(cvalue, "newest") == 0 ||
379 strcasecmp(cvalue, "recent") == 0 ||
380 strcasecmp(cvalue, "last") == 0) {
381 current_match->urgent = U_LATEST;
382 } else if (strcasecmp(cvalue, "oldest") == 0 ||
383 strcasecmp(cvalue, "first") == 0) {
384 current_match->urgent = U_OLDEST;
389 ELOG("Unknown criterion: %s\n", ctype);
393 * Implementation of 'move [window|container] [to] workspace
394 * next|prev|next_on_output|prev_on_output|current'.
397 void cmd_move_con_to_workspace(I3_CMD, char *which) {
400 DLOG("which=%s\n", which);
402 /* We have nothing to move:
403 * when criteria was specified but didn't match any window or
404 * when criteria wasn't specified and we don't have any window focused. */
405 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
406 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
407 !con_has_children(focused))) {
414 /* get the workspace */
416 if (strcmp(which, "next") == 0)
417 ws = workspace_next();
418 else if (strcmp(which, "prev") == 0)
419 ws = workspace_prev();
420 else if (strcmp(which, "next_on_output") == 0)
421 ws = workspace_next_on_output();
422 else if (strcmp(which, "prev_on_output") == 0)
423 ws = workspace_prev_on_output();
424 else if (strcmp(which, "current") == 0)
425 ws = con_get_workspace(focused);
427 ELOG("BUG: called with which=%s\n", which);
432 TAILQ_FOREACH(current, &owindows, owindows) {
433 DLOG("matching: %p / %s\n", current->con, current->con->name);
434 con_move_to_workspace(current->con, ws, true, false);
437 cmd_output->needs_tree_render = true;
438 // XXX: default reply for now, make this a better reply
443 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
446 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
450 ws = workspace_back_and_forth_get();
453 yerror("No workspace was previously active.");
459 TAILQ_FOREACH(current, &owindows, owindows) {
460 DLOG("matching: %p / %s\n", current->con, current->con->name);
461 con_move_to_workspace(current->con, ws, true, false);
464 cmd_output->needs_tree_render = true;
465 // XXX: default reply for now, make this a better reply
470 * Implementation of 'move [window|container] [to] workspace <name>'.
473 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
474 if (strncasecmp(name, "__", strlen("__")) == 0) {
475 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
482 /* We have nothing to move:
483 * when criteria was specified but didn't match any window or
484 * when criteria wasn't specified and we don't have any window focused. */
485 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
486 ELOG("No windows match your criteria, cannot move.\n");
489 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
490 !con_has_children(focused)) {
495 LOG("should move window to workspace %s\n", name);
496 /* get the workspace */
497 Con *ws = workspace_get(name, NULL);
499 ws = maybe_auto_back_and_forth_workspace(ws);
503 TAILQ_FOREACH(current, &owindows, owindows) {
504 DLOG("matching: %p / %s\n", current->con, current->con->name);
505 con_move_to_workspace(current->con, ws, true, false);
508 cmd_output->needs_tree_render = true;
509 // XXX: default reply for now, make this a better reply
514 * Implementation of 'move [window|container] [to] workspace number <name>'.
517 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
520 /* We have nothing to move:
521 * when criteria was specified but didn't match any window or
522 * when criteria wasn't specified and we don't have any window focused. */
523 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
524 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
525 !con_has_children(focused))) {
530 LOG("should move window to workspace %s\n", which);
531 /* get the workspace */
532 Con *output, *workspace = NULL;
534 long parsed_num = ws_name_to_number(which);
536 if (parsed_num == -1) {
537 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
538 yerror("Could not parse number \"%s\"", which);
542 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
543 GREP_FIRST(workspace, output_get_content(output),
544 child->num == parsed_num);
547 workspace = workspace_get(which, NULL);
550 workspace = maybe_auto_back_and_forth_workspace(workspace);
554 TAILQ_FOREACH(current, &owindows, owindows) {
555 DLOG("matching: %p / %s\n", current->con, current->con->name);
556 con_move_to_workspace(current->con, workspace, true, false);
559 cmd_output->needs_tree_render = true;
560 // XXX: default reply for now, make this a better reply
564 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
565 LOG("floating resize\n");
566 Rect old_rect = floating_con->rect;
567 Con *focused_con = con_descend_focused(floating_con);
569 /* ensure that resize will take place even if pixel increment is smaller than
570 * height increment or width increment.
572 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
573 strcmp(direction, "height") == 0) {
575 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
577 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
578 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
580 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
582 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
585 if (strcmp(direction, "up") == 0) {
586 floating_con->rect.height += px;
587 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
588 floating_con->rect.height += px;
589 } else if (strcmp(direction, "left") == 0) {
590 floating_con->rect.width += px;
592 floating_con->rect.width += px;
595 floating_check_size(floating_con);
597 /* Did we actually resize anything or did the size constraints prevent us?
598 * If we could not resize, exit now to not move the window. */
599 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
602 if (strcmp(direction, "up") == 0) {
603 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
604 } else if (strcmp(direction, "left") == 0) {
605 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
608 /* If this is a scratchpad window, don't auto center it from now on. */
609 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
610 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
613 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
614 LOG("tiling resize\n");
616 Con *first = current;
617 direction_t search_direction;
618 if (!strcmp(direction, "left"))
619 search_direction = D_LEFT;
620 else if (!strcmp(direction, "right"))
621 search_direction = D_RIGHT;
622 else if (!strcmp(direction, "up"))
623 search_direction = D_UP;
625 search_direction = D_DOWN;
627 bool res = resize_find_tiling_participants(&first, &second, search_direction);
629 LOG("No second container in this direction found.\n");
634 /* get the default percentage */
635 int children = con_num_children(first->parent);
636 LOG("ins. %d children\n", children);
637 double percentage = 1.0 / children;
638 LOG("default percentage = %f\n", percentage);
641 LOG("second->percent = %f\n", second->percent);
642 LOG("first->percent before = %f\n", first->percent);
643 if (first->percent == 0.0)
644 first->percent = percentage;
645 if (second->percent == 0.0)
646 second->percent = percentage;
647 double new_first_percent = first->percent + ((double)ppt / 100.0);
648 double new_second_percent = second->percent - ((double)ppt / 100.0);
649 LOG("new_first_percent = %f\n", new_first_percent);
650 LOG("new_second_percent = %f\n", new_second_percent);
651 /* Ensure that the new percentages are positive and greater than
652 * 0.05 to have a reasonable minimum size. */
653 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
654 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
655 first->percent += ((double)ppt / 100.0);
656 second->percent -= ((double)ppt / 100.0);
657 LOG("first->percent after = %f\n", first->percent);
658 LOG("second->percent after = %f\n", second->percent);
660 LOG("Not resizing, already at minimum size\n");
666 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
667 LOG("width/height resize\n");
668 /* get the appropriate current container (skip stacked/tabbed cons) */
669 while (current->parent->layout == L_STACKED ||
670 current->parent->layout == L_TABBED)
671 current = current->parent;
673 /* Then further go up until we find one with the matching orientation. */
674 orientation_t search_orientation =
675 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
677 while (current->type != CT_WORKSPACE &&
678 current->type != CT_FLOATING_CON &&
679 con_orientation(current->parent) != search_orientation)
680 current = current->parent;
682 /* get the default percentage */
683 int children = con_num_children(current->parent);
684 LOG("ins. %d children\n", children);
685 double percentage = 1.0 / children;
686 LOG("default percentage = %f\n", percentage);
688 orientation_t orientation = con_orientation(current->parent);
690 if ((orientation == HORIZ &&
691 strcmp(direction, "height") == 0) ||
692 (orientation == VERT &&
693 strcmp(direction, "width") == 0)) {
694 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
695 (orientation == HORIZ ? "horizontal" : "vertical"));
701 LOG("This is the only container, cannot resize.\n");
706 /* Ensure all the other children have a percentage set. */
708 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
709 LOG("child->percent = %f (child %p)\n", child->percent, child);
710 if (child->percent == 0.0)
711 child->percent = percentage;
714 double new_current_percent = current->percent + ((double)ppt / 100.0);
715 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
716 LOG("new_current_percent = %f\n", new_current_percent);
717 LOG("subtract_percent = %f\n", subtract_percent);
718 /* Ensure that the new percentages are positive and greater than
719 * 0.05 to have a reasonable minimum size. */
720 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
721 if (child == current)
723 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
724 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
729 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
730 LOG("Not resizing, already at minimum size\n");
735 current->percent += ((double)ppt / 100.0);
736 LOG("current->percent after = %f\n", current->percent);
738 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
739 if (child == current)
741 child->percent -= subtract_percent;
742 LOG("child->percent after (%p) = %f\n", child, child->percent);
749 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
752 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
753 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
754 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
755 // 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
756 int px = atoi(resize_px);
757 int ppt = atoi(resize_ppt);
758 if (strcmp(way, "shrink") == 0) {
766 TAILQ_FOREACH(current, &owindows, owindows) {
767 /* Don't handle dock windows (issue #1201) */
768 if (current->con->window && current->con->window->dock) {
769 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
774 if ((floating_con = con_inside_floating(current->con))) {
775 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
777 if (strcmp(direction, "width") == 0 ||
778 strcmp(direction, "height") == 0) {
779 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
782 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
788 cmd_output->needs_tree_render = true;
789 // XXX: default reply for now, make this a better reply
794 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
797 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
798 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
803 TAILQ_FOREACH(current, &owindows, owindows) {
804 DLOG("matching: %p / %s\n", current->con, current->con->name);
805 int border_style = current->con->border_style;
807 int tmp_border_width = -1;
808 tmp_border_width = strtol(border_width, &end, 10);
809 if (end == border_width) {
810 /* no valid digits found */
811 tmp_border_width = -1;
813 if (strcmp(border_style_str, "toggle") == 0) {
816 if (border_style == BS_NORMAL)
817 tmp_border_width = 2;
818 else if (border_style == BS_NONE)
819 tmp_border_width = 0;
820 else if (border_style == BS_PIXEL)
821 tmp_border_width = 1;
823 if (strcmp(border_style_str, "normal") == 0)
824 border_style = BS_NORMAL;
825 else if (strcmp(border_style_str, "pixel") == 0)
826 border_style = BS_PIXEL;
827 else if (strcmp(border_style_str, "1pixel") == 0) {
828 border_style = BS_PIXEL;
829 tmp_border_width = 1;
830 } else if (strcmp(border_style_str, "none") == 0)
831 border_style = BS_NONE;
833 ELOG("BUG: called with border_style=%s\n", border_style_str);
838 con_set_border_style(current->con, border_style, tmp_border_width);
841 cmd_output->needs_tree_render = true;
842 // XXX: default reply for now, make this a better reply
847 * Implementation of 'nop <comment>'.
850 void cmd_nop(I3_CMD, char *comment) {
851 LOG("-------------------------------------------------\n");
852 LOG(" NOP: %s\n", comment);
853 LOG("-------------------------------------------------\n");
857 * Implementation of 'append_layout <path>'.
860 void cmd_append_layout(I3_CMD, char *path) {
861 LOG("Appending layout \"%s\"\n", path);
863 /* Make sure we allow paths like '~/.i3/layout.json' */
864 path = resolve_tilde(path);
866 json_content_t content = json_determine_content(path);
867 LOG("JSON content = %d\n", content);
868 if (content == JSON_CONTENT_UNKNOWN) {
869 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
870 yerror("Could not determine the contents of \"%s\".", path);
875 Con *parent = focused;
876 if (content == JSON_CONTENT_WORKSPACE) {
877 parent = output_get_content(con_get_output(parent));
879 /* We need to append the layout to a split container, since a leaf
880 * container must not have any children (by definition).
881 * Note that we explicitly check for workspaces, since they are okay for
882 * this purpose, but con_accepts_window() returns false for workspaces. */
883 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
884 parent = parent->parent;
886 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
887 char *errormsg = NULL;
888 tree_append_json(parent, path, &errormsg);
889 if (errormsg != NULL) {
892 /* Note that we continue executing since tree_append_json() has
893 * side-effects — user-provided layouts can be partly valid, partly
894 * invalid, leading to half of the placeholder containers being
900 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
901 // false); should be enough, but when sending 'workspace 4; append_layout
902 // /tmp/foo.json', the needs_tree_render == true of the workspace command
903 // is not executed yet and will be batched with append_layout’s
904 // needs_tree_render after the parser finished. We should check if that is
906 render_con(croot, false);
908 restore_open_placeholder_windows(parent);
910 if (content == JSON_CONTENT_WORKSPACE)
911 ipc_send_workspace_event("restored", parent, NULL);
914 cmd_output->needs_tree_render = true;
918 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
921 void cmd_workspace(I3_CMD, char *which) {
924 DLOG("which=%s\n", which);
926 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
927 LOG("Cannot switch workspace while in global fullscreen\n");
932 if (strcmp(which, "next") == 0)
933 ws = workspace_next();
934 else if (strcmp(which, "prev") == 0)
935 ws = workspace_prev();
936 else if (strcmp(which, "next_on_output") == 0)
937 ws = workspace_next_on_output();
938 else if (strcmp(which, "prev_on_output") == 0)
939 ws = workspace_prev_on_output();
941 ELOG("BUG: called with which=%s\n", which);
948 cmd_output->needs_tree_render = true;
949 // XXX: default reply for now, make this a better reply
954 * Implementation of 'workspace number <name>'
957 void cmd_workspace_number(I3_CMD, char *which) {
958 Con *output, *workspace = NULL;
960 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
961 LOG("Cannot switch workspace while in global fullscreen\n");
966 long parsed_num = ws_name_to_number(which);
968 if (parsed_num == -1) {
969 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
970 yerror("Could not parse number \"%s\"", which);
974 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
975 GREP_FIRST(workspace, output_get_content(output),
976 child->num == parsed_num);
979 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
981 workspace_show_by_name(which);
982 cmd_output->needs_tree_render = true;
985 if (maybe_back_and_forth(cmd_output, workspace->name))
987 workspace_show(workspace);
989 cmd_output->needs_tree_render = true;
990 // XXX: default reply for now, make this a better reply
995 * Implementation of 'workspace back_and_forth'.
998 void cmd_workspace_back_and_forth(I3_CMD) {
999 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1000 LOG("Cannot switch workspace while in global fullscreen\n");
1005 workspace_back_and_forth();
1007 cmd_output->needs_tree_render = true;
1008 // XXX: default reply for now, make this a better reply
1013 * Implementation of 'workspace <name>'
1016 void cmd_workspace_name(I3_CMD, char *name) {
1017 if (strncasecmp(name, "__", strlen("__")) == 0) {
1018 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1023 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1024 LOG("Cannot switch workspace while in global fullscreen\n");
1029 DLOG("should switch to workspace %s\n", name);
1030 if (maybe_back_and_forth(cmd_output, name))
1032 workspace_show_by_name(name);
1034 cmd_output->needs_tree_render = true;
1035 // XXX: default reply for now, make this a better reply
1040 * Implementation of 'mark [--toggle] <mark>'
1043 void cmd_mark(I3_CMD, char *mark, char *toggle) {
1047 TAILQ_FOREACH(current, &owindows, owindows) {
1048 DLOG("matching: %p / %s\n", current->con, current->con->name);
1049 if (toggle != NULL && current->con->mark && strcmp(current->con->mark, mark) == 0) {
1050 DLOG("removing window mark %s\n", mark);
1051 FREE(current->con->mark);
1053 DLOG("marking window with str %s\n", mark);
1054 FREE(current->con->mark);
1055 current->con->mark = sstrdup(mark);
1059 DLOG("Clearing all non-matched windows with this mark\n");
1061 TAILQ_FOREACH(con, &all_cons, all_cons) {
1062 /* Skip matched windows, we took care of them already. */
1063 bool matched = false;
1064 TAILQ_FOREACH(current, &owindows, owindows) {
1065 if (current->con == con) {
1073 if (con->mark && strcmp(con->mark, mark) == 0)
1077 cmd_output->needs_tree_render = true;
1078 // XXX: default reply for now, make this a better reply
1083 * Implementation of 'unmark [mark]'
1086 void cmd_unmark(I3_CMD, char *mark) {
1089 TAILQ_FOREACH(con, &all_cons, all_cons) {
1092 DLOG("removed all window marks");
1095 TAILQ_FOREACH(con, &all_cons, all_cons) {
1096 if (con->mark && strcmp(con->mark, mark) == 0)
1099 DLOG("removed window mark %s\n", mark);
1102 cmd_output->needs_tree_render = true;
1103 // XXX: default reply for now, make this a better reply
1108 * Implementation of 'mode <string>'.
1111 void cmd_mode(I3_CMD, char *mode) {
1112 DLOG("mode=%s\n", mode);
1115 // XXX: default reply for now, make this a better reply
1120 * Implementation of 'move [window|container] [to] output <str>'.
1123 void cmd_move_con_to_output(I3_CMD, char *name) {
1126 DLOG("should move window to output %s\n", name);
1130 Output *current_output = NULL;
1131 // TODO: fix the handling of criteria
1132 TAILQ_FOREACH(current, &owindows, owindows)
1133 current_output = get_output_of_con(current->con);
1134 assert(current_output != NULL);
1136 Output *output = get_output_from_string(current_output, name);
1138 LOG("No such output found.\n");
1143 /* get visible workspace on output */
1145 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1151 TAILQ_FOREACH(current, &owindows, owindows) {
1152 DLOG("matching: %p / %s\n", current->con, current->con->name);
1153 con_move_to_workspace(current->con, ws, true, false);
1156 cmd_output->needs_tree_render = true;
1157 // XXX: default reply for now, make this a better reply
1162 * Implementation of 'floating enable|disable|toggle'
1165 void cmd_floating(I3_CMD, char *floating_mode) {
1168 DLOG("floating_mode=%s\n", floating_mode);
1172 TAILQ_FOREACH(current, &owindows, owindows) {
1173 DLOG("matching: %p / %s\n", current->con, current->con->name);
1174 if (strcmp(floating_mode, "toggle") == 0) {
1175 DLOG("should toggle mode\n");
1176 toggle_floating_mode(current->con, false);
1178 DLOG("should switch mode to %s\n", floating_mode);
1179 if (strcmp(floating_mode, "enable") == 0) {
1180 floating_enable(current->con, false);
1182 floating_disable(current->con, false);
1187 cmd_output->needs_tree_render = true;
1188 // XXX: default reply for now, make this a better reply
1193 * Implementation of 'move workspace to [output] <str>'.
1196 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1197 DLOG("should move workspace to output %s\n", name);
1202 TAILQ_FOREACH(current, &owindows, owindows) {
1203 Con *ws = con_get_workspace(current->con);
1204 bool success = workspace_move_to_output(ws, name);
1206 ELOG("Failed to move workspace to output.\n");
1212 cmd_output->needs_tree_render = true;
1213 // XXX: default reply for now, make this a better reply
1218 * Implementation of 'split v|h|vertical|horizontal'.
1221 void cmd_split(I3_CMD, char *direction) {
1223 /* TODO: use matches */
1224 LOG("splitting in direction %c\n", direction[0]);
1225 if (match_is_empty(current_match))
1226 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1228 TAILQ_FOREACH(current, &owindows, owindows) {
1229 DLOG("matching: %p / %s\n", current->con, current->con->name);
1230 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1234 cmd_output->needs_tree_render = true;
1235 // XXX: default reply for now, make this a better reply
1240 * Implementation of 'kill [window|client]'.
1243 void cmd_kill(I3_CMD, char *kill_mode_str) {
1244 if (kill_mode_str == NULL)
1245 kill_mode_str = "window";
1248 DLOG("kill_mode=%s\n", kill_mode_str);
1251 if (strcmp(kill_mode_str, "window") == 0)
1252 kill_mode = KILL_WINDOW;
1253 else if (strcmp(kill_mode_str, "client") == 0)
1254 kill_mode = KILL_CLIENT;
1256 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1261 /* check if the match is empty, not if the result is empty */
1262 if (match_is_empty(current_match))
1263 tree_close_con(kill_mode);
1265 TAILQ_FOREACH(current, &owindows, owindows) {
1266 DLOG("matching: %p / %s\n", current->con, current->con->name);
1267 tree_close(current->con, kill_mode, false, false);
1271 cmd_output->needs_tree_render = true;
1272 // XXX: default reply for now, make this a better reply
1277 * Implementation of 'exec [--no-startup-id] <command>'.
1280 void cmd_exec(I3_CMD, char *nosn, char *command) {
1281 bool no_startup_id = (nosn != NULL);
1283 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1284 start_application(command, no_startup_id);
1286 // XXX: default reply for now, make this a better reply
1291 * Implementation of 'focus left|right|up|down'.
1294 void cmd_focus_direction(I3_CMD, char *direction) {
1295 DLOG("direction = *%s*\n", direction);
1297 if (strcmp(direction, "left") == 0)
1298 tree_next('p', HORIZ);
1299 else if (strcmp(direction, "right") == 0)
1300 tree_next('n', HORIZ);
1301 else if (strcmp(direction, "up") == 0)
1302 tree_next('p', VERT);
1303 else if (strcmp(direction, "down") == 0)
1304 tree_next('n', VERT);
1306 ELOG("Invalid focus direction (%s)\n", direction);
1311 cmd_output->needs_tree_render = true;
1312 // XXX: default reply for now, make this a better reply
1317 * Implementation of 'focus tiling|floating|mode_toggle'.
1320 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1321 DLOG("window_mode = %s\n", window_mode);
1323 Con *ws = con_get_workspace(focused);
1326 if (strcmp(window_mode, "mode_toggle") == 0) {
1327 current = TAILQ_FIRST(&(ws->focus_head));
1328 if (current != NULL && current->type == CT_FLOATING_CON)
1329 window_mode = "tiling";
1331 window_mode = "floating";
1333 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1334 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1335 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1338 con_focus(con_descend_focused(current));
1343 cmd_output->needs_tree_render = true;
1344 // XXX: default reply for now, make this a better reply
1349 * Implementation of 'focus parent|child'.
1352 void cmd_focus_level(I3_CMD, char *level) {
1353 DLOG("level = %s\n", level);
1354 bool success = false;
1356 /* Focusing the parent can only be allowed if the newly
1357 * focused container won't escape the fullscreen container. */
1358 if (strcmp(level, "parent") == 0) {
1359 if (focused && focused->parent) {
1360 if (con_fullscreen_permits_focusing(focused->parent))
1361 success = level_up();
1363 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1367 /* Focusing a child should always be allowed. */
1369 success = level_down();
1371 cmd_output->needs_tree_render = success;
1372 // XXX: default reply for now, make this a better reply
1377 * Implementation of 'focus'.
1380 void cmd_focus(I3_CMD) {
1381 DLOG("current_match = %p\n", current_match);
1383 if (match_is_empty(current_match)) {
1384 ELOG("You have to specify which window/container should be focused.\n");
1385 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1387 yerror("You have to specify which window/container should be focused");
1392 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1395 TAILQ_FOREACH(current, &owindows, owindows) {
1396 Con *ws = con_get_workspace(current->con);
1397 /* If no workspace could be found, this was a dock window.
1398 * Just skip it, you cannot focus dock windows. */
1402 /* Check the fullscreen focus constraints. */
1403 if (!con_fullscreen_permits_focusing(current->con)) {
1404 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1409 /* In case this is a scratchpad window, call scratchpad_show(). */
1410 if (ws == __i3_scratch) {
1411 scratchpad_show(current->con);
1413 /* While for the normal focus case we can change focus multiple
1414 * times and only a single window ends up focused, we could show
1415 * multiple scratchpad windows. So, rather break here. */
1419 /* If the container is not on the current workspace,
1420 * workspace_show() will switch to a different workspace and (if
1421 * enabled) trigger a mouse pointer warp to the currently focused
1422 * container (!) on the target workspace.
1424 * Therefore, before calling workspace_show(), we make sure that
1425 * 'current' will be focused on the workspace. However, we cannot
1426 * just con_focus(current) because then the pointer will not be
1427 * warped at all (the code thinks we are already there).
1429 * So we focus 'current' to make it the currently focused window of
1430 * the target workspace, then revert focus. */
1431 Con *currently_focused = focused;
1432 con_focus(current->con);
1433 con_focus(currently_focused);
1435 /* Now switch to the workspace, then focus */
1437 LOG("focusing %p / %s\n", current->con, current->con->name);
1438 con_focus(current->con);
1443 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1444 "while only exactly one container can be focused at a time.\n",
1447 cmd_output->needs_tree_render = true;
1448 // XXX: default reply for now, make this a better reply
1453 * Implementation of 'fullscreen enable|toggle [global]' and
1454 * 'fullscreen disable'
1457 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1458 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1459 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1464 TAILQ_FOREACH(current, &owindows, owindows) {
1465 DLOG("matching: %p / %s\n", current->con, current->con->name);
1466 if (strcmp(action, "toggle") == 0) {
1467 con_toggle_fullscreen(current->con, mode);
1468 } else if (strcmp(action, "enable") == 0) {
1469 con_enable_fullscreen(current->con, mode);
1470 } else if (strcmp(action, "disable") == 0) {
1471 con_disable_fullscreen(current->con);
1475 cmd_output->needs_tree_render = true;
1476 // XXX: default reply for now, make this a better reply
1481 * Implementation of 'move <direction> [<pixels> [px]]'.
1484 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1485 // 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
1486 int px = atoi(move_px);
1491 Con *initially_focused = focused;
1493 TAILQ_FOREACH(current, &owindows, owindows) {
1494 DLOG("moving in direction %s, px %s\n", direction, move_px);
1495 if (con_is_floating(current->con)) {
1496 DLOG("floating move with %d pixels\n", px);
1497 Rect newrect = current->con->parent->rect;
1498 if (strcmp(direction, "left") == 0) {
1500 } else if (strcmp(direction, "right") == 0) {
1502 } else if (strcmp(direction, "up") == 0) {
1504 } else if (strcmp(direction, "down") == 0) {
1507 floating_reposition(current->con->parent, newrect);
1509 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1510 cmd_output->needs_tree_render = true;
1514 /* the move command should not disturb focus */
1515 if (focused != initially_focused)
1516 con_focus(initially_focused);
1518 // XXX: default reply for now, make this a better reply
1523 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1526 void cmd_layout(I3_CMD, char *layout_str) {
1527 if (strcmp(layout_str, "stacking") == 0)
1528 layout_str = "stacked";
1531 /* default is a special case which will be handled in con_set_layout(). */
1532 if (strcmp(layout_str, "default") == 0)
1534 else if (strcmp(layout_str, "stacked") == 0)
1536 else if (strcmp(layout_str, "tabbed") == 0)
1538 else if (strcmp(layout_str, "splitv") == 0)
1540 else if (strcmp(layout_str, "splith") == 0)
1543 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1547 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1549 /* check if the match is empty, not if the result is empty */
1550 if (match_is_empty(current_match))
1551 con_set_layout(focused, layout);
1553 TAILQ_FOREACH(current, &owindows, owindows) {
1554 DLOG("matching: %p / %s\n", current->con, current->con->name);
1555 con_set_layout(current->con, layout);
1559 cmd_output->needs_tree_render = true;
1560 // XXX: default reply for now, make this a better reply
1565 * Implementation of 'layout toggle [all|split]'.
1568 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1571 if (toggle_mode == NULL)
1572 toggle_mode = "default";
1574 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1576 /* check if the match is empty, not if the result is empty */
1577 if (match_is_empty(current_match))
1578 con_toggle_layout(focused, toggle_mode);
1580 TAILQ_FOREACH(current, &owindows, owindows) {
1581 DLOG("matching: %p / %s\n", current->con, current->con->name);
1582 con_toggle_layout(current->con, toggle_mode);
1586 cmd_output->needs_tree_render = true;
1587 // XXX: default reply for now, make this a better reply
1592 * Implementation of 'exit'.
1595 void cmd_exit(I3_CMD) {
1596 LOG("Exiting due to user command.\n");
1598 unlink(config.ipc_socket_path);
1599 xcb_disconnect(conn);
1606 * Implementation of 'reload'.
1609 void cmd_reload(I3_CMD) {
1611 kill_nagbar(&config_error_nagbar_pid, false);
1612 kill_nagbar(&command_error_nagbar_pid, false);
1613 load_configuration(conn, NULL, true);
1615 /* Send an IPC event just in case the ws names have changed */
1616 ipc_send_workspace_event("reload", NULL, NULL);
1617 /* Send an update event for the barconfig just in case it has changed */
1620 // XXX: default reply for now, make this a better reply
1625 * Implementation of 'restart'.
1628 void cmd_restart(I3_CMD) {
1629 LOG("restarting i3\n");
1631 unlink(config.ipc_socket_path);
1632 /* We need to call this manually since atexit handlers don’t get called
1634 purge_zerobyte_logfile();
1637 // XXX: default reply for now, make this a better reply
1642 * Implementation of 'open'.
1645 void cmd_open(I3_CMD) {
1646 LOG("opening new container\n");
1647 Con *con = tree_open_con(NULL, NULL);
1648 con->layout = L_SPLITH;
1655 y(integer, (long int)con);
1658 cmd_output->needs_tree_render = true;
1662 * Implementation of 'focus output <output>'.
1665 void cmd_focus_output(I3_CMD, char *name) {
1668 DLOG("name = %s\n", name);
1672 /* get the output */
1673 Output *current_output = NULL;
1676 TAILQ_FOREACH(current, &owindows, owindows)
1677 current_output = get_output_of_con(current->con);
1678 assert(current_output != NULL);
1680 output = get_output_from_string(current_output, name);
1683 LOG("No such output found.\n");
1688 /* get visible workspace on output */
1690 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1698 cmd_output->needs_tree_render = true;
1699 // XXX: default reply for now, make this a better reply
1704 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1707 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1710 bool has_error = false;
1715 TAILQ_FOREACH(current, &owindows, owindows) {
1716 if (!con_is_floating(current->con)) {
1717 ELOG("Cannot change position. The window/container is not floating\n");
1720 yerror("Cannot change position of a window/container because it is not floating.");
1727 if (strcmp(method, "absolute") == 0) {
1728 current->con->parent->rect.x = x;
1729 current->con->parent->rect.y = y;
1731 DLOG("moving to absolute position %d %d\n", x, y);
1732 floating_maybe_reassign_ws(current->con->parent);
1733 cmd_output->needs_tree_render = true;
1736 if (strcmp(method, "position") == 0) {
1737 Rect newrect = current->con->parent->rect;
1739 DLOG("moving to position %d %d\n", x, y);
1743 floating_reposition(current->con->parent, newrect);
1747 // XXX: default reply for now, make this a better reply
1753 * Implementation of 'move [window|container] [to] [absolute] position center
1756 void cmd_move_window_to_center(I3_CMD, char *method) {
1757 if (!con_is_floating(focused)) {
1758 ELOG("Cannot change position. The window/container is not floating\n");
1759 yerror("Cannot change position. The window/container is not floating.");
1763 if (strcmp(method, "absolute") == 0) {
1764 DLOG("moving to absolute center\n");
1765 floating_center(focused->parent, croot->rect);
1767 floating_maybe_reassign_ws(focused->parent);
1768 cmd_output->needs_tree_render = true;
1771 if (strcmp(method, "position") == 0) {
1772 DLOG("moving to center\n");
1773 floating_center(focused->parent, con_get_workspace(focused)->rect);
1775 cmd_output->needs_tree_render = true;
1778 // XXX: default reply for now, make this a better reply
1783 * Implementation of 'move scratchpad'.
1786 void cmd_move_scratchpad(I3_CMD) {
1787 DLOG("should move window to scratchpad\n");
1792 TAILQ_FOREACH(current, &owindows, owindows) {
1793 DLOG("matching: %p / %s\n", current->con, current->con->name);
1794 scratchpad_move(current->con);
1797 cmd_output->needs_tree_render = true;
1798 // XXX: default reply for now, make this a better reply
1803 * Implementation of 'scratchpad show'.
1806 void cmd_scratchpad_show(I3_CMD) {
1807 DLOG("should show scratchpad window\n");
1810 if (match_is_empty(current_match)) {
1811 scratchpad_show(NULL);
1813 TAILQ_FOREACH(current, &owindows, owindows) {
1814 DLOG("matching: %p / %s\n", current->con, current->con->name);
1815 scratchpad_show(current->con);
1819 cmd_output->needs_tree_render = true;
1820 // XXX: default reply for now, make this a better reply
1825 * Implementation of 'rename workspace [<name>] to <name>'
1828 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1829 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1830 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1835 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1837 LOG("Renaming current workspace to \"%s\"\n", new_name);
1840 Con *output, *workspace = NULL;
1842 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1843 GREP_FIRST(workspace, output_get_content(output),
1844 !strcasecmp(child->name, old_name));
1846 workspace = con_get_workspace(focused);
1847 old_name = workspace->name;
1851 yerror("Old workspace \"%s\" not found", old_name);
1855 Con *check_dest = NULL;
1856 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1857 GREP_FIRST(check_dest, output_get_content(output),
1858 !strcasecmp(child->name, new_name));
1860 if (check_dest != NULL) {
1861 yerror("New workspace \"%s\" already exists", new_name);
1865 /* Change the name and try to parse it as a number. */
1866 FREE(workspace->name);
1867 workspace->name = sstrdup(new_name);
1869 workspace->num = ws_name_to_number(new_name);
1870 LOG("num = %d\n", workspace->num);
1872 /* By re-attaching, the sort order will be correct afterwards. */
1873 Con *previously_focused = focused;
1874 Con *parent = workspace->parent;
1875 con_detach(workspace);
1876 con_attach(workspace, parent, false);
1878 /* Move the workspace to the correct output if it has an assignment */
1879 struct Workspace_Assignment *assignment = NULL;
1880 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1881 if (assignment->output == NULL)
1883 if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1887 workspace_move_to_output(workspace, assignment->output);
1889 if (previously_focused)
1890 workspace_show(con_get_workspace(previously_focused));
1895 /* Restore the previous focus since con_attach messes with the focus. */
1896 con_focus(previously_focused);
1898 cmd_output->needs_tree_render = true;
1901 ipc_send_workspace_event("rename", workspace, NULL);
1902 ewmh_update_desktop_names();
1903 ewmh_update_desktop_viewport();
1904 ewmh_update_current_desktop();
1906 startup_sequence_rename_workspace(old_name, new_name);
1910 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1913 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1915 bool toggle = false;
1916 if (strcmp(bar_mode, "dock") == 0)
1918 else if (strcmp(bar_mode, "hide") == 0)
1920 else if (strcmp(bar_mode, "invisible") == 0)
1922 else if (strcmp(bar_mode, "toggle") == 0)
1925 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1929 bool changed_sth = false;
1930 Barconfig *current = NULL;
1931 TAILQ_FOREACH(current, &barconfigs, configs) {
1932 if (bar_id && strcmp(current->id, bar_id) != 0)
1936 mode = (current->mode + 1) % 2;
1938 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1939 current->mode = mode;
1946 if (bar_id && !changed_sth) {
1947 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1955 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
1958 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
1959 int hidden_state = S_SHOW;
1960 bool toggle = false;
1961 if (strcmp(bar_hidden_state, "hide") == 0)
1962 hidden_state = S_HIDE;
1963 else if (strcmp(bar_hidden_state, "show") == 0)
1964 hidden_state = S_SHOW;
1965 else if (strcmp(bar_hidden_state, "toggle") == 0)
1968 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
1972 bool changed_sth = false;
1973 Barconfig *current = NULL;
1974 TAILQ_FOREACH(current, &barconfigs, configs) {
1975 if (bar_id && strcmp(current->id, bar_id) != 0)
1979 hidden_state = (current->hidden_state + 1) % 2;
1981 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
1982 current->hidden_state = hidden_state;
1989 if (bar_id && !changed_sth) {
1990 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
1998 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2001 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2003 if (strcmp(bar_type, "mode") == 0)
2004 ret = cmd_bar_mode(bar_value, bar_id);
2005 else if (strcmp(bar_type, "hidden_state") == 0)
2006 ret = cmd_bar_hidden_state(bar_value, bar_id);
2008 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2020 * Implementation of 'shmlog <size>|toggle|on|off'
2023 void cmd_shmlog(I3_CMD, char *argument) {
2024 if (!strcmp(argument, "toggle"))
2025 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2026 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2027 else if (!strcmp(argument, "on"))
2028 shmlog_size = default_shmlog_size;
2029 else if (!strcmp(argument, "off"))
2032 /* If shm logging now, restart logging with the new size. */
2033 if (shmlog_size > 0) {
2035 LOG("Restarting shm logging...\n");
2038 shmlog_size = atoi(argument);
2039 /* Make a weakly attempt at ensuring the argument is valid. */
2040 if (shmlog_size <= 0)
2041 shmlog_size = default_shmlog_size;
2043 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2045 update_shmlog_atom();
2046 // XXX: default reply for now, make this a better reply
2051 * Implementation of 'debuglog toggle|on|off'
2054 void cmd_debuglog(I3_CMD, char *argument) {
2055 bool logging = get_debug_logging();
2056 if (!strcmp(argument, "toggle")) {
2057 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2058 set_debug_logging(!logging);
2059 } else if (!strcmp(argument, "on") && !logging) {
2060 LOG("Enabling debug logging\n");
2061 set_debug_logging(true);
2062 } else if (!strcmp(argument, "off") && logging) {
2063 LOG("Disabling debug logging\n");
2064 set_debug_logging(false);
2066 // XXX: default reply for now, make this a better reply