2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * commands.c: all command functions (see commands_parser.c)
15 /** When the command did not include match criteria (!), we use the currently
16 * focused command. Do not confuse this case with a command which included
17 * criteria but which did not match any windows. This macro has to be called in
20 #define HANDLE_EMPTY_MATCH do { \
21 if (match_is_empty(current_match)) { \
22 owindow *ow = smalloc(sizeof(owindow)); \
24 TAILQ_INIT(&owindows); \
25 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
29 static owindows_head owindows;
32 * Returns true if a is definitely greater than b (using the given epsilon)
35 static bool definitelyGreaterThan(float a, float b, float epsilon) {
36 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
40 * Returns an 'output' corresponding to one of left/right/down/up or a specific
44 static Output *get_output_from_string(Output *current_output, const char *output_str) {
47 if (strcasecmp(output_str, "left") == 0) {
48 output = get_output_next(D_LEFT, current_output);
50 output = get_output_most(D_RIGHT, current_output);
51 } else if (strcasecmp(output_str, "right") == 0) {
52 output = get_output_next(D_RIGHT, current_output);
54 output = get_output_most(D_LEFT, current_output);
55 } else if (strcasecmp(output_str, "up") == 0) {
56 output = get_output_next(D_UP, current_output);
58 output = get_output_most(D_DOWN, current_output);
59 } else if (strcasecmp(output_str, "down") == 0) {
60 output = get_output_next(D_DOWN, current_output);
62 output = get_output_most(D_UP, current_output);
63 } else output = get_output_by_name(output_str);
68 // This code is commented out because we might recycle it for popping up error
69 // messages on parser errors.
71 static pid_t migration_pid = -1;
74 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
75 * it exited (or could not be started, depending on the exit code).
78 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
79 ev_child_stop(EV_A_ watcher);
80 if (!WIFEXITED(watcher->rstatus)) {
81 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
85 int exitcode = WEXITSTATUS(watcher->rstatus);
86 printf("i3-nagbar process exited with status %d\n", exitcode);
88 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
94 /* We need ev >= 4 for the following code. Since it is not *that* important (it
95 * only makes sure that there are no i3-nagbar instances left behind) we still
96 * support old systems with libev 3. */
97 #if EV_VERSION_MAJOR >= 4
99 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
100 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
103 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
104 if (migration_pid != -1) {
105 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
106 kill(migration_pid, SIGKILL);
111 void cmd_MIGRATION_start_nagbar() {
112 if (migration_pid != -1) {
113 fprintf(stderr, "i3-nagbar already running.\n");
116 fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
117 ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
118 ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
119 ELOG("FYI: Your i3 version is " I3_VERSION "\n");
120 migration_pid = fork();
121 if (migration_pid == -1) {
122 warn("Could not fork()");
127 if (migration_pid == 0) {
129 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
131 NULL, /* will be replaced by the executable path */
135 "You found a parsing error. Please, please, please, report it!",
141 exec_i3_utility("i3-nagbar", argv);
145 /* install a child watcher */
146 ev_child *child = smalloc(sizeof(ev_child));
147 ev_child_init(child, &nagbar_exited, migration_pid, 0);
148 ev_child_start(main_loop, child);
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151 * only makes sure that there are no i3-nagbar instances left behind) we still
152 * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
154 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
156 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
157 ev_cleanup_init(cleanup, nagbar_cleanup);
158 ev_cleanup_start(main_loop, cleanup);
164 /*******************************************************************************
165 * Criteria functions.
166 ******************************************************************************/
169 * Initializes the specified 'Match' data structure and the initial state of
170 * commands.c for matching target windows of a command.
173 void cmd_criteria_init(I3_CMD) {
177 DLOG("Initializing criteria, current_match = %p\n", current_match);
178 match_init(current_match);
179 while (!TAILQ_EMPTY(&owindows)) {
180 ow = TAILQ_FIRST(&owindows);
181 TAILQ_REMOVE(&owindows, ow, owindows);
184 TAILQ_INIT(&owindows);
186 TAILQ_FOREACH(con, &all_cons, all_cons) {
187 ow = smalloc(sizeof(owindow));
189 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
194 * A match specification just finished (the closing square bracket was found),
195 * so we filter the list of owindows.
198 void cmd_criteria_match_windows(I3_CMD) {
199 owindow *next, *current;
201 DLOG("match specification finished, matching...\n");
202 /* copy the old list head to iterate through it and start with a fresh
203 * list which will contain only matching windows */
204 struct owindows_head old = owindows;
205 TAILQ_INIT(&owindows);
206 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
207 /* make a copy of the next pointer and advance the pointer to the
208 * next element as we are going to invalidate the element’s
209 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
211 next = TAILQ_NEXT(next, owindows);
213 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
214 if (current_match->con_id != NULL) {
215 if (current_match->con_id == current->con) {
216 DLOG("matches container!\n");
217 TAILQ_INSERT_TAIL(&owindows, current, owindows);
219 } else if (current_match->mark != NULL && current->con->mark != NULL &&
220 regex_matches(current_match->mark, current->con->mark)) {
221 DLOG("match by mark\n");
222 TAILQ_INSERT_TAIL(&owindows, current, owindows);
224 if (current->con->window == NULL)
226 if (match_matches_window(current_match, current->con->window)) {
227 DLOG("matches window!\n");
228 TAILQ_INSERT_TAIL(&owindows, current, owindows);
230 DLOG("doesnt match\n");
236 TAILQ_FOREACH(current, &owindows, owindows) {
237 DLOG("matching: %p / %s\n", current->con, current->con->name);
242 * Interprets a ctype=cvalue pair and adds it to the current match
246 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
247 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
249 if (strcmp(ctype, "class") == 0) {
250 current_match->class = regex_new(cvalue);
254 if (strcmp(ctype, "instance") == 0) {
255 current_match->instance = regex_new(cvalue);
259 if (strcmp(ctype, "window_role") == 0) {
260 current_match->role = regex_new(cvalue);
264 if (strcmp(ctype, "con_id") == 0) {
266 long parsed = strtol(cvalue, &end, 10);
267 if (parsed == LONG_MIN ||
268 parsed == LONG_MAX ||
270 (end && *end != '\0')) {
271 ELOG("Could not parse con id \"%s\"\n", cvalue);
273 current_match->con_id = (Con*)parsed;
274 printf("id as int = %p\n", current_match->con_id);
279 if (strcmp(ctype, "id") == 0) {
281 long parsed = strtol(cvalue, &end, 10);
282 if (parsed == LONG_MIN ||
283 parsed == LONG_MAX ||
285 (end && *end != '\0')) {
286 ELOG("Could not parse window id \"%s\"\n", cvalue);
288 current_match->id = parsed;
289 printf("window id as int = %d\n", current_match->id);
294 if (strcmp(ctype, "con_mark") == 0) {
295 current_match->mark = regex_new(cvalue);
299 if (strcmp(ctype, "title") == 0) {
300 current_match->title = regex_new(cvalue);
304 if (strcmp(ctype, "urgent") == 0) {
305 if (strcasecmp(cvalue, "latest") == 0 ||
306 strcasecmp(cvalue, "newest") == 0 ||
307 strcasecmp(cvalue, "recent") == 0 ||
308 strcasecmp(cvalue, "last") == 0) {
309 current_match->urgent = U_LATEST;
310 } else if (strcasecmp(cvalue, "oldest") == 0 ||
311 strcasecmp(cvalue, "first") == 0) {
312 current_match->urgent = U_OLDEST;
317 ELOG("Unknown criterion: %s\n", ctype);
321 * Implementation of 'move [window|container] [to] workspace
322 * next|prev|next_on_output|prev_on_output'.
325 void cmd_move_con_to_workspace(I3_CMD, char *which) {
328 DLOG("which=%s\n", which);
332 /* get the workspace */
334 if (strcmp(which, "next") == 0)
335 ws = workspace_next();
336 else if (strcmp(which, "prev") == 0)
337 ws = workspace_prev();
338 else if (strcmp(which, "next_on_output") == 0)
339 ws = workspace_next_on_output();
340 else if (strcmp(which, "prev_on_output") == 0)
341 ws = workspace_prev_on_output();
343 ELOG("BUG: called with which=%s\n", which);
344 cmd_output->json_output = sstrdup("{\"sucess\": false}");
348 TAILQ_FOREACH(current, &owindows, owindows) {
349 DLOG("matching: %p / %s\n", current->con, current->con->name);
350 con_move_to_workspace(current->con, ws, true, false);
353 cmd_output->needs_tree_render = true;
354 // XXX: default reply for now, make this a better reply
355 cmd_output->json_output = sstrdup("{\"success\": true}");
359 * Implementation of 'move [window|container] [to] workspace <name>'.
362 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
363 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
364 LOG("You cannot switch to the i3 internal workspaces.\n");
365 cmd_output->json_output = sstrdup("{\"sucess\": false}");
371 /* Error out early to not create a non-existing workspace (in
372 * workspace_get()) if we are not actually able to move anything. */
373 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
374 cmd_output->json_output = sstrdup("{\"sucess\": false}");
378 LOG("should move window to workspace %s\n", name);
379 /* get the workspace */
380 Con *ws = workspace_get(name, NULL);
384 TAILQ_FOREACH(current, &owindows, owindows) {
385 DLOG("matching: %p / %s\n", current->con, current->con->name);
386 con_move_to_workspace(current->con, ws, true, false);
389 cmd_output->needs_tree_render = true;
390 // XXX: default reply for now, make this a better reply
391 cmd_output->json_output = sstrdup("{\"success\": true}");
395 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
398 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
399 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
400 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
401 // 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
402 int px = atoi(resize_px);
403 int ppt = atoi(resize_ppt);
404 if (strcmp(way, "shrink") == 0) {
410 if ((floating_con = con_inside_floating(focused))) {
411 printf("floating resize\n");
412 if (strcmp(direction, "up") == 0) {
413 floating_con->rect.y -= px;
414 floating_con->rect.height += px;
415 } else if (strcmp(direction, "down") == 0) {
416 floating_con->rect.height += px;
417 } else if (strcmp(direction, "left") == 0) {
418 floating_con->rect.x -= px;
419 floating_con->rect.width += px;
421 floating_con->rect.width += px;
424 LOG("tiling resize\n");
425 /* get the appropriate current container (skip stacked/tabbed cons) */
426 Con *current = focused;
427 while (current->parent->layout == L_STACKED ||
428 current->parent->layout == L_TABBED)
429 current = current->parent;
431 /* Then further go up until we find one with the matching orientation. */
432 orientation_t search_orientation =
433 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
435 while (current->type != CT_WORKSPACE &&
436 current->type != CT_FLOATING_CON &&
437 current->parent->orientation != search_orientation)
438 current = current->parent;
440 /* get the default percentage */
441 int children = con_num_children(current->parent);
443 LOG("ins. %d children\n", children);
444 double percentage = 1.0 / children;
445 LOG("default percentage = %f\n", percentage);
447 orientation_t orientation = current->parent->orientation;
449 if ((orientation == HORIZ &&
450 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
451 (orientation == VERT &&
452 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
453 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
454 (orientation == HORIZ ? "horizontal" : "vertical"));
455 cmd_output->json_output = sstrdup("{\"sucess\": false}");
459 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
460 other = TAILQ_PREV(current, nodes_head, nodes);
462 other = TAILQ_NEXT(current, nodes);
464 if (other == TAILQ_END(workspaces)) {
465 LOG("No other container in this direction found, cannot resize.\n");
466 cmd_output->json_output = sstrdup("{\"sucess\": false}");
469 LOG("other->percent = %f\n", other->percent);
470 LOG("current->percent before = %f\n", current->percent);
471 if (current->percent == 0.0)
472 current->percent = percentage;
473 if (other->percent == 0.0)
474 other->percent = percentage;
475 double new_current_percent = current->percent + ((double)ppt / 100.0);
476 double new_other_percent = other->percent - ((double)ppt / 100.0);
477 LOG("new_current_percent = %f\n", new_current_percent);
478 LOG("new_other_percent = %f\n", new_other_percent);
479 /* Ensure that the new percentages are positive and greater than
480 * 0.05 to have a reasonable minimum size. */
481 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
482 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
483 current->percent += ((double)ppt / 100.0);
484 other->percent -= ((double)ppt / 100.0);
485 LOG("current->percent after = %f\n", current->percent);
486 LOG("other->percent after = %f\n", other->percent);
488 LOG("Not resizing, already at minimum size\n");
492 cmd_output->needs_tree_render = true;
493 // XXX: default reply for now, make this a better reply
494 cmd_output->json_output = sstrdup("{\"success\": true}");
498 * Implementation of 'border normal|none|1pixel|toggle'.
501 void cmd_border(I3_CMD, char *border_style_str) {
502 DLOG("border style should be changed to %s\n", border_style_str);
507 TAILQ_FOREACH(current, &owindows, owindows) {
508 DLOG("matching: %p / %s\n", current->con, current->con->name);
509 int border_style = current->con->border_style;
510 if (strcmp(border_style_str, "toggle") == 0) {
514 if (strcmp(border_style_str, "normal") == 0)
515 border_style = BS_NORMAL;
516 else if (strcmp(border_style_str, "none") == 0)
517 border_style = BS_NONE;
518 else if (strcmp(border_style_str, "1pixel") == 0)
519 border_style = BS_1PIXEL;
521 ELOG("BUG: called with border_style=%s\n", border_style_str);
522 cmd_output->json_output = sstrdup("{\"sucess\": false}");
526 con_set_border_style(current->con, border_style);
529 cmd_output->needs_tree_render = true;
530 // XXX: default reply for now, make this a better reply
531 cmd_output->json_output = sstrdup("{\"success\": true}");
535 * Implementation of 'nop <comment>'.
538 void cmd_nop(I3_CMD, char *comment) {
539 LOG("-------------------------------------------------\n");
540 LOG(" NOP: %s\n", comment);
541 LOG("-------------------------------------------------\n");
545 * Implementation of 'append_layout <path>'.
548 void cmd_append_layout(I3_CMD, char *path) {
549 LOG("Appending layout \"%s\"\n", path);
550 tree_append_json(path);
552 cmd_output->needs_tree_render = true;
553 // XXX: default reply for now, make this a better reply
554 cmd_output->json_output = sstrdup("{\"success\": true}");
558 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
561 void cmd_workspace(I3_CMD, char *which) {
564 DLOG("which=%s\n", which);
566 if (strcmp(which, "next") == 0)
567 ws = workspace_next();
568 else if (strcmp(which, "prev") == 0)
569 ws = workspace_prev();
570 else if (strcmp(which, "next_on_output") == 0)
571 ws = workspace_next_on_output();
572 else if (strcmp(which, "prev_on_output") == 0)
573 ws = workspace_prev_on_output();
575 ELOG("BUG: called with which=%s\n", which);
576 cmd_output->json_output = sstrdup("{\"sucess\": false}");
582 cmd_output->needs_tree_render = true;
583 // XXX: default reply for now, make this a better reply
584 cmd_output->json_output = sstrdup("{\"success\": true}");
588 * Implementation of 'workspace back_and_forth'.
591 void cmd_workspace_back_and_forth(I3_CMD) {
592 workspace_back_and_forth();
594 cmd_output->needs_tree_render = true;
595 // XXX: default reply for now, make this a better reply
596 cmd_output->json_output = sstrdup("{\"success\": true}");
600 * Implementation of 'workspace <name>'
603 void cmd_workspace_name(I3_CMD, char *name) {
604 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
605 LOG("You cannot switch to the i3 internal workspaces.\n");
606 cmd_output->json_output = sstrdup("{\"sucess\": false}");
610 DLOG("should switch to workspace %s\n", name);
612 Con *ws = con_get_workspace(focused);
614 /* Check if the command wants to switch to the current workspace */
615 if (strcmp(ws->name, name) == 0) {
616 DLOG("This workspace is already focused.\n");
617 if (config.workspace_auto_back_and_forth) {
618 workspace_back_and_forth();
621 cmd_output->json_output = sstrdup("{\"sucess\": false}");
625 workspace_show_by_name(name);
627 cmd_output->needs_tree_render = true;
628 // XXX: default reply for now, make this a better reply
629 cmd_output->json_output = sstrdup("{\"success\": true}");
633 * Implementation of 'mark <mark>'
636 void cmd_mark(I3_CMD, char *mark) {
637 DLOG("Clearing all windows which have that mark first\n");
640 TAILQ_FOREACH(con, &all_cons, all_cons) {
641 if (con->mark && strcmp(con->mark, mark) == 0)
645 DLOG("marking window with str %s\n", mark);
650 TAILQ_FOREACH(current, &owindows, owindows) {
651 DLOG("matching: %p / %s\n", current->con, current->con->name);
652 current->con->mark = sstrdup(mark);
655 cmd_output->needs_tree_render = true;
656 // XXX: default reply for now, make this a better reply
657 cmd_output->json_output = sstrdup("{\"success\": true}");
661 * Implementation of 'mode <string>'.
664 void cmd_mode(I3_CMD, char *mode) {
665 DLOG("mode=%s\n", mode);
668 // XXX: default reply for now, make this a better reply
669 cmd_output->json_output = sstrdup("{\"success\": true}");
673 * Implementation of 'move [window|container] [to] output <str>'.
676 void cmd_move_con_to_output(I3_CMD, char *name) {
679 DLOG("should move window to output %s\n", name);
684 Output *current_output = NULL;
687 // TODO: fix the handling of criteria
688 TAILQ_FOREACH(current, &owindows, owindows)
689 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
691 assert(current_output != NULL);
693 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
694 if (strcasecmp(name, "up") == 0)
695 output = get_output_next(D_UP, current_output);
696 else if (strcasecmp(name, "down") == 0)
697 output = get_output_next(D_DOWN, current_output);
698 else if (strcasecmp(name, "left") == 0)
699 output = get_output_next(D_LEFT, current_output);
700 else if (strcasecmp(name, "right") == 0)
701 output = get_output_next(D_RIGHT, current_output);
703 output = get_output_by_name(name);
706 LOG("No such output found.\n");
707 cmd_output->json_output = sstrdup("{\"sucess\": false}");
711 /* get visible workspace on output */
713 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
715 cmd_output->json_output = sstrdup("{\"sucess\": false}");
719 TAILQ_FOREACH(current, &owindows, owindows) {
720 DLOG("matching: %p / %s\n", current->con, current->con->name);
721 con_move_to_workspace(current->con, ws, true, false);
724 cmd_output->needs_tree_render = true;
725 // XXX: default reply for now, make this a better reply
726 cmd_output->json_output = sstrdup("{\"success\": true}");
730 * Implementation of 'floating enable|disable|toggle'
733 void cmd_floating(I3_CMD, char *floating_mode) {
736 DLOG("floating_mode=%s\n", floating_mode);
740 TAILQ_FOREACH(current, &owindows, owindows) {
741 DLOG("matching: %p / %s\n", current->con, current->con->name);
742 if (strcmp(floating_mode, "toggle") == 0) {
743 DLOG("should toggle mode\n");
744 toggle_floating_mode(current->con, false);
746 DLOG("should switch mode to %s\n", floating_mode);
747 if (strcmp(floating_mode, "enable") == 0) {
748 floating_enable(current->con, false);
750 floating_disable(current->con, false);
755 cmd_output->needs_tree_render = true;
756 // XXX: default reply for now, make this a better reply
757 cmd_output->json_output = sstrdup("{\"success\": true}");
761 * Implementation of 'move workspace to [output] <str>'.
764 void cmd_move_workspace_to_output(I3_CMD, char *name) {
765 DLOG("should move workspace to output %s\n", name);
770 TAILQ_FOREACH(current, &owindows, owindows) {
771 Output *current_output = get_output_containing(current->con->rect.x,
772 current->con->rect.y);
773 Output *output = get_output_from_string(current_output, name);
775 LOG("No such output\n");
776 cmd_output->json_output = sstrdup("{\"sucess\": false}");
780 Con *content = output_get_content(output->con);
781 LOG("got output %p with content %p\n", output, content);
783 Con *ws = con_get_workspace(current->con);
784 LOG("should move workspace %p / %s\n", ws, ws->name);
786 if (con_num_children(ws->parent) == 1) {
787 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
789 /* check if we can find a workspace assigned to this output */
790 bool used_assignment = false;
791 struct Workspace_Assignment *assignment;
792 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
793 if (strcmp(assignment->output, current_output->name) != 0)
796 /* check if this workspace is already attached to the tree */
797 Con *workspace = NULL, *out;
798 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
799 GREP_FIRST(workspace, output_get_content(out),
800 !strcasecmp(child->name, assignment->name));
801 if (workspace != NULL)
804 /* so create the workspace referenced to by this assignment */
805 LOG("Creating workspace from assignment %s.\n", assignment->name);
806 workspace_get(assignment->name, NULL);
807 used_assignment = true;
811 /* if we couldn't create the workspace using an assignment, create
812 * it on the output */
813 if (!used_assignment)
814 create_workspace_on_output(current_output, ws->parent);
816 /* notify the IPC listeners */
817 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
820 /* detach from the old output and attach to the new output */
821 bool workspace_was_visible = workspace_is_visible(ws);
822 Con *old_content = ws->parent;
824 if (workspace_was_visible) {
825 /* The workspace which we just detached was visible, so focus
826 * the next one in the focus-stack. */
827 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
828 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
829 workspace_show(focus_ws);
831 con_attach(ws, content, false);
833 /* fix the coordinates of the floating containers */
835 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
836 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
838 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
839 if (workspace_was_visible) {
840 /* Focus the moved workspace on the destination output. */
845 cmd_output->needs_tree_render = true;
846 // XXX: default reply for now, make this a better reply
847 cmd_output->json_output = sstrdup("{\"success\": true}");
851 * Implementation of 'split v|h|vertical|horizontal'.
854 void cmd_split(I3_CMD, char *direction) {
855 /* TODO: use matches */
856 LOG("splitting in direction %c\n", direction[0]);
857 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
859 cmd_output->needs_tree_render = true;
860 // XXX: default reply for now, make this a better reply
861 cmd_output->json_output = sstrdup("{\"success\": true}");
865 * Implementaiton of 'kill [window|client]'.
868 void cmd_kill(I3_CMD, char *kill_mode_str) {
869 if (kill_mode_str == NULL)
870 kill_mode_str = "window";
873 DLOG("kill_mode=%s\n", kill_mode_str);
876 if (strcmp(kill_mode_str, "window") == 0)
877 kill_mode = KILL_WINDOW;
878 else if (strcmp(kill_mode_str, "client") == 0)
879 kill_mode = KILL_CLIENT;
881 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
882 cmd_output->json_output = sstrdup("{\"sucess\": false}");
886 /* check if the match is empty, not if the result is empty */
887 if (match_is_empty(current_match))
888 tree_close_con(kill_mode);
890 TAILQ_FOREACH(current, &owindows, owindows) {
891 DLOG("matching: %p / %s\n", current->con, current->con->name);
892 tree_close(current->con, kill_mode, false, false);
896 cmd_output->needs_tree_render = true;
897 // XXX: default reply for now, make this a better reply
898 cmd_output->json_output = sstrdup("{\"success\": true}");
902 * Implementation of 'exec [--no-startup-id] <command>'.
905 void cmd_exec(I3_CMD, char *nosn, char *command) {
906 bool no_startup_id = (nosn != NULL);
908 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
909 start_application(command, no_startup_id);
911 // XXX: default reply for now, make this a better reply
912 cmd_output->json_output = sstrdup("{\"success\": true}");
916 * Implementation of 'focus left|right|up|down'.
919 void cmd_focus_direction(I3_CMD, char *direction) {
921 focused->type != CT_WORKSPACE &&
922 focused->fullscreen_mode != CF_NONE) {
923 LOG("Cannot change focus while in fullscreen mode.\n");
924 cmd_output->json_output = sstrdup("{\"sucess\": false}");
928 DLOG("direction = *%s*\n", direction);
930 if (strcmp(direction, "left") == 0)
931 tree_next('p', HORIZ);
932 else if (strcmp(direction, "right") == 0)
933 tree_next('n', HORIZ);
934 else if (strcmp(direction, "up") == 0)
935 tree_next('p', VERT);
936 else if (strcmp(direction, "down") == 0)
937 tree_next('n', VERT);
939 ELOG("Invalid focus direction (%s)\n", direction);
940 cmd_output->json_output = sstrdup("{\"sucess\": false}");
944 cmd_output->needs_tree_render = true;
945 // XXX: default reply for now, make this a better reply
946 cmd_output->json_output = sstrdup("{\"success\": true}");
950 * Implementation of 'focus tiling|floating|mode_toggle'.
953 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
955 focused->type != CT_WORKSPACE &&
956 focused->fullscreen_mode != CF_NONE) {
957 LOG("Cannot change focus while in fullscreen mode.\n");
958 cmd_output->json_output = sstrdup("{\"sucess\": false}");
962 DLOG("window_mode = %s\n", window_mode);
964 Con *ws = con_get_workspace(focused);
967 if (strcmp(window_mode, "mode_toggle") == 0) {
968 current = TAILQ_FIRST(&(ws->focus_head));
969 if (current != NULL && current->type == CT_FLOATING_CON)
970 window_mode = "tiling";
971 else window_mode = "floating";
973 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
974 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
975 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
978 con_focus(con_descend_focused(current));
983 cmd_output->needs_tree_render = true;
984 // XXX: default reply for now, make this a better reply
985 cmd_output->json_output = sstrdup("{\"success\": true}");
989 * Implementation of 'focus parent|child'.
992 void cmd_focus_level(I3_CMD, char *level) {
994 focused->type != CT_WORKSPACE &&
995 focused->fullscreen_mode != CF_NONE) {
996 LOG("Cannot change focus while in fullscreen mode.\n");
997 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1001 DLOG("level = %s\n", level);
1003 if (strcmp(level, "parent") == 0)
1007 cmd_output->needs_tree_render = true;
1008 // XXX: default reply for now, make this a better reply
1009 cmd_output->json_output = sstrdup("{\"success\": true}");
1013 * Implementation of 'focus'.
1016 void cmd_focus(I3_CMD) {
1017 DLOG("current_match = %p\n", current_match);
1019 focused->type != CT_WORKSPACE &&
1020 focused->fullscreen_mode != CF_NONE) {
1021 LOG("Cannot change focus while in fullscreen mode.\n");
1022 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1028 if (match_is_empty(current_match)) {
1029 ELOG("You have to specify which window/container should be focused.\n");
1030 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1032 sasprintf(&(cmd_output->json_output),
1033 "{\"success\":false, \"error\":\"You have to "
1034 "specify which window/container should be focused\"}");
1039 TAILQ_FOREACH(current, &owindows, owindows) {
1040 Con *ws = con_get_workspace(current->con);
1041 /* If no workspace could be found, this was a dock window.
1042 * Just skip it, you cannot focus dock windows. */
1046 /* If the container is not on the current workspace,
1047 * workspace_show() will switch to a different workspace and (if
1048 * enabled) trigger a mouse pointer warp to the currently focused
1049 * container (!) on the target workspace.
1051 * Therefore, before calling workspace_show(), we make sure that
1052 * 'current' will be focused on the workspace. However, we cannot
1053 * just con_focus(current) because then the pointer will not be
1054 * warped at all (the code thinks we are already there).
1056 * So we focus 'current' to make it the currently focused window of
1057 * the target workspace, then revert focus. */
1058 Con *currently_focused = focused;
1059 con_focus(current->con);
1060 con_focus(currently_focused);
1062 /* Now switch to the workspace, then focus */
1064 LOG("focusing %p / %s\n", current->con, current->con->name);
1065 con_focus(current->con);
1070 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1071 "while only exactly one container can be focused at a time.\n", count);
1073 cmd_output->needs_tree_render = true;
1074 // XXX: default reply for now, make this a better reply
1075 cmd_output->json_output = sstrdup("{\"success\": true}");
1079 * Implementation of 'fullscreen [global]'.
1082 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1083 if (fullscreen_mode == NULL)
1084 fullscreen_mode = "output";
1085 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1090 TAILQ_FOREACH(current, &owindows, owindows) {
1091 printf("matching: %p / %s\n", current->con, current->con->name);
1092 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1095 cmd_output->needs_tree_render = true;
1096 // XXX: default reply for now, make this a better reply
1097 cmd_output->json_output = sstrdup("{\"success\": true}");
1101 * Implementation of 'move <direction> [<pixels> [px]]'.
1104 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1105 // 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
1106 int px = atoi(move_px);
1108 /* TODO: make 'move' work with criteria. */
1109 DLOG("moving in direction %s, px %s\n", direction, move_px);
1110 if (con_is_floating(focused)) {
1111 DLOG("floating move with %d pixels\n", px);
1112 Rect newrect = focused->parent->rect;
1113 if (strcmp(direction, "left") == 0) {
1115 } else if (strcmp(direction, "right") == 0) {
1117 } else if (strcmp(direction, "up") == 0) {
1119 } else if (strcmp(direction, "down") == 0) {
1122 floating_reposition(focused->parent, newrect);
1124 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1125 (strcmp(direction, "left") == 0 ? D_LEFT :
1126 (strcmp(direction, "up") == 0 ? D_UP :
1128 cmd_output->needs_tree_render = true;
1131 // XXX: default reply for now, make this a better reply
1132 cmd_output->json_output = sstrdup("{\"success\": true}");
1136 * Implementation of 'layout default|stacked|stacking|tabbed'.
1139 void cmd_layout(I3_CMD, char *layout_str) {
1140 if (strcmp(layout_str, "stacking") == 0)
1141 layout_str = "stacked";
1142 DLOG("changing layout to %s\n", layout_str);
1144 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1145 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1148 /* check if the match is empty, not if the result is empty */
1149 if (match_is_empty(current_match))
1150 con_set_layout(focused->parent, layout);
1152 TAILQ_FOREACH(current, &owindows, owindows) {
1153 DLOG("matching: %p / %s\n", current->con, current->con->name);
1154 con_set_layout(current->con, layout);
1158 cmd_output->needs_tree_render = true;
1159 // XXX: default reply for now, make this a better reply
1160 cmd_output->json_output = sstrdup("{\"success\": true}");
1164 * Implementaiton of 'exit'.
1167 void cmd_exit(I3_CMD) {
1168 LOG("Exiting due to user command.\n");
1175 * Implementaiton of 'reload'.
1178 void cmd_reload(I3_CMD) {
1180 kill_configerror_nagbar(false);
1181 load_configuration(conn, NULL, true);
1183 /* Send an IPC event just in case the ws names have changed */
1184 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1186 // XXX: default reply for now, make this a better reply
1187 cmd_output->json_output = sstrdup("{\"success\": true}");
1191 * Implementaiton of 'restart'.
1194 void cmd_restart(I3_CMD) {
1195 LOG("restarting i3\n");
1198 // XXX: default reply for now, make this a better reply
1199 cmd_output->json_output = sstrdup("{\"success\": true}");
1203 * Implementaiton of 'open'.
1206 void cmd_open(I3_CMD) {
1207 LOG("opening new container\n");
1208 Con *con = tree_open_con(NULL, NULL);
1210 sasprintf(&(cmd_output->json_output),
1211 "{\"success\":true, \"id\":%ld}", (long int)con);
1213 cmd_output->needs_tree_render = true;
1217 * Implementation of 'focus output <output>'.
1220 void cmd_focus_output(I3_CMD, char *name) {
1223 DLOG("name = %s\n", name);
1227 /* get the output */
1228 Output *current_output = NULL;
1231 TAILQ_FOREACH(current, &owindows, owindows)
1232 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1233 assert(current_output != NULL);
1235 output = get_output_from_string(current_output, name);
1238 LOG("No such output found.\n");
1239 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1243 /* get visible workspace on output */
1245 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1247 cmd_output->json_output = sstrdup("{\"sucess\": false}");
1253 cmd_output->needs_tree_render = true;
1254 // XXX: default reply for now, make this a better reply
1255 cmd_output->json_output = sstrdup("{\"success\": true}");
1259 * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1262 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1267 if (!con_is_floating(focused)) {
1268 ELOG("Cannot change position. The window/container is not floating\n");
1269 sasprintf(&(cmd_output->json_output),
1270 "{\"success\":false, \"error\":\"Cannot change position. "
1271 "The window/container is not floating.\"}");
1275 if (strcmp(method, "absolute") == 0) {
1276 focused->parent->rect.x = x;
1277 focused->parent->rect.y = y;
1279 DLOG("moving to absolute position %d %d\n", x, y);
1280 floating_maybe_reassign_ws(focused->parent);
1281 cmd_output->needs_tree_render = true;
1284 if (strcmp(method, "position") == 0) {
1285 Rect newrect = focused->parent->rect;
1287 DLOG("moving to position %d %d\n", x, y);
1291 floating_reposition(focused->parent, newrect);
1294 // XXX: default reply for now, make this a better reply
1295 cmd_output->json_output = sstrdup("{\"success\": true}");
1299 * Implementation of 'move [window|container] [to] [absolute] position center
1302 void cmd_move_window_to_center(I3_CMD, char *method) {
1304 if (!con_is_floating(focused)) {
1305 ELOG("Cannot change position. The window/container is not floating\n");
1306 sasprintf(&(cmd_output->json_output),
1307 "{\"success\":false, \"error\":\"Cannot change position. "
1308 "The window/container is not floating.\"}");
1311 if (strcmp(method, "absolute") == 0) {
1312 Rect *rect = &focused->parent->rect;
1314 DLOG("moving to absolute center\n");
1315 rect->x = croot->rect.width/2 - rect->width/2;
1316 rect->y = croot->rect.height/2 - rect->height/2;
1318 floating_maybe_reassign_ws(focused->parent);
1319 cmd_output->needs_tree_render = true;
1322 if (strcmp(method, "position") == 0) {
1323 Rect *wsrect = &con_get_workspace(focused)->rect;
1324 Rect newrect = focused->parent->rect;
1326 DLOG("moving to center\n");
1327 newrect.x = wsrect->width/2 - newrect.width/2;
1328 newrect.y = wsrect->height/2 - newrect.height/2;
1330 floating_reposition(focused->parent, newrect);
1333 // XXX: default reply for now, make this a better reply
1334 cmd_output->json_output = sstrdup("{\"success\": true}");
1338 * Implementation of 'move scratchpad'.
1341 void cmd_move_scratchpad(I3_CMD) {
1342 DLOG("should move window to scratchpad\n");
1347 TAILQ_FOREACH(current, &owindows, owindows) {
1348 DLOG("matching: %p / %s\n", current->con, current->con->name);
1349 scratchpad_move(current->con);
1352 cmd_output->needs_tree_render = true;
1353 // XXX: default reply for now, make this a better reply
1354 cmd_output->json_output = sstrdup("{\"success\": true}");
1358 * Implementation of 'scratchpad show'.
1361 void cmd_scratchpad_show(I3_CMD) {
1362 DLOG("should show scratchpad window\n");
1365 if (match_is_empty(current_match)) {
1366 scratchpad_show(NULL);
1368 TAILQ_FOREACH(current, &owindows, owindows) {
1369 DLOG("matching: %p / %s\n", current->con, current->con->name);
1370 scratchpad_show(current->con);
1374 cmd_output->needs_tree_render = true;
1375 // XXX: default reply for now, make this a better reply
1376 cmd_output->json_output = sstrdup("{\"success\": true}");