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 the output containing the given container.
68 static Output *get_output_of_con(Con *con) {
69 Con *output_con = con_get_output(con);
70 Output *output = get_output_by_name(output_con->name);
71 assert(output != NULL);
77 * Checks whether we switched to a new workspace and returns false in that case,
78 * signaling that further workspace switching should be done by the calling function
79 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
80 * and return true, signaling that no further workspace switching should occur in the calling function.
83 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, char *name) {
84 Con *ws = con_get_workspace(focused);
86 /* If we switched to a different workspace, do nothing */
87 if (strcmp(ws->name, name) != 0)
90 DLOG("This workspace is already focused.\n");
91 if (config.workspace_auto_back_and_forth) {
92 workspace_back_and_forth();
93 cmd_output->needs_tree_render = true;
99 * Return the passed workspace unless it is the current one and auto back and
100 * forth is enabled, in which case the back_and_forth workspace is returned.
102 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
105 if (!config.workspace_auto_back_and_forth)
108 current = con_get_workspace(focused);
110 if (current == workspace) {
111 baf = workspace_back_and_forth_get();
113 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
121 // This code is commented out because we might recycle it for popping up error
122 // messages on parser errors.
124 static pid_t migration_pid = -1;
127 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
128 * it exited (or could not be started, depending on the exit code).
131 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
132 ev_child_stop(EV_A_ watcher);
133 if (!WIFEXITED(watcher->rstatus)) {
134 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
138 int exitcode = WEXITSTATUS(watcher->rstatus);
139 printf("i3-nagbar process exited with status %d\n", exitcode);
141 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
147 /* We need ev >= 4 for the following code. Since it is not *that* important (it
148 * only makes sure that there are no i3-nagbar instances left behind) we still
149 * support old systems with libev 3. */
150 #if EV_VERSION_MAJOR >= 4
152 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
153 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
156 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
157 if (migration_pid != -1) {
158 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
159 kill(migration_pid, SIGKILL);
164 void cmd_MIGRATION_start_nagbar(void) {
165 if (migration_pid != -1) {
166 fprintf(stderr, "i3-nagbar already running.\n");
169 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
170 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
171 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
172 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
173 migration_pid = fork();
174 if (migration_pid == -1) {
175 warn("Could not fork()");
180 if (migration_pid == 0) {
182 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
184 NULL, /* will be replaced by the executable path */
188 "You found a parsing error. Please, please, please, report it!",
194 exec_i3_utility("i3-nagbar", argv);
198 /* install a child watcher */
199 ev_child *child = smalloc(sizeof(ev_child));
200 ev_child_init(child, &nagbar_exited, migration_pid, 0);
201 ev_child_start(main_loop, child);
203 /* We need ev >= 4 for the following code. Since it is not *that* important (it
204 * only makes sure that there are no i3-nagbar instances left behind) we still
205 * support old systems with libev 3. */
206 #if EV_VERSION_MAJOR >= 4
207 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
209 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
210 ev_cleanup_init(cleanup, nagbar_cleanup);
211 ev_cleanup_start(main_loop, cleanup);
217 /*******************************************************************************
218 * Criteria functions.
219 ******************************************************************************/
222 * Helper data structure for an operation window (window on which the operation
223 * will be performed). Used to build the TAILQ owindows.
226 typedef struct owindow {
228 TAILQ_ENTRY(owindow) owindows;
231 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
233 static owindows_head owindows;
236 * Initializes the specified 'Match' data structure and the initial state of
237 * commands.c for matching target windows of a command.
240 void cmd_criteria_init(I3_CMD) {
244 DLOG("Initializing criteria, current_match = %p\n", current_match);
245 match_free(current_match);
246 match_init(current_match);
247 while (!TAILQ_EMPTY(&owindows)) {
248 ow = TAILQ_FIRST(&owindows);
249 TAILQ_REMOVE(&owindows, ow, owindows);
252 TAILQ_INIT(&owindows);
254 TAILQ_FOREACH(con, &all_cons, all_cons) {
255 ow = smalloc(sizeof(owindow));
257 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
262 * A match specification just finished (the closing square bracket was found),
263 * so we filter the list of owindows.
266 void cmd_criteria_match_windows(I3_CMD) {
267 owindow *next, *current;
269 DLOG("match specification finished, matching...\n");
270 /* copy the old list head to iterate through it and start with a fresh
271 * list which will contain only matching windows */
272 struct owindows_head old = owindows;
273 TAILQ_INIT(&owindows);
274 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
275 /* make a copy of the next pointer and advance the pointer to the
276 * next element as we are going to invalidate the element’s
277 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
279 next = TAILQ_NEXT(next, owindows);
281 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
282 if (current_match->con_id != NULL) {
283 if (current_match->con_id == current->con) {
284 DLOG("matches container!\n");
285 TAILQ_INSERT_TAIL(&owindows, current, owindows);
287 DLOG("doesnt match\n");
290 } else if (current_match->mark != NULL && current->con->mark != NULL &&
291 regex_matches(current_match->mark, current->con->mark)) {
292 DLOG("match by mark\n");
293 TAILQ_INSERT_TAIL(&owindows, current, owindows);
295 if (current->con->window && match_matches_window(current_match, current->con->window)) {
296 DLOG("matches window!\n");
297 TAILQ_INSERT_TAIL(&owindows, current, owindows);
299 DLOG("doesnt match\n");
305 TAILQ_FOREACH(current, &owindows, owindows) {
306 DLOG("matching: %p / %s\n", current->con, current->con->name);
311 * Interprets a ctype=cvalue pair and adds it to the current match
315 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
316 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
318 if (strcmp(ctype, "class") == 0) {
319 current_match->class = regex_new(cvalue);
323 if (strcmp(ctype, "instance") == 0) {
324 current_match->instance = regex_new(cvalue);
328 if (strcmp(ctype, "window_role") == 0) {
329 current_match->window_role = regex_new(cvalue);
333 if (strcmp(ctype, "con_id") == 0) {
335 long parsed = strtol(cvalue, &end, 10);
336 if (parsed == LONG_MIN ||
337 parsed == LONG_MAX ||
339 (end && *end != '\0')) {
340 ELOG("Could not parse con id \"%s\"\n", cvalue);
342 current_match->con_id = (Con *)parsed;
343 DLOG("id as int = %p\n", current_match->con_id);
348 if (strcmp(ctype, "id") == 0) {
350 long parsed = strtol(cvalue, &end, 10);
351 if (parsed == LONG_MIN ||
352 parsed == LONG_MAX ||
354 (end && *end != '\0')) {
355 ELOG("Could not parse window id \"%s\"\n", cvalue);
357 current_match->id = parsed;
358 DLOG("window id as int = %d\n", current_match->id);
363 if (strcmp(ctype, "con_mark") == 0) {
364 current_match->mark = regex_new(cvalue);
368 if (strcmp(ctype, "title") == 0) {
369 current_match->title = regex_new(cvalue);
373 if (strcmp(ctype, "urgent") == 0) {
374 if (strcasecmp(cvalue, "latest") == 0 ||
375 strcasecmp(cvalue, "newest") == 0 ||
376 strcasecmp(cvalue, "recent") == 0 ||
377 strcasecmp(cvalue, "last") == 0) {
378 current_match->urgent = U_LATEST;
379 } else if (strcasecmp(cvalue, "oldest") == 0 ||
380 strcasecmp(cvalue, "first") == 0) {
381 current_match->urgent = U_OLDEST;
386 ELOG("Unknown criterion: %s\n", ctype);
390 * Implementation of 'move [window|container] [to] workspace
391 * next|prev|next_on_output|prev_on_output|current'.
394 void cmd_move_con_to_workspace(I3_CMD, char *which) {
397 DLOG("which=%s\n", which);
399 /* We have nothing to move:
400 * when criteria was specified but didn't match any window or
401 * when criteria wasn't specified and we don't have any window focused. */
402 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
403 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
404 !con_has_children(focused))) {
411 /* get the workspace */
413 if (strcmp(which, "next") == 0)
414 ws = workspace_next();
415 else if (strcmp(which, "prev") == 0)
416 ws = workspace_prev();
417 else if (strcmp(which, "next_on_output") == 0)
418 ws = workspace_next_on_output();
419 else if (strcmp(which, "prev_on_output") == 0)
420 ws = workspace_prev_on_output();
421 else if (strcmp(which, "current") == 0)
422 ws = con_get_workspace(focused);
424 ELOG("BUG: called with which=%s\n", which);
429 TAILQ_FOREACH(current, &owindows, owindows) {
430 DLOG("matching: %p / %s\n", current->con, current->con->name);
431 con_move_to_workspace(current->con, ws, true, false);
434 cmd_output->needs_tree_render = true;
435 // XXX: default reply for now, make this a better reply
440 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
443 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
447 ws = workspace_back_and_forth_get();
450 yerror("No workspace was previously active.");
456 TAILQ_FOREACH(current, &owindows, owindows) {
457 DLOG("matching: %p / %s\n", current->con, current->con->name);
458 con_move_to_workspace(current->con, ws, true, false);
461 cmd_output->needs_tree_render = true;
462 // XXX: default reply for now, make this a better reply
467 * Implementation of 'move [window|container] [to] workspace <name>'.
470 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
471 if (strncasecmp(name, "__", strlen("__")) == 0) {
472 LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
479 /* We have nothing to move:
480 * when criteria was specified but didn't match any window or
481 * when criteria wasn't specified and we don't have any window focused. */
482 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
483 ELOG("No windows match your criteria, cannot move.\n");
486 } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
487 !con_has_children(focused)) {
492 LOG("should move window to workspace %s\n", name);
493 /* get the workspace */
497 /* first look for a workspace with this name */
498 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
499 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
502 /* if the name is plain digits, we interpret this as a "workspace number"
504 if (!ws && name_is_digits(name)) {
505 long parsed_num = ws_name_to_number(name);
506 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
507 GREP_FIRST(ws, output_get_content(output),
508 child->num == parsed_num);
512 /* if no workspace was found, make a new one */
514 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;
551 long parsed_num = ws_name_to_number(which);
553 if (parsed_num == -1) {
554 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
555 // TODO: better error message
556 yerror("Could not parse number");
560 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
561 GREP_FIRST(workspace, output_get_content(output),
562 child->num == parsed_num);
565 workspace = workspace_get(which, NULL);
568 workspace = maybe_auto_back_and_forth_workspace(workspace);
572 TAILQ_FOREACH(current, &owindows, owindows) {
573 DLOG("matching: %p / %s\n", current->con, current->con->name);
574 con_move_to_workspace(current->con, workspace, true, false);
577 cmd_output->needs_tree_render = true;
578 // XXX: default reply for now, make this a better reply
582 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
583 LOG("floating resize\n");
584 Rect old_rect = floating_con->rect;
585 Con *focused_con = con_descend_focused(floating_con);
587 /* ensure that resize will take place even if pixel increment is smaller than
588 * height increment or width increment.
590 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
591 strcmp(direction, "height") == 0) {
593 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
595 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
596 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
598 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
600 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
603 if (strcmp(direction, "up") == 0) {
604 floating_con->rect.height += px;
605 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
606 floating_con->rect.height += px;
607 } else if (strcmp(direction, "left") == 0) {
608 floating_con->rect.width += px;
610 floating_con->rect.width += px;
613 floating_check_size(floating_con);
615 /* Did we actually resize anything or did the size constraints prevent us?
616 * If we could not resize, exit now to not move the window. */
617 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
620 if (strcmp(direction, "up") == 0) {
621 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
622 } else if (strcmp(direction, "left") == 0) {
623 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
626 /* If this is a scratchpad window, don't auto center it from now on. */
627 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
628 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
631 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
632 LOG("tiling resize\n");
634 Con *first = current;
635 direction_t search_direction;
636 if (!strcmp(direction, "left"))
637 search_direction = D_LEFT;
638 else if (!strcmp(direction, "right"))
639 search_direction = D_RIGHT;
640 else if (!strcmp(direction, "up"))
641 search_direction = D_UP;
643 search_direction = D_DOWN;
645 bool res = resize_find_tiling_participants(&first, &second, search_direction);
647 LOG("No second container in this direction found.\n");
652 /* get the default percentage */
653 int children = con_num_children(first->parent);
654 LOG("ins. %d children\n", children);
655 double percentage = 1.0 / children;
656 LOG("default percentage = %f\n", percentage);
659 LOG("second->percent = %f\n", second->percent);
660 LOG("first->percent before = %f\n", first->percent);
661 if (first->percent == 0.0)
662 first->percent = percentage;
663 if (second->percent == 0.0)
664 second->percent = percentage;
665 double new_first_percent = first->percent + ((double)ppt / 100.0);
666 double new_second_percent = second->percent - ((double)ppt / 100.0);
667 LOG("new_first_percent = %f\n", new_first_percent);
668 LOG("new_second_percent = %f\n", new_second_percent);
669 /* Ensure that the new percentages are positive and greater than
670 * 0.05 to have a reasonable minimum size. */
671 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
672 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
673 first->percent += ((double)ppt / 100.0);
674 second->percent -= ((double)ppt / 100.0);
675 LOG("first->percent after = %f\n", first->percent);
676 LOG("second->percent after = %f\n", second->percent);
678 LOG("Not resizing, already at minimum size\n");
684 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
685 LOG("width/height resize\n");
686 /* get the appropriate current container (skip stacked/tabbed cons) */
687 while (current->parent->layout == L_STACKED ||
688 current->parent->layout == L_TABBED)
689 current = current->parent;
691 /* Then further go up until we find one with the matching orientation. */
692 orientation_t search_orientation =
693 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
695 while (current->type != CT_WORKSPACE &&
696 current->type != CT_FLOATING_CON &&
697 con_orientation(current->parent) != search_orientation)
698 current = current->parent;
700 /* get the default percentage */
701 int children = con_num_children(current->parent);
702 LOG("ins. %d children\n", children);
703 double percentage = 1.0 / children;
704 LOG("default percentage = %f\n", percentage);
706 orientation_t orientation = con_orientation(current->parent);
708 if ((orientation == HORIZ &&
709 strcmp(direction, "height") == 0) ||
710 (orientation == VERT &&
711 strcmp(direction, "width") == 0)) {
712 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
713 (orientation == HORIZ ? "horizontal" : "vertical"));
719 LOG("This is the only container, cannot resize.\n");
724 /* Ensure all the other children have a percentage set. */
726 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
727 LOG("child->percent = %f (child %p)\n", child->percent, child);
728 if (child->percent == 0.0)
729 child->percent = percentage;
732 double new_current_percent = current->percent + ((double)ppt / 100.0);
733 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
734 LOG("new_current_percent = %f\n", new_current_percent);
735 LOG("subtract_percent = %f\n", subtract_percent);
736 /* Ensure that the new percentages are positive and greater than
737 * 0.05 to have a reasonable minimum size. */
738 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
739 if (child == current)
741 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
742 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
747 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
748 LOG("Not resizing, already at minimum size\n");
753 current->percent += ((double)ppt / 100.0);
754 LOG("current->percent after = %f\n", current->percent);
756 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
757 if (child == current)
759 child->percent -= subtract_percent;
760 LOG("child->percent after (%p) = %f\n", child, child->percent);
767 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
770 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
771 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
772 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
773 // 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
774 int px = atoi(resize_px);
775 int ppt = atoi(resize_ppt);
776 if (strcmp(way, "shrink") == 0) {
784 TAILQ_FOREACH(current, &owindows, owindows) {
785 /* Don't handle dock windows (issue #1201) */
786 if (current->con->window && current->con->window->dock) {
787 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
792 if ((floating_con = con_inside_floating(current->con))) {
793 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
795 if (strcmp(direction, "width") == 0 ||
796 strcmp(direction, "height") == 0) {
797 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
800 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
806 cmd_output->needs_tree_render = true;
807 // XXX: default reply for now, make this a better reply
812 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
815 void cmd_border(I3_CMD, char *border_style_str, char *border_width) {
816 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
821 TAILQ_FOREACH(current, &owindows, owindows) {
822 DLOG("matching: %p / %s\n", current->con, current->con->name);
823 int border_style = current->con->border_style;
825 int tmp_border_width = -1;
826 tmp_border_width = strtol(border_width, &end, 10);
827 if (end == border_width) {
828 /* no valid digits found */
829 tmp_border_width = -1;
831 if (strcmp(border_style_str, "toggle") == 0) {
834 if (border_style == BS_NORMAL)
835 tmp_border_width = 2;
836 else if (border_style == BS_NONE)
837 tmp_border_width = 0;
838 else if (border_style == BS_PIXEL)
839 tmp_border_width = 1;
841 if (strcmp(border_style_str, "normal") == 0)
842 border_style = BS_NORMAL;
843 else if (strcmp(border_style_str, "pixel") == 0)
844 border_style = BS_PIXEL;
845 else if (strcmp(border_style_str, "1pixel") == 0) {
846 border_style = BS_PIXEL;
847 tmp_border_width = 1;
848 } else if (strcmp(border_style_str, "none") == 0)
849 border_style = BS_NONE;
851 ELOG("BUG: called with border_style=%s\n", border_style_str);
856 con_set_border_style(current->con, border_style, tmp_border_width);
859 cmd_output->needs_tree_render = true;
860 // XXX: default reply for now, make this a better reply
865 * Implementation of 'nop <comment>'.
868 void cmd_nop(I3_CMD, char *comment) {
869 LOG("-------------------------------------------------\n");
870 LOG(" NOP: %s\n", comment);
871 LOG("-------------------------------------------------\n");
875 * Implementation of 'append_layout <path>'.
878 void cmd_append_layout(I3_CMD, char *path) {
879 LOG("Appending layout \"%s\"\n", path);
881 json_content_t content = json_determine_content(path);
882 LOG("JSON content = %d\n", content);
883 if (content == JSON_CONTENT_UNKNOWN) {
884 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
889 Con *parent = focused;
890 if (content == JSON_CONTENT_WORKSPACE) {
891 parent = output_get_content(con_get_output(parent));
893 /* We need to append the layout to a split container, since a leaf
894 * container must not have any children (by definition).
895 * Note that we explicitly check for workspaces, since they are okay for
896 * this purpose, but con_accepts_window() returns false for workspaces. */
897 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
898 parent = parent->parent;
900 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
901 char *errormsg = NULL;
902 tree_append_json(parent, path, &errormsg);
903 if (errormsg != NULL) {
906 /* Note that we continue executing since tree_append_json() has
907 * side-effects — user-provided layouts can be partly valid, partly
908 * invalid, leading to half of the placeholder containers being
914 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
915 // false); should be enough, but when sending 'workspace 4; append_layout
916 // /tmp/foo.json', the needs_tree_render == true of the workspace command
917 // is not executed yet and will be batched with append_layout’s
918 // needs_tree_render after the parser finished. We should check if that is
920 render_con(croot, false);
922 restore_open_placeholder_windows(parent);
924 if (content == JSON_CONTENT_WORKSPACE)
925 ipc_send_workspace_event("restored", parent, NULL);
927 cmd_output->needs_tree_render = true;
931 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
934 void cmd_workspace(I3_CMD, char *which) {
937 DLOG("which=%s\n", which);
939 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
940 LOG("Cannot switch workspace while in global fullscreen\n");
945 if (strcmp(which, "next") == 0)
946 ws = workspace_next();
947 else if (strcmp(which, "prev") == 0)
948 ws = workspace_prev();
949 else if (strcmp(which, "next_on_output") == 0)
950 ws = workspace_next_on_output();
951 else if (strcmp(which, "prev_on_output") == 0)
952 ws = workspace_prev_on_output();
954 ELOG("BUG: called with which=%s\n", which);
961 cmd_output->needs_tree_render = true;
962 // XXX: default reply for now, make this a better reply
967 * Implementation of 'workspace number <name>'
970 void cmd_workspace_number(I3_CMD, char *which) {
971 Con *output, *workspace = NULL;
973 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
974 LOG("Cannot switch workspace while in global fullscreen\n");
979 long parsed_num = ws_name_to_number(which);
981 if (parsed_num == -1) {
982 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
983 // TODO: better error message
984 yerror("Could not parse number");
988 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
989 GREP_FIRST(workspace, output_get_content(output),
990 child->num == parsed_num);
993 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
995 workspace_show_by_name(which);
996 cmd_output->needs_tree_render = true;
999 if (maybe_back_and_forth(cmd_output, workspace->name))
1001 workspace_show(workspace);
1003 cmd_output->needs_tree_render = true;
1004 // XXX: default reply for now, make this a better reply
1009 * Implementation of 'workspace back_and_forth'.
1012 void cmd_workspace_back_and_forth(I3_CMD) {
1013 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1014 LOG("Cannot switch workspace while in global fullscreen\n");
1019 workspace_back_and_forth();
1021 cmd_output->needs_tree_render = true;
1022 // XXX: default reply for now, make this a better reply
1027 * Implementation of 'workspace <name>'
1030 void cmd_workspace_name(I3_CMD, char *name) {
1031 if (strncasecmp(name, "__", strlen("__")) == 0) {
1032 LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
1037 if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
1038 LOG("Cannot switch workspace while in global fullscreen\n");
1043 DLOG("should switch to workspace %s\n", name);
1044 if (maybe_back_and_forth(cmd_output, name))
1050 /* first look for a workspace with this name */
1051 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1052 GREP_FIRST(ws, output_get_content(output), !strcasecmp(child->name, name));
1055 /* if the name is only digits, we interpret this as a "workspace number"
1057 if (!ws && name_is_digits(name)) {
1058 long parsed_num = ws_name_to_number(name);
1059 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
1060 GREP_FIRST(ws, output_get_content(output),
1061 child->num == parsed_num);
1065 /* if no workspace was found, make a new one */
1067 ws = workspace_get(name, NULL);
1071 cmd_output->needs_tree_render = true;
1072 // XXX: default reply for now, make this a better reply
1077 * Implementation of 'mark <mark>'
1080 void cmd_mark(I3_CMD, char *mark) {
1081 DLOG("Clearing all windows which have that mark first\n");
1084 TAILQ_FOREACH(con, &all_cons, all_cons) {
1085 if (con->mark && strcmp(con->mark, mark) == 0)
1089 DLOG("marking window with str %s\n", mark);
1094 TAILQ_FOREACH(current, &owindows, owindows) {
1095 DLOG("matching: %p / %s\n", current->con, current->con->name);
1096 current->con->mark = sstrdup(mark);
1099 cmd_output->needs_tree_render = true;
1100 // XXX: default reply for now, make this a better reply
1105 * Implementation of 'unmark [mark]'
1108 void cmd_unmark(I3_CMD, char *mark) {
1111 TAILQ_FOREACH(con, &all_cons, all_cons) {
1114 DLOG("removed all window marks");
1117 TAILQ_FOREACH(con, &all_cons, all_cons) {
1118 if (con->mark && strcmp(con->mark, mark) == 0)
1121 DLOG("removed window mark %s\n", mark);
1124 cmd_output->needs_tree_render = true;
1125 // XXX: default reply for now, make this a better reply
1130 * Implementation of 'mode <string>'.
1133 void cmd_mode(I3_CMD, char *mode) {
1134 DLOG("mode=%s\n", mode);
1137 // XXX: default reply for now, make this a better reply
1142 * Implementation of 'move [window|container] [to] output <str>'.
1145 void cmd_move_con_to_output(I3_CMD, char *name) {
1148 DLOG("should move window to output %s\n", name);
1152 /* get the output */
1153 Output *current_output = NULL;
1156 // TODO: fix the handling of criteria
1157 TAILQ_FOREACH(current, &owindows, owindows)
1158 current_output = get_output_of_con(current->con);
1160 assert(current_output != NULL);
1162 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1163 if (strcasecmp(name, "up") == 0)
1164 output = get_output_next_wrap(D_UP, current_output);
1165 else if (strcasecmp(name, "down") == 0)
1166 output = get_output_next_wrap(D_DOWN, current_output);
1167 else if (strcasecmp(name, "left") == 0)
1168 output = get_output_next_wrap(D_LEFT, current_output);
1169 else if (strcasecmp(name, "right") == 0)
1170 output = get_output_next_wrap(D_RIGHT, current_output);
1172 output = get_output_by_name(name);
1175 LOG("No such output found.\n");
1180 /* get visible workspace on output */
1182 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1188 TAILQ_FOREACH(current, &owindows, owindows) {
1189 DLOG("matching: %p / %s\n", current->con, current->con->name);
1190 con_move_to_workspace(current->con, ws, true, false);
1193 cmd_output->needs_tree_render = true;
1194 // XXX: default reply for now, make this a better reply
1199 * Implementation of 'floating enable|disable|toggle'
1202 void cmd_floating(I3_CMD, char *floating_mode) {
1205 DLOG("floating_mode=%s\n", floating_mode);
1209 TAILQ_FOREACH(current, &owindows, owindows) {
1210 DLOG("matching: %p / %s\n", current->con, current->con->name);
1211 if (strcmp(floating_mode, "toggle") == 0) {
1212 DLOG("should toggle mode\n");
1213 toggle_floating_mode(current->con, false);
1215 DLOG("should switch mode to %s\n", floating_mode);
1216 if (strcmp(floating_mode, "enable") == 0) {
1217 floating_enable(current->con, false);
1219 floating_disable(current->con, false);
1224 cmd_output->needs_tree_render = true;
1225 // XXX: default reply for now, make this a better reply
1230 * Implementation of 'move workspace to [output] <str>'.
1233 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1234 DLOG("should move workspace to output %s\n", name);
1239 TAILQ_FOREACH(current, &owindows, owindows) {
1240 Con *ws = con_get_workspace(current->con);
1241 bool success = workspace_move_to_output(ws, name);
1243 ELOG("Failed to move workspace to output.\n");
1249 cmd_output->needs_tree_render = true;
1250 // XXX: default reply for now, make this a better reply
1255 * Implementation of 'split v|h|vertical|horizontal'.
1258 void cmd_split(I3_CMD, char *direction) {
1260 /* TODO: use matches */
1261 LOG("splitting in direction %c\n", direction[0]);
1262 if (match_is_empty(current_match))
1263 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1265 TAILQ_FOREACH(current, &owindows, owindows) {
1266 DLOG("matching: %p / %s\n", current->con, current->con->name);
1267 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1271 cmd_output->needs_tree_render = true;
1272 // XXX: default reply for now, make this a better reply
1277 * Implementation of 'kill [window|client]'.
1280 void cmd_kill(I3_CMD, char *kill_mode_str) {
1281 if (kill_mode_str == NULL)
1282 kill_mode_str = "window";
1285 DLOG("kill_mode=%s\n", kill_mode_str);
1288 if (strcmp(kill_mode_str, "window") == 0)
1289 kill_mode = KILL_WINDOW;
1290 else if (strcmp(kill_mode_str, "client") == 0)
1291 kill_mode = KILL_CLIENT;
1293 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1298 /* check if the match is empty, not if the result is empty */
1299 if (match_is_empty(current_match))
1300 tree_close_con(kill_mode);
1302 TAILQ_FOREACH(current, &owindows, owindows) {
1303 DLOG("matching: %p / %s\n", current->con, current->con->name);
1304 tree_close(current->con, kill_mode, false, false);
1308 cmd_output->needs_tree_render = true;
1309 // XXX: default reply for now, make this a better reply
1314 * Implementation of 'exec [--no-startup-id] <command>'.
1317 void cmd_exec(I3_CMD, char *nosn, char *command) {
1318 bool no_startup_id = (nosn != NULL);
1320 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1321 start_application(command, no_startup_id);
1323 // XXX: default reply for now, make this a better reply
1328 * Implementation of 'focus left|right|up|down'.
1331 void cmd_focus_direction(I3_CMD, char *direction) {
1332 DLOG("direction = *%s*\n", direction);
1334 if (strcmp(direction, "left") == 0)
1335 tree_next('p', HORIZ);
1336 else if (strcmp(direction, "right") == 0)
1337 tree_next('n', HORIZ);
1338 else if (strcmp(direction, "up") == 0)
1339 tree_next('p', VERT);
1340 else if (strcmp(direction, "down") == 0)
1341 tree_next('n', VERT);
1343 ELOG("Invalid focus direction (%s)\n", direction);
1348 cmd_output->needs_tree_render = true;
1349 // XXX: default reply for now, make this a better reply
1354 * Implementation of 'focus tiling|floating|mode_toggle'.
1357 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1358 DLOG("window_mode = %s\n", window_mode);
1360 Con *ws = con_get_workspace(focused);
1363 if (strcmp(window_mode, "mode_toggle") == 0) {
1364 current = TAILQ_FIRST(&(ws->focus_head));
1365 if (current != NULL && current->type == CT_FLOATING_CON)
1366 window_mode = "tiling";
1368 window_mode = "floating";
1370 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1371 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1372 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1375 con_focus(con_descend_focused(current));
1380 cmd_output->needs_tree_render = true;
1381 // XXX: default reply for now, make this a better reply
1386 * Implementation of 'focus parent|child'.
1389 void cmd_focus_level(I3_CMD, char *level) {
1390 DLOG("level = %s\n", level);
1391 bool success = false;
1393 /* Focusing the parent can only be allowed if the newly
1394 * focused container won't escape the fullscreen container. */
1395 if (strcmp(level, "parent") == 0) {
1396 if (focused && focused->parent) {
1397 if (con_fullscreen_permits_focusing(focused->parent))
1398 success = level_up();
1400 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1404 /* Focusing a child should always be allowed. */
1406 success = level_down();
1408 cmd_output->needs_tree_render = success;
1409 // XXX: default reply for now, make this a better reply
1414 * Implementation of 'focus'.
1417 void cmd_focus(I3_CMD) {
1418 DLOG("current_match = %p\n", current_match);
1420 if (match_is_empty(current_match)) {
1421 ELOG("You have to specify which window/container should be focused.\n");
1422 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1424 yerror("You have to specify which window/container should be focused");
1429 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1432 TAILQ_FOREACH(current, &owindows, owindows) {
1433 Con *ws = con_get_workspace(current->con);
1434 /* If no workspace could be found, this was a dock window.
1435 * Just skip it, you cannot focus dock windows. */
1439 /* Check the fullscreen focus constraints. */
1440 if (!con_fullscreen_permits_focusing(current->con)) {
1441 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1446 /* In case this is a scratchpad window, call scratchpad_show(). */
1447 if (ws == __i3_scratch) {
1448 scratchpad_show(current->con);
1450 /* While for the normal focus case we can change focus multiple
1451 * times and only a single window ends up focused, we could show
1452 * multiple scratchpad windows. So, rather break here. */
1456 /* If the container is not on the current workspace,
1457 * workspace_show() will switch to a different workspace and (if
1458 * enabled) trigger a mouse pointer warp to the currently focused
1459 * container (!) on the target workspace.
1461 * Therefore, before calling workspace_show(), we make sure that
1462 * 'current' will be focused on the workspace. However, we cannot
1463 * just con_focus(current) because then the pointer will not be
1464 * warped at all (the code thinks we are already there).
1466 * So we focus 'current' to make it the currently focused window of
1467 * the target workspace, then revert focus. */
1468 Con *currently_focused = focused;
1469 con_focus(current->con);
1470 con_focus(currently_focused);
1472 /* Now switch to the workspace, then focus */
1474 LOG("focusing %p / %s\n", current->con, current->con->name);
1475 con_focus(current->con);
1480 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1481 "while only exactly one container can be focused at a time.\n",
1484 cmd_output->needs_tree_render = true;
1485 // XXX: default reply for now, make this a better reply
1490 * Implementation of 'fullscreen enable|toggle [global]' and
1491 * 'fullscreen disable'
1494 void cmd_fullscreen(I3_CMD, char *action, char *fullscreen_mode) {
1495 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1496 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1501 TAILQ_FOREACH(current, &owindows, owindows) {
1502 DLOG("matching: %p / %s\n", current->con, current->con->name);
1503 if (strcmp(action, "toggle") == 0) {
1504 con_toggle_fullscreen(current->con, mode);
1505 } else if (strcmp(action, "enable") == 0) {
1506 con_enable_fullscreen(current->con, mode);
1507 } else if (strcmp(action, "disable") == 0) {
1508 con_disable_fullscreen(current->con);
1512 cmd_output->needs_tree_render = true;
1513 // XXX: default reply for now, make this a better reply
1518 * Implementation of 'move <direction> [<pixels> [px]]'.
1521 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1522 // 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
1523 int px = atoi(move_px);
1528 Con *initially_focused = focused;
1530 TAILQ_FOREACH(current, &owindows, owindows) {
1531 DLOG("moving in direction %s, px %s\n", direction, move_px);
1532 if (con_is_floating(current->con)) {
1533 DLOG("floating move with %d pixels\n", px);
1534 Rect newrect = current->con->parent->rect;
1535 if (strcmp(direction, "left") == 0) {
1537 } else if (strcmp(direction, "right") == 0) {
1539 } else if (strcmp(direction, "up") == 0) {
1541 } else if (strcmp(direction, "down") == 0) {
1544 floating_reposition(current->con->parent, newrect);
1546 tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1547 cmd_output->needs_tree_render = true;
1551 /* the move command should not disturb focus */
1552 if (focused != initially_focused)
1553 con_focus(initially_focused);
1555 // XXX: default reply for now, make this a better reply
1560 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1563 void cmd_layout(I3_CMD, char *layout_str) {
1564 if (strcmp(layout_str, "stacking") == 0)
1565 layout_str = "stacked";
1568 /* default is a special case which will be handled in con_set_layout(). */
1569 if (strcmp(layout_str, "default") == 0)
1571 else if (strcmp(layout_str, "stacked") == 0)
1573 else if (strcmp(layout_str, "tabbed") == 0)
1575 else if (strcmp(layout_str, "splitv") == 0)
1577 else if (strcmp(layout_str, "splith") == 0)
1580 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1584 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1586 /* check if the match is empty, not if the result is empty */
1587 if (match_is_empty(current_match))
1588 con_set_layout(focused, layout);
1590 TAILQ_FOREACH(current, &owindows, owindows) {
1591 DLOG("matching: %p / %s\n", current->con, current->con->name);
1592 con_set_layout(current->con, layout);
1596 cmd_output->needs_tree_render = true;
1597 // XXX: default reply for now, make this a better reply
1602 * Implementation of 'layout toggle [all|split]'.
1605 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1608 if (toggle_mode == NULL)
1609 toggle_mode = "default";
1611 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1613 /* check if the match is empty, not if the result is empty */
1614 if (match_is_empty(current_match))
1615 con_toggle_layout(focused, toggle_mode);
1617 TAILQ_FOREACH(current, &owindows, owindows) {
1618 DLOG("matching: %p / %s\n", current->con, current->con->name);
1619 con_toggle_layout(current->con, toggle_mode);
1623 cmd_output->needs_tree_render = true;
1624 // XXX: default reply for now, make this a better reply
1629 * Implementation of 'exit'.
1632 void cmd_exit(I3_CMD) {
1633 LOG("Exiting due to user command.\n");
1635 unlink(config.ipc_socket_path);
1636 xcb_disconnect(conn);
1643 * Implementation of 'reload'.
1646 void cmd_reload(I3_CMD) {
1648 kill_nagbar(&config_error_nagbar_pid, false);
1649 kill_nagbar(&command_error_nagbar_pid, false);
1650 load_configuration(conn, NULL, true);
1652 /* Send an IPC event just in case the ws names have changed */
1653 ipc_send_workspace_event("reload", NULL, NULL);
1654 /* Send an update event for the barconfig just in case it has changed */
1657 // XXX: default reply for now, make this a better reply
1662 * Implementation of 'restart'.
1665 void cmd_restart(I3_CMD) {
1666 LOG("restarting i3\n");
1668 unlink(config.ipc_socket_path);
1669 /* We need to call this manually since atexit handlers don’t get called
1671 purge_zerobyte_logfile();
1674 // XXX: default reply for now, make this a better reply
1679 * Implementation of 'open'.
1682 void cmd_open(I3_CMD) {
1683 LOG("opening new container\n");
1684 Con *con = tree_open_con(NULL, NULL);
1685 con->layout = L_SPLITH;
1692 y(integer, (long int)con);
1695 cmd_output->needs_tree_render = true;
1699 * Implementation of 'focus output <output>'.
1702 void cmd_focus_output(I3_CMD, char *name) {
1705 DLOG("name = %s\n", name);
1709 /* get the output */
1710 Output *current_output = NULL;
1713 TAILQ_FOREACH(current, &owindows, owindows)
1714 current_output = get_output_of_con(current->con);
1715 assert(current_output != NULL);
1717 output = get_output_from_string(current_output, name);
1720 LOG("No such output found.\n");
1725 /* get visible workspace on output */
1727 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1735 cmd_output->needs_tree_render = true;
1736 // XXX: default reply for now, make this a better reply
1741 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1744 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1747 bool has_error = false;
1752 TAILQ_FOREACH(current, &owindows, owindows) {
1753 if (!con_is_floating(current->con)) {
1754 ELOG("Cannot change position. The window/container is not floating\n");
1757 yerror("Cannot change position of a window/container because it is not floating.");
1764 if (strcmp(method, "absolute") == 0) {
1765 current->con->parent->rect.x = x;
1766 current->con->parent->rect.y = y;
1768 DLOG("moving to absolute position %d %d\n", x, y);
1769 floating_maybe_reassign_ws(current->con->parent);
1770 cmd_output->needs_tree_render = true;
1773 if (strcmp(method, "position") == 0) {
1774 Rect newrect = current->con->parent->rect;
1776 DLOG("moving to position %d %d\n", x, y);
1780 floating_reposition(current->con->parent, newrect);
1784 // XXX: default reply for now, make this a better reply
1790 * Implementation of 'move [window|container] [to] [absolute] position center
1793 void cmd_move_window_to_center(I3_CMD, char *method) {
1794 if (!con_is_floating(focused)) {
1795 ELOG("Cannot change position. The window/container is not floating\n");
1796 yerror("Cannot change position. The window/container is not floating.");
1800 if (strcmp(method, "absolute") == 0) {
1801 Rect *rect = &focused->parent->rect;
1803 DLOG("moving to absolute center\n");
1804 rect->x = croot->rect.width / 2 - rect->width / 2;
1805 rect->y = croot->rect.height / 2 - rect->height / 2;
1807 floating_maybe_reassign_ws(focused->parent);
1808 cmd_output->needs_tree_render = true;
1811 if (strcmp(method, "position") == 0) {
1812 Rect *wsrect = &con_get_workspace(focused)->rect;
1813 Rect newrect = focused->parent->rect;
1815 DLOG("moving to center\n");
1816 newrect.x = wsrect->width / 2 - newrect.width / 2;
1817 newrect.y = wsrect->height / 2 - newrect.height / 2;
1819 floating_reposition(focused->parent, newrect);
1822 // XXX: default reply for now, make this a better reply
1827 * Implementation of 'move scratchpad'.
1830 void cmd_move_scratchpad(I3_CMD) {
1831 DLOG("should move window to scratchpad\n");
1836 TAILQ_FOREACH(current, &owindows, owindows) {
1837 DLOG("matching: %p / %s\n", current->con, current->con->name);
1838 scratchpad_move(current->con);
1841 cmd_output->needs_tree_render = true;
1842 // XXX: default reply for now, make this a better reply
1847 * Implementation of 'scratchpad show'.
1850 void cmd_scratchpad_show(I3_CMD) {
1851 DLOG("should show scratchpad window\n");
1854 if (match_is_empty(current_match)) {
1855 scratchpad_show(NULL);
1857 TAILQ_FOREACH(current, &owindows, owindows) {
1858 DLOG("matching: %p / %s\n", current->con, current->con->name);
1859 scratchpad_show(current->con);
1863 cmd_output->needs_tree_render = true;
1864 // XXX: default reply for now, make this a better reply
1869 * Implementation of 'rename workspace [<name>] to <name>'
1872 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1873 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1874 LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1879 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1881 LOG("Renaming current workspace to \"%s\"\n", new_name);
1884 Con *output, *workspace = NULL;
1886 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1887 GREP_FIRST(workspace, output_get_content(output),
1888 !strcasecmp(child->name, old_name));
1890 workspace = con_get_workspace(focused);
1894 // TODO: we should include the old workspace name here and use yajl for
1895 // generating the reply.
1896 // TODO: better error message
1897 yerror("Old workspace not found");
1901 Con *check_dest = NULL;
1902 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1903 GREP_FIRST(check_dest, output_get_content(output),
1904 !strcasecmp(child->name, new_name));
1906 if (check_dest != NULL) {
1907 // TODO: we should include the new workspace name here and use yajl for
1908 // generating the reply.
1909 // TODO: better error message
1910 yerror("New workspace already exists");
1914 /* Change the name and try to parse it as a number. */
1915 FREE(workspace->name);
1916 workspace->name = sstrdup(new_name);
1918 workspace->num = ws_name_to_number(new_name);
1919 LOG("num = %d\n", workspace->num);
1921 /* By re-attaching, the sort order will be correct afterwards. */
1922 Con *previously_focused = focused;
1923 Con *parent = workspace->parent;
1924 con_detach(workspace);
1925 con_attach(workspace, parent, false);
1927 /* Move the workspace to the correct output if it has an assignment */
1928 struct Workspace_Assignment *assignment = NULL;
1929 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1930 if (assignment->output == NULL)
1932 if (strcmp(assignment->name, workspace->name) != 0
1933 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1938 workspace_move_to_output(workspace, assignment->output);
1942 /* Restore the previous focus since con_attach messes with the focus. */
1943 con_focus(previously_focused);
1945 cmd_output->needs_tree_render = true;
1948 ipc_send_workspace_event("rename", workspace, NULL);
1949 ewmh_update_desktop_names();
1950 ewmh_update_desktop_viewport();
1951 ewmh_update_current_desktop();
1955 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1958 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1960 bool toggle = false;
1961 if (strcmp(bar_mode, "dock") == 0)
1963 else if (strcmp(bar_mode, "hide") == 0)
1965 else if (strcmp(bar_mode, "invisible") == 0)
1967 else if (strcmp(bar_mode, "toggle") == 0)
1970 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1974 bool changed_sth = false;
1975 Barconfig *current = NULL;
1976 TAILQ_FOREACH(current, &barconfigs, configs) {
1977 if (bar_id && strcmp(current->id, bar_id) != 0)
1981 mode = (current->mode + 1) % 2;
1983 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1984 current->mode = mode;
1991 if (bar_id && !changed_sth) {
1992 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2000 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2003 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
2004 int hidden_state = S_SHOW;
2005 bool toggle = false;
2006 if (strcmp(bar_hidden_state, "hide") == 0)
2007 hidden_state = S_HIDE;
2008 else if (strcmp(bar_hidden_state, "show") == 0)
2009 hidden_state = S_SHOW;
2010 else if (strcmp(bar_hidden_state, "toggle") == 0)
2013 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2017 bool changed_sth = false;
2018 Barconfig *current = NULL;
2019 TAILQ_FOREACH(current, &barconfigs, configs) {
2020 if (bar_id && strcmp(current->id, bar_id) != 0)
2024 hidden_state = (current->hidden_state + 1) % 2;
2026 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2027 current->hidden_state = hidden_state;
2034 if (bar_id && !changed_sth) {
2035 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2043 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2046 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2048 if (strcmp(bar_type, "mode") == 0)
2049 ret = cmd_bar_mode(bar_value, bar_id);
2050 else if (strcmp(bar_type, "hidden_state") == 0)
2051 ret = cmd_bar_hidden_state(bar_value, bar_id);
2053 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2065 * Implementation of 'shmlog <size>|toggle|on|off'
2068 void cmd_shmlog(I3_CMD, char *argument) {
2069 if (!strcmp(argument, "toggle"))
2070 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2071 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2072 else if (!strcmp(argument, "on"))
2073 shmlog_size = default_shmlog_size;
2074 else if (!strcmp(argument, "off"))
2077 /* If shm logging now, restart logging with the new size. */
2078 if (shmlog_size > 0) {
2080 LOG("Restarting shm logging...\n");
2083 shmlog_size = atoi(argument);
2084 /* Make a weakly attempt at ensuring the argument is valid. */
2085 if (shmlog_size <= 0)
2086 shmlog_size = default_shmlog_size;
2088 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2090 update_shmlog_atom();
2091 // XXX: default reply for now, make this a better reply
2096 * Implementation of 'debuglog toggle|on|off'
2099 void cmd_debuglog(I3_CMD, char *argument) {
2100 bool logging = get_debug_logging();
2101 if (!strcmp(argument, "toggle")) {
2102 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2103 set_debug_logging(!logging);
2104 } else if (!strcmp(argument, "on") && !logging) {
2105 LOG("Enabling debug logging\n");
2106 set_debug_logging(true);
2107 } else if (!strcmp(argument, "off") && logging) {
2108 LOG("Disabling debug logging\n");
2109 set_debug_logging(false);
2111 // XXX: default reply for now, make this a better reply