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 */
519 /* first look for a workspace with this name */
520 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
521 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
524 /* if the name is plain digits, we interpret this as a "workspace number"
526 if (!ws && name_is_digits(name)) {
527 long parsed_num = ws_name_to_number(name);
528 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
529 GREP_FIRST(ws, output_get_content(output),
530 child->num == parsed_num);
534 /* if no workspace was found, make a new one */
536 ws = workspace_get(name, NULL);
538 ws = maybe_auto_back_and_forth_workspace(ws);
542 TAILQ_FOREACH(current, &owindows, owindows) {
543 DLOG("matching: %p / %s\n", current->con, current->con->name);
544 con_move_to_workspace(current->con, ws, true, false);
547 cmd_output->needs_tree_render = true;
548 // XXX: default reply for now, make this a better reply
553 * Implementation of 'move [window|container] [to] workspace number <name>'.
556 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
559 /* We have nothing to move:
560 * when criteria was specified but didn't match any window or
561 * when criteria wasn't specified and we don't have any window focused. */
562 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
563 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
564 !con_has_children(focused))) {
569 LOG("should move window to workspace %s\n", which);
570 /* get the workspace */
571 Con *output, *workspace = NULL;
573 long parsed_num = ws_name_to_number(which);
575 if (parsed_num == -1) {
576 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
577 // TODO: better error message
578 yerror("Could not parse number");
582 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
583 GREP_FIRST(workspace, output_get_content(output),
584 child->num == parsed_num);
587 workspace = workspace_get(which, NULL);
590 workspace = maybe_auto_back_and_forth_workspace(workspace);
594 TAILQ_FOREACH(current, &owindows, owindows) {
595 DLOG("matching: %p / %s\n", current->con, current->con->name);
596 con_move_to_workspace(current->con, workspace, true, false);
599 cmd_output->needs_tree_render = true;
600 // XXX: default reply for now, make this a better reply
604 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
605 LOG("floating resize\n");
606 Rect old_rect = floating_con->rect;
607 Con *focused_con = con_descend_focused(floating_con);
609 /* ensure that resize will take place even if pixel increment is smaller than
610 * height increment or width increment.
612 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
613 strcmp(direction, "height") == 0) {
615 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
617 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
618 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
620 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
622 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
625 if (strcmp(direction, "up") == 0) {
626 floating_con->rect.height += px;
627 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
628 floating_con->rect.height += px;
629 } else if (strcmp(direction, "left") == 0) {
630 floating_con->rect.width += px;
632 floating_con->rect.width += px;
635 floating_check_size(floating_con);
637 /* Did we actually resize anything or did the size constraints prevent us?
638 * If we could not resize, exit now to not move the window. */
639 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
642 if (strcmp(direction, "up") == 0) {
643 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
644 } else if (strcmp(direction, "left") == 0) {
645 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
648 /* If this is a scratchpad window, don't auto center it from now on. */
649 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
650 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
653 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
654 LOG("tiling resize\n");
656 Con *first = current;
657 direction_t search_direction;
658 if (!strcmp(direction, "left"))
659 search_direction = D_LEFT;
660 else if (!strcmp(direction, "right"))
661 search_direction = D_RIGHT;
662 else if (!strcmp(direction, "up"))
663 search_direction = D_UP;
665 search_direction = D_DOWN;
667 bool res = resize_find_tiling_participants(&first, &second, search_direction);
669 LOG("No second container in this direction found.\n");
674 /* get the default percentage */
675 int children = con_num_children(first->parent);
676 LOG("ins. %d children\n", children);
677 double percentage = 1.0 / children;
678 LOG("default percentage = %f\n", percentage);
681 LOG("second->percent = %f\n", second->percent);
682 LOG("first->percent before = %f\n", first->percent);
683 if (first->percent == 0.0)
684 first->percent = percentage;
685 if (second->percent == 0.0)
686 second->percent = percentage;
687 double new_first_percent = first->percent + ((double)ppt / 100.0);
688 double new_second_percent = second->percent - ((double)ppt / 100.0);
689 LOG("new_first_percent = %f\n", new_first_percent);
690 LOG("new_second_percent = %f\n", new_second_percent);
691 /* Ensure that the new percentages are positive and greater than
692 * 0.05 to have a reasonable minimum size. */
693 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
694 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
695 first->percent += ((double)ppt / 100.0);
696 second->percent -= ((double)ppt / 100.0);
697 LOG("first->percent after = %f\n", first->percent);
698 LOG("second->percent after = %f\n", second->percent);
700 LOG("Not resizing, already at minimum size\n");
706 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
707 LOG("width/height resize\n");
708 /* get the appropriate current container (skip stacked/tabbed cons) */
709 while (current->parent->layout == L_STACKED ||
710 current->parent->layout == L_TABBED)
711 current = current->parent;
713 /* Then further go up until we find one with the matching orientation. */
714 orientation_t search_orientation =
715 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
717 while (current->type != CT_WORKSPACE &&
718 current->type != CT_FLOATING_CON &&
719 con_orientation(current->parent) != search_orientation)
720 current = current->parent;
722 /* get the default percentage */
723 int children = con_num_children(current->parent);
724 LOG("ins. %d children\n", children);
725 double percentage = 1.0 / children;
726 LOG("default percentage = %f\n", percentage);
728 orientation_t orientation = con_orientation(current->parent);
730 if ((orientation == HORIZ &&
731 strcmp(direction, "height") == 0) ||
732 (orientation == VERT &&
733 strcmp(direction, "width") == 0)) {
734 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
735 (orientation == HORIZ ? "horizontal" : "vertical"));
741 LOG("This is the only container, cannot resize.\n");
746 /* Ensure all the other children have a percentage set. */
748 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
749 LOG("child->percent = %f (child %p)\n", child->percent, child);
750 if (child->percent == 0.0)
751 child->percent = percentage;
754 double new_current_percent = current->percent + ((double)ppt / 100.0);
755 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
756 LOG("new_current_percent = %f\n", new_current_percent);
757 LOG("subtract_percent = %f\n", subtract_percent);
758 /* Ensure that the new percentages are positive and greater than
759 * 0.05 to have a reasonable minimum size. */
760 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
761 if (child == current)
763 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
764 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
769 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
770 LOG("Not resizing, already at minimum size\n");
775 current->percent += ((double)ppt / 100.0);
776 LOG("current->percent after = %f\n", current->percent);
778 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
779 if (child == current)
781 child->percent -= subtract_percent;
782 LOG("child->percent after (%p) = %f\n", child, child->percent);
789 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
792 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
793 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
794 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
795 // 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
796 int px = atoi(resize_px);
797 int ppt = atoi(resize_ppt);
798 if (strcmp(way, "shrink") == 0) {
806 TAILQ_FOREACH(current, &owindows, owindows) {
807 /* Don't handle dock windows (issue #1201) */
808 if (current->con->window && current->con->window->dock) {
809 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
814 if ((floating_con = con_inside_floating(current->con))) {
815 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
817 if (strcmp(direction, "width") == 0 ||
818 strcmp(direction, "height") == 0) {
819 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
822 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
828 cmd_output->needs_tree_render = true;
829 // XXX: default reply for now, make this a better reply
834 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
837 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
838 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
843 TAILQ_FOREACH(current, &owindows, owindows) {
844 DLOG("matching: %p / %s\n", current->con, current->con->name);
845 int border_style = current->con->border_style;
847 int tmp_border_width = -1;
848 tmp_border_width = strtol(border_width, &end, 10);
849 if (end == border_width) {
850 /* no valid digits found */
851 tmp_border_width = -1;
853 if (strcmp(border_style_str, "toggle") == 0) {
856 if (border_style == BS_NORMAL)
857 tmp_border_width = 2;
858 else if (border_style == BS_NONE)
859 tmp_border_width = 0;
860 else if (border_style == BS_PIXEL)
861 tmp_border_width = 1;
863 if (strcmp(border_style_str, "normal") == 0)
864 border_style = BS_NORMAL;
865 else if (strcmp(border_style_str, "pixel") == 0)
866 border_style = BS_PIXEL;
867 else if (strcmp(border_style_str, "1pixel") == 0) {
868 border_style = BS_PIXEL;
869 tmp_border_width = 1;
870 } else if (strcmp(border_style_str, "none") == 0)
871 border_style = BS_NONE;
873 ELOG("BUG: called with border_style=%s\n", border_style_str);
878 con_set_border_style(current->con, border_style, tmp_border_width);
881 cmd_output->needs_tree_render = true;
882 // XXX: default reply for now, make this a better reply
887 * Implementation of 'nop <comment>'.
890 void cmd_nop(I3_CMD, char *comment) {
891 LOG("-------------------------------------------------\n");
892 LOG(" NOP: %s\n", comment);
893 LOG("-------------------------------------------------\n");
897 * Implementation of 'append_layout <path>'.
900 void cmd_append_layout(I3_CMD, char *path) {
901 LOG("Appending layout \"%s\"\n", path);
903 json_content_t content = json_determine_content(path);
904 LOG("JSON content = %d\n", content);
905 if (content == JSON_CONTENT_UNKNOWN) {
906 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
911 Con *parent = focused;
912 if (content == JSON_CONTENT_WORKSPACE) {
913 parent = output_get_content(con_get_output(parent));
915 /* We need to append the layout to a split container, since a leaf
916 * container must not have any children (by definition).
917 * Note that we explicitly check for workspaces, since they are okay for
918 * this purpose, but con_accepts_window() returns false for workspaces. */
919 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
920 parent = parent->parent;
922 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
923 char *errormsg = NULL;
924 tree_append_json(parent, path, &errormsg);
925 if (errormsg != NULL) {
928 /* Note that we continue executing since tree_append_json() has
929 * side-effects — user-provided layouts can be partly valid, partly
930 * invalid, leading to half of the placeholder containers being
936 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
937 // false); should be enough, but when sending 'workspace 4; append_layout
938 // /tmp/foo.json', the needs_tree_render == true of the workspace command
939 // is not executed yet and will be batched with append_layout’s
940 // needs_tree_render after the parser finished. We should check if that is
942 render_con(croot, false);
944 restore_open_placeholder_windows(parent);
946 if (content == JSON_CONTENT_WORKSPACE)
947 ipc_send_workspace_event("restored", parent, NULL);
949 cmd_output->needs_tree_render = true;
953 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
956 void cmd_workspace(I3_CMD, char *which) {
959 DLOG("which=%s\n", which);
961 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
962 LOG("Cannot switch workspace while in global fullscreen\n");
967 if (strcmp(which, "next") == 0)
968 ws = workspace_next();
969 else if (strcmp(which, "prev") == 0)
970 ws = workspace_prev();
971 else if (strcmp(which, "next_on_output") == 0)
972 ws = workspace_next_on_output();
973 else if (strcmp(which, "prev_on_output") == 0)
974 ws = workspace_prev_on_output();
976 ELOG("BUG: called with which=%s\n", which);
983 cmd_output->needs_tree_render = true;
984 // XXX: default reply for now, make this a better reply
989 * Implementation of 'workspace number <name>'
992 void cmd_workspace_number(I3_CMD, char *which) {
993 Con *output, *workspace = NULL;
995 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
996 LOG("Cannot switch workspace while in global fullscreen\n");
1001 long parsed_num = ws_name_to_number(which);
1003 if (parsed_num == -1) {
1004 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
1005 // TODO: better error message
1006 yerror("Could not parse number");
1010 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1011 GREP_FIRST(workspace, output_get_content(output),
1012 child->num == parsed_num);
1015 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
1017 workspace_show_by_name(which);
1018 cmd_output->needs_tree_render = true;
1021 if (maybe_back_and_forth(cmd_output, workspace->name))
1023 workspace_show(workspace);
1025 cmd_output->needs_tree_render = true;
1026 // XXX: default reply for now, make this a better reply
1031 * Implementation of 'workspace back_and_forth'.
1034 void cmd_workspace_back_and_forth(I3_CMD) {
1035 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1036 LOG("Cannot switch workspace while in global fullscreen\n");
1041 workspace_back_and_forth();
1043 cmd_output->needs_tree_render = true;
1044 // XXX: default reply for now, make this a better reply
1049 * Implementation of 'workspace <name>'
1052 void cmd_workspace_name(I3_CMD, char *name) {
1053 if (strncasecmp(name, "__", strlen("__")) == 0) {
1054 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1059 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1060 LOG("Cannot switch workspace while in global fullscreen\n");
1065 DLOG("should switch to workspace %s\n", name);
1066 if (maybe_back_and_forth(cmd_output, name))
1072 /* first look for a workspace with this name */
1073 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1074 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1077 /* if the name is only digits, we interpret this as a "workspace number"
1079 if (!ws && name_is_digits(name)) {
1080 long parsed_num = ws_name_to_number(name);
1081 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1082 GREP_FIRST(ws, output_get_content(output),
1083 child->num == parsed_num);
1087 /* if no workspace was found, make a new one */
1089 ws = workspace_get(name, NULL);
1093 cmd_output->needs_tree_render = true;
1094 // XXX: default reply for now, make this a better reply
1099 * Implementation of 'mark <mark>'
1102 void cmd_mark(I3_CMD, char *mark) {
1103 DLOG("Clearing all windows which have that mark first\n");
1106 TAILQ_FOREACH(con, &all_cons, all_cons) {
1107 if (con->mark && strcmp(con->mark, mark) == 0)
1111 DLOG("marking window with str %s\n", mark);
1116 TAILQ_FOREACH(current, &owindows, owindows) {
1117 DLOG("matching: %p / %s\n", current->con, current->con->name);
1118 current->con->mark = sstrdup(mark);
1121 cmd_output->needs_tree_render = true;
1122 // XXX: default reply for now, make this a better reply
1127 * Implementation of 'unmark [mark]'
1130 void cmd_unmark(I3_CMD, char *mark) {
1133 TAILQ_FOREACH(con, &all_cons, all_cons) {
1136 DLOG("removed all window marks");
1139 TAILQ_FOREACH(con, &all_cons, all_cons) {
1140 if (con->mark && strcmp(con->mark, mark) == 0)
1143 DLOG("removed window mark %s\n", mark);
1146 cmd_output->needs_tree_render = true;
1147 // XXX: default reply for now, make this a better reply
1152 * Implementation of 'mode <string>'.
1155 void cmd_mode(I3_CMD, char *mode) {
1156 DLOG("mode=%s\n", mode);
1159 // XXX: default reply for now, make this a better reply
1164 * Implementation of 'move [window|container] [to] output <str>'.
1167 void cmd_move_con_to_output(I3_CMD, char *name) {
1170 DLOG("should move window to output %s\n", name);
1174 /* get the output */
1175 Output *current_output = NULL;
1178 // TODO: fix the handling of criteria
1179 TAILQ_FOREACH(current, &owindows, owindows)
1180 current_output = get_output_of_con(current->con);
1182 assert(current_output != NULL);
1184 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1185 if (strcasecmp(name, "up") == 0)
1186 output = get_output_next_wrap(D_UP, current_output);
1187 else if (strcasecmp(name, "down") == 0)
1188 output = get_output_next_wrap(D_DOWN, current_output);
1189 else if (strcasecmp(name, "left") == 0)
1190 output = get_output_next_wrap(D_LEFT, current_output);
1191 else if (strcasecmp(name, "right") == 0)
1192 output = get_output_next_wrap(D_RIGHT, current_output);
1194 output = get_output_by_name(name);
1197 LOG("No such output found.\n");
1202 /* get visible workspace on output */
1204 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1210 TAILQ_FOREACH(current, &owindows, owindows) {
1211 DLOG("matching: %p / %s\n", current->con, current->con->name);
1212 con_move_to_workspace(current->con, ws, true, false);
1215 cmd_output->needs_tree_render = true;
1216 // XXX: default reply for now, make this a better reply
1221 * Implementation of 'floating enable|disable|toggle'
1224 void cmd_floating(I3_CMD, char *floating_mode) {
1227 DLOG("floating_mode=%s\n", floating_mode);
1231 TAILQ_FOREACH(current, &owindows, owindows) {
1232 DLOG("matching: %p / %s\n", current->con, current->con->name);
1233 if (strcmp(floating_mode, "toggle") == 0) {
1234 DLOG("should toggle mode\n");
1235 toggle_floating_mode(current->con, false);
1237 DLOG("should switch mode to %s\n", floating_mode);
1238 if (strcmp(floating_mode, "enable") == 0) {
1239 floating_enable(current->con, false);
1241 floating_disable(current->con, false);
1246 cmd_output->needs_tree_render = true;
1247 // XXX: default reply for now, make this a better reply
1252 * Implementation of 'move workspace to [output] <str>'.
1255 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1256 DLOG("should move workspace to output %s\n", name);
1261 TAILQ_FOREACH(current, &owindows, owindows) {
1262 Output *current_output = get_output_of_con(current->con);
1263 if (!current_output) {
1264 ELOG("Cannot get current output. This is a bug in i3.\n");
1268 Output *output = get_output_from_string(current_output, name);
1270 ELOG("Could not get output from string \"%s\"\n", name);
1275 Con *content = output_get_content(output->con);
1276 LOG("got output %p with content %p\n", output, content);
1278 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1279 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1281 Con *ws = con_get_workspace(current->con);
1282 LOG("should move workspace %p / %s\n", ws, ws->name);
1283 bool workspace_was_visible = workspace_is_visible(ws);
1285 if (con_num_children(ws->parent) == 1) {
1286 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1288 /* check if we can find a workspace assigned to this output */
1289 bool used_assignment = false;
1290 struct Workspace_Assignment *assignment;
1291 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1292 if (strcmp(assignment->output, current_output->name) != 0)
1295 /* check if this workspace is already attached to the tree */
1296 Con *workspace = NULL, *out;
1297 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1298 GREP_FIRST(workspace, output_get_content(out),
1299 !strcasecmp(child->name, assignment->name));
1300 if (workspace != NULL)
1303 /* so create the workspace referenced to by this assignment */
1304 LOG("Creating workspace from assignment %s.\n", assignment->name);
1305 workspace_get(assignment->name, NULL);
1306 used_assignment = true;
1310 /* if we couldn't create the workspace using an assignment, create
1311 * it on the output */
1312 if (!used_assignment)
1313 create_workspace_on_output(current_output, ws->parent);
1315 /* notify the IPC listeners */
1316 ipc_send_workspace_event("init", ws, NULL);
1318 DLOG("Detaching\n");
1320 /* detach from the old output and attach to the new output */
1321 Con *old_content = ws->parent;
1323 if (workspace_was_visible) {
1324 /* The workspace which we just detached was visible, so focus
1325 * the next one in the focus-stack. */
1326 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1327 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1328 workspace_show(focus_ws);
1330 con_attach(ws, content, false);
1332 /* fix the coordinates of the floating containers */
1334 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1335 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1337 ipc_send_workspace_event("move", ws, NULL);
1338 if (workspace_was_visible) {
1339 /* Focus the moved workspace on the destination output. */
1343 /* NB: We cannot simply work with previously_visible_ws since it might
1344 * have been cleaned up by workspace_show() already, depending on the
1345 * focus order/number of other workspaces on the output.
1346 * Instead, we loop through the available workspaces and only work with
1347 * previously_visible_ws if we still find it. */
1348 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1349 if (ws != previously_visible_ws)
1352 /* Call the on_remove_child callback of the workspace which previously
1353 * was visible on the destination output. Since it is no longer
1354 * visible, it might need to get cleaned up. */
1355 CALL(previously_visible_ws, on_remove_child);
1360 cmd_output->needs_tree_render = true;
1361 // XXX: default reply for now, make this a better reply
1366 * Implementation of 'split v|h|vertical|horizontal'.
1369 void cmd_split(I3_CMD, char *direction) {
1371 /* TODO: use matches */
1372 LOG("splitting in direction %c\n", direction[0]);
1373 if (match_is_empty(current_match))
1374 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1376 TAILQ_FOREACH(current, &owindows, owindows) {
1377 DLOG("matching: %p / %s\n", current->con, current->con->name);
1378 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1382 cmd_output->needs_tree_render = true;
1383 // XXX: default reply for now, make this a better reply
1388 * Implementation of 'kill [window|client]'.
1391 void cmd_kill(I3_CMD, char *kill_mode_str) {
1392 if (kill_mode_str == NULL)
1393 kill_mode_str = "window";
1396 DLOG("kill_mode=%s\n", kill_mode_str);
1399 if (strcmp(kill_mode_str, "window") == 0)
1400 kill_mode = KILL_WINDOW;
1401 else if (strcmp(kill_mode_str, "client") == 0)
1402 kill_mode = KILL_CLIENT;
1404 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1409 /* check if the match is empty, not if the result is empty */
1410 if (match_is_empty(current_match))
1411 tree_close_con(kill_mode);
1413 TAILQ_FOREACH(current, &owindows, owindows) {
1414 DLOG("matching: %p / %s\n", current->con, current->con->name);
1415 tree_close(current->con, kill_mode, false, false);
1419 cmd_output->needs_tree_render = true;
1420 // XXX: default reply for now, make this a better reply
1425 * Implementation of 'exec [--no-startup-id] <command>'.
1428 void cmd_exec(I3_CMD, char *nosn, char *command) {
1429 bool no_startup_id = (nosn != NULL);
1431 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1432 start_application(command, no_startup_id);
1434 // XXX: default reply for now, make this a better reply
1439 * Implementation of 'focus left|right|up|down'.
1442 void cmd_focus_direction(I3_CMD, char *direction) {
1443 DLOG("direction = *%s*\n", direction);
1445 if (strcmp(direction, "left") == 0)
1446 tree_next('p', HORIZ);
1447 else if (strcmp(direction, "right") == 0)
1448 tree_next('n', HORIZ);
1449 else if (strcmp(direction, "up") == 0)
1450 tree_next('p', VERT);
1451 else if (strcmp(direction, "down") == 0)
1452 tree_next('n', VERT);
1454 ELOG("Invalid focus direction (%s)\n", direction);
1459 cmd_output->needs_tree_render = true;
1460 // XXX: default reply for now, make this a better reply
1465 * Implementation of 'focus tiling|floating|mode_toggle'.
1468 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1469 DLOG("window_mode = %s\n", window_mode);
1471 Con *ws = con_get_workspace(focused);
1474 if (strcmp(window_mode, "mode_toggle") == 0) {
1475 current = TAILQ_FIRST(&(ws->focus_head));
1476 if (current != NULL && current->type == CT_FLOATING_CON)
1477 window_mode = "tiling";
1479 window_mode = "floating";
1481 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1482 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1483 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1486 con_focus(con_descend_focused(current));
1491 cmd_output->needs_tree_render = true;
1492 // XXX: default reply for now, make this a better reply
1497 * Implementation of 'focus parent|child'.
1500 void cmd_focus_level(I3_CMD, char *level) {
1501 DLOG("level = %s\n", level);
1502 bool success = false;
1504 /* Focusing the parent can only be allowed if the newly
1505 * focused container won't escape the fullscreen container. */
1506 if (strcmp(level, "parent") == 0) {
1507 if (focused && focused->parent) {
1508 if (con_fullscreen_permits_focusing(focused->parent))
1509 success = level_up();
1511 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1515 /* Focusing a child should always be allowed. */
1517 success = level_down();
1519 cmd_output->needs_tree_render = success;
1520 // XXX: default reply for now, make this a better reply
1525 * Implementation of 'focus'.
1528 void cmd_focus(I3_CMD) {
1529 DLOG("current_match = %p\n", current_match);
1531 if (match_is_empty(current_match)) {
1532 ELOG("You have to specify which window/container should be focused.\n");
1533 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1535 yerror("You have to specify which window/container should be focused");
1540 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1543 TAILQ_FOREACH(current, &owindows, owindows) {
1544 Con *ws = con_get_workspace(current->con);
1545 /* If no workspace could be found, this was a dock window.
1546 * Just skip it, you cannot focus dock windows. */
1550 /* Check the fullscreen focus constraints. */
1551 if (!con_fullscreen_permits_focusing(current->con)) {
1552 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1557 /* In case this is a scratchpad window, call scratchpad_show(). */
1558 if (ws == __i3_scratch) {
1559 scratchpad_show(current->con);
1561 /* While for the normal focus case we can change focus multiple
1562 * times and only a single window ends up focused, we could show
1563 * multiple scratchpad windows. So, rather break here. */
1567 /* If the container is not on the current workspace,
1568 * workspace_show() will switch to a different workspace and (if
1569 * enabled) trigger a mouse pointer warp to the currently focused
1570 * container (!) on the target workspace.
1572 * Therefore, before calling workspace_show(), we make sure that
1573 * 'current' will be focused on the workspace. However, we cannot
1574 * just con_focus(current) because then the pointer will not be
1575 * warped at all (the code thinks we are already there).
1577 * So we focus 'current' to make it the currently focused window of
1578 * the target workspace, then revert focus. */
1579 Con *currently_focused = focused;
1580 con_focus(current->con);
1581 con_focus(currently_focused);
1583 /* Now switch to the workspace, then focus */
1585 LOG("focusing %p / %s\n", current->con, current->con->name);
1586 con_focus(current->con);
1591 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1592 "while only exactly one container can be focused at a time.\n",
1595 cmd_output->needs_tree_render = true;
1596 // XXX: default reply for now, make this a better reply
1601 * Implementation of 'fullscreen enable|toggle [global]' and
1602 * 'fullscreen disable'
1605 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1606 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1607 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1612 TAILQ_FOREACH(current, &owindows, owindows) {
1613 DLOG("matching: %p / %s\n", current->con, current->con->name);
1614 if (strcmp(action, "toggle") == 0) {
1615 con_toggle_fullscreen(current->con, mode);
1616 } else if (strcmp(action, "enable") == 0) {
1617 con_enable_fullscreen(current->con, mode);
1618 } else if (strcmp(action, "disable") == 0) {
1619 con_disable_fullscreen(current->con);
1623 cmd_output->needs_tree_render = true;
1624 // XXX: default reply for now, make this a better reply
1629 * Implementation of 'move <direction> [<pixels> [px]]'.
1632 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1633 // 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
1634 int px = atoi(move_px);
1639 Con *initially_focused = focused;
1641 TAILQ_FOREACH(current, &owindows, owindows) {
1642 DLOG("moving in direction %s, px %s\n", direction, move_px);
1643 if (con_is_floating(current->con)) {
1644 DLOG("floating move with %d pixels\n", px);
1645 Rect newrect = current->con->parent->rect;
1646 if (strcmp(direction, "left") == 0) {
1648 } else if (strcmp(direction, "right") == 0) {
1650 } else if (strcmp(direction, "up") == 0) {
1652 } else if (strcmp(direction, "down") == 0) {
1655 floating_reposition(current->con->parent, newrect);
1657 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1658 cmd_output->needs_tree_render = true;
1662 /* the move command should not disturb focus */
1663 if (focused != initially_focused)
1664 con_focus(initially_focused);
1666 // XXX: default reply for now, make this a better reply
1671 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1674 void cmd_layout(I3_CMD, char *layout_str) {
1675 if (strcmp(layout_str, "stacking") == 0)
1676 layout_str = "stacked";
1679 /* default is a special case which will be handled in con_set_layout(). */
1680 if (strcmp(layout_str, "default") == 0)
1682 else if (strcmp(layout_str, "stacked") == 0)
1684 else if (strcmp(layout_str, "tabbed") == 0)
1686 else if (strcmp(layout_str, "splitv") == 0)
1688 else if (strcmp(layout_str, "splith") == 0)
1691 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1695 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1697 /* check if the match is empty, not if the result is empty */
1698 if (match_is_empty(current_match))
1699 con_set_layout(focused, layout);
1701 TAILQ_FOREACH(current, &owindows, owindows) {
1702 DLOG("matching: %p / %s\n", current->con, current->con->name);
1703 con_set_layout(current->con, layout);
1707 cmd_output->needs_tree_render = true;
1708 // XXX: default reply for now, make this a better reply
1713 * Implementation of 'layout toggle [all|split]'.
1716 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1719 if (toggle_mode == NULL)
1720 toggle_mode = "default";
1722 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1724 /* check if the match is empty, not if the result is empty */
1725 if (match_is_empty(current_match))
1726 con_toggle_layout(focused, toggle_mode);
1728 TAILQ_FOREACH(current, &owindows, owindows) {
1729 DLOG("matching: %p / %s\n", current->con, current->con->name);
1730 con_toggle_layout(current->con, toggle_mode);
1734 cmd_output->needs_tree_render = true;
1735 // XXX: default reply for now, make this a better reply
1740 * Implementation of 'exit'.
1743 void cmd_exit(I3_CMD) {
1744 LOG("Exiting due to user command.\n");
1746 unlink(config.ipc_socket_path);
1747 xcb_disconnect(conn);
1754 * Implementation of 'reload'.
1757 void cmd_reload(I3_CMD) {
1759 kill_nagbar(&config_error_nagbar_pid, false);
1760 kill_nagbar(&command_error_nagbar_pid, false);
1761 load_configuration(conn, NULL, true);
1763 /* Send an IPC event just in case the ws names have changed */
1764 ipc_send_workspace_event("reload", NULL, NULL);
1765 /* Send an update event for the barconfig just in case it has changed */
1768 // XXX: default reply for now, make this a better reply
1773 * Implementation of 'restart'.
1776 void cmd_restart(I3_CMD) {
1777 LOG("restarting i3\n");
1779 unlink(config.ipc_socket_path);
1780 /* We need to call this manually since atexit handlers don’t get called
1782 purge_zerobyte_logfile();
1785 // XXX: default reply for now, make this a better reply
1790 * Implementation of 'open'.
1793 void cmd_open(I3_CMD) {
1794 LOG("opening new container\n");
1795 Con *con = tree_open_con(NULL, NULL);
1796 con->layout = L_SPLITH;
1803 y(integer, (long int)con);
1806 cmd_output->needs_tree_render = true;
1810 * Implementation of 'focus output <output>'.
1813 void cmd_focus_output(I3_CMD, char *name) {
1816 DLOG("name = %s\n", name);
1820 /* get the output */
1821 Output *current_output = NULL;
1824 TAILQ_FOREACH(current, &owindows, owindows)
1825 current_output = get_output_of_con(current->con);
1826 assert(current_output != NULL);
1828 output = get_output_from_string(current_output, name);
1831 LOG("No such output found.\n");
1836 /* get visible workspace on output */
1838 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1846 cmd_output->needs_tree_render = true;
1847 // XXX: default reply for now, make this a better reply
1852 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1855 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1858 bool has_error = false;
1863 TAILQ_FOREACH(current, &owindows, owindows) {
1864 if (!con_is_floating(current->con)) {
1865 ELOG("Cannot change position. The window/container is not floating\n");
1868 yerror("Cannot change position of a window/container because it is not floating.");
1875 if (strcmp(method, "absolute") == 0) {
1876 current->con->parent->rect.x = x;
1877 current->con->parent->rect.y = y;
1879 DLOG("moving to absolute position %d %d\n", x, y);
1880 floating_maybe_reassign_ws(current->con->parent);
1881 cmd_output->needs_tree_render = true;
1884 if (strcmp(method, "position") == 0) {
1885 Rect newrect = current->con->parent->rect;
1887 DLOG("moving to position %d %d\n", x, y);
1891 floating_reposition(current->con->parent, newrect);
1895 // XXX: default reply for now, make this a better reply
1901 * Implementation of 'move [window|container] [to] [absolute] position center
1904 void cmd_move_window_to_center(I3_CMD, char *method) {
1905 if (!con_is_floating(focused)) {
1906 ELOG("Cannot change position. The window/container is not floating\n");
1907 yerror("Cannot change position. The window/container is not floating.");
1911 if (strcmp(method, "absolute") == 0) {
1912 Rect *rect = &focused->parent->rect;
1914 DLOG("moving to absolute center\n");
1915 rect->x = croot->rect.width / 2 - rect->width / 2;
1916 rect->y = croot->rect.height / 2 - rect->height / 2;
1918 floating_maybe_reassign_ws(focused->parent);
1919 cmd_output->needs_tree_render = true;
1922 if (strcmp(method, "position") == 0) {
1923 Rect *wsrect = &con_get_workspace(focused)->rect;
1924 Rect newrect = focused->parent->rect;
1926 DLOG("moving to center\n");
1927 newrect.x = wsrect->width / 2 - newrect.width / 2;
1928 newrect.y = wsrect->height / 2 - newrect.height / 2;
1930 floating_reposition(focused->parent, newrect);
1933 // XXX: default reply for now, make this a better reply
1938 * Implementation of 'move scratchpad'.
1941 void cmd_move_scratchpad(I3_CMD) {
1942 DLOG("should move window to scratchpad\n");
1947 TAILQ_FOREACH(current, &owindows, owindows) {
1948 DLOG("matching: %p / %s\n", current->con, current->con->name);
1949 scratchpad_move(current->con);
1952 cmd_output->needs_tree_render = true;
1953 // XXX: default reply for now, make this a better reply
1958 * Implementation of 'scratchpad show'.
1961 void cmd_scratchpad_show(I3_CMD) {
1962 DLOG("should show scratchpad window\n");
1965 if (match_is_empty(current_match)) {
1966 scratchpad_show(NULL);
1968 TAILQ_FOREACH(current, &owindows, owindows) {
1969 DLOG("matching: %p / %s\n", current->con, current->con->name);
1970 scratchpad_show(current->con);
1974 cmd_output->needs_tree_render = true;
1975 // XXX: default reply for now, make this a better reply
1980 * Implementation of 'rename workspace [<name>] to <name>'
1983 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1984 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1985 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1990 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1992 LOG("Renaming current workspace to \"%s\"\n", new_name);
1995 Con *output, *workspace = NULL;
1997 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1998 GREP_FIRST(workspace, output_get_content(output),
1999 !strcasecmp(child->name, old_name));
2001 workspace = con_get_workspace(focused);
2005 // TODO: we should include the old workspace name here and use yajl for
2006 // generating the reply.
2007 // TODO: better error message
2008 yerror("Old workspace not found");
2012 Con *check_dest = NULL;
2013 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
2014 GREP_FIRST(check_dest, output_get_content(output),
2015 !strcasecmp(child->name, new_name));
2017 if (check_dest != NULL) {
2018 // TODO: we should include the new workspace name here and use yajl for
2019 // generating the reply.
2020 // TODO: better error message
2021 yerror("New workspace already exists");
2025 /* Change the name and try to parse it as a number. */
2026 FREE(workspace->name);
2027 workspace->name = sstrdup(new_name);
2029 workspace->num = ws_name_to_number(new_name);
2030 LOG("num = %d\n", workspace->num);
2032 /* By re-attaching, the sort order will be correct afterwards. */
2033 Con *previously_focused = focused;
2034 Con *parent = workspace->parent;
2035 con_detach(workspace);
2036 con_attach(workspace, parent, false);
2037 /* Restore the previous focus since con_attach messes with the focus. */
2038 con_focus(previously_focused);
2040 cmd_output->needs_tree_render = true;
2043 ipc_send_workspace_event("rename", workspace, NULL);
2044 ewmh_update_desktop_names();
2045 ewmh_update_desktop_viewport();
2046 ewmh_update_current_desktop();
2050 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2053 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
2055 bool toggle = false;
2056 if (strcmp(bar_mode, "dock") == 0)
2058 else if (strcmp(bar_mode, "hide") == 0)
2060 else if (strcmp(bar_mode, "invisible") == 0)
2062 else if (strcmp(bar_mode, "toggle") == 0)
2065 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2069 bool changed_sth = false;
2070 Barconfig *current = NULL;
2071 TAILQ_FOREACH(current, &barconfigs, configs) {
2072 if (bar_id && strcmp(current->id, bar_id) != 0)
2076 mode = (current->mode + 1) % 2;
2078 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2079 current->mode = mode;
2086 if (bar_id && !changed_sth) {
2087 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2095 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2098 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2099 int hidden_state = S_SHOW;
2100 bool toggle = false;
2101 if (strcmp(bar_hidden_state, "hide") == 0)
2102 hidden_state = S_HIDE;
2103 else if (strcmp(bar_hidden_state, "show") == 0)
2104 hidden_state = S_SHOW;
2105 else if (strcmp(bar_hidden_state, "toggle") == 0)
2108 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2112 bool changed_sth = false;
2113 Barconfig *current = NULL;
2114 TAILQ_FOREACH(current, &barconfigs, configs) {
2115 if (bar_id && strcmp(current->id, bar_id) != 0)
2119 hidden_state = (current->hidden_state + 1) % 2;
2121 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2122 current->hidden_state = hidden_state;
2129 if (bar_id && !changed_sth) {
2130 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2138 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2141 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2143 if (strcmp(bar_type, "mode") == 0)
2144 ret = cmd_bar_mode(bar_value, bar_id);
2145 else if (strcmp(bar_type, "hidden_state") == 0)
2146 ret = cmd_bar_hidden_state(bar_value, bar_id);
2148 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2160 * Implementation of 'shmlog <size>|toggle|on|off'
2163 void cmd_shmlog(I3_CMD, char *argument) {
2164 if (!strcmp(argument, "toggle"))
2165 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2166 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2167 else if (!strcmp(argument, "on"))
2168 shmlog_size = default_shmlog_size;
2169 else if (!strcmp(argument, "off"))
2172 /* If shm logging now, restart logging with the new size. */
2173 if (shmlog_size > 0) {
2175 LOG("Restarting shm logging...\n");
2178 shmlog_size = atoi(argument);
2179 /* Make a weakly attempt at ensuring the argument is valid. */
2180 if (shmlog_size <= 0)
2181 shmlog_size = default_shmlog_size;
2183 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2185 update_shmlog_atom();
2186 // XXX: default reply for now, make this a better reply
2191 * Implementation of 'debuglog toggle|on|off'
2194 void cmd_debuglog(I3_CMD, char *argument) {
2195 bool logging = get_debug_logging();
2196 if (!strcmp(argument, "toggle")) {
2197 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2198 set_debug_logging(!logging);
2199 } else if (!strcmp(argument, "on") && !logging) {
2200 LOG("Enabling debug logging\n");
2201 set_debug_logging(true);
2202 } else if (!strcmp(argument, "off") && logging) {
2203 LOG("Disabling debug logging\n");
2204 set_debug_logging(false);
2206 // XXX: default reply for now, make this a better reply