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 'unmark [mark]'
1038 void cmd_unmark(I3_CMD, char *mark) {
1041 TAILQ_FOREACH(con, &all_cons, all_cons) {
1044 DLOG("removed all window marks");
1047 TAILQ_FOREACH(con, &all_cons, all_cons) {
1048 if (con->mark && strcmp(con->mark, mark) == 0)
1051 DLOG("removed window mark %s\n", mark);
1054 cmd_output->needs_tree_render = true;
1055 // XXX: default reply for now, make this a better reply
1060 * Implementation of 'mode <string>'.
1063 void cmd_mode(I3_CMD, char *mode) {
1064 DLOG("mode=%s\n", mode);
1067 // XXX: default reply for now, make this a better reply
1072 * Implementation of 'move [window|container] [to] output <str>'.
1075 void cmd_move_con_to_output(I3_CMD, char *name) {
1078 DLOG("should move window to output %s\n", name);
1082 /* get the output */
1083 Output *current_output = NULL;
1086 // TODO: fix the handling of criteria
1087 TAILQ_FOREACH(current, &owindows, owindows)
1088 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1090 assert(current_output != NULL);
1092 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1093 if (strcasecmp(name, "up") == 0)
1094 output = get_output_next_wrap(D_UP, current_output);
1095 else if (strcasecmp(name, "down") == 0)
1096 output = get_output_next_wrap(D_DOWN, current_output);
1097 else if (strcasecmp(name, "left") == 0)
1098 output = get_output_next_wrap(D_LEFT, current_output);
1099 else if (strcasecmp(name, "right") == 0)
1100 output = get_output_next_wrap(D_RIGHT, current_output);
1102 output = get_output_by_name(name);
1105 LOG("No such output found.\n");
1110 /* get visible workspace on output */
1112 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1118 TAILQ_FOREACH(current, &owindows, owindows) {
1119 DLOG("matching: %p / %s\n", current->con, current->con->name);
1120 con_move_to_workspace(current->con, ws, true, false);
1123 cmd_output->needs_tree_render = true;
1124 // XXX: default reply for now, make this a better reply
1129 * Implementation of 'floating enable|disable|toggle'
1132 void cmd_floating(I3_CMD, char *floating_mode) {
1135 DLOG("floating_mode=%s\n", floating_mode);
1139 TAILQ_FOREACH(current, &owindows, owindows) {
1140 DLOG("matching: %p / %s\n", current->con, current->con->name);
1141 if (strcmp(floating_mode, "toggle") == 0) {
1142 DLOG("should toggle mode\n");
1143 toggle_floating_mode(current->con, false);
1145 DLOG("should switch mode to %s\n", floating_mode);
1146 if (strcmp(floating_mode, "enable") == 0) {
1147 floating_enable(current->con, false);
1149 floating_disable(current->con, false);
1154 cmd_output->needs_tree_render = true;
1155 // XXX: default reply for now, make this a better reply
1160 * Implementation of 'move workspace to [output] <str>'.
1163 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1164 DLOG("should move workspace to output %s\n", name);
1169 TAILQ_FOREACH(current, &owindows, owindows) {
1170 Output *current_output = get_output_containing(current->con->rect.x,
1171 current->con->rect.y);
1172 if (!current_output) {
1173 ELOG("Cannot get current output. This is a bug in i3.\n");
1177 Output *output = get_output_from_string(current_output, name);
1179 ELOG("Could not get output from string \"%s\"\n", name);
1184 Con *content = output_get_content(output->con);
1185 LOG("got output %p with content %p\n", output, content);
1187 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
1188 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1190 Con *ws = con_get_workspace(current->con);
1191 LOG("should move workspace %p / %s\n", ws, ws->name);
1192 bool workspace_was_visible = workspace_is_visible(ws);
1194 if (con_num_children(ws->parent) == 1) {
1195 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1197 /* check if we can find a workspace assigned to this output */
1198 bool used_assignment = false;
1199 struct Workspace_Assignment *assignment;
1200 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1201 if (strcmp(assignment->output, current_output->name) != 0)
1204 /* check if this workspace is already attached to the tree */
1205 Con *workspace = NULL, *out;
1206 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1207 GREP_FIRST(workspace, output_get_content(out),
1208 !strcasecmp(child->name, assignment->name));
1209 if (workspace != NULL)
1212 /* so create the workspace referenced to by this assignment */
1213 LOG("Creating workspace from assignment %s.\n", assignment->name);
1214 workspace_get(assignment->name, NULL);
1215 used_assignment = true;
1219 /* if we couldn't create the workspace using an assignment, create
1220 * it on the output */
1221 if (!used_assignment)
1222 create_workspace_on_output(current_output, ws->parent);
1224 /* notify the IPC listeners */
1225 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1227 DLOG("Detaching\n");
1229 /* detach from the old output and attach to the new output */
1230 Con *old_content = ws->parent;
1232 if (workspace_was_visible) {
1233 /* The workspace which we just detached was visible, so focus
1234 * the next one in the focus-stack. */
1235 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1236 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1237 workspace_show(focus_ws);
1239 con_attach(ws, content, false);
1241 /* fix the coordinates of the floating containers */
1243 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1244 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1246 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1247 if (workspace_was_visible) {
1248 /* Focus the moved workspace on the destination output. */
1252 /* NB: We cannot simply work with previously_visible_ws since it might
1253 * have been cleaned up by workspace_show() already, depending on the
1254 * focus order/number of other workspaces on the output.
1255 * Instead, we loop through the available workspaces and only work with
1256 * previously_visible_ws if we still find it. */
1257 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1258 if (ws != previously_visible_ws)
1261 /* Call the on_remove_child callback of the workspace which previously
1262 * was visible on the destination output. Since it is no longer
1263 * visible, it might need to get cleaned up. */
1264 CALL(previously_visible_ws, on_remove_child);
1269 cmd_output->needs_tree_render = true;
1270 // XXX: default reply for now, make this a better reply
1275 * Implementation of 'split v|h|vertical|horizontal'.
1278 void cmd_split(I3_CMD, char *direction) {
1280 /* TODO: use matches */
1281 LOG("splitting in direction %c\n", direction[0]);
1282 if (match_is_empty(current_match))
1283 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1285 TAILQ_FOREACH(current, &owindows, owindows) {
1286 DLOG("matching: %p / %s\n", current->con, current->con->name);
1287 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1291 cmd_output->needs_tree_render = true;
1292 // XXX: default reply for now, make this a better reply
1297 * Implementation of 'kill [window|client]'.
1300 void cmd_kill(I3_CMD, char *kill_mode_str) {
1301 if (kill_mode_str == NULL)
1302 kill_mode_str = "window";
1305 DLOG("kill_mode=%s\n", kill_mode_str);
1308 if (strcmp(kill_mode_str, "window") == 0)
1309 kill_mode = KILL_WINDOW;
1310 else if (strcmp(kill_mode_str, "client") == 0)
1311 kill_mode = KILL_CLIENT;
1313 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1318 /* check if the match is empty, not if the result is empty */
1319 if (match_is_empty(current_match))
1320 tree_close_con(kill_mode);
1322 TAILQ_FOREACH(current, &owindows, owindows) {
1323 DLOG("matching: %p / %s\n", current->con, current->con->name);
1324 tree_close(current->con, kill_mode, false, false);
1328 cmd_output->needs_tree_render = true;
1329 // XXX: default reply for now, make this a better reply
1334 * Implementation of 'exec [--no-startup-id] <command>'.
1337 void cmd_exec(I3_CMD, char *nosn, char *command) {
1338 bool no_startup_id = (nosn != NULL);
1340 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1341 start_application(command, no_startup_id);
1343 // XXX: default reply for now, make this a better reply
1348 * Implementation of 'focus left|right|up|down'.
1351 void cmd_focus_direction(I3_CMD, char *direction) {
1352 DLOG("direction = *%s*\n", direction);
1354 if (strcmp(direction, "left") == 0)
1355 tree_next('p', HORIZ);
1356 else if (strcmp(direction, "right") == 0)
1357 tree_next('n', HORIZ);
1358 else if (strcmp(direction, "up") == 0)
1359 tree_next('p', VERT);
1360 else if (strcmp(direction, "down") == 0)
1361 tree_next('n', VERT);
1363 ELOG("Invalid focus direction (%s)\n", direction);
1368 cmd_output->needs_tree_render = true;
1369 // XXX: default reply for now, make this a better reply
1374 * Implementation of 'focus tiling|floating|mode_toggle'.
1377 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1378 DLOG("window_mode = %s\n", window_mode);
1380 Con *ws = con_get_workspace(focused);
1383 if (strcmp(window_mode, "mode_toggle") == 0) {
1384 current = TAILQ_FIRST(&(ws->focus_head));
1385 if (current != NULL && current->type == CT_FLOATING_CON)
1386 window_mode = "tiling";
1387 else window_mode = "floating";
1389 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1390 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1391 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1394 con_focus(con_descend_focused(current));
1399 cmd_output->needs_tree_render = true;
1400 // XXX: default reply for now, make this a better reply
1405 * Implementation of 'focus parent|child'.
1408 void cmd_focus_level(I3_CMD, char *level) {
1409 DLOG("level = %s\n", level);
1410 bool success = false;
1412 /* Focusing the parent can only be allowed if the newly
1413 * focused container won't escape the fullscreen container. */
1414 if (strcmp(level, "parent") == 0) {
1415 if (focused && focused->parent) {
1416 if (con_fullscreen_permits_focusing(focused->parent))
1417 success = level_up();
1419 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1423 /* Focusing a child should always be allowed. */
1424 else success = level_down();
1426 cmd_output->needs_tree_render = success;
1427 // XXX: default reply for now, make this a better reply
1432 * Implementation of 'focus'.
1435 void cmd_focus(I3_CMD) {
1436 DLOG("current_match = %p\n", current_match);
1438 if (match_is_empty(current_match)) {
1439 ELOG("You have to specify which window/container should be focused.\n");
1440 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1446 ystr("You have to specify which window/container should be focused");
1452 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1455 TAILQ_FOREACH(current, &owindows, owindows) {
1456 Con *ws = con_get_workspace(current->con);
1457 /* If no workspace could be found, this was a dock window.
1458 * Just skip it, you cannot focus dock windows. */
1462 /* Check the fullscreen focus constraints. */
1463 if (!con_fullscreen_permits_focusing(current->con)) {
1464 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1469 /* In case this is a scratchpad window, call scratchpad_show(). */
1470 if (ws == __i3_scratch) {
1471 scratchpad_show(current->con);
1473 /* While for the normal focus case we can change focus multiple
1474 * times and only a single window ends up focused, we could show
1475 * multiple scratchpad windows. So, rather break here. */
1479 /* If the container is not on the current workspace,
1480 * workspace_show() will switch to a different workspace and (if
1481 * enabled) trigger a mouse pointer warp to the currently focused
1482 * container (!) on the target workspace.
1484 * Therefore, before calling workspace_show(), we make sure that
1485 * 'current' will be focused on the workspace. However, we cannot
1486 * just con_focus(current) because then the pointer will not be
1487 * warped at all (the code thinks we are already there).
1489 * So we focus 'current' to make it the currently focused window of
1490 * the target workspace, then revert focus. */
1491 Con *currently_focused = focused;
1492 con_focus(current->con);
1493 con_focus(currently_focused);
1495 /* Now switch to the workspace, then focus */
1497 LOG("focusing %p / %s\n", current->con, current->con->name);
1498 con_focus(current->con);
1503 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1504 "while only exactly one container can be focused at a time.\n", count);
1506 cmd_output->needs_tree_render = true;
1507 // XXX: default reply for now, make this a better reply
1512 * Implementation of 'fullscreen [global]'.
1515 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1516 if (fullscreen_mode == NULL)
1517 fullscreen_mode = "output";
1518 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1523 TAILQ_FOREACH(current, &owindows, owindows) {
1524 DLOG("matching: %p / %s\n", current->con, current->con->name);
1525 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1528 cmd_output->needs_tree_render = true;
1529 // XXX: default reply for now, make this a better reply
1534 * Implementation of 'move <direction> [<pixels> [px]]'.
1537 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1538 // 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
1539 int px = atoi(move_px);
1541 /* TODO: make 'move' work with criteria. */
1542 DLOG("moving in direction %s, px %s\n", direction, move_px);
1543 if (con_is_floating(focused)) {
1544 DLOG("floating move with %d pixels\n", px);
1545 Rect newrect = focused->parent->rect;
1546 if (strcmp(direction, "left") == 0) {
1548 } else if (strcmp(direction, "right") == 0) {
1550 } else if (strcmp(direction, "up") == 0) {
1552 } else if (strcmp(direction, "down") == 0) {
1555 floating_reposition(focused->parent, newrect);
1557 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1558 (strcmp(direction, "left") == 0 ? D_LEFT :
1559 (strcmp(direction, "up") == 0 ? D_UP :
1561 cmd_output->needs_tree_render = true;
1564 // XXX: default reply for now, make this a better reply
1569 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1572 void cmd_layout(I3_CMD, char *layout_str) {
1573 if (strcmp(layout_str, "stacking") == 0)
1574 layout_str = "stacked";
1577 /* default is a special case which will be handled in con_set_layout(). */
1578 if (strcmp(layout_str, "default") == 0)
1580 else if (strcmp(layout_str, "stacked") == 0)
1582 else if (strcmp(layout_str, "tabbed") == 0)
1584 else if (strcmp(layout_str, "splitv") == 0)
1586 else if (strcmp(layout_str, "splith") == 0)
1589 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1593 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1595 /* check if the match is empty, not if the result is empty */
1596 if (match_is_empty(current_match))
1597 con_set_layout(focused, layout);
1599 TAILQ_FOREACH(current, &owindows, owindows) {
1600 DLOG("matching: %p / %s\n", current->con, current->con->name);
1601 con_set_layout(current->con, layout);
1605 cmd_output->needs_tree_render = true;
1606 // XXX: default reply for now, make this a better reply
1611 * Implementation of 'layout toggle [all|split]'.
1614 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1617 if (toggle_mode == NULL)
1618 toggle_mode = "default";
1620 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1622 /* check if the match is empty, not if the result is empty */
1623 if (match_is_empty(current_match))
1624 con_toggle_layout(focused, toggle_mode);
1626 TAILQ_FOREACH(current, &owindows, owindows) {
1627 DLOG("matching: %p / %s\n", current->con, current->con->name);
1628 con_toggle_layout(current->con, toggle_mode);
1632 cmd_output->needs_tree_render = true;
1633 // XXX: default reply for now, make this a better reply
1638 * Implementation of 'exit'.
1641 void cmd_exit(I3_CMD) {
1642 LOG("Exiting due to user command.\n");
1643 xcb_disconnect(conn);
1650 * Implementation of 'reload'.
1653 void cmd_reload(I3_CMD) {
1655 kill_nagbar(&config_error_nagbar_pid, false);
1656 kill_nagbar(&command_error_nagbar_pid, false);
1657 load_configuration(conn, NULL, true);
1659 /* Send an IPC event just in case the ws names have changed */
1660 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1661 /* Send an update event for the barconfig just in case it has changed */
1664 // XXX: default reply for now, make this a better reply
1669 * Implementation of 'restart'.
1672 void cmd_restart(I3_CMD) {
1673 LOG("restarting i3\n");
1676 // XXX: default reply for now, make this a better reply
1681 * Implementation of 'open'.
1684 void cmd_open(I3_CMD) {
1685 LOG("opening new container\n");
1686 Con *con = tree_open_con(NULL, NULL);
1687 con->layout = L_SPLITH;
1694 y(integer, (long int)con);
1697 cmd_output->needs_tree_render = true;
1701 * Implementation of 'focus output <output>'.
1704 void cmd_focus_output(I3_CMD, char *name) {
1707 DLOG("name = %s\n", name);
1711 /* get the output */
1712 Output *current_output = NULL;
1715 TAILQ_FOREACH(current, &owindows, owindows)
1716 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1717 assert(current_output != NULL);
1719 output = get_output_from_string(current_output, name);
1722 LOG("No such output found.\n");
1727 /* get visible workspace on output */
1729 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1737 cmd_output->needs_tree_render = true;
1738 // XXX: default reply for now, make this a better reply
1743 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1746 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1751 if (!con_is_floating(focused)) {
1752 ELOG("Cannot change position. The window/container is not floating\n");
1757 ystr("Cannot change position. The window/container is not floating.");
1762 if (strcmp(method, "absolute") == 0) {
1763 focused->parent->rect.x = x;
1764 focused->parent->rect.y = y;
1766 DLOG("moving to absolute position %d %d\n", x, y);
1767 floating_maybe_reassign_ws(focused->parent);
1768 cmd_output->needs_tree_render = true;
1771 if (strcmp(method, "position") == 0) {
1772 Rect newrect = focused->parent->rect;
1774 DLOG("moving to position %d %d\n", x, y);
1778 floating_reposition(focused->parent, newrect);
1781 // XXX: default reply for now, make this a better reply
1786 * Implementation of 'move [window|container] [to] [absolute] position center
1789 void cmd_move_window_to_center(I3_CMD, char *method) {
1791 if (!con_is_floating(focused)) {
1792 ELOG("Cannot change position. The window/container is not floating\n");
1797 ystr("Cannot change position. The window/container is not floating.");
1802 if (strcmp(method, "absolute") == 0) {
1803 Rect *rect = &focused->parent->rect;
1805 DLOG("moving to absolute center\n");
1806 rect->x = croot->rect.width/2 - rect->width/2;
1807 rect->y = croot->rect.height/2 - rect->height/2;
1809 floating_maybe_reassign_ws(focused->parent);
1810 cmd_output->needs_tree_render = true;
1813 if (strcmp(method, "position") == 0) {
1814 Rect *wsrect = &con_get_workspace(focused)->rect;
1815 Rect newrect = focused->parent->rect;
1817 DLOG("moving to center\n");
1818 newrect.x = wsrect->width/2 - newrect.width/2;
1819 newrect.y = wsrect->height/2 - newrect.height/2;
1821 floating_reposition(focused->parent, newrect);
1824 // XXX: default reply for now, make this a better reply
1829 * Implementation of 'move scratchpad'.
1832 void cmd_move_scratchpad(I3_CMD) {
1833 DLOG("should move window to scratchpad\n");
1838 TAILQ_FOREACH(current, &owindows, owindows) {
1839 DLOG("matching: %p / %s\n", current->con, current->con->name);
1840 scratchpad_move(current->con);
1843 cmd_output->needs_tree_render = true;
1844 // XXX: default reply for now, make this a better reply
1849 * Implementation of 'scratchpad show'.
1852 void cmd_scratchpad_show(I3_CMD) {
1853 DLOG("should show scratchpad window\n");
1856 if (match_is_empty(current_match)) {
1857 scratchpad_show(NULL);
1859 TAILQ_FOREACH(current, &owindows, owindows) {
1860 DLOG("matching: %p / %s\n", current->con, current->con->name);
1861 scratchpad_show(current->con);
1865 cmd_output->needs_tree_render = true;
1866 // XXX: default reply for now, make this a better reply
1871 * Implementation of 'rename workspace [<name>] to <name>'
1874 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1876 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1878 LOG("Renaming current workspace to \"%s\"\n", new_name);
1881 Con *output, *workspace = NULL;
1883 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1884 GREP_FIRST(workspace, output_get_content(output),
1885 !strcasecmp(child->name, old_name));
1887 workspace = con_get_workspace(focused);
1891 // TODO: we should include the old workspace name here and use yajl for
1892 // generating the reply.
1897 // TODO: better error message
1898 ystr("Old workspace not found");
1903 Con *check_dest = NULL;
1904 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1905 GREP_FIRST(check_dest, output_get_content(output),
1906 !strcasecmp(child->name, new_name));
1908 if (check_dest != NULL) {
1909 // TODO: we should include the new workspace name here and use yajl for
1910 // generating the reply.
1915 // TODO: better error message
1916 ystr("New workspace already exists");
1921 /* Change the name and try to parse it as a number. */
1922 FREE(workspace->name);
1923 workspace->name = sstrdup(new_name);
1924 char *endptr = NULL;
1925 long parsed_num = strtol(new_name, &endptr, 10);
1926 if (parsed_num == LONG_MIN ||
1927 parsed_num == LONG_MAX ||
1930 workspace->num = -1;
1931 else workspace->num = parsed_num;
1932 LOG("num = %d\n", workspace->num);
1934 /* By re-attaching, the sort order will be correct afterwards. */
1935 Con *previously_focused = focused;
1936 Con *parent = workspace->parent;
1937 con_detach(workspace);
1938 con_attach(workspace, parent, false);
1939 /* Restore the previous focus since con_attach messes with the focus. */
1940 con_focus(previously_focused);
1942 cmd_output->needs_tree_render = true;
1945 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1949 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1952 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1954 bool toggle = false;
1955 if (strcmp(bar_mode, "dock") == 0)
1957 else if (strcmp(bar_mode, "hide") == 0)
1959 else if (strcmp(bar_mode, "invisible") == 0)
1961 else if (strcmp(bar_mode, "toggle") == 0)
1964 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1968 bool changed_sth = false;
1969 Barconfig *current = NULL;
1970 TAILQ_FOREACH(current, &barconfigs, configs) {
1971 if (bar_id && strcmp(current->id, bar_id) != 0)
1975 mode = (current->mode + 1) % 2;
1977 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1978 current->mode = mode;
1985 if (bar_id && !changed_sth) {
1986 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1994 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
1997 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
1998 int hidden_state = S_SHOW;
1999 bool toggle = false;
2000 if (strcmp(bar_hidden_state, "hide") == 0)
2001 hidden_state = S_HIDE;
2002 else if (strcmp(bar_hidden_state, "show") == 0)
2003 hidden_state = S_SHOW;
2004 else if (strcmp(bar_hidden_state, "toggle") == 0)
2007 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2011 bool changed_sth = false;
2012 Barconfig *current = NULL;
2013 TAILQ_FOREACH(current, &barconfigs, configs) {
2014 if (bar_id && strcmp(current->id, bar_id) != 0)
2018 hidden_state = (current->hidden_state + 1) % 2;
2020 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2021 current->hidden_state = hidden_state;
2028 if (bar_id && !changed_sth) {
2029 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2037 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2040 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2042 if (strcmp(bar_type, "mode") == 0)
2043 ret = cmd_bar_mode(bar_value, bar_id);
2044 else if (strcmp(bar_type, "hidden_state") == 0)
2045 ret = cmd_bar_hidden_state(bar_value, bar_id);
2047 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2059 * Implementation of 'shmlog <size>|toggle|on|off'
2062 void cmd_shmlog(I3_CMD, char *argument) {
2063 if (!strcmp(argument,"toggle"))
2064 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2065 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2066 else if (!strcmp(argument, "on"))
2067 shmlog_size = default_shmlog_size;
2068 else if (!strcmp(argument, "off"))
2071 /* If shm logging now, restart logging with the new size. */
2072 if (shmlog_size > 0) {
2074 LOG("Restarting shm logging...\n");
2077 shmlog_size = atoi(argument);
2078 /* Make a weakly attempt at ensuring the argument is valid. */
2079 if (shmlog_size <= 0)
2080 shmlog_size = default_shmlog_size;
2082 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2084 update_shmlog_atom();
2085 // XXX: default reply for now, make this a better reply
2090 * Implementation of 'debuglog toggle|on|off'
2093 void cmd_debuglog(I3_CMD, char *argument) {
2094 bool logging = get_debug_logging();
2095 if (!strcmp(argument,"toggle")) {
2096 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2097 set_debug_logging(!logging);
2098 } else if (!strcmp(argument, "on") && !logging) {
2099 LOG("Enabling debug logging\n");
2100 set_debug_logging(true);
2101 } else if (!strcmp(argument, "off") && logging) {
2102 LOG("Disabling debug logging\n");
2103 set_debug_logging(false);
2105 // XXX: default reply for now, make this a better reply