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(message) \
32 if (cmd_output->json_gen != NULL) { \
42 /** When the command did not include match criteria (!), we use the currently
43 * focused container. Do not confuse this case with a command which included
44 * criteria but which did not match any windows. This macro has to be called in
47 #define HANDLE_EMPTY_MATCH \
49 if (match_is_empty(current_match)) { \
50 owindow *ow = smalloc(sizeof(owindow)); \
52 TAILQ_INIT(&owindows); \
53 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
58 * Returns true if a is definitely greater than b (using the given epsilon)
61 static bool definitelyGreaterThan(float a, float b, float epsilon) {
62 return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
66 * Returns an 'output' corresponding to one of left/right/down/up or a specific
70 static Output *get_output_from_string(Output *current_output, const char *output_str) {
73 if (strcasecmp(output_str, "left") == 0)
74 output = get_output_next_wrap(D_LEFT, current_output);
75 else if (strcasecmp(output_str, "right") == 0)
76 output = get_output_next_wrap(D_RIGHT, current_output);
77 else if (strcasecmp(output_str, "up") == 0)
78 output = get_output_next_wrap(D_UP, current_output);
79 else if (strcasecmp(output_str, "down") == 0)
80 output = get_output_next_wrap(D_DOWN, current_output);
82 output = get_output_by_name(output_str);
88 * Returns the output containing the given container.
90 static Output *get_output_of_con(Con *con) {
91 Con *output_con = con_get_output(con);
92 Output *output = get_output_by_name(output_con->name);
93 assert(output != NULL);
99 * Checks whether we switched to a new workspace and returns false in that case,
100 * signaling that further workspace switching should be done by the calling function
101 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
102 * and return true, signaling that no further workspace switching should occur in the calling function.
105 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
106 Con *ws = con_get_workspace(focused);
108 /* If we switched to a different workspace, do nothing */
109 if (strcmp(ws->name, name) != 0)
112 DLOG("This workspace is already focused.\n");
113 if (config.workspace_auto_back_and_forth) {
114 workspace_back_and_forth();
115 cmd_output->needs_tree_render = true;
121 * Return the passed workspace unless it is the current one and auto back and
122 * forth is enabled, in which case the back_and_forth workspace is returned.
124 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
127 if (!config.workspace_auto_back_and_forth)
130 current = con_get_workspace(focused);
132 if (current == workspace) {
133 baf = workspace_back_and_forth_get();
135 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
143 // This code is commented out because we might recycle it for popping up error
144 // messages on parser errors.
146 static pid_t migration_pid = -1;
149 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
150 * it exited (or could not be started, depending on the exit code).
153 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
154 ev_child_stop(EV_A_ watcher);
155 if (!WIFEXITED(watcher->rstatus)) {
156 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
160 int exitcode = WEXITSTATUS(watcher->rstatus);
161 printf("i3-nagbar process exited with status %d\n", exitcode);
163 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
169 /* We need ev >= 4 for the following code. Since it is not *that* important (it
170 * only makes sure that there are no i3-nagbar instances left behind) we still
171 * support old systems with libev 3. */
172 #if EV_VERSION_MAJOR >= 4
174 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
175 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
178 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
179 if (migration_pid != -1) {
180 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
181 kill(migration_pid, SIGKILL);
186 void cmd_MIGRATION_start_nagbar(void) {
187 if (migration_pid != -1) {
188 fprintf(stderr, "i3-nagbar already running.\n");
191 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
192 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
193 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
194 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
195 migration_pid = fork();
196 if (migration_pid == -1) {
197 warn("Could not fork()");
202 if (migration_pid == 0) {
204 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
206 NULL, /* will be replaced by the executable path */
210 "You found a parsing error. Please, please, please, report it!",
216 exec_i3_utility("i3-nagbar", argv);
220 /* install a child watcher */
221 ev_child *child = smalloc(sizeof(ev_child));
222 ev_child_init(child, &nagbar_exited, migration_pid, 0);
223 ev_child_start(main_loop, child);
225 /* We need ev >= 4 for the following code. Since it is not *that* important (it
226 * only makes sure that there are no i3-nagbar instances left behind) we still
227 * support old systems with libev 3. */
228 #if EV_VERSION_MAJOR >= 4
229 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
231 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
232 ev_cleanup_init(cleanup, nagbar_cleanup);
233 ev_cleanup_start(main_loop, cleanup);
239 /*******************************************************************************
240 * Criteria functions.
241 ******************************************************************************/
244 * Helper data structure for an operation window (window on which the operation
245 * will be performed). Used to build the TAILQ owindows.
248 typedef struct owindow {
250 TAILQ_ENTRY(owindow) owindows;
253 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
255 static owindows_head owindows;
258 * Initializes the specified 'Match' data structure and the initial state of
259 * commands.c for matching target windows of a command.
262 void cmd_criteria_init(I3_CMD) {
266 DLOG("Initializing criteria, current_match = %p\n", current_match);
267 match_free(current_match);
268 match_init(current_match);
269 while (!TAILQ_EMPTY(&owindows)) {
270 ow = TAILQ_FIRST(&owindows);
271 TAILQ_REMOVE(&owindows, ow, owindows);
274 TAILQ_INIT(&owindows);
276 TAILQ_FOREACH (con, &all_cons, all_cons) {
277 ow = smalloc(sizeof(owindow));
279 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
284 * A match specification just finished (the closing square bracket was found),
285 * so we filter the list of owindows.
288 void cmd_criteria_match_windows(I3_CMD) {
289 owindow *next, *current;
291 DLOG("match specification finished, matching...\n");
292 /* copy the old list head to iterate through it and start with a fresh
293 * list which will contain only matching windows */
294 struct owindows_head old = owindows;
295 TAILQ_INIT(&owindows);
296 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
297 /* make a copy of the next pointer and advance the pointer to the
298 * next element as we are going to invalidate the element’s
299 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
301 next = TAILQ_NEXT(next, owindows);
303 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
304 if (current_match->con_id != NULL) {
305 if (current_match->con_id == current->con) {
306 DLOG("matches container!\n");
307 TAILQ_INSERT_TAIL(&owindows, current, owindows);
309 DLOG("doesnt match\n");
312 } else if (current_match->mark != NULL && current->con->mark != NULL &&
313 regex_matches(current_match->mark, current->con->mark)) {
314 DLOG("match by mark\n");
315 TAILQ_INSERT_TAIL(&owindows, current, owindows);
317 if (current->con->window && match_matches_window(current_match, current->con->window)) {
318 DLOG("matches window!\n");
319 TAILQ_INSERT_TAIL(&owindows, current, owindows);
321 DLOG("doesnt match\n");
327 TAILQ_FOREACH (current, &owindows, owindows) {
328 DLOG("matching: %p / %s\n", current->con, current->con->name);
333 * Interprets a ctype=cvalue pair and adds it to the current match
337 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
338 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
340 if (strcmp(ctype, "class") == 0) {
341 current_match->class = regex_new(cvalue);
345 if (strcmp(ctype, "instance") == 0) {
346 current_match->instance = regex_new(cvalue);
350 if (strcmp(ctype, "window_role") == 0) {
351 current_match->window_role = regex_new(cvalue);
355 if (strcmp(ctype, "con_id") == 0) {
357 long parsed = strtol(cvalue, &end, 10);
358 if (parsed == LONG_MIN ||
359 parsed == LONG_MAX ||
361 (end && *end != '\0')) {
362 ELOG("Could not parse con id \"%s\"\n", cvalue);
364 current_match->con_id = (Con *)parsed;
365 DLOG("id as int = %p\n", current_match->con_id);
370 if (strcmp(ctype, "id") == 0) {
372 long parsed = strtol(cvalue, &end, 10);
373 if (parsed == LONG_MIN ||
374 parsed == LONG_MAX ||
376 (end && *end != '\0')) {
377 ELOG("Could not parse window id \"%s\"\n", cvalue);
379 current_match->id = parsed;
380 DLOG("window id as int = %d\n", current_match->id);
385 if (strcmp(ctype, "con_mark") == 0) {
386 current_match->mark = regex_new(cvalue);
390 if (strcmp(ctype, "title") == 0) {
391 current_match->title = regex_new(cvalue);
395 if (strcmp(ctype, "urgent") == 0) {
396 if (strcasecmp(cvalue, "latest") == 0 ||
397 strcasecmp(cvalue, "newest") == 0 ||
398 strcasecmp(cvalue, "recent") == 0 ||
399 strcasecmp(cvalue, "last") == 0) {
400 current_match->urgent = U_LATEST;
401 } else if (strcasecmp(cvalue, "oldest") == 0 ||
402 strcasecmp(cvalue, "first") == 0) {
403 current_match->urgent = U_OLDEST;
408 ELOG("Unknown criterion: %s\n", ctype);
412 * Implementation of 'move [window|container] [to] workspace
413 * next|prev|next_on_output|prev_on_output|current'.
416 void cmd_move_con_to_workspace(I3_CMD, char *which) {
419 DLOG("which=%s\n", which);
421 /* We have nothing to move:
422 * when criteria was specified but didn't match any window or
423 * when criteria wasn't specified and we don't have any window focused. */
424 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
425 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
426 !con_has_children(focused))) {
433 /* get the workspace */
435 if (strcmp(which, "next") == 0)
436 ws = workspace_next();
437 else if (strcmp(which, "prev") == 0)
438 ws = workspace_prev();
439 else if (strcmp(which, "next_on_output") == 0)
440 ws = workspace_next_on_output();
441 else if (strcmp(which, "prev_on_output") == 0)
442 ws = workspace_prev_on_output();
443 else if (strcmp(which, "current") == 0)
444 ws = con_get_workspace(focused);
446 ELOG("BUG: called with which=%s\n", which);
451 TAILQ_FOREACH (current, &owindows, owindows) {
452 DLOG("matching: %p / %s\n", current->con, current->con->name);
453 con_move_to_workspace(current->con, ws, true, false);
456 cmd_output->needs_tree_render = true;
457 // XXX: default reply for now, make this a better reply
462 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
465 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
469 ws = workspace_back_and_forth_get();
472 yerror("No workspace was previously active.");
478 TAILQ_FOREACH (current, &owindows, owindows) {
479 DLOG("matching: %p / %s\n", current->con, current->con->name);
480 con_move_to_workspace(current->con, ws, true, false);
483 cmd_output->needs_tree_render = true;
484 // XXX: default reply for now, make this a better reply
489 * Implementation of 'move [window|container] [to] workspace <name>'.
492 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
493 if (strncasecmp(name, "__", strlen("__")) == 0) {
494 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
501 /* We have nothing to move:
502 * when criteria was specified but didn't match any window or
503 * when criteria wasn't specified and we don't have any window focused. */
504 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
505 ELOG("No windows match your criteria, cannot move.\n");
508 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
509 !con_has_children(focused)) {
514 LOG("should move window to workspace %s\n", name);
515 /* get the workspace */
516 Con *ws = workspace_get(name, NULL);
518 ws = maybe_auto_back_and_forth_workspace(ws);
522 TAILQ_FOREACH (current, &owindows, owindows) {
523 DLOG("matching: %p / %s\n", current->con, current->con->name);
524 con_move_to_workspace(current->con, ws, true, false);
527 cmd_output->needs_tree_render = true;
528 // XXX: default reply for now, make this a better reply
533 * Implementation of 'move [window|container] [to] workspace number <name>'.
536 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
539 /* We have nothing to move:
540 * when criteria was specified but didn't match any window or
541 * when criteria wasn't specified and we don't have any window focused. */
542 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
543 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
544 !con_has_children(focused))) {
549 LOG("should move window to workspace %s\n", which);
550 /* get the workspace */
551 Con *output, *workspace = NULL;
554 long parsed_num = strtol(which, &endptr, 10);
555 if (parsed_num == LONG_MIN ||
556 parsed_num == LONG_MAX ||
559 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
560 // TODO: better error message
561 yerror("Could not parse number");
565 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
566 GREP_FIRST(workspace, output_get_content(output),
567 child->num == parsed_num);
570 workspace = workspace_get(which, NULL);
573 workspace = maybe_auto_back_and_forth_workspace(workspace);
577 TAILQ_FOREACH (current, &owindows, owindows) {
578 DLOG("matching: %p / %s\n", current->con, current->con->name);
579 con_move_to_workspace(current->con, workspace, true, false);
582 cmd_output->needs_tree_render = true;
583 // XXX: default reply for now, make this a better reply
587 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
588 LOG("floating resize\n");
589 Rect old_rect = floating_con->rect;
590 Con *focused_con = con_descend_focused(floating_con);
592 /* ensure that resize will take place even if pixel increment is smaller than
593 * height increment or width increment.
595 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
596 strcmp(direction, "height") == 0) {
598 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
600 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
601 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
603 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
605 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
608 if (strcmp(direction, "up") == 0) {
609 floating_con->rect.height += px;
610 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
611 floating_con->rect.height += px;
612 } else if (strcmp(direction, "left") == 0) {
613 floating_con->rect.width += px;
615 floating_con->rect.width += px;
618 floating_check_size(floating_con);
620 /* Did we actually resize anything or did the size constraints prevent us?
621 * If we could not resize, exit now to not move the window. */
622 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
625 if (strcmp(direction, "up") == 0) {
626 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
627 } else if (strcmp(direction, "left") == 0) {
628 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
631 /* If this is a scratchpad window, don't auto center it from now on. */
632 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
633 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
636 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
637 LOG("tiling resize\n");
639 Con *first = current;
640 direction_t search_direction;
641 if (!strcmp(direction, "left"))
642 search_direction = D_LEFT;
643 else if (!strcmp(direction, "right"))
644 search_direction = D_RIGHT;
645 else if (!strcmp(direction, "up"))
646 search_direction = D_UP;
648 search_direction = D_DOWN;
650 bool res = resize_find_tiling_participants(&first, &second, search_direction);
652 LOG("No second container in this direction found.\n");
657 /* get the default percentage */
658 int children = con_num_children(first->parent);
659 LOG("ins. %d children\n", children);
660 double percentage = 1.0 / children;
661 LOG("default percentage = %f\n", percentage);
664 LOG("second->percent = %f\n", second->percent);
665 LOG("first->percent before = %f\n", first->percent);
666 if (first->percent == 0.0)
667 first->percent = percentage;
668 if (second->percent == 0.0)
669 second->percent = percentage;
670 double new_first_percent = first->percent + ((double)ppt / 100.0);
671 double new_second_percent = second->percent - ((double)ppt / 100.0);
672 LOG("new_first_percent = %f\n", new_first_percent);
673 LOG("new_second_percent = %f\n", new_second_percent);
674 /* Ensure that the new percentages are positive and greater than
675 * 0.05 to have a reasonable minimum size. */
676 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
677 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
678 first->percent += ((double)ppt / 100.0);
679 second->percent -= ((double)ppt / 100.0);
680 LOG("first->percent after = %f\n", first->percent);
681 LOG("second->percent after = %f\n", second->percent);
683 LOG("Not resizing, already at minimum size\n");
689 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
690 LOG("width/height resize\n");
691 /* get the appropriate current container (skip stacked/tabbed cons) */
692 while (current->parent->layout == L_STACKED ||
693 current->parent->layout == L_TABBED)
694 current = current->parent;
696 /* Then further go up until we find one with the matching orientation. */
697 orientation_t search_orientation =
698 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
700 while (current->type != CT_WORKSPACE &&
701 current->type != CT_FLOATING_CON &&
702 con_orientation(current->parent) != search_orientation)
703 current = current->parent;
705 /* get the default percentage */
706 int children = con_num_children(current->parent);
707 LOG("ins. %d children\n", children);
708 double percentage = 1.0 / children;
709 LOG("default percentage = %f\n", percentage);
711 orientation_t orientation = con_orientation(current->parent);
713 if ((orientation == HORIZ &&
714 strcmp(direction, "height") == 0) ||
715 (orientation == VERT &&
716 strcmp(direction, "width") == 0)) {
717 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
718 (orientation == HORIZ ? "horizontal" : "vertical"));
724 LOG("This is the only container, cannot resize.\n");
729 /* Ensure all the other children have a percentage set. */
731 TAILQ_FOREACH (child, &(current->parent->nodes_head), nodes) {
732 LOG("child->percent = %f (child %p)\n", child->percent, child);
733 if (child->percent == 0.0)
734 child->percent = percentage;
737 double new_current_percent = current->percent + ((double)ppt / 100.0);
738 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
739 LOG("new_current_percent = %f\n", new_current_percent);
740 LOG("subtract_percent = %f\n", subtract_percent);
741 /* Ensure that the new percentages are positive and greater than
742 * 0.05 to have a reasonable minimum size. */
743 TAILQ_FOREACH (child, &(current->parent->nodes_head), nodes) {
744 if (child == current)
746 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
747 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
752 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
753 LOG("Not resizing, already at minimum size\n");
758 current->percent += ((double)ppt / 100.0);
759 LOG("current->percent after = %f\n", current->percent);
761 TAILQ_FOREACH (child, &(current->parent->nodes_head), nodes) {
762 if (child == current)
764 child->percent -= subtract_percent;
765 LOG("child->percent after (%p) = %f\n", child, child->percent);
772 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
775 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
776 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
777 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
778 // 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
779 int px = atoi(resize_px);
780 int ppt = atoi(resize_ppt);
781 if (strcmp(way, "shrink") == 0) {
789 TAILQ_FOREACH (current, &owindows, owindows) {
790 /* Don't handle dock windows (issue #1201) */
791 if (current->con->window && current->con->window->dock) {
792 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
797 if ((floating_con = con_inside_floating(current->con))) {
798 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
800 if (strcmp(direction, "width") == 0 ||
801 strcmp(direction, "height") == 0) {
802 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
805 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
811 cmd_output->needs_tree_render = true;
812 // XXX: default reply for now, make this a better reply
817 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
820 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
821 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
826 TAILQ_FOREACH (current, &owindows, owindows) {
827 DLOG("matching: %p / %s\n", current->con, current->con->name);
828 int border_style = current->con->border_style;
830 int tmp_border_width = -1;
831 tmp_border_width = strtol(border_width, &end, 10);
832 if (end == border_width) {
833 /* no valid digits found */
834 tmp_border_width = -1;
836 if (strcmp(border_style_str, "toggle") == 0) {
839 if (border_style == BS_NORMAL)
840 tmp_border_width = 2;
841 else if (border_style == BS_NONE)
842 tmp_border_width = 0;
843 else if (border_style == BS_PIXEL)
844 tmp_border_width = 1;
846 if (strcmp(border_style_str, "normal") == 0)
847 border_style = BS_NORMAL;
848 else if (strcmp(border_style_str, "pixel") == 0)
849 border_style = BS_PIXEL;
850 else if (strcmp(border_style_str, "1pixel") == 0) {
851 border_style = BS_PIXEL;
852 tmp_border_width = 1;
853 } else if (strcmp(border_style_str, "none") == 0)
854 border_style = BS_NONE;
856 ELOG("BUG: called with border_style=%s\n", border_style_str);
861 con_set_border_style(current->con, border_style, tmp_border_width);
864 cmd_output->needs_tree_render = true;
865 // XXX: default reply for now, make this a better reply
870 * Implementation of 'nop <comment>'.
873 void cmd_nop(I3_CMD, char *comment) {
874 LOG("-------------------------------------------------\n");
875 LOG(" NOP: %s\n", comment);
876 LOG("-------------------------------------------------\n");
880 * Implementation of 'append_layout <path>'.
883 void cmd_append_layout(I3_CMD, char *path) {
884 LOG("Appending layout \"%s\"\n", path);
886 json_content_t content = json_determine_content(path);
887 LOG("JSON content = %d\n", content);
888 if (content == JSON_CONTENT_UNKNOWN) {
889 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
894 Con *parent = focused;
895 if (content == JSON_CONTENT_WORKSPACE) {
896 parent = output_get_content(con_get_output(parent));
898 /* We need to append the layout to a split container, since a leaf
899 * container must not have any children (by definition).
900 * Note that we explicitly check for workspaces, since they are okay for
901 * this purpose, but con_accepts_window() returns false for workspaces. */
902 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
903 parent = parent->parent;
905 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
906 char *errormsg = NULL;
907 tree_append_json(parent, path, &errormsg);
908 if (errormsg != NULL) {
911 /* Note that we continue executing since tree_append_json() has
912 * side-effects — user-provided layouts can be partly valid, partly
913 * invalid, leading to half of the placeholder containers being
919 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
920 // false); should be enough, but when sending 'workspace 4; append_layout
921 // /tmp/foo.json', the needs_tree_render == true of the workspace command
922 // is not executed yet and will be batched with append_layout’s
923 // needs_tree_render after the parser finished. We should check if that is
925 render_con(croot, false);
927 restore_open_placeholder_windows(parent);
929 if (content == JSON_CONTENT_WORKSPACE)
930 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"restored\"}");
932 cmd_output->needs_tree_render = true;
936 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
939 void cmd_workspace(I3_CMD, char *which) {
942 DLOG("which=%s\n", which);
944 if (strcmp(which, "next") == 0)
945 ws = workspace_next();
946 else if (strcmp(which, "prev") == 0)
947 ws = workspace_prev();
948 else if (strcmp(which, "next_on_output") == 0)
949 ws = workspace_next_on_output();
950 else if (strcmp(which, "prev_on_output") == 0)
951 ws = workspace_prev_on_output();
953 ELOG("BUG: called with which=%s\n", which);
960 cmd_output->needs_tree_render = true;
961 // XXX: default reply for now, make this a better reply
966 * Implementation of 'workspace number <name>'
969 void cmd_workspace_number(I3_CMD, char *which) {
970 Con *output, *workspace = NULL;
973 long parsed_num = strtol(which, &endptr, 10);
974 if (parsed_num == LONG_MIN ||
975 parsed_num == LONG_MAX ||
978 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
979 // TODO: better error message
980 yerror("Could not parse number");
985 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
986 GREP_FIRST(workspace, output_get_content(output),
987 child->num == parsed_num);
990 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
992 workspace_show_by_name(which);
993 cmd_output->needs_tree_render = true;
996 if (maybe_back_and_forth(cmd_output, workspace->name))
998 workspace_show(workspace);
1000 cmd_output->needs_tree_render = true;
1001 // XXX: default reply for now, make this a better reply
1006 * Implementation of 'workspace back_and_forth'.
1009 void cmd_workspace_back_and_forth(I3_CMD) {
1010 workspace_back_and_forth();
1012 cmd_output->needs_tree_render = true;
1013 // XXX: default reply for now, make this a better reply
1018 * Implementation of 'workspace <name>'
1021 void cmd_workspace_name(I3_CMD, char *name) {
1022 if (strncasecmp(name, "__", strlen("__")) == 0) {
1023 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1028 DLOG("should switch to workspace %s\n", name);
1029 if (maybe_back_and_forth(cmd_output, name))
1031 workspace_show_by_name(name);
1033 cmd_output->needs_tree_render = true;
1034 // XXX: default reply for now, make this a better reply
1039 * Implementation of 'mark <mark>'
1042 void cmd_mark(I3_CMD, char *mark) {
1043 DLOG("Clearing all windows which have that mark first\n");
1046 TAILQ_FOREACH (con, &all_cons, all_cons) {
1047 if (con->mark && strcmp(con->mark, mark) == 0)
1051 DLOG("marking window with str %s\n", mark);
1056 TAILQ_FOREACH (current, &owindows, owindows) {
1057 DLOG("matching: %p / %s\n", current->con, current->con->name);
1058 current->con->mark = sstrdup(mark);
1061 cmd_output->needs_tree_render = true;
1062 // XXX: default reply for now, make this a better reply
1067 * Implementation of 'unmark [mark]'
1070 void cmd_unmark(I3_CMD, char *mark) {
1073 TAILQ_FOREACH (con, &all_cons, all_cons) {
1076 DLOG("removed all window marks");
1079 TAILQ_FOREACH (con, &all_cons, all_cons) {
1080 if (con->mark && strcmp(con->mark, mark) == 0)
1083 DLOG("removed window mark %s\n", mark);
1086 cmd_output->needs_tree_render = true;
1087 // XXX: default reply for now, make this a better reply
1092 * Implementation of 'mode <string>'.
1095 void cmd_mode(I3_CMD, char *mode) {
1096 DLOG("mode=%s\n", mode);
1099 // XXX: default reply for now, make this a better reply
1104 * Implementation of 'move [window|container] [to] output <str>'.
1107 void cmd_move_con_to_output(I3_CMD, char *name) {
1110 DLOG("should move window to output %s\n", name);
1114 /* get the output */
1115 Output *current_output = NULL;
1118 // TODO: fix the handling of criteria
1119 TAILQ_FOREACH (current, &owindows, owindows)
1120 current_output = get_output_of_con(current->con);
1122 assert(current_output != NULL);
1124 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1125 if (strcasecmp(name, "up") == 0)
1126 output = get_output_next_wrap(D_UP, current_output);
1127 else if (strcasecmp(name, "down") == 0)
1128 output = get_output_next_wrap(D_DOWN, current_output);
1129 else if (strcasecmp(name, "left") == 0)
1130 output = get_output_next_wrap(D_LEFT, current_output);
1131 else if (strcasecmp(name, "right") == 0)
1132 output = get_output_next_wrap(D_RIGHT, current_output);
1134 output = get_output_by_name(name);
1137 LOG("No such output found.\n");
1142 /* get visible workspace on output */
1144 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1150 TAILQ_FOREACH (current, &owindows, owindows) {
1151 DLOG("matching: %p / %s\n", current->con, current->con->name);
1152 con_move_to_workspace(current->con, ws, true, false);
1155 cmd_output->needs_tree_render = true;
1156 // XXX: default reply for now, make this a better reply
1161 * Implementation of 'floating enable|disable|toggle'
1164 void cmd_floating(I3_CMD, char *floating_mode) {
1167 DLOG("floating_mode=%s\n", floating_mode);
1171 TAILQ_FOREACH (current, &owindows, owindows) {
1172 DLOG("matching: %p / %s\n", current->con, current->con->name);
1173 if (strcmp(floating_mode, "toggle") == 0) {
1174 DLOG("should toggle mode\n");
1175 toggle_floating_mode(current->con, false);
1177 DLOG("should switch mode to %s\n", floating_mode);
1178 if (strcmp(floating_mode, "enable") == 0) {
1179 floating_enable(current->con, false);
1181 floating_disable(current->con, false);
1186 cmd_output->needs_tree_render = true;
1187 // XXX: default reply for now, make this a better reply
1192 * Implementation of 'move workspace to [output] <str>'.
1195 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1196 DLOG("should move workspace to output %s\n", name);
1201 TAILQ_FOREACH (current, &owindows, owindows) {
1202 Output *current_output = get_output_of_con(current->con);
1203 if (!current_output) {
1204 ELOG("Cannot get current output. This is a bug in i3.\n");
1208 Output *output = get_output_from_string(current_output, name);
1210 ELOG("Could not get output from string \"%s\"\n", name);
1215 Con *content = output_get_content(output->con);
1216 LOG("got output %p with content %p\n", output, content);
1218 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1219 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1221 Con *ws = con_get_workspace(current->con);
1222 LOG("should move workspace %p / %s\n", ws, ws->name);
1223 bool workspace_was_visible = workspace_is_visible(ws);
1225 if (con_num_children(ws->parent) == 1) {
1226 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1228 /* check if we can find a workspace assigned to this output */
1229 bool used_assignment = false;
1230 struct Workspace_Assignment *assignment;
1231 TAILQ_FOREACH (assignment, &ws_assignments, ws_assignments) {
1232 if (strcmp(assignment->output, current_output->name) != 0)
1235 /* check if this workspace is already attached to the tree */
1236 Con *workspace = NULL, *out;
1237 TAILQ_FOREACH (out, &(croot->nodes_head), nodes)
1238 GREP_FIRST(workspace, output_get_content(out),
1239 !strcasecmp(child->name, assignment->name));
1240 if (workspace != NULL)
1243 /* so create the workspace referenced to by this assignment */
1244 LOG("Creating workspace from assignment %s.\n", assignment->name);
1245 workspace_get(assignment->name, NULL);
1246 used_assignment = true;
1250 /* if we couldn't create the workspace using an assignment, create
1251 * it on the output */
1252 if (!used_assignment)
1253 create_workspace_on_output(current_output, ws->parent);
1255 /* notify the IPC listeners */
1256 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1258 DLOG("Detaching\n");
1260 /* detach from the old output and attach to the new output */
1261 Con *old_content = ws->parent;
1263 if (workspace_was_visible) {
1264 /* The workspace which we just detached was visible, so focus
1265 * the next one in the focus-stack. */
1266 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1267 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1268 workspace_show(focus_ws);
1270 con_attach(ws, content, false);
1272 /* fix the coordinates of the floating containers */
1274 TAILQ_FOREACH (floating_con, &(ws->floating_head), floating_windows)
1275 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1277 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1278 if (workspace_was_visible) {
1279 /* Focus the moved workspace on the destination output. */
1283 /* NB: We cannot simply work with previously_visible_ws since it might
1284 * have been cleaned up by workspace_show() already, depending on the
1285 * focus order/number of other workspaces on the output.
1286 * Instead, we loop through the available workspaces and only work with
1287 * previously_visible_ws if we still find it. */
1288 TAILQ_FOREACH (ws, &(content->nodes_head), nodes) {
1289 if (ws != previously_visible_ws)
1292 /* Call the on_remove_child callback of the workspace which previously
1293 * was visible on the destination output. Since it is no longer
1294 * visible, it might need to get cleaned up. */
1295 CALL(previously_visible_ws, on_remove_child);
1300 cmd_output->needs_tree_render = true;
1301 // XXX: default reply for now, make this a better reply
1306 * Implementation of 'split v|h|vertical|horizontal'.
1309 void cmd_split(I3_CMD, char *direction) {
1311 /* TODO: use matches */
1312 LOG("splitting in direction %c\n", direction[0]);
1313 if (match_is_empty(current_match))
1314 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1316 TAILQ_FOREACH (current, &owindows, owindows) {
1317 DLOG("matching: %p / %s\n", current->con, current->con->name);
1318 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1322 cmd_output->needs_tree_render = true;
1323 // XXX: default reply for now, make this a better reply
1328 * Implementation of 'kill [window|client]'.
1331 void cmd_kill(I3_CMD, char *kill_mode_str) {
1332 if (kill_mode_str == NULL)
1333 kill_mode_str = "window";
1336 DLOG("kill_mode=%s\n", kill_mode_str);
1339 if (strcmp(kill_mode_str, "window") == 0)
1340 kill_mode = KILL_WINDOW;
1341 else if (strcmp(kill_mode_str, "client") == 0)
1342 kill_mode = KILL_CLIENT;
1344 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1349 /* check if the match is empty, not if the result is empty */
1350 if (match_is_empty(current_match))
1351 tree_close_con(kill_mode);
1353 TAILQ_FOREACH (current, &owindows, owindows) {
1354 DLOG("matching: %p / %s\n", current->con, current->con->name);
1355 tree_close(current->con, kill_mode, false, false);
1359 cmd_output->needs_tree_render = true;
1360 // XXX: default reply for now, make this a better reply
1365 * Implementation of 'exec [--no-startup-id] <command>'.
1368 void cmd_exec(I3_CMD, char *nosn, char *command) {
1369 bool no_startup_id = (nosn != NULL);
1371 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1372 start_application(command, no_startup_id);
1374 // XXX: default reply for now, make this a better reply
1379 * Implementation of 'focus left|right|up|down'.
1382 void cmd_focus_direction(I3_CMD, char *direction) {
1383 DLOG("direction = *%s*\n", direction);
1385 if (strcmp(direction, "left") == 0)
1386 tree_next('p', HORIZ);
1387 else if (strcmp(direction, "right") == 0)
1388 tree_next('n', HORIZ);
1389 else if (strcmp(direction, "up") == 0)
1390 tree_next('p', VERT);
1391 else if (strcmp(direction, "down") == 0)
1392 tree_next('n', VERT);
1394 ELOG("Invalid focus direction (%s)\n", direction);
1399 cmd_output->needs_tree_render = true;
1400 // XXX: default reply for now, make this a better reply
1405 * Implementation of 'focus tiling|floating|mode_toggle'.
1408 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1409 DLOG("window_mode = %s\n", window_mode);
1411 Con *ws = con_get_workspace(focused);
1414 if (strcmp(window_mode, "mode_toggle") == 0) {
1415 current = TAILQ_FIRST(&(ws->focus_head));
1416 if (current != NULL && current->type == CT_FLOATING_CON)
1417 window_mode = "tiling";
1419 window_mode = "floating";
1421 TAILQ_FOREACH (current, &(ws->focus_head), focused) {
1422 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1423 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1426 con_focus(con_descend_focused(current));
1431 cmd_output->needs_tree_render = true;
1432 // XXX: default reply for now, make this a better reply
1437 * Implementation of 'focus parent|child'.
1440 void cmd_focus_level(I3_CMD, char *level) {
1441 DLOG("level = %s\n", level);
1442 bool success = false;
1444 /* Focusing the parent can only be allowed if the newly
1445 * focused container won't escape the fullscreen container. */
1446 if (strcmp(level, "parent") == 0) {
1447 if (focused && focused->parent) {
1448 if (con_fullscreen_permits_focusing(focused->parent))
1449 success = level_up();
1451 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1455 /* Focusing a child should always be allowed. */
1457 success = level_down();
1459 cmd_output->needs_tree_render = success;
1460 // XXX: default reply for now, make this a better reply
1465 * Implementation of 'focus'.
1468 void cmd_focus(I3_CMD) {
1469 DLOG("current_match = %p\n", current_match);
1471 if (match_is_empty(current_match)) {
1472 ELOG("You have to specify which window/container should be focused.\n");
1473 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1475 yerror("You have to specify which window/container should be focused");
1480 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1483 TAILQ_FOREACH (current, &owindows, owindows) {
1484 Con *ws = con_get_workspace(current->con);
1485 /* If no workspace could be found, this was a dock window.
1486 * Just skip it, you cannot focus dock windows. */
1490 /* Check the fullscreen focus constraints. */
1491 if (!con_fullscreen_permits_focusing(current->con)) {
1492 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1497 /* In case this is a scratchpad window, call scratchpad_show(). */
1498 if (ws == __i3_scratch) {
1499 scratchpad_show(current->con);
1501 /* While for the normal focus case we can change focus multiple
1502 * times and only a single window ends up focused, we could show
1503 * multiple scratchpad windows. So, rather break here. */
1507 /* If the container is not on the current workspace,
1508 * workspace_show() will switch to a different workspace and (if
1509 * enabled) trigger a mouse pointer warp to the currently focused
1510 * container (!) on the target workspace.
1512 * Therefore, before calling workspace_show(), we make sure that
1513 * 'current' will be focused on the workspace. However, we cannot
1514 * just con_focus(current) because then the pointer will not be
1515 * warped at all (the code thinks we are already there).
1517 * So we focus 'current' to make it the currently focused window of
1518 * the target workspace, then revert focus. */
1519 Con *currently_focused = focused;
1520 con_focus(current->con);
1521 con_focus(currently_focused);
1523 /* Now switch to the workspace, then focus */
1525 LOG("focusing %p / %s\n", current->con, current->con->name);
1526 con_focus(current->con);
1531 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1532 "while only exactly one container can be focused at a time.\n",
1535 cmd_output->needs_tree_render = true;
1536 // XXX: default reply for now, make this a better reply
1541 * Implementation of 'fullscreen [global]'.
1544 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1545 if (fullscreen_mode == NULL)
1546 fullscreen_mode = "output";
1547 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1552 TAILQ_FOREACH (current, &owindows, owindows) {
1553 DLOG("matching: %p / %s\n", current->con, current->con->name);
1554 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1557 cmd_output->needs_tree_render = true;
1558 // XXX: default reply for now, make this a better reply
1563 * Implementation of 'move <direction> [<pixels> [px]]'.
1566 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1567 // 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
1568 int px = atoi(move_px);
1570 /* TODO: make 'move' work with criteria. */
1571 DLOG("moving in direction %s, px %s\n", direction, move_px);
1572 if (con_is_floating(focused)) {
1573 DLOG("floating move with %d pixels\n", px);
1574 Rect newrect = focused->parent->rect;
1575 if (strcmp(direction, "left") == 0) {
1577 } else if (strcmp(direction, "right") == 0) {
1579 } else if (strcmp(direction, "up") == 0) {
1581 } else if (strcmp(direction, "down") == 0) {
1584 floating_reposition(focused->parent, newrect);
1586 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1587 cmd_output->needs_tree_render = true;
1590 // XXX: default reply for now, make this a better reply
1595 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1598 void cmd_layout(I3_CMD, char *layout_str) {
1599 if (strcmp(layout_str, "stacking") == 0)
1600 layout_str = "stacked";
1603 /* default is a special case which will be handled in con_set_layout(). */
1604 if (strcmp(layout_str, "default") == 0)
1606 else if (strcmp(layout_str, "stacked") == 0)
1608 else if (strcmp(layout_str, "tabbed") == 0)
1610 else if (strcmp(layout_str, "splitv") == 0)
1612 else if (strcmp(layout_str, "splith") == 0)
1615 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1619 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1621 /* check if the match is empty, not if the result is empty */
1622 if (match_is_empty(current_match))
1623 con_set_layout(focused, layout);
1625 TAILQ_FOREACH (current, &owindows, owindows) {
1626 DLOG("matching: %p / %s\n", current->con, current->con->name);
1627 con_set_layout(current->con, layout);
1631 cmd_output->needs_tree_render = true;
1632 // XXX: default reply for now, make this a better reply
1637 * Implementation of 'layout toggle [all|split]'.
1640 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1643 if (toggle_mode == NULL)
1644 toggle_mode = "default";
1646 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1648 /* check if the match is empty, not if the result is empty */
1649 if (match_is_empty(current_match))
1650 con_toggle_layout(focused, toggle_mode);
1652 TAILQ_FOREACH (current, &owindows, owindows) {
1653 DLOG("matching: %p / %s\n", current->con, current->con->name);
1654 con_toggle_layout(current->con, toggle_mode);
1658 cmd_output->needs_tree_render = true;
1659 // XXX: default reply for now, make this a better reply
1664 * Implementation of 'exit'.
1667 void cmd_exit(I3_CMD) {
1668 LOG("Exiting due to user command.\n");
1670 unlink(config.ipc_socket_path);
1671 xcb_disconnect(conn);
1678 * Implementation of 'reload'.
1681 void cmd_reload(I3_CMD) {
1683 kill_nagbar(&config_error_nagbar_pid, false);
1684 kill_nagbar(&command_error_nagbar_pid, false);
1685 load_configuration(conn, NULL, true);
1687 /* Send an IPC event just in case the ws names have changed */
1688 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1689 /* Send an update event for the barconfig just in case it has changed */
1692 // XXX: default reply for now, make this a better reply
1697 * Implementation of 'restart'.
1700 void cmd_restart(I3_CMD) {
1701 LOG("restarting i3\n");
1703 unlink(config.ipc_socket_path);
1704 /* We need to call this manually since atexit handlers don’t get called
1706 purge_zerobyte_logfile();
1709 // XXX: default reply for now, make this a better reply
1714 * Implementation of 'open'.
1717 void cmd_open(I3_CMD) {
1718 LOG("opening new container\n");
1719 Con *con = tree_open_con(NULL, NULL);
1720 con->layout = L_SPLITH;
1727 y(integer, (long int)con);
1730 cmd_output->needs_tree_render = true;
1734 * Implementation of 'focus output <output>'.
1737 void cmd_focus_output(I3_CMD, char *name) {
1740 DLOG("name = %s\n", name);
1744 /* get the output */
1745 Output *current_output = NULL;
1748 TAILQ_FOREACH (current, &owindows, owindows)
1749 current_output = get_output_of_con(current->con);
1750 assert(current_output != NULL);
1752 output = get_output_from_string(current_output, name);
1755 LOG("No such output found.\n");
1760 /* get visible workspace on output */
1762 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1770 cmd_output->needs_tree_render = true;
1771 // XXX: default reply for now, make this a better reply
1776 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1779 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1783 if (!con_is_floating(focused)) {
1784 ELOG("Cannot change position. The window/container is not floating\n");
1785 yerror("Cannot change position. The window/container is not floating.");
1789 if (strcmp(method, "absolute") == 0) {
1790 focused->parent->rect.x = x;
1791 focused->parent->rect.y = y;
1793 DLOG("moving to absolute position %d %d\n", x, y);
1794 floating_maybe_reassign_ws(focused->parent);
1795 cmd_output->needs_tree_render = true;
1798 if (strcmp(method, "position") == 0) {
1799 Rect newrect = focused->parent->rect;
1801 DLOG("moving to position %d %d\n", x, y);
1805 floating_reposition(focused->parent, newrect);
1808 // XXX: default reply for now, make this a better reply
1813 * Implementation of 'move [window|container] [to] [absolute] position center
1816 void cmd_move_window_to_center(I3_CMD, char *method) {
1817 if (!con_is_floating(focused)) {
1818 ELOG("Cannot change position. The window/container is not floating\n");
1819 yerror("Cannot change position. The window/container is not floating.");
1823 if (strcmp(method, "absolute") == 0) {
1824 Rect *rect = &focused->parent->rect;
1826 DLOG("moving to absolute center\n");
1827 rect->x = croot->rect.width / 2 - rect->width / 2;
1828 rect->y = croot->rect.height / 2 - rect->height / 2;
1830 floating_maybe_reassign_ws(focused->parent);
1831 cmd_output->needs_tree_render = true;
1834 if (strcmp(method, "position") == 0) {
1835 Rect *wsrect = &con_get_workspace(focused)->rect;
1836 Rect newrect = focused->parent->rect;
1838 DLOG("moving to center\n");
1839 newrect.x = wsrect->width / 2 - newrect.width / 2;
1840 newrect.y = wsrect->height / 2 - newrect.height / 2;
1842 floating_reposition(focused->parent, newrect);
1845 // XXX: default reply for now, make this a better reply
1850 * Implementation of 'move scratchpad'.
1853 void cmd_move_scratchpad(I3_CMD) {
1854 DLOG("should move window to scratchpad\n");
1859 TAILQ_FOREACH (current, &owindows, owindows) {
1860 DLOG("matching: %p / %s\n", current->con, current->con->name);
1861 scratchpad_move(current->con);
1864 cmd_output->needs_tree_render = true;
1865 // XXX: default reply for now, make this a better reply
1870 * Implementation of 'scratchpad show'.
1873 void cmd_scratchpad_show(I3_CMD) {
1874 DLOG("should show scratchpad window\n");
1877 if (match_is_empty(current_match)) {
1878 scratchpad_show(NULL);
1880 TAILQ_FOREACH (current, &owindows, owindows) {
1881 DLOG("matching: %p / %s\n", current->con, current->con->name);
1882 scratchpad_show(current->con);
1886 cmd_output->needs_tree_render = true;
1887 // XXX: default reply for now, make this a better reply
1892 * Implementation of 'rename workspace [<name>] to <name>'
1895 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1896 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1897 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1902 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1904 LOG("Renaming current workspace to \"%s\"\n", new_name);
1907 Con *output, *workspace = NULL;
1909 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
1910 GREP_FIRST(workspace, output_get_content(output),
1911 !strcasecmp(child->name, old_name));
1913 workspace = con_get_workspace(focused);
1917 // TODO: we should include the old workspace name here and use yajl for
1918 // generating the reply.
1919 // TODO: better error message
1920 yerror("Old workspace not found");
1924 Con *check_dest = NULL;
1925 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
1926 GREP_FIRST(check_dest, output_get_content(output),
1927 !strcasecmp(child->name, new_name));
1929 if (check_dest != NULL) {
1930 // TODO: we should include the new workspace name here and use yajl for
1931 // generating the reply.
1932 // TODO: better error message
1933 yerror("New workspace already exists");
1937 /* Change the name and try to parse it as a number. */
1938 FREE(workspace->name);
1939 workspace->name = sstrdup(new_name);
1940 char *endptr = NULL;
1941 long parsed_num = strtol(new_name, &endptr, 10);
1942 if (parsed_num == LONG_MIN ||
1943 parsed_num == LONG_MAX ||
1946 workspace->num = -1;
1948 workspace->num = parsed_num;
1949 LOG("num = %d\n", workspace->num);
1951 /* By re-attaching, the sort order will be correct afterwards. */
1952 Con *previously_focused = focused;
1953 Con *parent = workspace->parent;
1954 con_detach(workspace);
1955 con_attach(workspace, parent, false);
1956 /* Restore the previous focus since con_attach messes with the focus. */
1957 con_focus(previously_focused);
1959 cmd_output->needs_tree_render = true;
1962 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1966 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1969 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1971 bool toggle = false;
1972 if (strcmp(bar_mode, "dock") == 0)
1974 else if (strcmp(bar_mode, "hide") == 0)
1976 else if (strcmp(bar_mode, "invisible") == 0)
1978 else if (strcmp(bar_mode, "toggle") == 0)
1981 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1985 bool changed_sth = false;
1986 Barconfig *current = NULL;
1987 TAILQ_FOREACH (current, &barconfigs, configs) {
1988 if (bar_id && strcmp(current->id, bar_id) != 0)
1992 mode = (current->mode + 1) % 2;
1994 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1995 current->mode = mode;
2002 if (bar_id && !changed_sth) {
2003 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2011 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2014 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2015 int hidden_state = S_SHOW;
2016 bool toggle = false;
2017 if (strcmp(bar_hidden_state, "hide") == 0)
2018 hidden_state = S_HIDE;
2019 else if (strcmp(bar_hidden_state, "show") == 0)
2020 hidden_state = S_SHOW;
2021 else if (strcmp(bar_hidden_state, "toggle") == 0)
2024 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2028 bool changed_sth = false;
2029 Barconfig *current = NULL;
2030 TAILQ_FOREACH (current, &barconfigs, configs) {
2031 if (bar_id && strcmp(current->id, bar_id) != 0)
2035 hidden_state = (current->hidden_state + 1) % 2;
2037 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2038 current->hidden_state = hidden_state;
2045 if (bar_id && !changed_sth) {
2046 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2054 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2057 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2059 if (strcmp(bar_type, "mode") == 0)
2060 ret = cmd_bar_mode(bar_value, bar_id);
2061 else if (strcmp(bar_type, "hidden_state") == 0)
2062 ret = cmd_bar_hidden_state(bar_value, bar_id);
2064 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2076 * Implementation of 'shmlog <size>|toggle|on|off'
2079 void cmd_shmlog(I3_CMD, char *argument) {
2080 if (!strcmp(argument, "toggle"))
2081 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2082 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2083 else if (!strcmp(argument, "on"))
2084 shmlog_size = default_shmlog_size;
2085 else if (!strcmp(argument, "off"))
2088 /* If shm logging now, restart logging with the new size. */
2089 if (shmlog_size > 0) {
2091 LOG("Restarting shm logging...\n");
2094 shmlog_size = atoi(argument);
2095 /* Make a weakly attempt at ensuring the argument is valid. */
2096 if (shmlog_size <= 0)
2097 shmlog_size = default_shmlog_size;
2099 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2101 update_shmlog_atom();
2102 // XXX: default reply for now, make this a better reply
2107 * Implementation of 'debuglog toggle|on|off'
2110 void cmd_debuglog(I3_CMD, char *argument) {
2111 bool logging = get_debug_logging();
2112 if (!strcmp(argument, "toggle")) {
2113 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2114 set_debug_logging(!logging);
2115 } else if (!strcmp(argument, "on") && !logging) {
2116 LOG("Enabling debug logging\n");
2117 set_debug_logging(true);
2118 } else if (!strcmp(argument, "off") && logging) {
2119 LOG("Disabling debug logging\n");
2120 set_debug_logging(false);
2122 // XXX: default reply for now, make this a better reply