2 #define I3__FILE__ "commands.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * commands.c: all command functions (see commands_parser.c)
18 // Macros to make the YAJL API a bit easier to use.
19 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
20 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
21 #define ysuccess(success) do { \
27 #define yerror(message) do { \
36 /** When the command did not include match criteria (!), we use the currently
37 * focused container. Do not confuse this case with a command which included
38 * criteria but which did not match any windows. This macro has to be called in
41 #define HANDLE_EMPTY_MATCH do { \
42 if (match_is_empty(current_match)) { \
43 owindow *ow = smalloc(sizeof(owindow)); \
45 TAILQ_INIT(&owindows); \
46 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
52 * Returns true if a is definitely greater than b (using the given epsilon)
55 static bool definitelyGreaterThan(float a, float b, float epsilon) {
56 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
60 * Returns an 'output' corresponding to one of left/right/down/up or a specific
64 static Output *get_output_from_string(Output *current_output, const char *output_str) {
67 if (strcasecmp(output_str, "left") == 0)
68 output = get_output_next_wrap(D_LEFT, current_output);
69 else if (strcasecmp(output_str, "right") == 0)
70 output = get_output_next_wrap(D_RIGHT, current_output);
71 else if (strcasecmp(output_str, "up") == 0)
72 output = get_output_next_wrap(D_UP, current_output);
73 else if (strcasecmp(output_str, "down") == 0)
74 output = get_output_next_wrap(D_DOWN, current_output);
75 else output = get_output_by_name(output_str);
81 * Returns the output containing the given container.
83 static Output *get_output_of_con(Con *con) {
84 Con *output_con = con_get_output(con);
85 Output *output = get_output_by_name(output_con->name);
86 assert(output != NULL);
92 * Checks whether we switched to a new workspace and returns false in that case,
93 * signaling that further workspace switching should be done by the calling function
94 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
95 * and return true, signaling that no further workspace switching should occur in the calling function.
98 static bool maybe_back_and_forth(struct CommandResult *cmd_output, char *name) {
99 Con *ws = con_get_workspace(focused);
101 /* If we switched to a different workspace, do nothing */
102 if (strcmp(ws->name, name) != 0)
105 DLOG("This workspace is already focused.\n");
106 if (config.workspace_auto_back_and_forth) {
107 workspace_back_and_forth();
108 cmd_output->needs_tree_render = true;
114 * Return the passed workspace unless it is the current one and auto back and
115 * forth is enabled, in which case the back_and_forth workspace is returned.
117 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
120 if (!config.workspace_auto_back_and_forth)
123 current = con_get_workspace(focused);
125 if (current == workspace) {
126 baf = workspace_back_and_forth_get();
128 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
136 // This code is commented out because we might recycle it for popping up error
137 // messages on parser errors.
139 static pid_t migration_pid = -1;
142 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
143 * it exited (or could not be started, depending on the exit code).
146 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
147 ev_child_stop(EV_A_ watcher);
148 if (!WIFEXITED(watcher->rstatus)) {
149 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
153 int exitcode = WEXITSTATUS(watcher->rstatus);
154 printf("i3-nagbar process exited with status %d\n", exitcode);
156 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
162 /* We need ev >= 4 for the following code. Since it is not *that* important (it
163 * only makes sure that there are no i3-nagbar instances left behind) we still
164 * support old systems with libev 3. */
165 #if EV_VERSION_MAJOR >= 4
167 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
168 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
171 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
172 if (migration_pid != -1) {
173 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
174 kill(migration_pid, SIGKILL);
179 void cmd_MIGRATION_start_nagbar(void) {
180 if (migration_pid != -1) {
181 fprintf(stderr, "i3-nagbar already running.\n");
184 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
185 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
186 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
187 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
188 migration_pid = fork();
189 if (migration_pid == -1) {
190 warn("Could not fork()");
195 if (migration_pid == 0) {
197 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
199 NULL, /* will be replaced by the executable path */
203 "You found a parsing error. Please, please, please, report it!",
209 exec_i3_utility("i3-nagbar", argv);
213 /* install a child watcher */
214 ev_child *child = smalloc(sizeof(ev_child));
215 ev_child_init(child, &nagbar_exited, migration_pid, 0);
216 ev_child_start(main_loop, child);
218 /* We need ev >= 4 for the following code. Since it is not *that* important (it
219 * only makes sure that there are no i3-nagbar instances left behind) we still
220 * support old systems with libev 3. */
221 #if EV_VERSION_MAJOR >= 4
222 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
224 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
225 ev_cleanup_init(cleanup, nagbar_cleanup);
226 ev_cleanup_start(main_loop, cleanup);
232 /*******************************************************************************
233 * Criteria functions.
234 ******************************************************************************/
237 * Helper data structure for an operation window (window on which the operation
238 * will be performed). Used to build the TAILQ owindows.
241 typedef struct owindow {
243 TAILQ_ENTRY(owindow) owindows;
246 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
248 static owindows_head owindows;
251 * Initializes the specified 'Match' data structure and the initial state of
252 * commands.c for matching target windows of a command.
255 void cmd_criteria_init(I3_CMD) {
259 DLOG("Initializing criteria, current_match = %p\n", current_match);
260 match_init(current_match);
261 while (!TAILQ_EMPTY(&owindows)) {
262 ow = TAILQ_FIRST(&owindows);
263 TAILQ_REMOVE(&owindows, ow, owindows);
266 TAILQ_INIT(&owindows);
268 TAILQ_FOREACH(con, &all_cons, all_cons) {
269 ow = smalloc(sizeof(owindow));
271 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
276 * A match specification just finished (the closing square bracket was found),
277 * so we filter the list of owindows.
280 void cmd_criteria_match_windows(I3_CMD) {
281 owindow *next, *current;
283 DLOG("match specification finished, matching...\n");
284 /* copy the old list head to iterate through it and start with a fresh
285 * list which will contain only matching windows */
286 struct owindows_head old = owindows;
287 TAILQ_INIT(&owindows);
288 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
289 /* make a copy of the next pointer and advance the pointer to the
290 * next element as we are going to invalidate the element’s
291 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
293 next = TAILQ_NEXT(next, owindows);
295 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
296 if (current_match->con_id != NULL) {
297 if (current_match->con_id == current->con) {
298 DLOG("matches container!\n");
299 TAILQ_INSERT_TAIL(&owindows, current, owindows);
301 } else if (current_match->mark != NULL && current->con->mark != NULL &&
302 regex_matches(current_match->mark, current->con->mark)) {
303 DLOG("match by mark\n");
304 TAILQ_INSERT_TAIL(&owindows, current, owindows);
306 if (current->con->window == NULL)
308 if (match_matches_window(current_match, current->con->window)) {
309 DLOG("matches window!\n");
310 TAILQ_INSERT_TAIL(&owindows, current, owindows);
312 DLOG("doesnt match\n");
318 TAILQ_FOREACH(current, &owindows, owindows) {
319 DLOG("matching: %p / %s\n", current->con, current->con->name);
324 * Interprets a ctype=cvalue pair and adds it to the current match
328 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
329 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
331 if (strcmp(ctype, "class") == 0) {
332 current_match->class = regex_new(cvalue);
336 if (strcmp(ctype, "instance") == 0) {
337 current_match->instance = regex_new(cvalue);
341 if (strcmp(ctype, "window_role") == 0) {
342 current_match->window_role = regex_new(cvalue);
346 if (strcmp(ctype, "con_id") == 0) {
348 long parsed = strtol(cvalue, &end, 10);
349 if (parsed == LONG_MIN ||
350 parsed == LONG_MAX ||
352 (end && *end != '\0')) {
353 ELOG("Could not parse con id \"%s\"\n", cvalue);
355 current_match->con_id = (Con*)parsed;
356 printf("id as int = %p\n", current_match->con_id);
361 if (strcmp(ctype, "id") == 0) {
363 long parsed = strtol(cvalue, &end, 10);
364 if (parsed == LONG_MIN ||
365 parsed == LONG_MAX ||
367 (end && *end != '\0')) {
368 ELOG("Could not parse window id \"%s\"\n", cvalue);
370 current_match->id = parsed;
371 printf("window id as int = %d\n", current_match->id);
376 if (strcmp(ctype, "con_mark") == 0) {
377 current_match->mark = regex_new(cvalue);
381 if (strcmp(ctype, "title") == 0) {
382 current_match->title = regex_new(cvalue);
386 if (strcmp(ctype, "urgent") == 0) {
387 if (strcasecmp(cvalue, "latest") == 0 ||
388 strcasecmp(cvalue, "newest") == 0 ||
389 strcasecmp(cvalue, "recent") == 0 ||
390 strcasecmp(cvalue, "last") == 0) {
391 current_match->urgent = U_LATEST;
392 } else if (strcasecmp(cvalue, "oldest") == 0 ||
393 strcasecmp(cvalue, "first") == 0) {
394 current_match->urgent = U_OLDEST;
399 ELOG("Unknown criterion: %s\n", ctype);
403 * Implementation of 'move [window|container] [to] workspace
404 * next|prev|next_on_output|prev_on_output|current'.
407 void cmd_move_con_to_workspace(I3_CMD, char *which) {
410 DLOG("which=%s\n", which);
412 /* We have nothing to move:
413 * when criteria was specified but didn't match any window or
414 * when criteria wasn't specified and we don't have any window focused. */
415 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
416 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
417 !con_has_children(focused))) {
424 /* get the workspace */
426 if (strcmp(which, "next") == 0)
427 ws = workspace_next();
428 else if (strcmp(which, "prev") == 0)
429 ws = workspace_prev();
430 else if (strcmp(which, "next_on_output") == 0)
431 ws = workspace_next_on_output();
432 else if (strcmp(which, "prev_on_output") == 0)
433 ws = workspace_prev_on_output();
434 else if (strcmp(which, "current") == 0)
435 ws = con_get_workspace(focused);
437 ELOG("BUG: called with which=%s\n", which);
442 TAILQ_FOREACH(current, &owindows, owindows) {
443 DLOG("matching: %p / %s\n", current->con, current->con->name);
444 con_move_to_workspace(current->con, ws, true, false);
447 cmd_output->needs_tree_render = true;
448 // XXX: default reply for now, make this a better reply
453 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
456 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
460 ws = workspace_back_and_forth_get();
463 yerror("No workspace was previously active.");
469 TAILQ_FOREACH(current, &owindows, owindows) {
470 DLOG("matching: %p / %s\n", current->con, current->con->name);
471 con_move_to_workspace(current->con, ws, true, false);
474 cmd_output->needs_tree_render = true;
475 // XXX: default reply for now, make this a better reply
480 * Implementation of 'move [window|container] [to] workspace <name>'.
483 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
484 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
485 LOG("You cannot switch to the i3 internal workspaces.\n");
492 /* We have nothing to move:
493 * when criteria was specified but didn't match any window or
494 * when criteria wasn't specified and we don't have any window focused. */
495 if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
496 ELOG("No windows match your criteria, cannot move.\n");
500 else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
501 !con_has_children(focused)) {
506 LOG("should move window to workspace %s\n", name);
507 /* get the workspace */
508 Con *ws = workspace_get(name, NULL);
510 ws = maybe_auto_back_and_forth_workspace(ws);
514 TAILQ_FOREACH(current, &owindows, owindows) {
515 DLOG("matching: %p / %s\n", current->con, current->con->name);
516 con_move_to_workspace(current->con, ws, true, false);
519 cmd_output->needs_tree_render = true;
520 // XXX: default reply for now, make this a better reply
525 * Implementation of 'move [window|container] [to] workspace number <name>'.
528 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
531 /* We have nothing to move:
532 * when criteria was specified but didn't match any window or
533 * when criteria wasn't specified and we don't have any window focused. */
534 if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
535 (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
536 !con_has_children(focused))) {
541 LOG("should move window to workspace %s\n", which);
542 /* get the workspace */
543 Con *output, *workspace = NULL;
546 long parsed_num = strtol(which, &endptr, 10);
547 if (parsed_num == LONG_MIN ||
548 parsed_num == LONG_MAX ||
551 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
552 // TODO: better error message
553 yerror("Could not parse number");
557 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
558 GREP_FIRST(workspace, output_get_content(output),
559 child->num == parsed_num);
562 workspace = workspace_get(which, NULL);
565 workspace = maybe_auto_back_and_forth_workspace(workspace);
569 TAILQ_FOREACH(current, &owindows, owindows) {
570 DLOG("matching: %p / %s\n", current->con, current->con->name);
571 con_move_to_workspace(current->con, workspace, true, false);
574 cmd_output->needs_tree_render = true;
575 // XXX: default reply for now, make this a better reply
579 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
580 LOG("floating resize\n");
581 Rect old_rect = floating_con->rect;
582 Con *focused_con = con_descend_focused(floating_con);
584 /* ensure that resize will take place even if pixel increment is smaller than
585 * height increment or width increment.
587 if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
588 strcmp(direction, "height") == 0) {
590 px = (-px < focused_con->height_increment) ? -focused_con->height_increment : px;
592 px = (px < focused_con->height_increment) ? focused_con->height_increment : px;
593 } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
595 px = (-px < focused_con->width_increment) ? -focused_con->width_increment : px;
597 px = (px < focused_con->width_increment) ? focused_con->width_increment : px;
600 if (strcmp(direction, "up") == 0) {
601 floating_con->rect.height += px;
602 } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
603 floating_con->rect.height += px;
604 } else if (strcmp(direction, "left") == 0) {
605 floating_con->rect.width += px;
607 floating_con->rect.width += px;
610 floating_check_size(floating_con);
612 /* Did we actually resize anything or did the size constraints prevent us?
613 * If we could not resize, exit now to not move the window. */
614 if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
617 if (strcmp(direction, "up") == 0) {
618 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
619 } else if (strcmp(direction, "left") == 0) {
620 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
623 /* If this is a scratchpad window, don't auto center it from now on. */
624 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
625 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
628 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, char *way, char *direction, int ppt) {
629 LOG("tiling resize\n");
631 Con *first = current;
632 direction_t search_direction;
633 if (!strcmp(direction, "left"))
634 search_direction = D_LEFT;
635 else if (!strcmp(direction, "right"))
636 search_direction = D_RIGHT;
637 else if (!strcmp(direction, "up"))
638 search_direction = D_UP;
640 search_direction = D_DOWN;
642 bool res = resize_find_tiling_participants(&first, &second, search_direction);
644 LOG("No second container in this direction found.\n");
649 /* get the default percentage */
650 int children = con_num_children(first->parent);
651 LOG("ins. %d children\n", children);
652 double percentage = 1.0 / children;
653 LOG("default percentage = %f\n", percentage);
656 LOG("second->percent = %f\n", second->percent);
657 LOG("first->percent before = %f\n", first->percent);
658 if (first->percent == 0.0)
659 first->percent = percentage;
660 if (second->percent == 0.0)
661 second->percent = percentage;
662 double new_first_percent = first->percent + ((double)ppt / 100.0);
663 double new_second_percent = second->percent - ((double)ppt / 100.0);
664 LOG("new_first_percent = %f\n", new_first_percent);
665 LOG("new_second_percent = %f\n", new_second_percent);
666 /* Ensure that the new percentages are positive and greater than
667 * 0.05 to have a reasonable minimum size. */
668 if (definitelyGreaterThan(new_first_percent, 0.05, DBL_EPSILON) &&
669 definitelyGreaterThan(new_second_percent, 0.05, DBL_EPSILON)) {
670 first->percent += ((double)ppt / 100.0);
671 second->percent -= ((double)ppt / 100.0);
672 LOG("first->percent after = %f\n", first->percent);
673 LOG("second->percent after = %f\n", second->percent);
675 LOG("Not resizing, already at minimum size\n");
681 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, char *way, char *direction, int ppt) {
682 LOG("width/height resize\n");
683 /* get the appropriate current container (skip stacked/tabbed cons) */
684 while (current->parent->layout == L_STACKED ||
685 current->parent->layout == L_TABBED)
686 current = current->parent;
688 /* Then further go up until we find one with the matching orientation. */
689 orientation_t search_orientation =
690 (strcmp(direction, "width") == 0 ? HORIZ : VERT);
692 while (current->type != CT_WORKSPACE &&
693 current->type != CT_FLOATING_CON &&
694 con_orientation(current->parent) != search_orientation)
695 current = current->parent;
697 /* get the default percentage */
698 int children = con_num_children(current->parent);
699 LOG("ins. %d children\n", children);
700 double percentage = 1.0 / children;
701 LOG("default percentage = %f\n", percentage);
703 orientation_t orientation = con_orientation(current->parent);
705 if ((orientation == HORIZ &&
706 strcmp(direction, "height") == 0) ||
707 (orientation == VERT &&
708 strcmp(direction, "width") == 0)) {
709 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
710 (orientation == HORIZ ? "horizontal" : "vertical"));
716 LOG("This is the only container, cannot resize.\n");
721 /* Ensure all the other children have a percentage set. */
723 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
724 LOG("child->percent = %f (child %p)\n", child->percent, child);
725 if (child->percent == 0.0)
726 child->percent = percentage;
729 double new_current_percent = current->percent + ((double)ppt / 100.0);
730 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
731 LOG("new_current_percent = %f\n", new_current_percent);
732 LOG("subtract_percent = %f\n", subtract_percent);
733 /* Ensure that the new percentages are positive and greater than
734 * 0.05 to have a reasonable minimum size. */
735 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
736 if (child == current)
738 if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
739 LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
744 if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
745 LOG("Not resizing, already at minimum size\n");
750 current->percent += ((double)ppt / 100.0);
751 LOG("current->percent after = %f\n", current->percent);
753 TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
754 if (child == current)
756 child->percent -= subtract_percent;
757 LOG("child->percent after (%p) = %f\n", child, child->percent);
764 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
767 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
768 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
769 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
770 // 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
771 int px = atoi(resize_px);
772 int ppt = atoi(resize_ppt);
773 if (strcmp(way, "shrink") == 0) {
781 TAILQ_FOREACH(current, &owindows, owindows) {
782 /* Don't handle dock windows (issue #1201) */
783 if (current->con->window->dock) {
784 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
789 if ((floating_con = con_inside_floating(current->con))) {
790 cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
792 if (strcmp(direction, "width") == 0 ||
793 strcmp(direction, "height") == 0) {
794 if (!cmd_resize_tiling_width_height(current_match, cmd_output, current->con, way, direction, ppt))
797 if (!cmd_resize_tiling_direction(current_match, cmd_output, current->con, way, direction, ppt))
803 cmd_output->needs_tree_render = true;
804 // XXX: default reply for now, make this a better reply
809 * Implementation of 'border normal|none|1pixel|toggle|pixel'.
812 void cmd_border(I3_CMD, char *border_style_str, char *border_width ) {
813 DLOG("border style should be changed to %s with border width %s\n", border_style_str, border_width);
818 TAILQ_FOREACH(current, &owindows, owindows) {
819 DLOG("matching: %p / %s\n", current->con, current->con->name);
820 int border_style = current->con->border_style;
822 int tmp_border_width = -1;
823 tmp_border_width = strtol(border_width, &end, 10);
824 if (end == border_width) {
825 /* no valid digits found */
826 tmp_border_width = -1;
828 if (strcmp(border_style_str, "toggle") == 0) {
831 if (border_style == BS_NORMAL)
832 tmp_border_width = 2;
833 else if (border_style == BS_NONE)
834 tmp_border_width = 0;
835 else if (border_style == BS_PIXEL)
836 tmp_border_width = 1;
838 if (strcmp(border_style_str, "normal") == 0)
839 border_style = BS_NORMAL;
840 else if (strcmp(border_style_str, "pixel") == 0)
841 border_style = BS_PIXEL;
842 else if (strcmp(border_style_str, "1pixel") == 0){
843 border_style = BS_PIXEL;
844 tmp_border_width = 1;
845 } else if (strcmp(border_style_str, "none") == 0)
846 border_style = BS_NONE;
848 ELOG("BUG: called with border_style=%s\n", border_style_str);
853 con_set_border_style(current->con, border_style, tmp_border_width);
856 cmd_output->needs_tree_render = true;
857 // XXX: default reply for now, make this a better reply
862 * Implementation of 'nop <comment>'.
865 void cmd_nop(I3_CMD, char *comment) {
866 LOG("-------------------------------------------------\n");
867 LOG(" NOP: %s\n", comment);
868 LOG("-------------------------------------------------\n");
872 * Implementation of 'append_layout <path>'.
875 void cmd_append_layout(I3_CMD, char *path) {
876 LOG("Appending layout \"%s\"\n", path);
877 Con *parent = focused;
878 char *errormsg = NULL;
879 tree_append_json(path, &errormsg);
880 if (errormsg != NULL) {
883 /* Note that we continue executing since tree_append_json() has
884 * side-effects — user-provided layouts can be partly valid, partly
885 * invalid, leading to half of the placeholder containers being
891 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
892 // false); should be enough, but when sending 'workspace 4; append_layout
893 // /tmp/foo.json', the needs_tree_render == true of the workspace command
894 // is not executed yet and will be batched with append_layout’s
895 // needs_tree_render after the parser finished. We should check if that is
897 render_con(croot, false);
899 restore_open_placeholder_windows(parent);
901 cmd_output->needs_tree_render = true;
905 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
908 void cmd_workspace(I3_CMD, char *which) {
911 DLOG("which=%s\n", which);
913 if (strcmp(which, "next") == 0)
914 ws = workspace_next();
915 else if (strcmp(which, "prev") == 0)
916 ws = workspace_prev();
917 else if (strcmp(which, "next_on_output") == 0)
918 ws = workspace_next_on_output();
919 else if (strcmp(which, "prev_on_output") == 0)
920 ws = workspace_prev_on_output();
922 ELOG("BUG: called with which=%s\n", which);
929 cmd_output->needs_tree_render = true;
930 // XXX: default reply for now, make this a better reply
935 * Implementation of 'workspace number <name>'
938 void cmd_workspace_number(I3_CMD, char *which) {
939 Con *output, *workspace = NULL;
942 long parsed_num = strtol(which, &endptr, 10);
943 if (parsed_num == LONG_MIN ||
944 parsed_num == LONG_MAX ||
947 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
948 // TODO: better error message
949 yerror("Could not parse number");
954 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
955 GREP_FIRST(workspace, output_get_content(output),
956 child->num == parsed_num);
959 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
961 workspace_show_by_name(which);
962 cmd_output->needs_tree_render = true;
965 if (maybe_back_and_forth(cmd_output, workspace->name))
967 workspace_show(workspace);
969 cmd_output->needs_tree_render = true;
970 // XXX: default reply for now, make this a better reply
975 * Implementation of 'workspace back_and_forth'.
978 void cmd_workspace_back_and_forth(I3_CMD) {
979 workspace_back_and_forth();
981 cmd_output->needs_tree_render = true;
982 // XXX: default reply for now, make this a better reply
987 * Implementation of 'workspace <name>'
990 void cmd_workspace_name(I3_CMD, char *name) {
991 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
992 LOG("You cannot switch to the i3 internal workspaces.\n");
997 DLOG("should switch to workspace %s\n", name);
998 if (maybe_back_and_forth(cmd_output, name))
1000 workspace_show_by_name(name);
1002 cmd_output->needs_tree_render = true;
1003 // XXX: default reply for now, make this a better reply
1008 * Implementation of 'mark <mark>'
1011 void cmd_mark(I3_CMD, char *mark) {
1012 DLOG("Clearing all windows which have that mark first\n");
1015 TAILQ_FOREACH(con, &all_cons, all_cons) {
1016 if (con->mark && strcmp(con->mark, mark) == 0)
1020 DLOG("marking window with str %s\n", mark);
1025 TAILQ_FOREACH(current, &owindows, owindows) {
1026 DLOG("matching: %p / %s\n", current->con, current->con->name);
1027 current->con->mark = sstrdup(mark);
1030 cmd_output->needs_tree_render = true;
1031 // XXX: default reply for now, make this a better reply
1036 * Implementation of 'unmark [mark]'
1039 void cmd_unmark(I3_CMD, char *mark) {
1042 TAILQ_FOREACH(con, &all_cons, all_cons) {
1045 DLOG("removed all window marks");
1048 TAILQ_FOREACH(con, &all_cons, all_cons) {
1049 if (con->mark && strcmp(con->mark, mark) == 0)
1052 DLOG("removed window mark %s\n", mark);
1055 cmd_output->needs_tree_render = true;
1056 // XXX: default reply for now, make this a better reply
1061 * Implementation of 'mode <string>'.
1064 void cmd_mode(I3_CMD, char *mode) {
1065 DLOG("mode=%s\n", mode);
1068 // XXX: default reply for now, make this a better reply
1073 * Implementation of 'move [window|container] [to] output <str>'.
1076 void cmd_move_con_to_output(I3_CMD, char *name) {
1079 DLOG("should move window to output %s\n", name);
1083 /* get the output */
1084 Output *current_output = NULL;
1087 // TODO: fix the handling of criteria
1088 TAILQ_FOREACH(current, &owindows, owindows)
1089 current_output = get_output_of_con(current->con);
1091 assert(current_output != NULL);
1093 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
1094 if (strcasecmp(name, "up") == 0)
1095 output = get_output_next_wrap(D_UP, current_output);
1096 else if (strcasecmp(name, "down") == 0)
1097 output = get_output_next_wrap(D_DOWN, current_output);
1098 else if (strcasecmp(name, "left") == 0)
1099 output = get_output_next_wrap(D_LEFT, current_output);
1100 else if (strcasecmp(name, "right") == 0)
1101 output = get_output_next_wrap(D_RIGHT, current_output);
1103 output = get_output_by_name(name);
1106 LOG("No such output found.\n");
1111 /* get visible workspace on output */
1113 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1119 TAILQ_FOREACH(current, &owindows, owindows) {
1120 DLOG("matching: %p / %s\n", current->con, current->con->name);
1121 con_move_to_workspace(current->con, ws, true, false);
1124 cmd_output->needs_tree_render = true;
1125 // XXX: default reply for now, make this a better reply
1130 * Implementation of 'floating enable|disable|toggle'
1133 void cmd_floating(I3_CMD, char *floating_mode) {
1136 DLOG("floating_mode=%s\n", floating_mode);
1140 TAILQ_FOREACH(current, &owindows, owindows) {
1141 DLOG("matching: %p / %s\n", current->con, current->con->name);
1142 if (strcmp(floating_mode, "toggle") == 0) {
1143 DLOG("should toggle mode\n");
1144 toggle_floating_mode(current->con, false);
1146 DLOG("should switch mode to %s\n", floating_mode);
1147 if (strcmp(floating_mode, "enable") == 0) {
1148 floating_enable(current->con, false);
1150 floating_disable(current->con, false);
1155 cmd_output->needs_tree_render = true;
1156 // XXX: default reply for now, make this a better reply
1161 * Implementation of 'move workspace to [output] <str>'.
1164 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1165 DLOG("should move workspace to output %s\n", name);
1170 TAILQ_FOREACH(current, &owindows, owindows) {
1171 Output *current_output = get_output_of_con(current->con);
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");
1442 yerror("You have to specify which window/container should be focused");
1447 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1450 TAILQ_FOREACH(current, &owindows, owindows) {
1451 Con *ws = con_get_workspace(current->con);
1452 /* If no workspace could be found, this was a dock window.
1453 * Just skip it, you cannot focus dock windows. */
1457 /* Check the fullscreen focus constraints. */
1458 if (!con_fullscreen_permits_focusing(current->con)) {
1459 LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1464 /* In case this is a scratchpad window, call scratchpad_show(). */
1465 if (ws == __i3_scratch) {
1466 scratchpad_show(current->con);
1468 /* While for the normal focus case we can change focus multiple
1469 * times and only a single window ends up focused, we could show
1470 * multiple scratchpad windows. So, rather break here. */
1474 /* If the container is not on the current workspace,
1475 * workspace_show() will switch to a different workspace and (if
1476 * enabled) trigger a mouse pointer warp to the currently focused
1477 * container (!) on the target workspace.
1479 * Therefore, before calling workspace_show(), we make sure that
1480 * 'current' will be focused on the workspace. However, we cannot
1481 * just con_focus(current) because then the pointer will not be
1482 * warped at all (the code thinks we are already there).
1484 * So we focus 'current' to make it the currently focused window of
1485 * the target workspace, then revert focus. */
1486 Con *currently_focused = focused;
1487 con_focus(current->con);
1488 con_focus(currently_focused);
1490 /* Now switch to the workspace, then focus */
1492 LOG("focusing %p / %s\n", current->con, current->con->name);
1493 con_focus(current->con);
1498 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1499 "while only exactly one container can be focused at a time.\n", count);
1501 cmd_output->needs_tree_render = true;
1502 // XXX: default reply for now, make this a better reply
1507 * Implementation of 'fullscreen [global]'.
1510 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1511 if (fullscreen_mode == NULL)
1512 fullscreen_mode = "output";
1513 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1518 TAILQ_FOREACH(current, &owindows, owindows) {
1519 DLOG("matching: %p / %s\n", current->con, current->con->name);
1520 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1523 cmd_output->needs_tree_render = true;
1524 // XXX: default reply for now, make this a better reply
1529 * Implementation of 'move <direction> [<pixels> [px]]'.
1532 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1533 // 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
1534 int px = atoi(move_px);
1536 /* TODO: make 'move' work with criteria. */
1537 DLOG("moving in direction %s, px %s\n", direction, move_px);
1538 if (con_is_floating(focused)) {
1539 DLOG("floating move with %d pixels\n", px);
1540 Rect newrect = focused->parent->rect;
1541 if (strcmp(direction, "left") == 0) {
1543 } else if (strcmp(direction, "right") == 0) {
1545 } else if (strcmp(direction, "up") == 0) {
1547 } else if (strcmp(direction, "down") == 0) {
1550 floating_reposition(focused->parent, newrect);
1552 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1553 (strcmp(direction, "left") == 0 ? D_LEFT :
1554 (strcmp(direction, "up") == 0 ? D_UP :
1556 cmd_output->needs_tree_render = true;
1559 // XXX: default reply for now, make this a better reply
1564 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1567 void cmd_layout(I3_CMD, char *layout_str) {
1568 if (strcmp(layout_str, "stacking") == 0)
1569 layout_str = "stacked";
1572 /* default is a special case which will be handled in con_set_layout(). */
1573 if (strcmp(layout_str, "default") == 0)
1575 else if (strcmp(layout_str, "stacked") == 0)
1577 else if (strcmp(layout_str, "tabbed") == 0)
1579 else if (strcmp(layout_str, "splitv") == 0)
1581 else if (strcmp(layout_str, "splith") == 0)
1584 ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1588 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1590 /* check if the match is empty, not if the result is empty */
1591 if (match_is_empty(current_match))
1592 con_set_layout(focused, layout);
1594 TAILQ_FOREACH(current, &owindows, owindows) {
1595 DLOG("matching: %p / %s\n", current->con, current->con->name);
1596 con_set_layout(current->con, layout);
1600 cmd_output->needs_tree_render = true;
1601 // XXX: default reply for now, make this a better reply
1606 * Implementation of 'layout toggle [all|split]'.
1609 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1612 if (toggle_mode == NULL)
1613 toggle_mode = "default";
1615 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1617 /* check if the match is empty, not if the result is empty */
1618 if (match_is_empty(current_match))
1619 con_toggle_layout(focused, toggle_mode);
1621 TAILQ_FOREACH(current, &owindows, owindows) {
1622 DLOG("matching: %p / %s\n", current->con, current->con->name);
1623 con_toggle_layout(current->con, toggle_mode);
1627 cmd_output->needs_tree_render = true;
1628 // XXX: default reply for now, make this a better reply
1633 * Implementation of 'exit'.
1636 void cmd_exit(I3_CMD) {
1637 LOG("Exiting due to user command.\n");
1638 xcb_disconnect(conn);
1645 * Implementation of 'reload'.
1648 void cmd_reload(I3_CMD) {
1650 kill_nagbar(&config_error_nagbar_pid, false);
1651 kill_nagbar(&command_error_nagbar_pid, false);
1652 load_configuration(conn, NULL, true);
1654 /* Send an IPC event just in case the ws names have changed */
1655 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1656 /* Send an update event for the barconfig just in case it has changed */
1659 // XXX: default reply for now, make this a better reply
1664 * Implementation of 'restart'.
1667 void cmd_restart(I3_CMD) {
1668 LOG("restarting i3\n");
1671 // XXX: default reply for now, make this a better reply
1676 * Implementation of 'open'.
1679 void cmd_open(I3_CMD) {
1680 LOG("opening new container\n");
1681 Con *con = tree_open_con(NULL, NULL);
1682 con->layout = L_SPLITH;
1689 y(integer, (long int)con);
1692 cmd_output->needs_tree_render = true;
1696 * Implementation of 'focus output <output>'.
1699 void cmd_focus_output(I3_CMD, char *name) {
1702 DLOG("name = %s\n", name);
1706 /* get the output */
1707 Output *current_output = NULL;
1710 TAILQ_FOREACH(current, &owindows, owindows)
1711 current_output = get_output_of_con(current->con);
1712 assert(current_output != NULL);
1714 output = get_output_from_string(current_output, name);
1717 LOG("No such output found.\n");
1722 /* get visible workspace on output */
1724 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1732 cmd_output->needs_tree_render = true;
1733 // XXX: default reply for now, make this a better reply
1738 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1741 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1746 if (!con_is_floating(focused)) {
1747 ELOG("Cannot change position. The window/container is not floating\n");
1748 yerror("Cannot change position. The window/container is not floating.");
1752 if (strcmp(method, "absolute") == 0) {
1753 focused->parent->rect.x = x;
1754 focused->parent->rect.y = y;
1756 DLOG("moving to absolute position %d %d\n", x, y);
1757 floating_maybe_reassign_ws(focused->parent);
1758 cmd_output->needs_tree_render = true;
1761 if (strcmp(method, "position") == 0) {
1762 Rect newrect = focused->parent->rect;
1764 DLOG("moving to position %d %d\n", x, y);
1768 floating_reposition(focused->parent, newrect);
1771 // XXX: default reply for now, make this a better reply
1776 * Implementation of 'move [window|container] [to] [absolute] position center
1779 void cmd_move_window_to_center(I3_CMD, char *method) {
1781 if (!con_is_floating(focused)) {
1782 ELOG("Cannot change position. The window/container is not floating\n");
1783 yerror("Cannot change position. The window/container is not floating.");
1787 if (strcmp(method, "absolute") == 0) {
1788 Rect *rect = &focused->parent->rect;
1790 DLOG("moving to absolute center\n");
1791 rect->x = croot->rect.width/2 - rect->width/2;
1792 rect->y = croot->rect.height/2 - rect->height/2;
1794 floating_maybe_reassign_ws(focused->parent);
1795 cmd_output->needs_tree_render = true;
1798 if (strcmp(method, "position") == 0) {
1799 Rect *wsrect = &con_get_workspace(focused)->rect;
1800 Rect newrect = focused->parent->rect;
1802 DLOG("moving to center\n");
1803 newrect.x = wsrect->width/2 - newrect.width/2;
1804 newrect.y = wsrect->height/2 - newrect.height/2;
1806 floating_reposition(focused->parent, newrect);
1809 // XXX: default reply for now, make this a better reply
1814 * Implementation of 'move scratchpad'.
1817 void cmd_move_scratchpad(I3_CMD) {
1818 DLOG("should move window to scratchpad\n");
1823 TAILQ_FOREACH(current, &owindows, owindows) {
1824 DLOG("matching: %p / %s\n", current->con, current->con->name);
1825 scratchpad_move(current->con);
1828 cmd_output->needs_tree_render = true;
1829 // XXX: default reply for now, make this a better reply
1834 * Implementation of 'scratchpad show'.
1837 void cmd_scratchpad_show(I3_CMD) {
1838 DLOG("should show scratchpad window\n");
1841 if (match_is_empty(current_match)) {
1842 scratchpad_show(NULL);
1844 TAILQ_FOREACH(current, &owindows, owindows) {
1845 DLOG("matching: %p / %s\n", current->con, current->con->name);
1846 scratchpad_show(current->con);
1850 cmd_output->needs_tree_render = true;
1851 // XXX: default reply for now, make this a better reply
1856 * Implementation of 'rename workspace [<name>] to <name>'
1859 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1861 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1863 LOG("Renaming current workspace to \"%s\"\n", new_name);
1866 Con *output, *workspace = NULL;
1868 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1869 GREP_FIRST(workspace, output_get_content(output),
1870 !strcasecmp(child->name, old_name));
1872 workspace = con_get_workspace(focused);
1876 // TODO: we should include the old workspace name here and use yajl for
1877 // generating the reply.
1878 // TODO: better error message
1879 yerror("Old workspace not found");
1883 Con *check_dest = NULL;
1884 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1885 GREP_FIRST(check_dest, output_get_content(output),
1886 !strcasecmp(child->name, new_name));
1888 if (check_dest != NULL) {
1889 // TODO: we should include the new workspace name here and use yajl for
1890 // generating the reply.
1891 // TODO: better error message
1892 yerror("New workspace already exists");
1896 /* Change the name and try to parse it as a number. */
1897 FREE(workspace->name);
1898 workspace->name = sstrdup(new_name);
1899 char *endptr = NULL;
1900 long parsed_num = strtol(new_name, &endptr, 10);
1901 if (parsed_num == LONG_MIN ||
1902 parsed_num == LONG_MAX ||
1905 workspace->num = -1;
1906 else workspace->num = parsed_num;
1907 LOG("num = %d\n", workspace->num);
1909 /* By re-attaching, the sort order will be correct afterwards. */
1910 Con *previously_focused = focused;
1911 Con *parent = workspace->parent;
1912 con_detach(workspace);
1913 con_attach(workspace, parent, false);
1914 /* Restore the previous focus since con_attach messes with the focus. */
1915 con_focus(previously_focused);
1917 cmd_output->needs_tree_render = true;
1920 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1924 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
1927 bool cmd_bar_mode(char *bar_mode, char *bar_id) {
1929 bool toggle = false;
1930 if (strcmp(bar_mode, "dock") == 0)
1932 else if (strcmp(bar_mode, "hide") == 0)
1934 else if (strcmp(bar_mode, "invisible") == 0)
1936 else if (strcmp(bar_mode, "toggle") == 0)
1939 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
1943 bool changed_sth = false;
1944 Barconfig *current = NULL;
1945 TAILQ_FOREACH(current, &barconfigs, configs) {
1946 if (bar_id && strcmp(current->id, bar_id) != 0)
1950 mode = (current->mode + 1) % 2;
1952 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
1953 current->mode = mode;
1960 if (bar_id && !changed_sth) {
1961 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
1969 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
1972 bool cmd_bar_hidden_state(char *bar_hidden_state, char *bar_id) {
1973 int hidden_state = S_SHOW;
1974 bool toggle = false;
1975 if (strcmp(bar_hidden_state, "hide") == 0)
1976 hidden_state = S_HIDE;
1977 else if (strcmp(bar_hidden_state, "show") == 0)
1978 hidden_state = S_SHOW;
1979 else if (strcmp(bar_hidden_state, "toggle") == 0)
1982 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
1986 bool changed_sth = false;
1987 Barconfig *current = NULL;
1988 TAILQ_FOREACH(current, &barconfigs, configs) {
1989 if (bar_id && strcmp(current->id, bar_id) != 0)
1993 hidden_state = (current->hidden_state + 1) % 2;
1995 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
1996 current->hidden_state = hidden_state;
2003 if (bar_id && !changed_sth) {
2004 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2012 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2015 void cmd_bar(I3_CMD, char *bar_type, char *bar_value, char *bar_id) {
2017 if (strcmp(bar_type, "mode") == 0)
2018 ret = cmd_bar_mode(bar_value, bar_id);
2019 else if (strcmp(bar_type, "hidden_state") == 0)
2020 ret = cmd_bar_hidden_state(bar_value, bar_id);
2022 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2034 * Implementation of 'shmlog <size>|toggle|on|off'
2037 void cmd_shmlog(I3_CMD, char *argument) {
2038 if (!strcmp(argument,"toggle"))
2039 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2040 shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2041 else if (!strcmp(argument, "on"))
2042 shmlog_size = default_shmlog_size;
2043 else if (!strcmp(argument, "off"))
2046 /* If shm logging now, restart logging with the new size. */
2047 if (shmlog_size > 0) {
2049 LOG("Restarting shm logging...\n");
2052 shmlog_size = atoi(argument);
2053 /* Make a weakly attempt at ensuring the argument is valid. */
2054 if (shmlog_size <= 0)
2055 shmlog_size = default_shmlog_size;
2057 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2059 update_shmlog_atom();
2060 // XXX: default reply for now, make this a better reply
2065 * Implementation of 'debuglog toggle|on|off'
2068 void cmd_debuglog(I3_CMD, char *argument) {
2069 bool logging = get_debug_logging();
2070 if (!strcmp(argument,"toggle")) {
2071 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2072 set_debug_logging(!logging);
2073 } else if (!strcmp(argument, "on") && !logging) {
2074 LOG("Enabling debug logging\n");
2075 set_debug_logging(true);
2076 } else if (!strcmp(argument, "off") && logging) {
2077 LOG("Disabling debug logging\n");
2078 set_debug_logging(false);
2080 // XXX: default reply for now, make this a better reply