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, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
20 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
21 #define ysuccess(success) do { \
27 #define yerror(message) do { \
36 /** When the command did not include match criteria (!), we use the currently
37 * focused container. Do not confuse this case with a command which included
38 * criteria but which did not match any windows. This macro has to be called in
41 #define HANDLE_EMPTY_MATCH do { \
42 if (match_is_empty(current_match)) { \
43 owindow *ow = smalloc(sizeof(owindow)); \
45 TAILQ_INIT(&owindows); \
46 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
52 * Returns true if a is definitely greater than b (using the given epsilon)
55 static bool definitelyGreaterThan(float a, float b, float epsilon) {
56 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
60 * Returns an 'output' corresponding to one of left/right/down/up or a specific
64 static Output *get_output_from_string(Output *current_output, const char *output_str) {
67 if (strcasecmp(output_str, "left") == 0)
68 output = get_output_next_wrap(D_LEFT, current_output);
69 else if (strcasecmp(output_str, "right") == 0)
70 output = get_output_next_wrap(D_RIGHT, current_output);
71 else if (strcasecmp(output_str, "up") == 0)
72 output = get_output_next_wrap(D_UP, current_output);
73 else if (strcasecmp(output_str, "down") == 0)
74 output = get_output_next_wrap(D_DOWN, current_output);
75 else output = get_output_by_name(output_str);
81 * Checks whether we switched to a new workspace and returns false in that case,
82 * signaling that further workspace switching should be done by the calling function
83 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
84 * and return true, signaling that no further workspace switching should occur in the calling function.
87 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
88 Con *ws = con_get_workspace(focused);
90 /* If we switched to a different workspace, do nothing */
91 if (strcmp(ws->name, name) != 0)
94 DLOG("This workspace is already focused.\n");
95 if (config.workspace_auto_back_and_forth) {
96 workspace_back_and_forth();
97 cmd_output->needs_tree_render = true;
103 * Return the passed workspace unless it is the current one and auto back and
104 * forth is enabled, in which case the back_and_forth workspace is returned.
106 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
109 if (!config.workspace_auto_back_and_forth)
112 current = con_get_workspace(focused);
114 if (current == workspace) {
115 baf = workspace_back_and_forth_get();
117 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
125 // This code is commented out because we might recycle it for popping up error
126 // messages on parser errors.
128 static pid_t migration_pid = -1;
131 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
132 * it exited (or could not be started, depending on the exit code).
135 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
136 ev_child_stop(EV_A_ watcher);
137 if (!WIFEXITED(watcher->rstatus)) {
138 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
142 int exitcode = WEXITSTATUS(watcher->rstatus);
143 printf("i3-nagbar process exited with status %d\n", exitcode);
145 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
151 /* We need ev >= 4 for the following code. Since it is not *that* important (it
152 * only makes sure that there are no i3-nagbar instances left behind) we still
153 * support old systems with libev 3. */
154 #if EV_VERSION_MAJOR >= 4
156 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
157 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
160 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
161 if (migration_pid != -1) {
162 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
163 kill(migration_pid, SIGKILL);
168 void cmd_MIGRATION_start_nagbar(void) {
169 if (migration_pid != -1) {
170 fprintf(stderr, "i3-nagbar already running.\n");
173 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
174 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
175 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
176 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
177 migration_pid = fork();
178 if (migration_pid == -1) {
179 warn("Could not fork()");
184 if (migration_pid == 0) {
186 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
188 NULL, /* will be replaced by the executable path */
192 "You found a parsing error. Please, please, please, report it!",
198 exec_i3_utility("i3-nagbar", argv);
202 /* install a child watcher */
203 ev_child *child = smalloc(sizeof(ev_child));
204 ev_child_init(child, &nagbar_exited, migration_pid, 0);
205 ev_child_start(main_loop, child);
207 /* We need ev >= 4 for the following code. Since it is not *that* important (it
208 * only makes sure that there are no i3-nagbar instances left behind) we still
209 * support old systems with libev 3. */
210 #if EV_VERSION_MAJOR >= 4
211 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
213 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
214 ev_cleanup_init(cleanup, nagbar_cleanup);
215 ev_cleanup_start(main_loop, cleanup);
221 /*******************************************************************************
222 * Criteria functions.
223 ******************************************************************************/
226 * Helper data structure for an operation window (window on which the operation
227 * will be performed). Used to build the TAILQ owindows.
230 typedef struct owindow {
232 TAILQ_ENTRY(owindow) owindows;
235 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
237 static owindows_head owindows;
240 * Initializes the specified 'Match' data structure and the initial state of
241 * commands.c for matching target windows of a command.
244 void cmd_criteria_init(I3_CMD) {
248 DLOG("Initializing criteria, current_match = %p\n", current_match);
249 match_init(current_match);
250 while (!TAILQ_EMPTY(&owindows)) {
251 ow = TAILQ_FIRST(&owindows);
252 TAILQ_REMOVE(&owindows, ow, owindows);
255 TAILQ_INIT(&owindows);
257 TAILQ_FOREACH(con, &all_cons, all_cons) {
258 ow = smalloc(sizeof(owindow));
260 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
265 * A match specification just finished (the closing square bracket was found),
266 * so we filter the list of owindows.
269 void cmd_criteria_match_windows(I3_CMD) {
270 owindow *next, *current;
272 DLOG("match specification finished, matching...\n");
273 /* copy the old list head to iterate through it and start with a fresh
274 * list which will contain only matching windows */
275 struct owindows_head old = owindows;
276 TAILQ_INIT(&owindows);
277 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
278 /* make a copy of the next pointer and advance the pointer to the
279 * next element as we are going to invalidate the element’s
280 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
282 next = TAILQ_NEXT(next, owindows);
284 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
285 if (current_match->con_id != NULL) {
286 if (current_match->con_id == current->con) {
287 DLOG("matches container!\n");
288 TAILQ_INSERT_TAIL(&owindows, current, owindows);
290 } 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 == NULL)
297 if (match_matches_window(current_match, current->con->window)) {
298 DLOG("matches window!\n");
299 TAILQ_INSERT_TAIL(&owindows, current, owindows);
301 DLOG("doesnt match\n");
307 TAILQ_FOREACH(current, &owindows, owindows) {
308 DLOG("matching: %p / %s\n", current->con, current->con->name);
313 * Interprets a ctype=cvalue pair and adds it to the current match
317 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
318 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
320 if (strcmp(ctype, "class") == 0) {
321 current_match->class = regex_new(cvalue);
325 if (strcmp(ctype, "instance") == 0) {
326 current_match->instance = regex_new(cvalue);
330 if (strcmp(ctype, "window_role") == 0) {
331 current_match->role = regex_new(cvalue);
335 if (strcmp(ctype, "con_id") == 0) {
337 long parsed = strtol(cvalue, &end, 10);
338 if (parsed == LONG_MIN ||
339 parsed == LONG_MAX ||
341 (end && *end != '\0')) {
342 ELOG("Could not parse con id \"%s\"\n", cvalue);
344 current_match->con_id = (Con*)parsed;
345 printf("id as int = %p\n", current_match->con_id);
350 if (strcmp(ctype, "id") == 0) {
352 long parsed = strtol(cvalue, &end, 10);
353 if (parsed == LONG_MIN ||
354 parsed == LONG_MAX ||
356 (end && *end != '\0')) {
357 ELOG("Could not parse window id \"%s\"\n", cvalue);
359 current_match->id = parsed;
360 printf("window id as int = %d\n", current_match->id);
365 if (strcmp(ctype, "con_mark") == 0) {
366 current_match->mark = regex_new(cvalue);
370 if (strcmp(ctype, "title") == 0) {
371 current_match->title = regex_new(cvalue);
375 if (strcmp(ctype, "urgent") == 0) {
376 if (strcasecmp(cvalue, "latest") == 0 ||
377 strcasecmp(cvalue, "newest") == 0 ||
378 strcasecmp(cvalue, "recent") == 0 ||
379 strcasecmp(cvalue, "last") == 0) {
380 current_match->urgent = U_LATEST;
381 } else if (strcasecmp(cvalue, "oldest") == 0 ||
382 strcasecmp(cvalue, "first") == 0) {
383 current_match->urgent = U_OLDEST;
388 ELOG("Unknown criterion: %s\n", ctype);
392 * Implementation of 'move [window|container] [to] workspace
393 * next|prev|next_on_output|prev_on_output|current'.
396 void cmd_move_con_to_workspace(I3_CMD, char *which) {
399 DLOG("which=%s\n", which);
401 /* We have nothing to move:
402 * when criteria was specified but didn't match any window or
403 * when criteria wasn't specified and we don't have any window focused. */
404 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
405 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
406 !con_has_children(focused))) {
413 /* get the workspace */
415 if (strcmp(which, "next") == 0)
416 ws = workspace_next();
417 else if (strcmp(which, "prev") == 0)
418 ws = workspace_prev();
419 else if (strcmp(which, "next_on_output") == 0)
420 ws = workspace_next_on_output();
421 else if (strcmp(which, "prev_on_output") == 0)
422 ws = workspace_prev_on_output();
423 else if (strcmp(which, "current") == 0)
424 ws = con_get_workspace(focused);
426 ELOG("BUG: called with which=%s\n", which);
431 TAILQ_FOREACH(current, &owindows, owindows) {
432 DLOG("matching: %p / %s\n", current->con, current->con->name);
433 con_move_to_workspace(current->con, ws, true, false);
436 cmd_output->needs_tree_render = true;
437 // XXX: default reply for now, make this a better reply
442 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
445 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
449 ws = workspace_back_and_forth_get();
452 yerror("No workspace was previously active.");
458 TAILQ_FOREACH(current, &owindows, owindows) {
459 DLOG("matching: %p / %s\n", current->con, current->con->name);
460 con_move_to_workspace(current->con, ws, true, false);
463 cmd_output->needs_tree_render = true;
464 // XXX: default reply for now, make this a better reply
469 * Implementation of 'move [window|container] [to] workspace <name>'.
472 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
473 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
474 LOG("You cannot switch to the i3 internal workspaces.\n");
481 /* We have nothing to move:
482 * when criteria was specified but didn't match any window or
483 * when criteria wasn't specified and we don't have any window focused. */
484 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
485 ELOG("No windows match your criteria, cannot move.\n");
489 else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
490 !con_has_children(focused)) {
495 LOG("should move window to workspace %s\n", name);
496 /* get the workspace */
497 Con *ws = workspace_get(name, NULL);
499 ws = maybe_auto_back_and_forth_workspace(ws);
503 TAILQ_FOREACH(current, &owindows, owindows) {
504 DLOG("matching: %p / %s\n", current->con, current->con->name);
505 con_move_to_workspace(current->con, ws, true, false);
508 cmd_output->needs_tree_render = true;
509 // XXX: default reply for now, make this a better reply
514 * Implementation of 'move [window|container] [to] workspace number <name>'.
517 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
520 /* We have nothing to move:
521 * when criteria was specified but didn't match any window or
522 * when criteria wasn't specified and we don't have any window focused. */
523 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
524 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
525 !con_has_children(focused))) {
530 LOG("should move window to workspace %s\n", which);
531 /* get the workspace */
532 Con *output, *workspace = NULL;
535 long parsed_num = strtol(which, &endptr, 10);
536 if (parsed_num == LONG_MIN ||
537 parsed_num == LONG_MAX ||
540 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
541 // TODO: better error message
542 yerror("Could not parse number");
546 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
547 GREP_FIRST(workspace, output_get_content(output),
548 child->num == parsed_num);
551 workspace = workspace_get(which, NULL);
554 workspace = maybe_auto_back_and_forth_workspace(workspace);
558 TAILQ_FOREACH(current, &owindows, owindows) {
559 DLOG("matching: %p / %s\n", current->con, current->con->name);
560 con_move_to_workspace(current->con, workspace, true, false);
563 cmd_output->needs_tree_render = true;
564 // XXX: default reply for now, make this a better reply
568 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
569 LOG("floating resize\n");
570 Rect old_rect = floating_con->rect;
571 Con *focused_con = con_descend_focused(floating_con);
573 /* ensure that resize will take place even if pixel increment is smaller than
574 * height increment or width increment.
576 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
577 strcmp(direction, "height") == 0) {
579 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
581 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
582 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
584 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
586 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
589 if (strcmp(direction, "up") == 0) {
590 floating_con->rect.height += px;
591 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
592 floating_con->rect.height += px;
593 } else if (strcmp(direction, "left") == 0) {
594 floating_con->rect.width += px;
596 floating_con->rect.width += px;
599 floating_check_size(floating_con);
601 /* Did we actually resize anything or did the size constraints prevent us?
602 * If we could not resize, exit now to not move the window. */
603 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
606 if (strcmp(direction, "up") == 0) {
607 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
608 } else if (strcmp(direction, "left") == 0) {
609 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
612 /* If this is a scratchpad window, don't auto center it from now on. */
613 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
614 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
617 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
618 LOG("tiling resize\n");
620 Con *first = current;
621 direction_t search_direction;
622 if (!strcmp(direction, "left"))
623 search_direction = D_LEFT;
624 else if (!strcmp(direction, "right"))
625 search_direction = D_RIGHT;
626 else if (!strcmp(direction, "up"))
627 search_direction = D_UP;
629 search_direction = D_DOWN;
631 bool res = resize_find_tiling_participants(&first, &second, search_direction);
633 LOG("No second container in this direction found.\n");
638 /* get the default percentage */
639 int children = con_num_children(first->parent);
640 LOG("ins. %d children\n", children);
641 double percentage = 1.0 / children;
642 LOG("default percentage = %f\n", percentage);
645 LOG("second->percent = %f\n", second->percent);
646 LOG("first->percent before = %f\n", first->percent);
647 if (first->percent == 0.0)
648 first->percent = percentage;
649 if (second->percent == 0.0)
650 second->percent = percentage;
651 double new_first_percent = first->percent + ((double)ppt / 100.0);
652 double new_second_percent = second->percent - ((double)ppt / 100.0);
653 LOG("new_first_percent = %f\n", new_first_percent);
654 LOG("new_second_percent = %f\n", new_second_percent);
655 /* Ensure that the new percentages are positive and greater than
656 * 0.05 to have a reasonable minimum size. */
657 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
658 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
659 first->percent += ((double)ppt / 100.0);
660 second->percent -= ((double)ppt / 100.0);
661 LOG("first->percent after = %f\n", first->percent);
662 LOG("second->percent after = %f\n", second->percent);
664 LOG("Not resizing, already at minimum size\n");
670 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
671 LOG("width/height resize\n");
672 /* get the appropriate current container (skip stacked/tabbed cons) */
673 while (current->parent->layout == L_STACKED ||
674 current->parent->layout == L_TABBED)
675 current = current->parent;
677 /* Then further go up until we find one with the matching orientation. */
678 orientation_t search_orientation =
679 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
681 while (current->type != CT_WORKSPACE &&
682 current->type != CT_FLOATING_CON &&
683 con_orientation(current->parent) != search_orientation)
684 current = current->parent;
686 /* get the default percentage */
687 int children = con_num_children(current->parent);
688 LOG("ins. %d children\n", children);
689 double percentage = 1.0 / children;
690 LOG("default percentage = %f\n", percentage);
692 orientation_t orientation = con_orientation(current->parent);
694 if ((orientation == HORIZ &&
695 strcmp(direction, "height") == 0) ||
696 (orientation == VERT &&
697 strcmp(direction, "width") == 0)) {
698 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
699 (orientation == HORIZ ? "horizontal" : "vertical"));
705 LOG("This is the only container, cannot resize.\n");
710 /* Ensure all the other children have a percentage set. */
712 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
713 LOG("child->percent = %f (child %p)\n", child->percent, child);
714 if (child->percent == 0.0)
715 child->percent = percentage;
718 double new_current_percent = current->percent + ((double)ppt / 100.0);
719 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
720 LOG("new_current_percent = %f\n", new_current_percent);
721 LOG("subtract_percent = %f\n", subtract_percent);
722 /* Ensure that the new percentages are positive and greater than
723 * 0.05 to have a reasonable minimum size. */
724 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
725 if (child == current)
727 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
728 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
733 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
734 LOG("Not resizing, already at minimum size\n");
739 current->percent += ((double)ppt / 100.0);
740 LOG("current->percent after = %f\n", current->percent);
742 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
743 if (child == current)
745 child->percent -= subtract_percent;
746 LOG("child->percent after (%p) = %f\n", child, child->percent);
753 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
756 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
757 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
758 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
759 // 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
760 int px = atoi(resize_px);
761 int ppt = atoi(resize_ppt);
762 if (strcmp(way, "shrink") == 0) {
770 TAILQ_FOREACH(current, &owindows, owindows) {
772 if ((floating_con = con_inside_floating(current->con))) {
773 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
775 if (strcmp(direction, "width") == 0 ||
776 strcmp(direction, "height") == 0) {
777 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
780 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
786 cmd_output->needs_tree_render = true;
787 // XXX: default reply for now, make this a better reply
792 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
795 void cmd_border(I3_CMD, char *border_style_str, char *border_width ) {
796 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
801 TAILQ_FOREACH(current, &owindows, owindows) {
802 DLOG("matching: %p / %s\n", current->con, current->con->name);
803 int border_style = current->con->border_style;
805 int tmp_border_width = -1;
806 tmp_border_width = strtol(border_width, &end, 10);
807 if (end == border_width) {
808 /* no valid digits found */
809 tmp_border_width = -1;
811 if (strcmp(border_style_str, "toggle") == 0) {
814 if (border_style == BS_NORMAL)
815 tmp_border_width = 2;
816 else if (border_style == BS_NONE)
817 tmp_border_width = 0;
818 else if (border_style == BS_PIXEL)
819 tmp_border_width = 1;
821 if (strcmp(border_style_str, "normal") == 0)
822 border_style = BS_NORMAL;
823 else if (strcmp(border_style_str, "pixel") == 0)
824 border_style = BS_PIXEL;
825 else if (strcmp(border_style_str, "1pixel") == 0){
826 border_style = BS_PIXEL;
827 tmp_border_width = 1;
828 } else if (strcmp(border_style_str, "none") == 0)
829 border_style = BS_NONE;
831 ELOG("BUG: called with border_style=%s\n", border_style_str);
836 con_set_border_style(current->con, border_style, tmp_border_width);
839 cmd_output->needs_tree_render = true;
840 // XXX: default reply for now, make this a better reply
845 * Implementation of 'nop <comment>'.
848 void cmd_nop(I3_CMD, char *comment) {
849 LOG("-------------------------------------------------\n");
850 LOG(" NOP: %s\n", comment);
851 LOG("-------------------------------------------------\n");
855 * Implementation of 'append_layout <path>'.
858 void cmd_append_layout(I3_CMD, char *path) {
859 LOG("Appending layout \"%s\"\n", path);
860 tree_append_json(path);
862 cmd_output->needs_tree_render = true;
863 // XXX: default reply for now, make this a better reply
868 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
871 void cmd_workspace(I3_CMD, char *which) {
874 DLOG("which=%s\n", which);
876 if (strcmp(which, "next") == 0)
877 ws = workspace_next();
878 else if (strcmp(which, "prev") == 0)
879 ws = workspace_prev();
880 else if (strcmp(which, "next_on_output") == 0)
881 ws = workspace_next_on_output();
882 else if (strcmp(which, "prev_on_output") == 0)
883 ws = workspace_prev_on_output();
885 ELOG("BUG: called with which=%s\n", which);
892 cmd_output->needs_tree_render = true;
893 // XXX: default reply for now, make this a better reply
898 * Implementation of 'workspace number <name>'
901 void cmd_workspace_number(I3_CMD, char *which) {
902 Con *output, *workspace = NULL;
905 long parsed_num = strtol(which, &endptr, 10);
906 if (parsed_num == LONG_MIN ||
907 parsed_num == LONG_MAX ||
910 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
911 // TODO: better error message
912 yerror("Could not parse number");
917 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
918 GREP_FIRST(workspace, output_get_content(output),
919 child->num == parsed_num);
922 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
924 workspace_show_by_name(which);
925 cmd_output->needs_tree_render = true;
928 if (maybe_back_and_forth(cmd_output, workspace->name))
930 workspace_show(workspace);
932 cmd_output->needs_tree_render = true;
933 // XXX: default reply for now, make this a better reply
938 * Implementation of 'workspace back_and_forth'.
941 void cmd_workspace_back_and_forth(I3_CMD) {
942 workspace_back_and_forth();
944 cmd_output->needs_tree_render = true;
945 // XXX: default reply for now, make this a better reply
950 * Implementation of 'workspace <name>'
953 void cmd_workspace_name(I3_CMD, char *name) {
954 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
955 LOG("You cannot switch to the i3 internal workspaces.\n");
960 DLOG("should switch to workspace %s\n", name);
961 if (maybe_back_and_forth(cmd_output, name))
963 workspace_show_by_name(name);
965 cmd_output->needs_tree_render = true;
966 // XXX: default reply for now, make this a better reply
971 * Implementation of 'mark <mark>'
974 void cmd_mark(I3_CMD, char *mark) {
975 DLOG("Clearing all windows which have that mark first\n");
978 TAILQ_FOREACH(con, &all_cons, all_cons) {
979 if (con->mark && strcmp(con->mark, mark) == 0)
983 DLOG("marking window with str %s\n", mark);
988 TAILQ_FOREACH(current, &owindows, owindows) {
989 DLOG("matching: %p / %s\n", current->con, current->con->name);
990 current->con->mark = sstrdup(mark);
993 cmd_output->needs_tree_render = true;
994 // XXX: default reply for now, make this a better reply
999 * Implementation of 'unmark [mark]'
1002 void cmd_unmark(I3_CMD, char *mark) {
1005 TAILQ_FOREACH(con, &all_cons, all_cons) {
1008 DLOG("removed all window marks");
1011 TAILQ_FOREACH(con, &all_cons, all_cons) {
1012 if (con->mark && strcmp(con->mark, mark) == 0)
1015 DLOG("removed window mark %s\n", mark);
1018 cmd_output->needs_tree_render = true;
1019 // XXX: default reply for now, make this a better reply
1024 * Implementation of 'mode <string>'.
1027 void cmd_mode(I3_CMD, char *mode) {
1028 DLOG("mode=%s\n", mode);
1031 // XXX: default reply for now, make this a better reply
1036 * Implementation of 'move [window|container] [to] output <str>'.
1039 void cmd_move_con_to_output(I3_CMD, char *name) {
1042 DLOG("should move window to output %s\n", name);
1046 /* get the output */
1047 Output *current_output = NULL;
1050 // TODO: fix the handling of criteria
1051 TAILQ_FOREACH(current, &owindows, owindows)
1052 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1054 assert(current_output != NULL);
1056 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1057 if (strcasecmp(name, "up") == 0)
1058 output = get_output_next_wrap(D_UP, current_output);
1059 else if (strcasecmp(name, "down") == 0)
1060 output = get_output_next_wrap(D_DOWN, current_output);
1061 else if (strcasecmp(name, "left") == 0)
1062 output = get_output_next_wrap(D_LEFT, current_output);
1063 else if (strcasecmp(name, "right") == 0)
1064 output = get_output_next_wrap(D_RIGHT, current_output);
1066 output = get_output_by_name(name);
1069 LOG("No such output found.\n");
1074 /* get visible workspace on output */
1076 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1082 TAILQ_FOREACH(current, &owindows, owindows) {
1083 DLOG("matching: %p / %s\n", current->con, current->con->name);
1084 con_move_to_workspace(current->con, ws, true, false);
1087 cmd_output->needs_tree_render = true;
1088 // XXX: default reply for now, make this a better reply
1093 * Implementation of 'floating enable|disable|toggle'
1096 void cmd_floating(I3_CMD, char *floating_mode) {
1099 DLOG("floating_mode=%s\n", floating_mode);
1103 TAILQ_FOREACH(current, &owindows, owindows) {
1104 DLOG("matching: %p / %s\n", current->con, current->con->name);
1105 if (strcmp(floating_mode, "toggle") == 0) {
1106 DLOG("should toggle mode\n");
1107 toggle_floating_mode(current->con, false);
1109 DLOG("should switch mode to %s\n", floating_mode);
1110 if (strcmp(floating_mode, "enable") == 0) {
1111 floating_enable(current->con, false);
1113 floating_disable(current->con, false);
1118 cmd_output->needs_tree_render = true;
1119 // XXX: default reply for now, make this a better reply
1124 * Implementation of 'move workspace to [output] <str>'.
1127 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1128 DLOG("should move workspace to output %s\n", name);
1133 TAILQ_FOREACH(current, &owindows, owindows) {
1134 Output *current_output = get_output_containing(current->con->rect.x,
1135 current->con->rect.y);
1136 if (!current_output) {
1137 ELOG("Cannot get current output. This is a bug in i3.\n");
1141 Output *output = get_output_from_string(current_output, name);
1143 ELOG("Could not get output from string \"%s\"\n", name);
1148 Con *content = output_get_content(output->con);
1149 LOG("got output %p with content %p\n", output, content);
1151 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1152 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1154 Con *ws = con_get_workspace(current->con);
1155 LOG("should move workspace %p / %s\n", ws, ws->name);
1156 bool workspace_was_visible = workspace_is_visible(ws);
1158 if (con_num_children(ws->parent) == 1) {
1159 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1161 /* check if we can find a workspace assigned to this output */
1162 bool used_assignment = false;
1163 struct Workspace_Assignment *assignment;
1164 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1165 if (strcmp(assignment->output, current_output->name) != 0)
1168 /* check if this workspace is already attached to the tree */
1169 Con *workspace = NULL, *out;
1170 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1171 GREP_FIRST(workspace, output_get_content(out),
1172 !strcasecmp(child->name, assignment->name));
1173 if (workspace != NULL)
1176 /* so create the workspace referenced to by this assignment */
1177 LOG("Creating workspace from assignment %s.\n", assignment->name);
1178 workspace_get(assignment->name, NULL);
1179 used_assignment = true;
1183 /* if we couldn't create the workspace using an assignment, create
1184 * it on the output */
1185 if (!used_assignment)
1186 create_workspace_on_output(current_output, ws->parent);
1188 /* notify the IPC listeners */
1189 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1191 DLOG("Detaching\n");
1193 /* detach from the old output and attach to the new output */
1194 Con *old_content = ws->parent;
1196 if (workspace_was_visible) {
1197 /* The workspace which we just detached was visible, so focus
1198 * the next one in the focus-stack. */
1199 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1200 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1201 workspace_show(focus_ws);
1203 con_attach(ws, content, false);
1205 /* fix the coordinates of the floating containers */
1207 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1208 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1210 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1211 if (workspace_was_visible) {
1212 /* Focus the moved workspace on the destination output. */
1216 /* NB: We cannot simply work with previously_visible_ws since it might
1217 * have been cleaned up by workspace_show() already, depending on the
1218 * focus order/number of other workspaces on the output.
1219 * Instead, we loop through the available workspaces and only work with
1220 * previously_visible_ws if we still find it. */
1221 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1222 if (ws != previously_visible_ws)
1225 /* Call the on_remove_child callback of the workspace which previously
1226 * was visible on the destination output. Since it is no longer
1227 * visible, it might need to get cleaned up. */
1228 CALL(previously_visible_ws, on_remove_child);
1233 cmd_output->needs_tree_render = true;
1234 // XXX: default reply for now, make this a better reply
1239 * Implementation of 'split v|h|vertical|horizontal'.
1242 void cmd_split(I3_CMD, char *direction) {
1244 /* TODO: use matches */
1245 LOG("splitting in direction %c\n", direction[0]);
1246 if (match_is_empty(current_match))
1247 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1249 TAILQ_FOREACH(current, &owindows, owindows) {
1250 DLOG("matching: %p / %s\n", current->con, current->con->name);
1251 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1255 cmd_output->needs_tree_render = true;
1256 // XXX: default reply for now, make this a better reply
1261 * Implementation of 'kill [window|client]'.
1264 void cmd_kill(I3_CMD, char *kill_mode_str) {
1265 if (kill_mode_str == NULL)
1266 kill_mode_str = "window";
1269 DLOG("kill_mode=%s\n", kill_mode_str);
1272 if (strcmp(kill_mode_str, "window") == 0)
1273 kill_mode = KILL_WINDOW;
1274 else if (strcmp(kill_mode_str, "client") == 0)
1275 kill_mode = KILL_CLIENT;
1277 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1282 /* check if the match is empty, not if the result is empty */
1283 if (match_is_empty(current_match))
1284 tree_close_con(kill_mode);
1286 TAILQ_FOREACH(current, &owindows, owindows) {
1287 DLOG("matching: %p / %s\n", current->con, current->con->name);
1288 tree_close(current->con, kill_mode, false, false);
1292 cmd_output->needs_tree_render = true;
1293 // XXX: default reply for now, make this a better reply
1298 * Implementation of 'exec [--no-startup-id] <command>'.
1301 void cmd_exec(I3_CMD, char *nosn, char *command) {
1302 bool no_startup_id = (nosn != NULL);
1304 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1305 start_application(command, no_startup_id);
1307 // XXX: default reply for now, make this a better reply
1312 * Implementation of 'focus left|right|up|down'.
1315 void cmd_focus_direction(I3_CMD, char *direction) {
1316 DLOG("direction = *%s*\n", direction);
1318 if (strcmp(direction, "left") == 0)
1319 tree_next('p', HORIZ);
1320 else if (strcmp(direction, "right") == 0)
1321 tree_next('n', HORIZ);
1322 else if (strcmp(direction, "up") == 0)
1323 tree_next('p', VERT);
1324 else if (strcmp(direction, "down") == 0)
1325 tree_next('n', VERT);
1327 ELOG("Invalid focus direction (%s)\n", direction);
1332 cmd_output->needs_tree_render = true;
1333 // XXX: default reply for now, make this a better reply
1338 * Implementation of 'focus tiling|floating|mode_toggle'.
1341 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1342 DLOG("window_mode = %s\n", window_mode);
1344 Con *ws = con_get_workspace(focused);
1347 if (strcmp(window_mode, "mode_toggle") == 0) {
1348 current = TAILQ_FIRST(&(ws->focus_head));
1349 if (current != NULL && current->type == CT_FLOATING_CON)
1350 window_mode = "tiling";
1351 else window_mode = "floating";
1353 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1354 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1355 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1358 con_focus(con_descend_focused(current));
1363 cmd_output->needs_tree_render = true;
1364 // XXX: default reply for now, make this a better reply
1369 * Implementation of 'focus parent|child'.
1372 void cmd_focus_level(I3_CMD, char *level) {
1373 DLOG("level = %s\n", level);
1374 bool success = false;
1376 /* Focusing the parent can only be allowed if the newly
1377 * focused container won't escape the fullscreen container. */
1378 if (strcmp(level, "parent") == 0) {
1379 if (focused && focused->parent) {
1380 if (con_fullscreen_permits_focusing(focused->parent))
1381 success = level_up();
1383 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1387 /* Focusing a child should always be allowed. */
1388 else success = level_down();
1390 cmd_output->needs_tree_render = success;
1391 // XXX: default reply for now, make this a better reply
1396 * Implementation of 'focus'.
1399 void cmd_focus(I3_CMD) {
1400 DLOG("current_match = %p\n", current_match);
1402 if (match_is_empty(current_match)) {
1403 ELOG("You have to specify which window/container should be focused.\n");
1404 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1406 yerror("You have to specify which window/container should be focused");
1411 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1414 TAILQ_FOREACH(current, &owindows, owindows) {
1415 Con *ws = con_get_workspace(current->con);
1416 /* If no workspace could be found, this was a dock window.
1417 * Just skip it, you cannot focus dock windows. */
1421 /* Check the fullscreen focus constraints. */
1422 if (!con_fullscreen_permits_focusing(current->con)) {
1423 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1428 /* In case this is a scratchpad window, call scratchpad_show(). */
1429 if (ws == __i3_scratch) {
1430 scratchpad_show(current->con);
1432 /* While for the normal focus case we can change focus multiple
1433 * times and only a single window ends up focused, we could show
1434 * multiple scratchpad windows. So, rather break here. */
1438 /* If the container is not on the current workspace,
1439 * workspace_show() will switch to a different workspace and (if
1440 * enabled) trigger a mouse pointer warp to the currently focused
1441 * container (!) on the target workspace.
1443 * Therefore, before calling workspace_show(), we make sure that
1444 * 'current' will be focused on the workspace. However, we cannot
1445 * just con_focus(current) because then the pointer will not be
1446 * warped at all (the code thinks we are already there).
1448 * So we focus 'current' to make it the currently focused window of
1449 * the target workspace, then revert focus. */
1450 Con *currently_focused = focused;
1451 con_focus(current->con);
1452 con_focus(currently_focused);
1454 /* Now switch to the workspace, then focus */
1456 LOG("focusing %p / %s\n", current->con, current->con->name);
1457 con_focus(current->con);
1462 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1463 "while only exactly one container can be focused at a time.\n", count);
1465 cmd_output->needs_tree_render = true;
1466 // XXX: default reply for now, make this a better reply
1471 * Implementation of 'fullscreen [global]'.
1474 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1475 if (fullscreen_mode == NULL)
1476 fullscreen_mode = "output";
1477 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1482 TAILQ_FOREACH(current, &owindows, owindows) {
1483 DLOG("matching: %p / %s\n", current->con, current->con->name);
1484 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1487 cmd_output->needs_tree_render = true;
1488 // XXX: default reply for now, make this a better reply
1493 * Implementation of 'move <direction> [<pixels> [px]]'.
1496 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1497 // 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
1498 int px = atoi(move_px);
1500 /* TODO: make 'move' work with criteria. */
1501 DLOG("moving in direction %s, px %s\n", direction, move_px);
1502 if (con_is_floating(focused)) {
1503 DLOG("floating move with %d pixels\n", px);
1504 Rect newrect = focused->parent->rect;
1505 if (strcmp(direction, "left") == 0) {
1507 } else if (strcmp(direction, "right") == 0) {
1509 } else if (strcmp(direction, "up") == 0) {
1511 } else if (strcmp(direction, "down") == 0) {
1514 floating_reposition(focused->parent, newrect);
1516 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1517 (strcmp(direction, "left") == 0 ? D_LEFT :
1518 (strcmp(direction, "up") == 0 ? D_UP :
1520 cmd_output->needs_tree_render = true;
1523 // XXX: default reply for now, make this a better reply
1528 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1531 void cmd_layout(I3_CMD, char *layout_str) {
1532 if (strcmp(layout_str, "stacking") == 0)
1533 layout_str = "stacked";
1536 /* default is a special case which will be handled in con_set_layout(). */
1537 if (strcmp(layout_str, "default") == 0)
1539 else if (strcmp(layout_str, "stacked") == 0)
1541 else if (strcmp(layout_str, "tabbed") == 0)
1543 else if (strcmp(layout_str, "splitv") == 0)
1545 else if (strcmp(layout_str, "splith") == 0)
1548 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1552 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1554 /* check if the match is empty, not if the result is empty */
1555 if (match_is_empty(current_match))
1556 con_set_layout(focused, layout);
1558 TAILQ_FOREACH(current, &owindows, owindows) {
1559 DLOG("matching: %p / %s\n", current->con, current->con->name);
1560 con_set_layout(current->con, layout);
1564 cmd_output->needs_tree_render = true;
1565 // XXX: default reply for now, make this a better reply
1570 * Implementation of 'layout toggle [all|split]'.
1573 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1576 if (toggle_mode == NULL)
1577 toggle_mode = "default";
1579 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1581 /* check if the match is empty, not if the result is empty */
1582 if (match_is_empty(current_match))
1583 con_toggle_layout(focused, toggle_mode);
1585 TAILQ_FOREACH(current, &owindows, owindows) {
1586 DLOG("matching: %p / %s\n", current->con, current->con->name);
1587 con_toggle_layout(current->con, toggle_mode);
1591 cmd_output->needs_tree_render = true;
1592 // XXX: default reply for now, make this a better reply
1597 * Implementation of 'exit'.
1600 void cmd_exit(I3_CMD) {
1601 LOG("Exiting due to user command.\n");
1602 xcb_disconnect(conn);
1609 * Implementation of 'reload'.
1612 void cmd_reload(I3_CMD) {
1614 kill_nagbar(&config_error_nagbar_pid, false);
1615 kill_nagbar(&command_error_nagbar_pid, false);
1616 load_configuration(conn, NULL, true);
1618 /* Send an IPC event just in case the ws names have changed */
1619 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1620 /* Send an update event for the barconfig just in case it has changed */
1623 // XXX: default reply for now, make this a better reply
1628 * Implementation of 'restart'.
1631 void cmd_restart(I3_CMD) {
1632 LOG("restarting i3\n");
1635 // XXX: default reply for now, make this a better reply
1640 * Implementation of 'open'.
1643 void cmd_open(I3_CMD) {
1644 LOG("opening new container\n");
1645 Con *con = tree_open_con(NULL, NULL);
1646 con->layout = L_SPLITH;
1653 y(integer, (long int)con);
1656 cmd_output->needs_tree_render = true;
1660 * Implementation of 'focus output <output>'.
1663 void cmd_focus_output(I3_CMD, char *name) {
1666 DLOG("name = %s\n", name);
1670 /* get the output */
1671 Output *current_output = NULL;
1674 TAILQ_FOREACH(current, &owindows, owindows)
1675 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1676 assert(current_output != NULL);
1678 output = get_output_from_string(current_output, name);
1681 LOG("No such output found.\n");
1686 /* get visible workspace on output */
1688 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1696 cmd_output->needs_tree_render = true;
1697 // XXX: default reply for now, make this a better reply
1702 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1705 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1710 if (!con_is_floating(focused)) {
1711 ELOG("Cannot change position. The window/container is not floating\n");
1712 yerror("Cannot change position. The window/container is not floating.");
1716 if (strcmp(method, "absolute") == 0) {
1717 focused->parent->rect.x = x;
1718 focused->parent->rect.y = y;
1720 DLOG("moving to absolute position %d %d\n", x, y);
1721 floating_maybe_reassign_ws(focused->parent);
1722 cmd_output->needs_tree_render = true;
1725 if (strcmp(method, "position") == 0) {
1726 Rect newrect = focused->parent->rect;
1728 DLOG("moving to position %d %d\n", x, y);
1732 floating_reposition(focused->parent, newrect);
1735 // XXX: default reply for now, make this a better reply
1740 * Implementation of 'move [window|container] [to] [absolute] position center
1743 void cmd_move_window_to_center(I3_CMD, char *method) {
1745 if (!con_is_floating(focused)) {
1746 ELOG("Cannot change position. The window/container is not floating\n");
1747 yerror("Cannot change position. The window/container is not floating.");
1751 if (strcmp(method, "absolute") == 0) {
1752 Rect *rect = &focused->parent->rect;
1754 DLOG("moving to absolute center\n");
1755 rect->x = croot->rect.width/2 - rect->width/2;
1756 rect->y = croot->rect.height/2 - rect->height/2;
1758 floating_maybe_reassign_ws(focused->parent);
1759 cmd_output->needs_tree_render = true;
1762 if (strcmp(method, "position") == 0) {
1763 Rect *wsrect = &con_get_workspace(focused)->rect;
1764 Rect newrect = focused->parent->rect;
1766 DLOG("moving to center\n");
1767 newrect.x = wsrect->width/2 - newrect.width/2;
1768 newrect.y = wsrect->height/2 - newrect.height/2;
1770 floating_reposition(focused->parent, newrect);
1773 // XXX: default reply for now, make this a better reply
1778 * Implementation of 'move scratchpad'.
1781 void cmd_move_scratchpad(I3_CMD) {
1782 DLOG("should move window to scratchpad\n");
1787 TAILQ_FOREACH(current, &owindows, owindows) {
1788 DLOG("matching: %p / %s\n", current->con, current->con->name);
1789 scratchpad_move(current->con);
1792 cmd_output->needs_tree_render = true;
1793 // XXX: default reply for now, make this a better reply
1798 * Implementation of 'scratchpad show'.
1801 void cmd_scratchpad_show(I3_CMD) {
1802 DLOG("should show scratchpad window\n");
1805 if (match_is_empty(current_match)) {
1806 scratchpad_show(NULL);
1808 TAILQ_FOREACH(current, &owindows, owindows) {
1809 DLOG("matching: %p / %s\n", current->con, current->con->name);
1810 scratchpad_show(current->con);
1814 cmd_output->needs_tree_render = true;
1815 // XXX: default reply for now, make this a better reply
1820 * Implementation of 'rename workspace [<name>] to <name>'
1823 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1825 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1827 LOG("Renaming current workspace to \"%s\"\n", new_name);
1830 Con *output, *workspace = NULL;
1832 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1833 GREP_FIRST(workspace, output_get_content(output),
1834 !strcasecmp(child->name, old_name));
1836 workspace = con_get_workspace(focused);
1840 // TODO: we should include the old workspace name here and use yajl for
1841 // generating the reply.
1842 // TODO: better error message
1843 yerror("Old workspace not found");
1847 Con *check_dest = NULL;
1848 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1849 GREP_FIRST(check_dest, output_get_content(output),
1850 !strcasecmp(child->name, new_name));
1852 if (check_dest != NULL) {
1853 // TODO: we should include the new workspace name here and use yajl for
1854 // generating the reply.
1855 // TODO: better error message
1856 yerror("New workspace already exists");
1860 /* Change the name and try to parse it as a number. */
1861 FREE(workspace->name);
1862 workspace->name = sstrdup(new_name);
1863 char *endptr = NULL;
1864 long parsed_num = strtol(new_name, &endptr, 10);
1865 if (parsed_num == LONG_MIN ||
1866 parsed_num == LONG_MAX ||
1869 workspace->num = -1;
1870 else workspace->num = parsed_num;
1871 LOG("num = %d\n", workspace->num);
1873 /* By re-attaching, the sort order will be correct afterwards. */
1874 Con *previously_focused = focused;
1875 Con *parent = workspace->parent;
1876 con_detach(workspace);
1877 con_attach(workspace, parent, false);
1878 /* Restore the previous focus since con_attach messes with the focus. */
1879 con_focus(previously_focused);
1881 cmd_output->needs_tree_render = true;
1884 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1888 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1891 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1893 bool toggle = false;
1894 if (strcmp(bar_mode, "dock") == 0)
1896 else if (strcmp(bar_mode, "hide") == 0)
1898 else if (strcmp(bar_mode, "invisible") == 0)
1900 else if (strcmp(bar_mode, "toggle") == 0)
1903 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1907 bool changed_sth = false;
1908 Barconfig *current = NULL;
1909 TAILQ_FOREACH(current, &barconfigs, configs) {
1910 if (bar_id && strcmp(current->id, bar_id) != 0)
1914 mode = (current->mode + 1) % 2;
1916 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1917 current->mode = mode;
1924 if (bar_id && !changed_sth) {
1925 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1933 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
1936 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
1937 int hidden_state = S_SHOW;
1938 bool toggle = false;
1939 if (strcmp(bar_hidden_state, "hide") == 0)
1940 hidden_state = S_HIDE;
1941 else if (strcmp(bar_hidden_state, "show") == 0)
1942 hidden_state = S_SHOW;
1943 else if (strcmp(bar_hidden_state, "toggle") == 0)
1946 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
1950 bool changed_sth = false;
1951 Barconfig *current = NULL;
1952 TAILQ_FOREACH(current, &barconfigs, configs) {
1953 if (bar_id && strcmp(current->id, bar_id) != 0)
1957 hidden_state = (current->hidden_state + 1) % 2;
1959 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
1960 current->hidden_state = hidden_state;
1967 if (bar_id && !changed_sth) {
1968 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
1976 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
1979 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
1981 if (strcmp(bar_type, "mode") == 0)
1982 ret = cmd_bar_mode(bar_value, bar_id);
1983 else if (strcmp(bar_type, "hidden_state") == 0)
1984 ret = cmd_bar_hidden_state(bar_value, bar_id);
1986 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
1998 * Implementation of 'shmlog <size>|toggle|on|off'
2001 void cmd_shmlog(I3_CMD, char *argument) {
2002 if (!strcmp(argument,"toggle"))
2003 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2004 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2005 else if (!strcmp(argument, "on"))
2006 shmlog_size = default_shmlog_size;
2007 else if (!strcmp(argument, "off"))
2010 /* If shm logging now, restart logging with the new size. */
2011 if (shmlog_size > 0) {
2013 LOG("Restarting shm logging...\n");
2016 shmlog_size = atoi(argument);
2017 /* Make a weakly attempt at ensuring the argument is valid. */
2018 if (shmlog_size <= 0)
2019 shmlog_size = default_shmlog_size;
2021 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2023 update_shmlog_atom();
2024 // XXX: default reply for now, make this a better reply
2029 * Implementation of 'debuglog toggle|on|off'
2032 void cmd_debuglog(I3_CMD, char *argument) {
2033 bool logging = get_debug_logging();
2034 if (!strcmp(argument,"toggle")) {
2035 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2036 set_debug_logging(!logging);
2037 } else if (!strcmp(argument, "on") && !logging) {
2038 LOG("Enabling debug logging\n");
2039 set_debug_logging(true);
2040 } else if (!strcmp(argument, "off") && logging) {
2041 LOG("Disabling debug logging\n");
2042 set_debug_logging(false);
2044 // XXX: default reply for now, make this a better reply