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 { \
28 /** When the command did not include match criteria (!), we use the currently
29 * focused container. Do not confuse this case with a command which included
30 * criteria but which did not match any windows. This macro has to be called in
33 #define HANDLE_EMPTY_MATCH do { \
34 if (match_is_empty(current_match)) { \
35 owindow *ow = smalloc(sizeof(owindow)); \
37 TAILQ_INIT(&owindows); \
38 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
44 * Returns true if a is definitely greater than b (using the given epsilon)
47 static bool definitelyGreaterThan(float a, float b, float epsilon) {
48 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
52 * Returns an 'output' corresponding to one of left/right/down/up or a specific
56 static Output *get_output_from_string(Output *current_output, const char *output_str) {
59 if (strcasecmp(output_str, "left") == 0)
60 output = get_output_next_wrap(D_LEFT, current_output);
61 else if (strcasecmp(output_str, "right") == 0)
62 output = get_output_next_wrap(D_RIGHT, current_output);
63 else if (strcasecmp(output_str, "up") == 0)
64 output = get_output_next_wrap(D_UP, current_output);
65 else if (strcasecmp(output_str, "down") == 0)
66 output = get_output_next_wrap(D_DOWN, current_output);
67 else output = get_output_by_name(output_str);
73 * Checks whether we switched to a new workspace and returns false in that case,
74 * signaling that further workspace switching should be done by the calling function
75 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
76 * and return true, signaling that no further workspace switching should occur in the calling function.
79 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
80 Con *ws = con_get_workspace(focused);
82 /* If we switched to a different workspace, do nothing */
83 if (strcmp(ws->name, name) != 0)
86 DLOG("This workspace is already focused.\n");
87 if (config.workspace_auto_back_and_forth) {
88 workspace_back_and_forth();
89 cmd_output->needs_tree_render = true;
95 * Return the passed workspace unless it is the current one and auto back and
96 * forth is enabled, in which case the back_and_forth workspace is returned.
98 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
101 if (!config.workspace_auto_back_and_forth)
104 current = con_get_workspace(focused);
106 if (current == workspace) {
107 baf = workspace_back_and_forth_get();
109 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
117 // This code is commented out because we might recycle it for popping up error
118 // messages on parser errors.
120 static pid_t migration_pid = -1;
123 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
124 * it exited (or could not be started, depending on the exit code).
127 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
128 ev_child_stop(EV_A_ watcher);
129 if (!WIFEXITED(watcher->rstatus)) {
130 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
134 int exitcode = WEXITSTATUS(watcher->rstatus);
135 printf("i3-nagbar process exited with status %d\n", exitcode);
137 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
143 /* We need ev >= 4 for the following code. Since it is not *that* important (it
144 * only makes sure that there are no i3-nagbar instances left behind) we still
145 * support old systems with libev 3. */
146 #if EV_VERSION_MAJOR >= 4
148 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
149 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
152 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
153 if (migration_pid != -1) {
154 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
155 kill(migration_pid, SIGKILL);
160 void cmd_MIGRATION_start_nagbar(void) {
161 if (migration_pid != -1) {
162 fprintf(stderr, "i3-nagbar already running.\n");
165 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
166 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
167 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
168 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
169 migration_pid = fork();
170 if (migration_pid == -1) {
171 warn("Could not fork()");
176 if (migration_pid == 0) {
178 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
180 NULL, /* will be replaced by the executable path */
184 "You found a parsing error. Please, please, please, report it!",
190 exec_i3_utility("i3-nagbar", argv);
194 /* install a child watcher */
195 ev_child *child = smalloc(sizeof(ev_child));
196 ev_child_init(child, &nagbar_exited, migration_pid, 0);
197 ev_child_start(main_loop, child);
199 /* We need ev >= 4 for the following code. Since it is not *that* important (it
200 * only makes sure that there are no i3-nagbar instances left behind) we still
201 * support old systems with libev 3. */
202 #if EV_VERSION_MAJOR >= 4
203 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
205 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
206 ev_cleanup_init(cleanup, nagbar_cleanup);
207 ev_cleanup_start(main_loop, cleanup);
213 /*******************************************************************************
214 * Criteria functions.
215 ******************************************************************************/
218 * Helper data structure for an operation window (window on which the operation
219 * will be performed). Used to build the TAILQ owindows.
222 typedef struct owindow {
224 TAILQ_ENTRY(owindow) owindows;
227 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
229 static owindows_head owindows;
232 * Initializes the specified 'Match' data structure and the initial state of
233 * commands.c for matching target windows of a command.
236 void cmd_criteria_init(I3_CMD) {
240 DLOG("Initializing criteria, current_match = %p\n", current_match);
241 match_init(current_match);
242 while (!TAILQ_EMPTY(&owindows)) {
243 ow = TAILQ_FIRST(&owindows);
244 TAILQ_REMOVE(&owindows, ow, owindows);
247 TAILQ_INIT(&owindows);
249 TAILQ_FOREACH(con, &all_cons, all_cons) {
250 ow = smalloc(sizeof(owindow));
252 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
257 * A match specification just finished (the closing square bracket was found),
258 * so we filter the list of owindows.
261 void cmd_criteria_match_windows(I3_CMD) {
262 owindow *next, *current;
264 DLOG("match specification finished, matching...\n");
265 /* copy the old list head to iterate through it and start with a fresh
266 * list which will contain only matching windows */
267 struct owindows_head old = owindows;
268 TAILQ_INIT(&owindows);
269 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
270 /* make a copy of the next pointer and advance the pointer to the
271 * next element as we are going to invalidate the element’s
272 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
274 next = TAILQ_NEXT(next, owindows);
276 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
277 if (current_match->con_id != NULL) {
278 if (current_match->con_id == current->con) {
279 DLOG("matches container!\n");
280 TAILQ_INSERT_TAIL(&owindows, current, owindows);
282 } else if (current_match->mark != NULL && current->con->mark != NULL &&
283 regex_matches(current_match->mark, current->con->mark)) {
284 DLOG("match by mark\n");
285 TAILQ_INSERT_TAIL(&owindows, current, owindows);
287 if (current->con->window == NULL)
289 if (match_matches_window(current_match, current->con->window)) {
290 DLOG("matches window!\n");
291 TAILQ_INSERT_TAIL(&owindows, current, owindows);
293 DLOG("doesnt match\n");
299 TAILQ_FOREACH(current, &owindows, owindows) {
300 DLOG("matching: %p / %s\n", current->con, current->con->name);
305 * Interprets a ctype=cvalue pair and adds it to the current match
309 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
310 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
312 if (strcmp(ctype, "class") == 0) {
313 current_match->class = regex_new(cvalue);
317 if (strcmp(ctype, "instance") == 0) {
318 current_match->instance = regex_new(cvalue);
322 if (strcmp(ctype, "window_role") == 0) {
323 current_match->role = regex_new(cvalue);
327 if (strcmp(ctype, "con_id") == 0) {
329 long parsed = strtol(cvalue, &end, 10);
330 if (parsed == LONG_MIN ||
331 parsed == LONG_MAX ||
333 (end && *end != '\0')) {
334 ELOG("Could not parse con id \"%s\"\n", cvalue);
336 current_match->con_id = (Con*)parsed;
337 printf("id as int = %p\n", current_match->con_id);
342 if (strcmp(ctype, "id") == 0) {
344 long parsed = strtol(cvalue, &end, 10);
345 if (parsed == LONG_MIN ||
346 parsed == LONG_MAX ||
348 (end && *end != '\0')) {
349 ELOG("Could not parse window id \"%s\"\n", cvalue);
351 current_match->id = parsed;
352 printf("window id as int = %d\n", current_match->id);
357 if (strcmp(ctype, "con_mark") == 0) {
358 current_match->mark = regex_new(cvalue);
362 if (strcmp(ctype, "title") == 0) {
363 current_match->title = regex_new(cvalue);
367 if (strcmp(ctype, "urgent") == 0) {
368 if (strcasecmp(cvalue, "latest") == 0 ||
369 strcasecmp(cvalue, "newest") == 0 ||
370 strcasecmp(cvalue, "recent") == 0 ||
371 strcasecmp(cvalue, "last") == 0) {
372 current_match->urgent = U_LATEST;
373 } else if (strcasecmp(cvalue, "oldest") == 0 ||
374 strcasecmp(cvalue, "first") == 0) {
375 current_match->urgent = U_OLDEST;
380 ELOG("Unknown criterion: %s\n", ctype);
384 * Implementation of 'move [window|container] [to] workspace
385 * next|prev|next_on_output|prev_on_output|current'.
388 void cmd_move_con_to_workspace(I3_CMD, char *which) {
391 DLOG("which=%s\n", which);
393 /* We have nothing to move:
394 * when criteria was specified but didn't match any window or
395 * when criteria wasn't specified and we don't have any window focused. */
396 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
397 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
398 !con_has_children(focused))) {
405 /* get the workspace */
407 if (strcmp(which, "next") == 0)
408 ws = workspace_next();
409 else if (strcmp(which, "prev") == 0)
410 ws = workspace_prev();
411 else if (strcmp(which, "next_on_output") == 0)
412 ws = workspace_next_on_output();
413 else if (strcmp(which, "prev_on_output") == 0)
414 ws = workspace_prev_on_output();
415 else if (strcmp(which, "current") == 0)
416 ws = con_get_workspace(focused);
418 ELOG("BUG: called with which=%s\n", which);
423 TAILQ_FOREACH(current, &owindows, owindows) {
424 DLOG("matching: %p / %s\n", current->con, current->con->name);
425 con_move_to_workspace(current->con, ws, true, false);
428 cmd_output->needs_tree_render = true;
429 // XXX: default reply for now, make this a better reply
434 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
437 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
441 ws = workspace_back_and_forth_get();
448 ystr("No workspace was previously active.");
455 TAILQ_FOREACH(current, &owindows, owindows) {
456 DLOG("matching: %p / %s\n", current->con, current->con->name);
457 con_move_to_workspace(current->con, ws, true, false);
460 cmd_output->needs_tree_render = true;
461 // XXX: default reply for now, make this a better reply
466 * Implementation of 'move [window|container] [to] workspace <name>'.
469 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
470 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
471 LOG("You cannot switch to the i3 internal workspaces.\n");
478 /* We have nothing to move:
479 * when criteria was specified but didn't match any window or
480 * when criteria wasn't specified and we don't have any window focused. */
481 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
482 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 */
494 Con *ws = workspace_get(name, NULL);
496 ws = maybe_auto_back_and_forth_workspace(ws);
500 TAILQ_FOREACH(current, &owindows, owindows) {
501 DLOG("matching: %p / %s\n", current->con, current->con->name);
502 con_move_to_workspace(current->con, ws, true, false);
505 cmd_output->needs_tree_render = true;
506 // XXX: default reply for now, make this a better reply
511 * Implementation of 'move [window|container] [to] workspace number <name>'.
514 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
517 /* We have nothing to move:
518 * when criteria was specified but didn't match any window or
519 * when criteria wasn't specified and we don't have any window focused. */
520 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
521 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
522 !con_has_children(focused))) {
527 LOG("should move window to workspace %s\n", which);
528 /* get the workspace */
529 Con *output, *workspace = NULL;
532 long parsed_num = strtol(which, &endptr, 10);
533 if (parsed_num == LONG_MIN ||
534 parsed_num == LONG_MAX ||
537 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
542 // TODO: better error message
543 ystr("Could not parse number");
548 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
549 GREP_FIRST(workspace, output_get_content(output),
550 child->num == parsed_num);
553 workspace = workspace_get(which, NULL);
556 workspace = maybe_auto_back_and_forth_workspace(workspace);
560 TAILQ_FOREACH(current, &owindows, owindows) {
561 DLOG("matching: %p / %s\n", current->con, current->con->name);
562 con_move_to_workspace(current->con, workspace, true, false);
565 cmd_output->needs_tree_render = true;
566 // XXX: default reply for now, make this a better reply
570 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
571 LOG("floating resize\n");
572 Rect old_rect = floating_con->rect;
573 Con *focused_con = con_descend_focused(floating_con);
575 /* ensure that resize will take place even if pixel increment is smaller than
576 * height increment or width increment.
578 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
579 strcmp(direction, "height") == 0) {
581 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
583 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
584 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
586 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
588 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
591 if (strcmp(direction, "up") == 0) {
592 floating_con->rect.height += px;
593 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
594 floating_con->rect.height += px;
595 } else if (strcmp(direction, "left") == 0) {
596 floating_con->rect.width += px;
598 floating_con->rect.width += px;
601 floating_check_size(floating_con);
603 /* Did we actually resize anything or did the size constraints prevent us?
604 * If we could not resize, exit now to not move the window. */
605 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
608 if (strcmp(direction, "up") == 0) {
609 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
610 } else if (strcmp(direction, "left") == 0) {
611 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
614 /* If this is a scratchpad window, don't auto center it from now on. */
615 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
616 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
619 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
620 LOG("tiling resize\n");
621 /* get the appropriate current container (skip stacked/tabbed cons) */
623 double percentage = 0;
624 while (current->parent->layout == L_STACKED ||
625 current->parent->layout == L_TABBED)
626 current = current->parent;
628 /* Then further go up until we find one with the matching orientation. */
629 orientation_t search_orientation =
630 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
633 if (con_orientation(current->parent) != search_orientation) {
634 current = current->parent;
638 /* get the default percentage */
639 int children = con_num_children(current->parent);
640 LOG("ins. %d children\n", children);
641 percentage = 1.0 / children;
642 LOG("default percentage = %f\n", percentage);
644 orientation_t orientation = con_orientation(current->parent);
646 if ((orientation == HORIZ &&
647 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
648 (orientation == VERT &&
649 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
650 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
651 (orientation == HORIZ ? "horizontal" : "vertical"));
656 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
657 other = TAILQ_PREV(current, nodes_head, nodes);
659 other = TAILQ_NEXT(current, nodes);
661 if (other == TAILQ_END(workspaces)) {
662 LOG("No other container in this direction found, trying to look further up in the tree...\n");
663 current = current->parent;
667 } while (current->type != CT_WORKSPACE &&
668 current->type != CT_FLOATING_CON);
671 LOG("No other container in this direction found, trying to look further up in the tree...\n");
676 LOG("other->percent = %f\n", other->percent);
677 LOG("current->percent before = %f\n", current->percent);
678 if (current->percent == 0.0)
679 current->percent = percentage;
680 if (other->percent == 0.0)
681 other->percent = percentage;
682 double new_current_percent = current->percent + ((double)ppt / 100.0);
683 double new_other_percent = other->percent - ((double)ppt / 100.0);
684 LOG("new_current_percent = %f\n", new_current_percent);
685 LOG("new_other_percent = %f\n", new_other_percent);
686 /* Ensure that the new percentages are positive and greater than
687 * 0.05 to have a reasonable minimum size. */
688 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
689 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
690 current->percent += ((double)ppt / 100.0);
691 other->percent -= ((double)ppt / 100.0);
692 LOG("current->percent after = %f\n", current->percent);
693 LOG("other->percent after = %f\n", other->percent);
695 LOG("Not resizing, already at minimum size\n");
701 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
702 LOG("width/height resize\n");
703 /* get the appropriate current container (skip stacked/tabbed cons) */
704 while (current->parent->layout == L_STACKED ||
705 current->parent->layout == L_TABBED)
706 current = current->parent;
708 /* Then further go up until we find one with the matching orientation. */
709 orientation_t search_orientation =
710 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
712 while (current->type != CT_WORKSPACE &&
713 current->type != CT_FLOATING_CON &&
714 con_orientation(current->parent) != search_orientation)
715 current = current->parent;
717 /* get the default percentage */
718 int children = con_num_children(current->parent);
719 LOG("ins. %d children\n", children);
720 double percentage = 1.0 / children;
721 LOG("default percentage = %f\n", percentage);
723 orientation_t orientation = con_orientation(current->parent);
725 if ((orientation == HORIZ &&
726 strcmp(direction, "height") == 0) ||
727 (orientation == VERT &&
728 strcmp(direction, "width") == 0)) {
729 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
730 (orientation == HORIZ ? "horizontal" : "vertical"));
736 LOG("This is the only container, cannot resize.\n");
741 /* Ensure all the other children have a percentage set. */
743 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
744 LOG("child->percent = %f (child %p)\n", child->percent, child);
745 if (child->percent == 0.0)
746 child->percent = percentage;
749 double new_current_percent = current->percent + ((double)ppt / 100.0);
750 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
751 LOG("new_current_percent = %f\n", new_current_percent);
752 LOG("subtract_percent = %f\n", subtract_percent);
753 /* Ensure that the new percentages are positive and greater than
754 * 0.05 to have a reasonable minimum size. */
755 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
756 if (child == current)
758 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
759 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
764 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
765 LOG("Not resizing, already at minimum size\n");
770 current->percent += ((double)ppt / 100.0);
771 LOG("current->percent after = %f\n", current->percent);
773 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
774 if (child == current)
776 child->percent -= subtract_percent;
777 LOG("child->percent after (%p) = %f\n", child, child->percent);
784 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
787 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
788 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
789 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
790 // 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
791 int px = atoi(resize_px);
792 int ppt = atoi(resize_ppt);
793 if (strcmp(way, "shrink") == 0) {
801 TAILQ_FOREACH(current, &owindows, owindows) {
803 if ((floating_con = con_inside_floating(current->con))) {
804 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
806 if (strcmp(direction, "width") == 0 ||
807 strcmp(direction, "height") == 0) {
808 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
811 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
817 cmd_output->needs_tree_render = true;
818 // XXX: default reply for now, make this a better reply
823 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
826 void cmd_border(I3_CMD, char *border_style_str, char *border_width ) {
827 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
832 TAILQ_FOREACH(current, &owindows, owindows) {
833 DLOG("matching: %p / %s\n", current->con, current->con->name);
834 int border_style = current->con->border_style;
836 int tmp_border_width = -1;
837 tmp_border_width = strtol(border_width, &end, 10);
838 if (end == border_width) {
839 /* no valid digits found */
840 tmp_border_width = -1;
842 if (strcmp(border_style_str, "toggle") == 0) {
845 if (border_style == BS_NORMAL)
846 tmp_border_width = 2;
847 else if (border_style == BS_NONE)
848 tmp_border_width = 0;
849 else if (border_style == BS_PIXEL)
850 tmp_border_width = 1;
852 if (strcmp(border_style_str, "normal") == 0)
853 border_style = BS_NORMAL;
854 else if (strcmp(border_style_str, "pixel") == 0)
855 border_style = BS_PIXEL;
856 else if (strcmp(border_style_str, "1pixel") == 0){
857 border_style = BS_PIXEL;
858 tmp_border_width = 1;
859 } else if (strcmp(border_style_str, "none") == 0)
860 border_style = BS_NONE;
862 ELOG("BUG: called with border_style=%s\n", border_style_str);
867 con_set_border_style(current->con, border_style, tmp_border_width);
870 cmd_output->needs_tree_render = true;
871 // XXX: default reply for now, make this a better reply
876 * Implementation of 'nop <comment>'.
879 void cmd_nop(I3_CMD, char *comment) {
880 LOG("-------------------------------------------------\n");
881 LOG(" NOP: %s\n", comment);
882 LOG("-------------------------------------------------\n");
886 * Implementation of 'append_layout <path>'.
889 void cmd_append_layout(I3_CMD, char *path) {
890 LOG("Appending layout \"%s\"\n", path);
891 tree_append_json(path);
893 cmd_output->needs_tree_render = true;
894 // XXX: default reply for now, make this a better reply
899 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
902 void cmd_workspace(I3_CMD, char *which) {
905 DLOG("which=%s\n", which);
907 if (strcmp(which, "next") == 0)
908 ws = workspace_next();
909 else if (strcmp(which, "prev") == 0)
910 ws = workspace_prev();
911 else if (strcmp(which, "next_on_output") == 0)
912 ws = workspace_next_on_output();
913 else if (strcmp(which, "prev_on_output") == 0)
914 ws = workspace_prev_on_output();
916 ELOG("BUG: called with which=%s\n", which);
923 cmd_output->needs_tree_render = true;
924 // XXX: default reply for now, make this a better reply
929 * Implementation of 'workspace number <name>'
932 void cmd_workspace_number(I3_CMD, char *which) {
933 Con *output, *workspace = NULL;
936 long parsed_num = strtol(which, &endptr, 10);
937 if (parsed_num == LONG_MIN ||
938 parsed_num == LONG_MAX ||
941 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
946 // TODO: better error message
947 ystr("Could not parse number");
953 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
954 GREP_FIRST(workspace, output_get_content(output),
955 child->num == parsed_num);
958 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
960 workspace_show_by_name(which);
961 cmd_output->needs_tree_render = true;
964 if (maybe_back_and_forth(cmd_output, workspace->name))
966 workspace_show(workspace);
968 cmd_output->needs_tree_render = true;
969 // XXX: default reply for now, make this a better reply
974 * Implementation of 'workspace back_and_forth'.
977 void cmd_workspace_back_and_forth(I3_CMD) {
978 workspace_back_and_forth();
980 cmd_output->needs_tree_render = true;
981 // XXX: default reply for now, make this a better reply
986 * Implementation of 'workspace <name>'
989 void cmd_workspace_name(I3_CMD, char *name) {
990 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
991 LOG("You cannot switch to the i3 internal workspaces.\n");
996 DLOG("should switch to workspace %s\n", name);
997 if (maybe_back_and_forth(cmd_output, name))
999 workspace_show_by_name(name);
1001 cmd_output->needs_tree_render = true;
1002 // XXX: default reply for now, make this a better reply
1007 * Implementation of 'mark <mark>'
1010 void cmd_mark(I3_CMD, char *mark) {
1011 DLOG("Clearing all windows which have that mark first\n");
1014 TAILQ_FOREACH(con, &all_cons, all_cons) {
1015 if (con->mark && strcmp(con->mark, mark) == 0)
1019 DLOG("marking window with str %s\n", mark);
1024 TAILQ_FOREACH(current, &owindows, owindows) {
1025 DLOG("matching: %p / %s\n", current->con, current->con->name);
1026 current->con->mark = sstrdup(mark);
1029 cmd_output->needs_tree_render = true;
1030 // XXX: default reply for now, make this a better reply
1035 * Implementation of 'mode <string>'.
1038 void cmd_mode(I3_CMD, char *mode) {
1039 DLOG("mode=%s\n", mode);
1042 // XXX: default reply for now, make this a better reply
1047 * Implementation of 'move [window|container] [to] output <str>'.
1050 void cmd_move_con_to_output(I3_CMD, char *name) {
1053 DLOG("should move window to output %s\n", name);
1057 /* get the output */
1058 Output *current_output = NULL;
1061 // TODO: fix the handling of criteria
1062 TAILQ_FOREACH(current, &owindows, owindows)
1063 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1065 assert(current_output != NULL);
1067 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1068 if (strcasecmp(name, "up") == 0)
1069 output = get_output_next_wrap(D_UP, current_output);
1070 else if (strcasecmp(name, "down") == 0)
1071 output = get_output_next_wrap(D_DOWN, current_output);
1072 else if (strcasecmp(name, "left") == 0)
1073 output = get_output_next_wrap(D_LEFT, current_output);
1074 else if (strcasecmp(name, "right") == 0)
1075 output = get_output_next_wrap(D_RIGHT, current_output);
1077 output = get_output_by_name(name);
1080 LOG("No such output found.\n");
1085 /* get visible workspace on output */
1087 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1093 TAILQ_FOREACH(current, &owindows, owindows) {
1094 DLOG("matching: %p / %s\n", current->con, current->con->name);
1095 con_move_to_workspace(current->con, ws, true, false);
1098 cmd_output->needs_tree_render = true;
1099 // XXX: default reply for now, make this a better reply
1104 * Implementation of 'floating enable|disable|toggle'
1107 void cmd_floating(I3_CMD, char *floating_mode) {
1110 DLOG("floating_mode=%s\n", floating_mode);
1114 TAILQ_FOREACH(current, &owindows, owindows) {
1115 DLOG("matching: %p / %s\n", current->con, current->con->name);
1116 if (strcmp(floating_mode, "toggle") == 0) {
1117 DLOG("should toggle mode\n");
1118 toggle_floating_mode(current->con, false);
1120 DLOG("should switch mode to %s\n", floating_mode);
1121 if (strcmp(floating_mode, "enable") == 0) {
1122 floating_enable(current->con, false);
1124 floating_disable(current->con, false);
1129 cmd_output->needs_tree_render = true;
1130 // XXX: default reply for now, make this a better reply
1135 * Implementation of 'move workspace to [output] <str>'.
1138 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1139 DLOG("should move workspace to output %s\n", name);
1144 TAILQ_FOREACH(current, &owindows, owindows) {
1145 Output *current_output = get_output_containing(current->con->rect.x,
1146 current->con->rect.y);
1147 if (!current_output) {
1148 ELOG("Cannot get current output. This is a bug in i3.\n");
1152 Output *output = get_output_from_string(current_output, name);
1154 ELOG("Could not get output from string \"%s\"\n", name);
1159 Con *content = output_get_content(output->con);
1160 LOG("got output %p with content %p\n", output, content);
1162 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1163 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1165 Con *ws = con_get_workspace(current->con);
1166 LOG("should move workspace %p / %s\n", ws, ws->name);
1167 bool workspace_was_visible = workspace_is_visible(ws);
1169 if (con_num_children(ws->parent) == 1) {
1170 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1172 /* check if we can find a workspace assigned to this output */
1173 bool used_assignment = false;
1174 struct Workspace_Assignment *assignment;
1175 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1176 if (strcmp(assignment->output, current_output->name) != 0)
1179 /* check if this workspace is already attached to the tree */
1180 Con *workspace = NULL, *out;
1181 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1182 GREP_FIRST(workspace, output_get_content(out),
1183 !strcasecmp(child->name, assignment->name));
1184 if (workspace != NULL)
1187 /* so create the workspace referenced to by this assignment */
1188 LOG("Creating workspace from assignment %s.\n", assignment->name);
1189 workspace_get(assignment->name, NULL);
1190 used_assignment = true;
1194 /* if we couldn't create the workspace using an assignment, create
1195 * it on the output */
1196 if (!used_assignment)
1197 create_workspace_on_output(current_output, ws->parent);
1199 /* notify the IPC listeners */
1200 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1202 DLOG("Detaching\n");
1204 /* detach from the old output and attach to the new output */
1205 Con *old_content = ws->parent;
1207 if (workspace_was_visible) {
1208 /* The workspace which we just detached was visible, so focus
1209 * the next one in the focus-stack. */
1210 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1211 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1212 workspace_show(focus_ws);
1214 con_attach(ws, content, false);
1216 /* fix the coordinates of the floating containers */
1218 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1219 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1221 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1222 if (workspace_was_visible) {
1223 /* Focus the moved workspace on the destination output. */
1227 /* NB: We cannot simply work with previously_visible_ws since it might
1228 * have been cleaned up by workspace_show() already, depending on the
1229 * focus order/number of other workspaces on the output.
1230 * Instead, we loop through the available workspaces and only work with
1231 * previously_visible_ws if we still find it. */
1232 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1233 if (ws != previously_visible_ws)
1236 /* Call the on_remove_child callback of the workspace which previously
1237 * was visible on the destination output. Since it is no longer
1238 * visible, it might need to get cleaned up. */
1239 CALL(previously_visible_ws, on_remove_child);
1244 cmd_output->needs_tree_render = true;
1245 // XXX: default reply for now, make this a better reply
1250 * Implementation of 'split v|h|vertical|horizontal'.
1253 void cmd_split(I3_CMD, char *direction) {
1255 /* TODO: use matches */
1256 LOG("splitting in direction %c\n", direction[0]);
1257 if (match_is_empty(current_match))
1258 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1260 TAILQ_FOREACH(current, &owindows, owindows) {
1261 DLOG("matching: %p / %s\n", current->con, current->con->name);
1262 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1266 cmd_output->needs_tree_render = true;
1267 // XXX: default reply for now, make this a better reply
1272 * Implementation of 'kill [window|client]'.
1275 void cmd_kill(I3_CMD, char *kill_mode_str) {
1276 if (kill_mode_str == NULL)
1277 kill_mode_str = "window";
1280 DLOG("kill_mode=%s\n", kill_mode_str);
1283 if (strcmp(kill_mode_str, "window") == 0)
1284 kill_mode = KILL_WINDOW;
1285 else if (strcmp(kill_mode_str, "client") == 0)
1286 kill_mode = KILL_CLIENT;
1288 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1293 /* check if the match is empty, not if the result is empty */
1294 if (match_is_empty(current_match))
1295 tree_close_con(kill_mode);
1297 TAILQ_FOREACH(current, &owindows, owindows) {
1298 DLOG("matching: %p / %s\n", current->con, current->con->name);
1299 tree_close(current->con, kill_mode, false, false);
1303 cmd_output->needs_tree_render = true;
1304 // XXX: default reply for now, make this a better reply
1309 * Implementation of 'exec [--no-startup-id] <command>'.
1312 void cmd_exec(I3_CMD, char *nosn, char *command) {
1313 bool no_startup_id = (nosn != NULL);
1315 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1316 start_application(command, no_startup_id);
1318 // XXX: default reply for now, make this a better reply
1323 * Implementation of 'focus left|right|up|down'.
1326 void cmd_focus_direction(I3_CMD, char *direction) {
1327 DLOG("direction = *%s*\n", direction);
1329 if (strcmp(direction, "left") == 0)
1330 tree_next('p', HORIZ);
1331 else if (strcmp(direction, "right") == 0)
1332 tree_next('n', HORIZ);
1333 else if (strcmp(direction, "up") == 0)
1334 tree_next('p', VERT);
1335 else if (strcmp(direction, "down") == 0)
1336 tree_next('n', VERT);
1338 ELOG("Invalid focus direction (%s)\n", direction);
1343 cmd_output->needs_tree_render = true;
1344 // XXX: default reply for now, make this a better reply
1349 * Implementation of 'focus tiling|floating|mode_toggle'.
1352 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1353 DLOG("window_mode = %s\n", window_mode);
1355 Con *ws = con_get_workspace(focused);
1358 if (strcmp(window_mode, "mode_toggle") == 0) {
1359 current = TAILQ_FIRST(&(ws->focus_head));
1360 if (current != NULL && current->type == CT_FLOATING_CON)
1361 window_mode = "tiling";
1362 else window_mode = "floating";
1364 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1365 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1366 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1369 con_focus(con_descend_focused(current));
1374 cmd_output->needs_tree_render = true;
1375 // XXX: default reply for now, make this a better reply
1380 * Implementation of 'focus parent|child'.
1383 void cmd_focus_level(I3_CMD, char *level) {
1384 DLOG("level = %s\n", level);
1385 bool success = false;
1387 /* Focusing the parent can only be allowed if the newly
1388 * focused container won't escape the fullscreen container. */
1389 if (strcmp(level, "parent") == 0) {
1390 if (focused && focused->parent) {
1391 if (con_fullscreen_permits_focusing(focused->parent))
1392 success = level_up();
1394 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1398 /* Focusing a child should always be allowed. */
1399 else success = level_down();
1401 cmd_output->needs_tree_render = success;
1402 // XXX: default reply for now, make this a better reply
1407 * Implementation of 'focus'.
1410 void cmd_focus(I3_CMD) {
1411 DLOG("current_match = %p\n", current_match);
1413 if (match_is_empty(current_match)) {
1414 ELOG("You have to specify which window/container should be focused.\n");
1415 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1421 ystr("You have to specify which window/container should be focused");
1427 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1430 TAILQ_FOREACH(current, &owindows, owindows) {
1431 Con *ws = con_get_workspace(current->con);
1432 /* If no workspace could be found, this was a dock window.
1433 * Just skip it, you cannot focus dock windows. */
1437 /* Check the fullscreen focus constraints. */
1438 if (!con_fullscreen_permits_focusing(current->con)) {
1439 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1444 /* In case this is a scratchpad window, call scratchpad_show(). */
1445 if (ws == __i3_scratch) {
1446 scratchpad_show(current->con);
1448 /* While for the normal focus case we can change focus multiple
1449 * times and only a single window ends up focused, we could show
1450 * multiple scratchpad windows. So, rather break here. */
1454 /* If the container is not on the current workspace,
1455 * workspace_show() will switch to a different workspace and (if
1456 * enabled) trigger a mouse pointer warp to the currently focused
1457 * container (!) on the target workspace.
1459 * Therefore, before calling workspace_show(), we make sure that
1460 * 'current' will be focused on the workspace. However, we cannot
1461 * just con_focus(current) because then the pointer will not be
1462 * warped at all (the code thinks we are already there).
1464 * So we focus 'current' to make it the currently focused window of
1465 * the target workspace, then revert focus. */
1466 Con *currently_focused = focused;
1467 con_focus(current->con);
1468 con_focus(currently_focused);
1470 /* Now switch to the workspace, then focus */
1472 LOG("focusing %p / %s\n", current->con, current->con->name);
1473 con_focus(current->con);
1478 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1479 "while only exactly one container can be focused at a time.\n", count);
1481 cmd_output->needs_tree_render = true;
1482 // XXX: default reply for now, make this a better reply
1487 * Implementation of 'fullscreen [global]'.
1490 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1491 if (fullscreen_mode == NULL)
1492 fullscreen_mode = "output";
1493 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1498 TAILQ_FOREACH(current, &owindows, owindows) {
1499 printf("matching: %p / %s\n", current->con, current->con->name);
1500 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1503 cmd_output->needs_tree_render = true;
1504 // XXX: default reply for now, make this a better reply
1509 * Implementation of 'move <direction> [<pixels> [px]]'.
1512 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1513 // 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
1514 int px = atoi(move_px);
1516 /* TODO: make 'move' work with criteria. */
1517 DLOG("moving in direction %s, px %s\n", direction, move_px);
1518 if (con_is_floating(focused)) {
1519 DLOG("floating move with %d pixels\n", px);
1520 Rect newrect = focused->parent->rect;
1521 if (strcmp(direction, "left") == 0) {
1523 } else if (strcmp(direction, "right") == 0) {
1525 } else if (strcmp(direction, "up") == 0) {
1527 } else if (strcmp(direction, "down") == 0) {
1530 floating_reposition(focused->parent, newrect);
1532 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1533 (strcmp(direction, "left") == 0 ? D_LEFT :
1534 (strcmp(direction, "up") == 0 ? D_UP :
1536 cmd_output->needs_tree_render = true;
1539 // XXX: default reply for now, make this a better reply
1544 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1547 void cmd_layout(I3_CMD, char *layout_str) {
1548 if (strcmp(layout_str, "stacking") == 0)
1549 layout_str = "stacked";
1552 /* default is a special case which will be handled in con_set_layout(). */
1553 if (strcmp(layout_str, "default") == 0)
1555 else if (strcmp(layout_str, "stacked") == 0)
1557 else if (strcmp(layout_str, "tabbed") == 0)
1559 else if (strcmp(layout_str, "splitv") == 0)
1561 else if (strcmp(layout_str, "splith") == 0)
1564 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1568 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1570 /* check if the match is empty, not if the result is empty */
1571 if (match_is_empty(current_match))
1572 con_set_layout(focused, layout);
1574 TAILQ_FOREACH(current, &owindows, owindows) {
1575 DLOG("matching: %p / %s\n", current->con, current->con->name);
1576 con_set_layout(current->con, layout);
1580 cmd_output->needs_tree_render = true;
1581 // XXX: default reply for now, make this a better reply
1586 * Implementation of 'layout toggle [all|split]'.
1589 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1592 if (toggle_mode == NULL)
1593 toggle_mode = "default";
1595 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1597 /* check if the match is empty, not if the result is empty */
1598 if (match_is_empty(current_match))
1599 con_toggle_layout(focused, toggle_mode);
1601 TAILQ_FOREACH(current, &owindows, owindows) {
1602 DLOG("matching: %p / %s\n", current->con, current->con->name);
1603 con_toggle_layout(current->con, toggle_mode);
1607 cmd_output->needs_tree_render = true;
1608 // XXX: default reply for now, make this a better reply
1613 * Implementation of 'exit'.
1616 void cmd_exit(I3_CMD) {
1617 LOG("Exiting due to user command.\n");
1618 xcb_disconnect(conn);
1625 * Implementation of 'reload'.
1628 void cmd_reload(I3_CMD) {
1630 kill_nagbar(&config_error_nagbar_pid, false);
1631 kill_nagbar(&command_error_nagbar_pid, false);
1632 load_configuration(conn, NULL, true);
1634 /* Send an IPC event just in case the ws names have changed */
1635 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1636 /* Send an update event for the barconfig just in case it has changed */
1639 // XXX: default reply for now, make this a better reply
1644 * Implementation of 'restart'.
1647 void cmd_restart(I3_CMD) {
1648 LOG("restarting i3\n");
1651 // XXX: default reply for now, make this a better reply
1656 * Implementation of 'open'.
1659 void cmd_open(I3_CMD) {
1660 LOG("opening new container\n");
1661 Con *con = tree_open_con(NULL, NULL);
1662 con->layout = L_SPLITH;
1669 y(integer, (long int)con);
1672 cmd_output->needs_tree_render = true;
1676 * Implementation of 'focus output <output>'.
1679 void cmd_focus_output(I3_CMD, char *name) {
1682 DLOG("name = %s\n", name);
1686 /* get the output */
1687 Output *current_output = NULL;
1690 TAILQ_FOREACH(current, &owindows, owindows)
1691 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1692 assert(current_output != NULL);
1694 output = get_output_from_string(current_output, name);
1697 LOG("No such output found.\n");
1702 /* get visible workspace on output */
1704 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1712 cmd_output->needs_tree_render = true;
1713 // XXX: default reply for now, make this a better reply
1718 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1721 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1726 if (!con_is_floating(focused)) {
1727 ELOG("Cannot change position. The window/container is not floating\n");
1732 ystr("Cannot change position. The window/container is not floating.");
1737 if (strcmp(method, "absolute") == 0) {
1738 focused->parent->rect.x = x;
1739 focused->parent->rect.y = y;
1741 DLOG("moving to absolute position %d %d\n", x, y);
1742 floating_maybe_reassign_ws(focused->parent);
1743 cmd_output->needs_tree_render = true;
1746 if (strcmp(method, "position") == 0) {
1747 Rect newrect = focused->parent->rect;
1749 DLOG("moving to position %d %d\n", x, y);
1753 floating_reposition(focused->parent, newrect);
1756 // XXX: default reply for now, make this a better reply
1761 * Implementation of 'move [window|container] [to] [absolute] position center
1764 void cmd_move_window_to_center(I3_CMD, char *method) {
1766 if (!con_is_floating(focused)) {
1767 ELOG("Cannot change position. The window/container is not floating\n");
1772 ystr("Cannot change position. The window/container is not floating.");
1776 if (strcmp(method, "absolute") == 0) {
1777 Rect *rect = &focused->parent->rect;
1779 DLOG("moving to absolute center\n");
1780 rect->x = croot->rect.width/2 - rect->width/2;
1781 rect->y = croot->rect.height/2 - rect->height/2;
1783 floating_maybe_reassign_ws(focused->parent);
1784 cmd_output->needs_tree_render = true;
1787 if (strcmp(method, "position") == 0) {
1788 Rect *wsrect = &con_get_workspace(focused)->rect;
1789 Rect newrect = focused->parent->rect;
1791 DLOG("moving to center\n");
1792 newrect.x = wsrect->width/2 - newrect.width/2;
1793 newrect.y = wsrect->height/2 - newrect.height/2;
1795 floating_reposition(focused->parent, newrect);
1798 // XXX: default reply for now, make this a better reply
1803 * Implementation of 'move scratchpad'.
1806 void cmd_move_scratchpad(I3_CMD) {
1807 DLOG("should move window to scratchpad\n");
1812 TAILQ_FOREACH(current, &owindows, owindows) {
1813 DLOG("matching: %p / %s\n", current->con, current->con->name);
1814 scratchpad_move(current->con);
1817 cmd_output->needs_tree_render = true;
1818 // XXX: default reply for now, make this a better reply
1823 * Implementation of 'scratchpad show'.
1826 void cmd_scratchpad_show(I3_CMD) {
1827 DLOG("should show scratchpad window\n");
1830 if (match_is_empty(current_match)) {
1831 scratchpad_show(NULL);
1833 TAILQ_FOREACH(current, &owindows, owindows) {
1834 DLOG("matching: %p / %s\n", current->con, current->con->name);
1835 scratchpad_show(current->con);
1839 cmd_output->needs_tree_render = true;
1840 // XXX: default reply for now, make this a better reply
1845 * Implementation of 'rename workspace [<name>] to <name>'
1848 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1850 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1852 LOG("Renaming current workspace to \"%s\"\n", new_name);
1855 Con *output, *workspace = NULL;
1857 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1858 GREP_FIRST(workspace, output_get_content(output),
1859 !strcasecmp(child->name, old_name));
1861 workspace = con_get_workspace(focused);
1865 // TODO: we should include the old workspace name here and use yajl for
1866 // generating the reply.
1871 // TODO: better error message
1872 ystr("Old workspace not found");
1877 Con *check_dest = NULL;
1878 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1879 GREP_FIRST(check_dest, output_get_content(output),
1880 !strcasecmp(child->name, new_name));
1882 if (check_dest != NULL) {
1883 // TODO: we should include the new workspace name here and use yajl for
1884 // generating the reply.
1889 // TODO: better error message
1890 ystr("New workspace already exists");
1895 /* Change the name and try to parse it as a number. */
1896 FREE(workspace->name);
1897 workspace->name = sstrdup(new_name);
1898 char *endptr = NULL;
1899 long parsed_num = strtol(new_name, &endptr, 10);
1900 if (parsed_num == LONG_MIN ||
1901 parsed_num == LONG_MAX ||
1904 workspace->num = -1;
1905 else workspace->num = parsed_num;
1906 LOG("num = %d\n", workspace->num);
1908 /* By re-attaching, the sort order will be correct afterwards. */
1909 Con *previously_focused = focused;
1910 Con *parent = workspace->parent;
1911 con_detach(workspace);
1912 con_attach(workspace, parent, false);
1913 /* Restore the previous focus since con_attach messes with the focus. */
1914 con_focus(previously_focused);
1916 cmd_output->needs_tree_render = true;
1919 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1923 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1926 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1928 bool toggle = false;
1929 if (strcmp(bar_mode, "dock") == 0)
1931 else if (strcmp(bar_mode, "hide") == 0)
1933 else if (strcmp(bar_mode, "invisible") == 0)
1935 else if (strcmp(bar_mode, "toggle") == 0)
1938 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1942 bool changed_sth = false;
1943 Barconfig *current = NULL;
1944 TAILQ_FOREACH(current, &barconfigs, configs) {
1945 if (bar_id && strcmp(current->id, bar_id) != 0)
1949 mode = (current->mode + 1) % 2;
1951 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1952 current->mode = mode;
1959 if (bar_id && !changed_sth) {
1960 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1968 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
1971 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
1973 bool toggle = false;
1974 if (strcmp(bar_hidden_state, "hide") == 0)
1975 hidden_state = S_HIDE;
1976 else if (strcmp(bar_hidden_state, "show") == 0)
1977 hidden_state = S_SHOW;
1978 else if (strcmp(bar_hidden_state, "toggle") == 0)
1981 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
1985 bool changed_sth = false;
1986 Barconfig *current = NULL;
1987 TAILQ_FOREACH(current, &barconfigs, configs) {
1988 if (bar_id && strcmp(current->id, bar_id) != 0)
1992 hidden_state = (current->hidden_state + 1) % 2;
1994 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
1995 current->hidden_state = hidden_state;
2002 if (bar_id && !changed_sth) {
2003 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2011 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2014 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2016 if (strcmp(bar_type, "mode") == 0)
2017 ret = cmd_bar_mode(bar_value, bar_id);
2018 else if (strcmp(bar_type, "hidden_state") == 0)
2019 ret = cmd_bar_hidden_state(bar_value, bar_id);
2021 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2033 * Implementation of 'shmlog <size>|toggle|on|off'
2036 void cmd_shmlog(I3_CMD, char *argument) {
2037 if (!strcmp(argument,"toggle"))
2038 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2039 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2040 else if (!strcmp(argument, "on"))
2041 shmlog_size = default_shmlog_size;
2042 else if (!strcmp(argument, "off"))
2045 /* If shm logging now, restart logging with the new size. */
2046 if (shmlog_size > 0) {
2048 LOG("Restarting shm logging...\n");
2051 shmlog_size = atoi(argument);
2052 /* Make a weakly attempt at ensuring the argument is valid. */
2053 if (shmlog_size <= 0)
2054 shmlog_size = default_shmlog_size;
2056 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2058 update_shmlog_atom();
2059 // XXX: default reply for now, make this a better reply
2064 * Implementation of 'debuglog toggle|on|off'
2067 void cmd_debuglog(I3_CMD, char *argument) {
2068 bool logging = get_debug_logging();
2069 if (!strcmp(argument,"toggle")) {
2070 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2071 set_debug_logging(!logging);
2072 } else if (!strcmp(argument, "on") && !logging) {
2073 LOG("Enabling debug logging\n");
2074 set_debug_logging(true);
2075 } else if (!strcmp(argument, "off") && logging) {
2076 LOG("Disabling debug logging\n");
2077 set_debug_logging(false);
2079 // XXX: default reply for now, make this a better reply