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) do { \
22 if (cmd_output->json_gen != NULL) { \
29 #define yerror(message) do { \
30 if (cmd_output->json_gen != NULL) { \
40 /** When the command did not include match criteria (!), we use the currently
41 * focused container. Do not confuse this case with a command which included
42 * criteria but which did not match any windows. This macro has to be called in
45 #define HANDLE_EMPTY_MATCH do { \
46 if (match_is_empty(current_match)) { \
47 owindow *ow = smalloc(sizeof(owindow)); \
49 TAILQ_INIT(&owindows); \
50 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
56 * Returns true if a is definitely greater than b (using the given epsilon)
59 static bool definitelyGreaterThan(float a, float b, float epsilon) {
60 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
64 * Returns an 'output' corresponding to one of left/right/down/up or a specific
68 static Output *get_output_from_string(Output *current_output, const char *output_str) {
71 if (strcasecmp(output_str, "left") == 0)
72 output = get_output_next_wrap(D_LEFT, current_output);
73 else if (strcasecmp(output_str, "right") == 0)
74 output = get_output_next_wrap(D_RIGHT, current_output);
75 else if (strcasecmp(output_str, "up") == 0)
76 output = get_output_next_wrap(D_UP, current_output);
77 else if (strcasecmp(output_str, "down") == 0)
78 output = get_output_next_wrap(D_DOWN, current_output);
79 else output = get_output_by_name(output_str);
85 * Returns the output containing the given container.
87 static Output *get_output_of_con(Con *con) {
88 Con *output_con = con_get_output(con);
89 Output *output = get_output_by_name(output_con->name);
90 assert(output != NULL);
96 * Checks whether we switched to a new workspace and returns false in that case,
97 * signaling that further workspace switching should be done by the calling function
98 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
99 * and return true, signaling that no further workspace switching should occur in the calling function.
102 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
103 Con *ws = con_get_workspace(focused);
105 /* If we switched to a different workspace, do nothing */
106 if (strcmp(ws->name, name) != 0)
109 DLOG("This workspace is already focused.\n");
110 if (config.workspace_auto_back_and_forth) {
111 workspace_back_and_forth();
112 cmd_output->needs_tree_render = true;
118 * Return the passed workspace unless it is the current one and auto back and
119 * forth is enabled, in which case the back_and_forth workspace is returned.
121 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
124 if (!config.workspace_auto_back_and_forth)
127 current = con_get_workspace(focused);
129 if (current == workspace) {
130 baf = workspace_back_and_forth_get();
132 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
140 // This code is commented out because we might recycle it for popping up error
141 // messages on parser errors.
143 static pid_t migration_pid = -1;
146 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
147 * it exited (or could not be started, depending on the exit code).
150 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
151 ev_child_stop(EV_A_ watcher);
152 if (!WIFEXITED(watcher->rstatus)) {
153 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
157 int exitcode = WEXITSTATUS(watcher->rstatus);
158 printf("i3-nagbar process exited with status %d\n", exitcode);
160 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
166 /* We need ev >= 4 for the following code. Since it is not *that* important (it
167 * only makes sure that there are no i3-nagbar instances left behind) we still
168 * support old systems with libev 3. */
169 #if EV_VERSION_MAJOR >= 4
171 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
172 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
175 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
176 if (migration_pid != -1) {
177 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
178 kill(migration_pid, SIGKILL);
183 void cmd_MIGRATION_start_nagbar(void) {
184 if (migration_pid != -1) {
185 fprintf(stderr, "i3-nagbar already running.\n");
188 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
189 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
190 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
191 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
192 migration_pid = fork();
193 if (migration_pid == -1) {
194 warn("Could not fork()");
199 if (migration_pid == 0) {
201 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
203 NULL, /* will be replaced by the executable path */
207 "You found a parsing error. Please, please, please, report it!",
213 exec_i3_utility("i3-nagbar", argv);
217 /* install a child watcher */
218 ev_child *child = smalloc(sizeof(ev_child));
219 ev_child_init(child, &nagbar_exited, migration_pid, 0);
220 ev_child_start(main_loop, child);
222 /* We need ev >= 4 for the following code. Since it is not *that* important (it
223 * only makes sure that there are no i3-nagbar instances left behind) we still
224 * support old systems with libev 3. */
225 #if EV_VERSION_MAJOR >= 4
226 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
228 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
229 ev_cleanup_init(cleanup, nagbar_cleanup);
230 ev_cleanup_start(main_loop, cleanup);
236 /*******************************************************************************
237 * Criteria functions.
238 ******************************************************************************/
241 * Helper data structure for an operation window (window on which the operation
242 * will be performed). Used to build the TAILQ owindows.
245 typedef struct owindow {
247 TAILQ_ENTRY(owindow) owindows;
250 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
252 static owindows_head owindows;
255 * Initializes the specified 'Match' data structure and the initial state of
256 * commands.c for matching target windows of a command.
259 void cmd_criteria_init(I3_CMD) {
263 DLOG("Initializing criteria, current_match = %p\n", current_match);
264 match_free(current_match);
265 match_init(current_match);
266 while (!TAILQ_EMPTY(&owindows)) {
267 ow = TAILQ_FIRST(&owindows);
268 TAILQ_REMOVE(&owindows, ow, owindows);
271 TAILQ_INIT(&owindows);
273 TAILQ_FOREACH(con, &all_cons, all_cons) {
274 ow = smalloc(sizeof(owindow));
276 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
281 * A match specification just finished (the closing square bracket was found),
282 * so we filter the list of owindows.
285 void cmd_criteria_match_windows(I3_CMD) {
286 owindow *next, *current;
288 DLOG("match specification finished, matching...\n");
289 /* copy the old list head to iterate through it and start with a fresh
290 * list which will contain only matching windows */
291 struct owindows_head old = owindows;
292 TAILQ_INIT(&owindows);
293 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
294 /* make a copy of the next pointer and advance the pointer to the
295 * next element as we are going to invalidate the element’s
296 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
298 next = TAILQ_NEXT(next, owindows);
300 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
301 if (current_match->con_id != NULL) {
302 if (current_match->con_id == current->con) {
303 DLOG("matches container!\n");
304 TAILQ_INSERT_TAIL(&owindows, current, owindows);
306 DLOG("doesnt match\n");
309 } else if (current_match->mark != NULL && current->con->mark != NULL &&
310 regex_matches(current_match->mark, current->con->mark)) {
311 DLOG("match by mark\n");
312 TAILQ_INSERT_TAIL(&owindows, current, owindows);
314 if (current->con->window && match_matches_window(current_match, current->con->window)) {
315 DLOG("matches window!\n");
316 TAILQ_INSERT_TAIL(&owindows, current, owindows);
318 DLOG("doesnt match\n");
324 TAILQ_FOREACH(current, &owindows, owindows) {
325 DLOG("matching: %p / %s\n", current->con, current->con->name);
330 * Interprets a ctype=cvalue pair and adds it to the current match
334 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
335 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
337 if (strcmp(ctype, "class") == 0) {
338 current_match->class = regex_new(cvalue);
342 if (strcmp(ctype, "instance") == 0) {
343 current_match->instance = regex_new(cvalue);
347 if (strcmp(ctype, "window_role") == 0) {
348 current_match->window_role = regex_new(cvalue);
352 if (strcmp(ctype, "con_id") == 0) {
354 long parsed = strtol(cvalue, &end, 10);
355 if (parsed == LONG_MIN ||
356 parsed == LONG_MAX ||
358 (end && *end != '\0')) {
359 ELOG("Could not parse con id \"%s\"\n", cvalue);
361 current_match->con_id = (Con*)parsed;
362 DLOG("id as int = %p\n", current_match->con_id);
367 if (strcmp(ctype, "id") == 0) {
369 long parsed = strtol(cvalue, &end, 10);
370 if (parsed == LONG_MIN ||
371 parsed == LONG_MAX ||
373 (end && *end != '\0')) {
374 ELOG("Could not parse window id \"%s\"\n", cvalue);
376 current_match->id = parsed;
377 DLOG("window id as int = %d\n", current_match->id);
382 if (strcmp(ctype, "con_mark") == 0) {
383 current_match->mark = regex_new(cvalue);
387 if (strcmp(ctype, "title") == 0) {
388 current_match->title = regex_new(cvalue);
392 if (strcmp(ctype, "urgent") == 0) {
393 if (strcasecmp(cvalue, "latest") == 0 ||
394 strcasecmp(cvalue, "newest") == 0 ||
395 strcasecmp(cvalue, "recent") == 0 ||
396 strcasecmp(cvalue, "last") == 0) {
397 current_match->urgent = U_LATEST;
398 } else if (strcasecmp(cvalue, "oldest") == 0 ||
399 strcasecmp(cvalue, "first") == 0) {
400 current_match->urgent = U_OLDEST;
405 ELOG("Unknown criterion: %s\n", ctype);
409 * Implementation of 'move [window|container] [to] workspace
410 * next|prev|next_on_output|prev_on_output|current'.
413 void cmd_move_con_to_workspace(I3_CMD, char *which) {
416 DLOG("which=%s\n", which);
418 /* We have nothing to move:
419 * when criteria was specified but didn't match any window or
420 * when criteria wasn't specified and we don't have any window focused. */
421 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
422 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
423 !con_has_children(focused))) {
430 /* get the workspace */
432 if (strcmp(which, "next") == 0)
433 ws = workspace_next();
434 else if (strcmp(which, "prev") == 0)
435 ws = workspace_prev();
436 else if (strcmp(which, "next_on_output") == 0)
437 ws = workspace_next_on_output();
438 else if (strcmp(which, "prev_on_output") == 0)
439 ws = workspace_prev_on_output();
440 else if (strcmp(which, "current") == 0)
441 ws = con_get_workspace(focused);
443 ELOG("BUG: called with which=%s\n", which);
448 TAILQ_FOREACH(current, &owindows, owindows) {
449 DLOG("matching: %p / %s\n", current->con, current->con->name);
450 con_move_to_workspace(current->con, ws, true, false);
453 cmd_output->needs_tree_render = true;
454 // XXX: default reply for now, make this a better reply
459 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
462 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
466 ws = workspace_back_and_forth_get();
469 yerror("No workspace was previously active.");
475 TAILQ_FOREACH(current, &owindows, owindows) {
476 DLOG("matching: %p / %s\n", current->con, current->con->name);
477 con_move_to_workspace(current->con, ws, true, false);
480 cmd_output->needs_tree_render = true;
481 // XXX: default reply for now, make this a better reply
486 * Implementation of 'move [window|container] [to] workspace <name>'.
489 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
490 if (strncasecmp(name, "__", strlen("__")) == 0) {
491 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
498 /* We have nothing to move:
499 * when criteria was specified but didn't match any window or
500 * when criteria wasn't specified and we don't have any window focused. */
501 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
502 ELOG("No windows match your criteria, cannot move.\n");
506 else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
507 !con_has_children(focused)) {
512 LOG("should move window to workspace %s\n", name);
513 /* get the workspace */
514 Con *ws = workspace_get(name, NULL);
516 ws = maybe_auto_back_and_forth_workspace(ws);
520 TAILQ_FOREACH(current, &owindows, owindows) {
521 DLOG("matching: %p / %s\n", current->con, current->con->name);
522 con_move_to_workspace(current->con, ws, true, false);
525 cmd_output->needs_tree_render = true;
526 // XXX: default reply for now, make this a better reply
531 * Implementation of 'move [window|container] [to] workspace number <name>'.
534 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
537 /* We have nothing to move:
538 * when criteria was specified but didn't match any window or
539 * when criteria wasn't specified and we don't have any window focused. */
540 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
541 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
542 !con_has_children(focused))) {
547 LOG("should move window to workspace %s\n", which);
548 /* get the workspace */
549 Con *output, *workspace = NULL;
552 long parsed_num = strtol(which, &endptr, 10);
553 if (parsed_num == LONG_MIN ||
554 parsed_num == LONG_MAX ||
557 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
558 // TODO: better error message
559 yerror("Could not parse number");
563 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
564 GREP_FIRST(workspace, output_get_content(output),
565 child->num == parsed_num);
568 workspace = workspace_get(which, NULL);
571 workspace = maybe_auto_back_and_forth_workspace(workspace);
575 TAILQ_FOREACH(current, &owindows, owindows) {
576 DLOG("matching: %p / %s\n", current->con, current->con->name);
577 con_move_to_workspace(current->con, workspace, true, false);
580 cmd_output->needs_tree_render = true;
581 // XXX: default reply for now, make this a better reply
585 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
586 LOG("floating resize\n");
587 Rect old_rect = floating_con->rect;
588 Con *focused_con = con_descend_focused(floating_con);
590 /* ensure that resize will take place even if pixel increment is smaller than
591 * height increment or width increment.
593 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
594 strcmp(direction, "height") == 0) {
596 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
598 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
599 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
601 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
603 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
606 if (strcmp(direction, "up") == 0) {
607 floating_con->rect.height += px;
608 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
609 floating_con->rect.height += px;
610 } else if (strcmp(direction, "left") == 0) {
611 floating_con->rect.width += px;
613 floating_con->rect.width += px;
616 floating_check_size(floating_con);
618 /* Did we actually resize anything or did the size constraints prevent us?
619 * If we could not resize, exit now to not move the window. */
620 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
623 if (strcmp(direction, "up") == 0) {
624 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
625 } else if (strcmp(direction, "left") == 0) {
626 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
629 /* If this is a scratchpad window, don't auto center it from now on. */
630 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
631 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
634 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
635 LOG("tiling resize\n");
637 Con *first = current;
638 direction_t search_direction;
639 if (!strcmp(direction, "left"))
640 search_direction = D_LEFT;
641 else if (!strcmp(direction, "right"))
642 search_direction = D_RIGHT;
643 else if (!strcmp(direction, "up"))
644 search_direction = D_UP;
646 search_direction = D_DOWN;
648 bool res = resize_find_tiling_participants(&first, &second, search_direction);
650 LOG("No second container in this direction found.\n");
655 /* get the default percentage */
656 int children = con_num_children(first->parent);
657 LOG("ins. %d children\n", children);
658 double percentage = 1.0 / children;
659 LOG("default percentage = %f\n", percentage);
662 LOG("second->percent = %f\n", second->percent);
663 LOG("first->percent before = %f\n", first->percent);
664 if (first->percent == 0.0)
665 first->percent = percentage;
666 if (second->percent == 0.0)
667 second->percent = percentage;
668 double new_first_percent = first->percent + ((double)ppt / 100.0);
669 double new_second_percent = second->percent - ((double)ppt / 100.0);
670 LOG("new_first_percent = %f\n", new_first_percent);
671 LOG("new_second_percent = %f\n", new_second_percent);
672 /* Ensure that the new percentages are positive and greater than
673 * 0.05 to have a reasonable minimum size. */
674 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
675 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
676 first->percent += ((double)ppt / 100.0);
677 second->percent -= ((double)ppt / 100.0);
678 LOG("first->percent after = %f\n", first->percent);
679 LOG("second->percent after = %f\n", second->percent);
681 LOG("Not resizing, already at minimum size\n");
687 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
688 LOG("width/height resize\n");
689 /* get the appropriate current container (skip stacked/tabbed cons) */
690 while (current->parent->layout == L_STACKED ||
691 current->parent->layout == L_TABBED)
692 current = current->parent;
694 /* Then further go up until we find one with the matching orientation. */
695 orientation_t search_orientation =
696 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
698 while (current->type != CT_WORKSPACE &&
699 current->type != CT_FLOATING_CON &&
700 con_orientation(current->parent) != search_orientation)
701 current = current->parent;
703 /* get the default percentage */
704 int children = con_num_children(current->parent);
705 LOG("ins. %d children\n", children);
706 double percentage = 1.0 / children;
707 LOG("default percentage = %f\n", percentage);
709 orientation_t orientation = con_orientation(current->parent);
711 if ((orientation == HORIZ &&
712 strcmp(direction, "height") == 0) ||
713 (orientation == VERT &&
714 strcmp(direction, "width") == 0)) {
715 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
716 (orientation == HORIZ ? "horizontal" : "vertical"));
722 LOG("This is the only container, cannot resize.\n");
727 /* Ensure all the other children have a percentage set. */
729 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
730 LOG("child->percent = %f (child %p)\n", child->percent, child);
731 if (child->percent == 0.0)
732 child->percent = percentage;
735 double new_current_percent = current->percent + ((double)ppt / 100.0);
736 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
737 LOG("new_current_percent = %f\n", new_current_percent);
738 LOG("subtract_percent = %f\n", subtract_percent);
739 /* Ensure that the new percentages are positive and greater than
740 * 0.05 to have a reasonable minimum size. */
741 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
742 if (child == current)
744 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
745 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
750 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
751 LOG("Not resizing, already at minimum size\n");
756 current->percent += ((double)ppt / 100.0);
757 LOG("current->percent after = %f\n", current->percent);
759 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
760 if (child == current)
762 child->percent -= subtract_percent;
763 LOG("child->percent after (%p) = %f\n", child, child->percent);
770 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
773 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
774 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
775 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
776 // 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
777 int px = atoi(resize_px);
778 int ppt = atoi(resize_ppt);
779 if (strcmp(way, "shrink") == 0) {
787 TAILQ_FOREACH(current, &owindows, owindows) {
788 /* Don't handle dock windows (issue #1201) */
789 if (current->con->window && current->con->window->dock) {
790 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
795 if ((floating_con = con_inside_floating(current->con))) {
796 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
798 if (strcmp(direction, "width") == 0 ||
799 strcmp(direction, "height") == 0) {
800 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
803 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
809 cmd_output->needs_tree_render = true;
810 // XXX: default reply for now, make this a better reply
815 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
818 void cmd_border(I3_CMD, char *border_style_str, char *border_width ) {
819 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
824 TAILQ_FOREACH(current, &owindows, owindows) {
825 DLOG("matching: %p / %s\n", current->con, current->con->name);
826 int border_style = current->con->border_style;
828 int tmp_border_width = -1;
829 tmp_border_width = strtol(border_width, &end, 10);
830 if (end == border_width) {
831 /* no valid digits found */
832 tmp_border_width = -1;
834 if (strcmp(border_style_str, "toggle") == 0) {
837 if (border_style == BS_NORMAL)
838 tmp_border_width = 2;
839 else if (border_style == BS_NONE)
840 tmp_border_width = 0;
841 else if (border_style == BS_PIXEL)
842 tmp_border_width = 1;
844 if (strcmp(border_style_str, "normal") == 0)
845 border_style = BS_NORMAL;
846 else if (strcmp(border_style_str, "pixel") == 0)
847 border_style = BS_PIXEL;
848 else if (strcmp(border_style_str, "1pixel") == 0){
849 border_style = BS_PIXEL;
850 tmp_border_width = 1;
851 } else if (strcmp(border_style_str, "none") == 0)
852 border_style = BS_NONE;
854 ELOG("BUG: called with border_style=%s\n", border_style_str);
859 con_set_border_style(current->con, border_style, tmp_border_width);
862 cmd_output->needs_tree_render = true;
863 // XXX: default reply for now, make this a better reply
868 * Implementation of 'nop <comment>'.
871 void cmd_nop(I3_CMD, char *comment) {
872 LOG("-------------------------------------------------\n");
873 LOG(" NOP: %s\n", comment);
874 LOG("-------------------------------------------------\n");
878 * Implementation of 'append_layout <path>'.
881 void cmd_append_layout(I3_CMD, char *path) {
882 LOG("Appending layout \"%s\"\n", path);
883 Con *parent = focused;
884 /* We need to append the layout to a split container, since a leaf
885 * container must not have any children (by definition).
886 * Note that we explicitly check for workspaces, since they are okay for
887 * this purpose, but con_accepts_window() returns false for workspaces. */
888 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
889 parent = parent->parent;
890 DLOG("Appending to parent=%p instead of focused=%p\n",
892 char *errormsg = NULL;
893 tree_append_json(parent, path, &errormsg);
894 if (errormsg != NULL) {
897 /* Note that we continue executing since tree_append_json() has
898 * side-effects — user-provided layouts can be partly valid, partly
899 * invalid, leading to half of the placeholder containers being
905 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
906 // false); should be enough, but when sending 'workspace 4; append_layout
907 // /tmp/foo.json', the needs_tree_render == true of the workspace command
908 // is not executed yet and will be batched with append_layout’s
909 // needs_tree_render after the parser finished. We should check if that is
911 render_con(croot, false);
913 restore_open_placeholder_windows(parent);
915 cmd_output->needs_tree_render = true;
919 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
922 void cmd_workspace(I3_CMD, char *which) {
925 DLOG("which=%s\n", which);
927 if (strcmp(which, "next") == 0)
928 ws = workspace_next();
929 else if (strcmp(which, "prev") == 0)
930 ws = workspace_prev();
931 else if (strcmp(which, "next_on_output") == 0)
932 ws = workspace_next_on_output();
933 else if (strcmp(which, "prev_on_output") == 0)
934 ws = workspace_prev_on_output();
936 ELOG("BUG: called with which=%s\n", which);
943 cmd_output->needs_tree_render = true;
944 // XXX: default reply for now, make this a better reply
949 * Implementation of 'workspace number <name>'
952 void cmd_workspace_number(I3_CMD, char *which) {
953 Con *output, *workspace = NULL;
956 long parsed_num = strtol(which, &endptr, 10);
957 if (parsed_num == LONG_MIN ||
958 parsed_num == LONG_MAX ||
961 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
962 // TODO: better error message
963 yerror("Could not parse number");
968 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
969 GREP_FIRST(workspace, output_get_content(output),
970 child->num == parsed_num);
973 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
975 workspace_show_by_name(which);
976 cmd_output->needs_tree_render = true;
979 if (maybe_back_and_forth(cmd_output, workspace->name))
981 workspace_show(workspace);
983 cmd_output->needs_tree_render = true;
984 // XXX: default reply for now, make this a better reply
989 * Implementation of 'workspace back_and_forth'.
992 void cmd_workspace_back_and_forth(I3_CMD) {
993 workspace_back_and_forth();
995 cmd_output->needs_tree_render = true;
996 // XXX: default reply for now, make this a better reply
1001 * Implementation of 'workspace <name>'
1004 void cmd_workspace_name(I3_CMD, char *name) {
1005 if (strncasecmp(name, "__", strlen("__")) == 0) {
1006 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1011 DLOG("should switch to workspace %s\n", name);
1012 if (maybe_back_and_forth(cmd_output, name))
1014 workspace_show_by_name(name);
1016 cmd_output->needs_tree_render = true;
1017 // XXX: default reply for now, make this a better reply
1022 * Implementation of 'mark <mark>'
1025 void cmd_mark(I3_CMD, char *mark) {
1026 DLOG("Clearing all windows which have that mark first\n");
1029 TAILQ_FOREACH(con, &all_cons, all_cons) {
1030 if (con->mark && strcmp(con->mark, mark) == 0)
1034 DLOG("marking window with str %s\n", mark);
1039 TAILQ_FOREACH(current, &owindows, owindows) {
1040 DLOG("matching: %p / %s\n", current->con, current->con->name);
1041 current->con->mark = sstrdup(mark);
1044 cmd_output->needs_tree_render = true;
1045 // XXX: default reply for now, make this a better reply
1050 * Implementation of 'unmark [mark]'
1053 void cmd_unmark(I3_CMD, char *mark) {
1056 TAILQ_FOREACH(con, &all_cons, all_cons) {
1059 DLOG("removed all window marks");
1062 TAILQ_FOREACH(con, &all_cons, all_cons) {
1063 if (con->mark && strcmp(con->mark, mark) == 0)
1066 DLOG("removed window mark %s\n", mark);
1069 cmd_output->needs_tree_render = true;
1070 // XXX: default reply for now, make this a better reply
1075 * Implementation of 'mode <string>'.
1078 void cmd_mode(I3_CMD, char *mode) {
1079 DLOG("mode=%s\n", mode);
1082 // XXX: default reply for now, make this a better reply
1087 * Implementation of 'move [window|container] [to] output <str>'.
1090 void cmd_move_con_to_output(I3_CMD, char *name) {
1093 DLOG("should move window to output %s\n", name);
1097 /* get the output */
1098 Output *current_output = NULL;
1101 // TODO: fix the handling of criteria
1102 TAILQ_FOREACH(current, &owindows, owindows)
1103 current_output = get_output_of_con(current->con);
1105 assert(current_output != NULL);
1107 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1108 if (strcasecmp(name, "up") == 0)
1109 output = get_output_next_wrap(D_UP, current_output);
1110 else if (strcasecmp(name, "down") == 0)
1111 output = get_output_next_wrap(D_DOWN, current_output);
1112 else if (strcasecmp(name, "left") == 0)
1113 output = get_output_next_wrap(D_LEFT, current_output);
1114 else if (strcasecmp(name, "right") == 0)
1115 output = get_output_next_wrap(D_RIGHT, current_output);
1117 output = get_output_by_name(name);
1120 LOG("No such output found.\n");
1125 /* get visible workspace on output */
1127 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1133 TAILQ_FOREACH(current, &owindows, owindows) {
1134 DLOG("matching: %p / %s\n", current->con, current->con->name);
1135 con_move_to_workspace(current->con, ws, true, false);
1138 cmd_output->needs_tree_render = true;
1139 // XXX: default reply for now, make this a better reply
1144 * Implementation of 'floating enable|disable|toggle'
1147 void cmd_floating(I3_CMD, char *floating_mode) {
1150 DLOG("floating_mode=%s\n", floating_mode);
1154 TAILQ_FOREACH(current, &owindows, owindows) {
1155 DLOG("matching: %p / %s\n", current->con, current->con->name);
1156 if (strcmp(floating_mode, "toggle") == 0) {
1157 DLOG("should toggle mode\n");
1158 toggle_floating_mode(current->con, false);
1160 DLOG("should switch mode to %s\n", floating_mode);
1161 if (strcmp(floating_mode, "enable") == 0) {
1162 floating_enable(current->con, false);
1164 floating_disable(current->con, false);
1169 cmd_output->needs_tree_render = true;
1170 // XXX: default reply for now, make this a better reply
1175 * Implementation of 'move workspace to [output] <str>'.
1178 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1179 DLOG("should move workspace to output %s\n", name);
1184 TAILQ_FOREACH(current, &owindows, owindows) {
1185 Output *current_output = get_output_of_con(current->con);
1186 if (!current_output) {
1187 ELOG("Cannot get current output. This is a bug in i3.\n");
1191 Output *output = get_output_from_string(current_output, name);
1193 ELOG("Could not get output from string \"%s\"\n", name);
1198 Con *content = output_get_content(output->con);
1199 LOG("got output %p with content %p\n", output, content);
1201 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1202 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1204 Con *ws = con_get_workspace(current->con);
1205 LOG("should move workspace %p / %s\n", ws, ws->name);
1206 bool workspace_was_visible = workspace_is_visible(ws);
1208 if (con_num_children(ws->parent) == 1) {
1209 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1211 /* check if we can find a workspace assigned to this output */
1212 bool used_assignment = false;
1213 struct Workspace_Assignment *assignment;
1214 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1215 if (strcmp(assignment->output, current_output->name) != 0)
1218 /* check if this workspace is already attached to the tree */
1219 Con *workspace = NULL, *out;
1220 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1221 GREP_FIRST(workspace, output_get_content(out),
1222 !strcasecmp(child->name, assignment->name));
1223 if (workspace != NULL)
1226 /* so create the workspace referenced to by this assignment */
1227 LOG("Creating workspace from assignment %s.\n", assignment->name);
1228 workspace_get(assignment->name, NULL);
1229 used_assignment = true;
1233 /* if we couldn't create the workspace using an assignment, create
1234 * it on the output */
1235 if (!used_assignment)
1236 create_workspace_on_output(current_output, ws->parent);
1238 /* notify the IPC listeners */
1239 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1241 DLOG("Detaching\n");
1243 /* detach from the old output and attach to the new output */
1244 Con *old_content = ws->parent;
1246 if (workspace_was_visible) {
1247 /* The workspace which we just detached was visible, so focus
1248 * the next one in the focus-stack. */
1249 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1250 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1251 workspace_show(focus_ws);
1253 con_attach(ws, content, false);
1255 /* fix the coordinates of the floating containers */
1257 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1258 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1260 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1261 if (workspace_was_visible) {
1262 /* Focus the moved workspace on the destination output. */
1266 /* NB: We cannot simply work with previously_visible_ws since it might
1267 * have been cleaned up by workspace_show() already, depending on the
1268 * focus order/number of other workspaces on the output.
1269 * Instead, we loop through the available workspaces and only work with
1270 * previously_visible_ws if we still find it. */
1271 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1272 if (ws != previously_visible_ws)
1275 /* Call the on_remove_child callback of the workspace which previously
1276 * was visible on the destination output. Since it is no longer
1277 * visible, it might need to get cleaned up. */
1278 CALL(previously_visible_ws, on_remove_child);
1283 cmd_output->needs_tree_render = true;
1284 // XXX: default reply for now, make this a better reply
1289 * Implementation of 'split v|h|vertical|horizontal'.
1292 void cmd_split(I3_CMD, char *direction) {
1294 /* TODO: use matches */
1295 LOG("splitting in direction %c\n", direction[0]);
1296 if (match_is_empty(current_match))
1297 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1299 TAILQ_FOREACH(current, &owindows, owindows) {
1300 DLOG("matching: %p / %s\n", current->con, current->con->name);
1301 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1305 cmd_output->needs_tree_render = true;
1306 // XXX: default reply for now, make this a better reply
1311 * Implementation of 'kill [window|client]'.
1314 void cmd_kill(I3_CMD, char *kill_mode_str) {
1315 if (kill_mode_str == NULL)
1316 kill_mode_str = "window";
1319 DLOG("kill_mode=%s\n", kill_mode_str);
1322 if (strcmp(kill_mode_str, "window") == 0)
1323 kill_mode = KILL_WINDOW;
1324 else if (strcmp(kill_mode_str, "client") == 0)
1325 kill_mode = KILL_CLIENT;
1327 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1332 /* check if the match is empty, not if the result is empty */
1333 if (match_is_empty(current_match))
1334 tree_close_con(kill_mode);
1336 TAILQ_FOREACH(current, &owindows, owindows) {
1337 DLOG("matching: %p / %s\n", current->con, current->con->name);
1338 tree_close(current->con, kill_mode, false, false);
1342 cmd_output->needs_tree_render = true;
1343 // XXX: default reply for now, make this a better reply
1348 * Implementation of 'exec [--no-startup-id] <command>'.
1351 void cmd_exec(I3_CMD, char *nosn, char *command) {
1352 bool no_startup_id = (nosn != NULL);
1354 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1355 start_application(command, no_startup_id);
1357 // XXX: default reply for now, make this a better reply
1362 * Implementation of 'focus left|right|up|down'.
1365 void cmd_focus_direction(I3_CMD, char *direction) {
1366 DLOG("direction = *%s*\n", direction);
1368 if (strcmp(direction, "left") == 0)
1369 tree_next('p', HORIZ);
1370 else if (strcmp(direction, "right") == 0)
1371 tree_next('n', HORIZ);
1372 else if (strcmp(direction, "up") == 0)
1373 tree_next('p', VERT);
1374 else if (strcmp(direction, "down") == 0)
1375 tree_next('n', VERT);
1377 ELOG("Invalid focus direction (%s)\n", direction);
1382 cmd_output->needs_tree_render = true;
1383 // XXX: default reply for now, make this a better reply
1388 * Implementation of 'focus tiling|floating|mode_toggle'.
1391 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1392 DLOG("window_mode = %s\n", window_mode);
1394 Con *ws = con_get_workspace(focused);
1397 if (strcmp(window_mode, "mode_toggle") == 0) {
1398 current = TAILQ_FIRST(&(ws->focus_head));
1399 if (current != NULL && current->type == CT_FLOATING_CON)
1400 window_mode = "tiling";
1401 else window_mode = "floating";
1403 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1404 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1405 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1408 con_focus(con_descend_focused(current));
1413 cmd_output->needs_tree_render = true;
1414 // XXX: default reply for now, make this a better reply
1419 * Implementation of 'focus parent|child'.
1422 void cmd_focus_level(I3_CMD, char *level) {
1423 DLOG("level = %s\n", level);
1424 bool success = false;
1426 /* Focusing the parent can only be allowed if the newly
1427 * focused container won't escape the fullscreen container. */
1428 if (strcmp(level, "parent") == 0) {
1429 if (focused && focused->parent) {
1430 if (con_fullscreen_permits_focusing(focused->parent))
1431 success = level_up();
1433 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1437 /* Focusing a child should always be allowed. */
1438 else success = level_down();
1440 cmd_output->needs_tree_render = success;
1441 // XXX: default reply for now, make this a better reply
1446 * Implementation of 'focus'.
1449 void cmd_focus(I3_CMD) {
1450 DLOG("current_match = %p\n", current_match);
1452 if (match_is_empty(current_match)) {
1453 ELOG("You have to specify which window/container should be focused.\n");
1454 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1456 yerror("You have to specify which window/container should be focused");
1461 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1464 TAILQ_FOREACH(current, &owindows, owindows) {
1465 Con *ws = con_get_workspace(current->con);
1466 /* If no workspace could be found, this was a dock window.
1467 * Just skip it, you cannot focus dock windows. */
1471 /* Check the fullscreen focus constraints. */
1472 if (!con_fullscreen_permits_focusing(current->con)) {
1473 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1478 /* In case this is a scratchpad window, call scratchpad_show(). */
1479 if (ws == __i3_scratch) {
1480 scratchpad_show(current->con);
1482 /* While for the normal focus case we can change focus multiple
1483 * times and only a single window ends up focused, we could show
1484 * multiple scratchpad windows. So, rather break here. */
1488 /* If the container is not on the current workspace,
1489 * workspace_show() will switch to a different workspace and (if
1490 * enabled) trigger a mouse pointer warp to the currently focused
1491 * container (!) on the target workspace.
1493 * Therefore, before calling workspace_show(), we make sure that
1494 * 'current' will be focused on the workspace. However, we cannot
1495 * just con_focus(current) because then the pointer will not be
1496 * warped at all (the code thinks we are already there).
1498 * So we focus 'current' to make it the currently focused window of
1499 * the target workspace, then revert focus. */
1500 Con *currently_focused = focused;
1501 con_focus(current->con);
1502 con_focus(currently_focused);
1504 /* Now switch to the workspace, then focus */
1506 LOG("focusing %p / %s\n", current->con, current->con->name);
1507 con_focus(current->con);
1512 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1513 "while only exactly one container can be focused at a time.\n", count);
1515 cmd_output->needs_tree_render = true;
1516 // XXX: default reply for now, make this a better reply
1521 * Implementation of 'fullscreen [global]'.
1524 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1525 if (fullscreen_mode == NULL)
1526 fullscreen_mode = "output";
1527 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1532 TAILQ_FOREACH(current, &owindows, owindows) {
1533 DLOG("matching: %p / %s\n", current->con, current->con->name);
1534 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1537 cmd_output->needs_tree_render = true;
1538 // XXX: default reply for now, make this a better reply
1543 * Implementation of 'move <direction> [<pixels> [px]]'.
1546 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1547 // 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
1548 int px = atoi(move_px);
1550 /* TODO: make 'move' work with criteria. */
1551 DLOG("moving in direction %s, px %s\n", direction, move_px);
1552 if (con_is_floating(focused)) {
1553 DLOG("floating move with %d pixels\n", px);
1554 Rect newrect = focused->parent->rect;
1555 if (strcmp(direction, "left") == 0) {
1557 } else if (strcmp(direction, "right") == 0) {
1559 } else if (strcmp(direction, "up") == 0) {
1561 } else if (strcmp(direction, "down") == 0) {
1564 floating_reposition(focused->parent, newrect);
1566 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1567 (strcmp(direction, "left") == 0 ? D_LEFT :
1568 (strcmp(direction, "up") == 0 ? D_UP :
1570 cmd_output->needs_tree_render = true;
1573 // XXX: default reply for now, make this a better reply
1578 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1581 void cmd_layout(I3_CMD, char *layout_str) {
1582 if (strcmp(layout_str, "stacking") == 0)
1583 layout_str = "stacked";
1586 /* default is a special case which will be handled in con_set_layout(). */
1587 if (strcmp(layout_str, "default") == 0)
1589 else if (strcmp(layout_str, "stacked") == 0)
1591 else if (strcmp(layout_str, "tabbed") == 0)
1593 else if (strcmp(layout_str, "splitv") == 0)
1595 else if (strcmp(layout_str, "splith") == 0)
1598 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1602 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1604 /* check if the match is empty, not if the result is empty */
1605 if (match_is_empty(current_match))
1606 con_set_layout(focused, layout);
1608 TAILQ_FOREACH(current, &owindows, owindows) {
1609 DLOG("matching: %p / %s\n", current->con, current->con->name);
1610 con_set_layout(current->con, layout);
1614 cmd_output->needs_tree_render = true;
1615 // XXX: default reply for now, make this a better reply
1620 * Implementation of 'layout toggle [all|split]'.
1623 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1626 if (toggle_mode == NULL)
1627 toggle_mode = "default";
1629 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1631 /* check if the match is empty, not if the result is empty */
1632 if (match_is_empty(current_match))
1633 con_toggle_layout(focused, toggle_mode);
1635 TAILQ_FOREACH(current, &owindows, owindows) {
1636 DLOG("matching: %p / %s\n", current->con, current->con->name);
1637 con_toggle_layout(current->con, toggle_mode);
1641 cmd_output->needs_tree_render = true;
1642 // XXX: default reply for now, make this a better reply
1647 * Implementation of 'exit'.
1650 void cmd_exit(I3_CMD) {
1651 LOG("Exiting due to user command.\n");
1653 unlink(config.ipc_socket_path);
1654 xcb_disconnect(conn);
1661 * Implementation of 'reload'.
1664 void cmd_reload(I3_CMD) {
1666 kill_nagbar(&config_error_nagbar_pid, false);
1667 kill_nagbar(&command_error_nagbar_pid, false);
1668 load_configuration(conn, NULL, true);
1670 /* Send an IPC event just in case the ws names have changed */
1671 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1672 /* Send an update event for the barconfig just in case it has changed */
1675 // XXX: default reply for now, make this a better reply
1680 * Implementation of 'restart'.
1683 void cmd_restart(I3_CMD) {
1684 LOG("restarting i3\n");
1686 /* We need to call this manually since atexit handlers don’t get called
1688 purge_zerobyte_logfile();
1689 /* The unlink call is intentionally after the purge_zerobyte_logfile() so
1690 * that the latter does not remove the directory yet. We need to store the
1691 * restart layout state in there. */
1692 unlink(config.ipc_socket_path);
1695 // XXX: default reply for now, make this a better reply
1700 * Implementation of 'open'.
1703 void cmd_open(I3_CMD) {
1704 LOG("opening new container\n");
1705 Con *con = tree_open_con(NULL, NULL);
1706 con->layout = L_SPLITH;
1713 y(integer, (long int)con);
1716 cmd_output->needs_tree_render = true;
1720 * Implementation of 'focus output <output>'.
1723 void cmd_focus_output(I3_CMD, char *name) {
1726 DLOG("name = %s\n", name);
1730 /* get the output */
1731 Output *current_output = NULL;
1734 TAILQ_FOREACH(current, &owindows, owindows)
1735 current_output = get_output_of_con(current->con);
1736 assert(current_output != NULL);
1738 output = get_output_from_string(current_output, name);
1741 LOG("No such output found.\n");
1746 /* get visible workspace on output */
1748 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1756 cmd_output->needs_tree_render = true;
1757 // XXX: default reply for now, make this a better reply
1762 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1765 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1770 if (!con_is_floating(focused)) {
1771 ELOG("Cannot change position. The window/container is not floating\n");
1772 yerror("Cannot change position. The window/container is not floating.");
1776 if (strcmp(method, "absolute") == 0) {
1777 focused->parent->rect.x = x;
1778 focused->parent->rect.y = y;
1780 DLOG("moving to absolute position %d %d\n", x, y);
1781 floating_maybe_reassign_ws(focused->parent);
1782 cmd_output->needs_tree_render = true;
1785 if (strcmp(method, "position") == 0) {
1786 Rect newrect = focused->parent->rect;
1788 DLOG("moving to position %d %d\n", x, y);
1792 floating_reposition(focused->parent, newrect);
1795 // XXX: default reply for now, make this a better reply
1800 * Implementation of 'move [window|container] [to] [absolute] position center
1803 void cmd_move_window_to_center(I3_CMD, char *method) {
1805 if (!con_is_floating(focused)) {
1806 ELOG("Cannot change position. The window/container is not floating\n");
1807 yerror("Cannot change position. The window/container is not floating.");
1811 if (strcmp(method, "absolute") == 0) {
1812 Rect *rect = &focused->parent->rect;
1814 DLOG("moving to absolute center\n");
1815 rect->x = croot->rect.width/2 - rect->width/2;
1816 rect->y = croot->rect.height/2 - rect->height/2;
1818 floating_maybe_reassign_ws(focused->parent);
1819 cmd_output->needs_tree_render = true;
1822 if (strcmp(method, "position") == 0) {
1823 Rect *wsrect = &con_get_workspace(focused)->rect;
1824 Rect newrect = focused->parent->rect;
1826 DLOG("moving to center\n");
1827 newrect.x = wsrect->width/2 - newrect.width/2;
1828 newrect.y = wsrect->height/2 - newrect.height/2;
1830 floating_reposition(focused->parent, newrect);
1833 // XXX: default reply for now, make this a better reply
1838 * Implementation of 'move scratchpad'.
1841 void cmd_move_scratchpad(I3_CMD) {
1842 DLOG("should move window to scratchpad\n");
1847 TAILQ_FOREACH(current, &owindows, owindows) {
1848 DLOG("matching: %p / %s\n", current->con, current->con->name);
1849 scratchpad_move(current->con);
1852 cmd_output->needs_tree_render = true;
1853 // XXX: default reply for now, make this a better reply
1858 * Implementation of 'scratchpad show'.
1861 void cmd_scratchpad_show(I3_CMD) {
1862 DLOG("should show scratchpad window\n");
1865 if (match_is_empty(current_match)) {
1866 scratchpad_show(NULL);
1868 TAILQ_FOREACH(current, &owindows, owindows) {
1869 DLOG("matching: %p / %s\n", current->con, current->con->name);
1870 scratchpad_show(current->con);
1874 cmd_output->needs_tree_render = true;
1875 // XXX: default reply for now, make this a better reply
1880 * Implementation of 'rename workspace [<name>] to <name>'
1883 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1884 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1885 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1890 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1892 LOG("Renaming current workspace to \"%s\"\n", new_name);
1895 Con *output, *workspace = NULL;
1897 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1898 GREP_FIRST(workspace, output_get_content(output),
1899 !strcasecmp(child->name, old_name));
1901 workspace = con_get_workspace(focused);
1905 // TODO: we should include the old workspace name here and use yajl for
1906 // generating the reply.
1907 // TODO: better error message
1908 yerror("Old workspace not found");
1912 Con *check_dest = NULL;
1913 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1914 GREP_FIRST(check_dest, output_get_content(output),
1915 !strcasecmp(child->name, new_name));
1917 if (check_dest != NULL) {
1918 // TODO: we should include the new workspace name here and use yajl for
1919 // generating the reply.
1920 // TODO: better error message
1921 yerror("New workspace already exists");
1925 /* Change the name and try to parse it as a number. */
1926 FREE(workspace->name);
1927 workspace->name = sstrdup(new_name);
1928 char *endptr = NULL;
1929 long parsed_num = strtol(new_name, &endptr, 10);
1930 if (parsed_num == LONG_MIN ||
1931 parsed_num == LONG_MAX ||
1934 workspace->num = -1;
1935 else workspace->num = parsed_num;
1936 LOG("num = %d\n", workspace->num);
1938 /* By re-attaching, the sort order will be correct afterwards. */
1939 Con *previously_focused = focused;
1940 Con *parent = workspace->parent;
1941 con_detach(workspace);
1942 con_attach(workspace, parent, false);
1943 /* Restore the previous focus since con_attach messes with the focus. */
1944 con_focus(previously_focused);
1946 cmd_output->needs_tree_render = true;
1949 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1953 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1956 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1958 bool toggle = false;
1959 if (strcmp(bar_mode, "dock") == 0)
1961 else if (strcmp(bar_mode, "hide") == 0)
1963 else if (strcmp(bar_mode, "invisible") == 0)
1965 else if (strcmp(bar_mode, "toggle") == 0)
1968 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1972 bool changed_sth = false;
1973 Barconfig *current = NULL;
1974 TAILQ_FOREACH(current, &barconfigs, configs) {
1975 if (bar_id && strcmp(current->id, bar_id) != 0)
1979 mode = (current->mode + 1) % 2;
1981 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1982 current->mode = mode;
1989 if (bar_id && !changed_sth) {
1990 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1998 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2001 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2002 int hidden_state = S_SHOW;
2003 bool toggle = false;
2004 if (strcmp(bar_hidden_state, "hide") == 0)
2005 hidden_state = S_HIDE;
2006 else if (strcmp(bar_hidden_state, "show") == 0)
2007 hidden_state = S_SHOW;
2008 else if (strcmp(bar_hidden_state, "toggle") == 0)
2011 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2015 bool changed_sth = false;
2016 Barconfig *current = NULL;
2017 TAILQ_FOREACH(current, &barconfigs, configs) {
2018 if (bar_id && strcmp(current->id, bar_id) != 0)
2022 hidden_state = (current->hidden_state + 1) % 2;
2024 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2025 current->hidden_state = hidden_state;
2032 if (bar_id && !changed_sth) {
2033 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2041 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2044 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2046 if (strcmp(bar_type, "mode") == 0)
2047 ret = cmd_bar_mode(bar_value, bar_id);
2048 else if (strcmp(bar_type, "hidden_state") == 0)
2049 ret = cmd_bar_hidden_state(bar_value, bar_id);
2051 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2063 * Implementation of 'shmlog <size>|toggle|on|off'
2066 void cmd_shmlog(I3_CMD, char *argument) {
2067 if (!strcmp(argument,"toggle"))
2068 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2069 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2070 else if (!strcmp(argument, "on"))
2071 shmlog_size = default_shmlog_size;
2072 else if (!strcmp(argument, "off"))
2075 /* If shm logging now, restart logging with the new size. */
2076 if (shmlog_size > 0) {
2078 LOG("Restarting shm logging...\n");
2081 shmlog_size = atoi(argument);
2082 /* Make a weakly attempt at ensuring the argument is valid. */
2083 if (shmlog_size <= 0)
2084 shmlog_size = default_shmlog_size;
2086 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2088 update_shmlog_atom();
2089 // XXX: default reply for now, make this a better reply
2094 * Implementation of 'debuglog toggle|on|off'
2097 void cmd_debuglog(I3_CMD, char *argument) {
2098 bool logging = get_debug_logging();
2099 if (!strcmp(argument,"toggle")) {
2100 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2101 set_debug_logging(!logging);
2102 } else if (!strcmp(argument, "on") && !logging) {
2103 LOG("Enabling debug logging\n");
2104 set_debug_logging(true);
2105 } else if (!strcmp(argument, "off") && logging) {
2106 LOG("Disabling debug logging\n");
2107 set_debug_logging(false);
2109 // XXX: default reply for now, make this a better reply