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 char *cmd_criteria_init(Match *current_match) {
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);
192 /* This command is internal and does not generate a JSON reply. */
197 * A match specification just finished (the closing square bracket was found),
198 * so we filter the list of owindows.
201 char *cmd_criteria_match_windows(Match *current_match) {
202 owindow *next, *current;
204 DLOG("match specification finished, matching...\n");
205 /* copy the old list head to iterate through it and start with a fresh
206 * list which will contain only matching windows */
207 struct owindows_head old = owindows;
208 TAILQ_INIT(&owindows);
209 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
210 /* make a copy of the next pointer and advance the pointer to the
211 * next element as we are going to invalidate the element’s
212 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
214 next = TAILQ_NEXT(next, owindows);
216 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
217 if (current_match->con_id != NULL) {
218 if (current_match->con_id == current->con) {
219 DLOG("matches container!\n");
220 TAILQ_INSERT_TAIL(&owindows, current, owindows);
222 } else if (current_match->mark != NULL && current->con->mark != NULL &&
223 regex_matches(current_match->mark, current->con->mark)) {
224 DLOG("match by mark\n");
225 TAILQ_INSERT_TAIL(&owindows, current, owindows);
227 if (current->con->window == NULL)
229 if (match_matches_window(current_match, current->con->window)) {
230 DLOG("matches window!\n");
231 TAILQ_INSERT_TAIL(&owindows, current, owindows);
233 DLOG("doesnt match\n");
239 TAILQ_FOREACH(current, &owindows, owindows) {
240 DLOG("matching: %p / %s\n", current->con, current->con->name);
243 /* This command is internal and does not generate a JSON reply. */
248 * Interprets a ctype=cvalue pair and adds it to the current match
252 char *cmd_criteria_add(Match *current_match, char *ctype, char *cvalue) {
253 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
255 if (strcmp(ctype, "class") == 0) {
256 current_match->class = regex_new(cvalue);
260 if (strcmp(ctype, "instance") == 0) {
261 current_match->instance = regex_new(cvalue);
265 if (strcmp(ctype, "window_role") == 0) {
266 current_match->role = regex_new(cvalue);
270 if (strcmp(ctype, "con_id") == 0) {
272 long parsed = strtol(cvalue, &end, 10);
273 if (parsed == LONG_MIN ||
274 parsed == LONG_MAX ||
276 (end && *end != '\0')) {
277 ELOG("Could not parse con id \"%s\"\n", cvalue);
279 current_match->con_id = (Con*)parsed;
280 printf("id as int = %p\n", current_match->con_id);
285 if (strcmp(ctype, "id") == 0) {
287 long parsed = strtol(cvalue, &end, 10);
288 if (parsed == LONG_MIN ||
289 parsed == LONG_MAX ||
291 (end && *end != '\0')) {
292 ELOG("Could not parse window id \"%s\"\n", cvalue);
294 current_match->id = parsed;
295 printf("window id as int = %d\n", current_match->id);
300 if (strcmp(ctype, "con_mark") == 0) {
301 current_match->mark = regex_new(cvalue);
305 if (strcmp(ctype, "title") == 0) {
306 current_match->title = regex_new(cvalue);
310 ELOG("Unknown criterion: %s\n", ctype);
312 /* This command is internal and does not generate a JSON reply. */
317 * Implementation of 'move [window|container] [to] workspace
318 * next|prev|next_on_output|prev_on_output'.
321 char *cmd_move_con_to_workspace(Match *current_match, char *which) {
324 DLOG("which=%s\n", which);
328 /* get the workspace */
330 if (strcmp(which, "next") == 0)
331 ws = workspace_next();
332 else if (strcmp(which, "prev") == 0)
333 ws = workspace_prev();
334 else if (strcmp(which, "next_on_output") == 0)
335 ws = workspace_next_on_output();
336 else if (strcmp(which, "prev_on_output") == 0)
337 ws = workspace_prev_on_output();
339 ELOG("BUG: called with which=%s\n", which);
340 return sstrdup("{\"sucess\": false}");
343 TAILQ_FOREACH(current, &owindows, owindows) {
344 DLOG("matching: %p / %s\n", current->con, current->con->name);
345 con_move_to_workspace(current->con, ws, true, false);
350 // XXX: default reply for now, make this a better reply
351 return sstrdup("{\"success\": true}");
355 * Implementation of 'move [window|container] [to] workspace <name>'.
358 char *cmd_move_con_to_workspace_name(Match *current_match, char *name) {
359 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
360 LOG("You cannot switch to the i3 internal workspaces.\n");
361 return sstrdup("{\"sucess\": false}");
366 /* Error out early to not create a non-existing workspace (in
367 * workspace_get()) if we are not actually able to move anything. */
368 if (match_is_empty(current_match) && focused->type == CT_WORKSPACE)
369 return sstrdup("{\"sucess\": false}");
371 LOG("should move window to workspace %s\n", name);
372 /* get the workspace */
373 Con *ws = workspace_get(name, NULL);
377 TAILQ_FOREACH(current, &owindows, owindows) {
378 DLOG("matching: %p / %s\n", current->con, current->con->name);
379 con_move_to_workspace(current->con, ws, true, false);
384 // XXX: default reply for now, make this a better reply
385 return sstrdup("{\"success\": true}");
389 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
392 char *cmd_resize(Match *current_match, char *way, char *direction, char *resize_px, char *resize_ppt) {
393 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
394 DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
395 // 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
396 int px = atoi(resize_px);
397 int ppt = atoi(resize_ppt);
398 if (strcmp(way, "shrink") == 0) {
404 if ((floating_con = con_inside_floating(focused))) {
405 printf("floating resize\n");
406 if (strcmp(direction, "up") == 0) {
407 floating_con->rect.y -= px;
408 floating_con->rect.height += px;
409 } else if (strcmp(direction, "down") == 0) {
410 floating_con->rect.height += px;
411 } else if (strcmp(direction, "left") == 0) {
412 floating_con->rect.x -= px;
413 floating_con->rect.width += px;
415 floating_con->rect.width += px;
418 LOG("tiling resize\n");
419 /* get the appropriate current container (skip stacked/tabbed cons) */
420 Con *current = focused;
421 while (current->parent->layout == L_STACKED ||
422 current->parent->layout == L_TABBED)
423 current = current->parent;
425 /* Then further go up until we find one with the matching orientation. */
426 orientation_t search_orientation =
427 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
429 while (current->type != CT_WORKSPACE &&
430 current->type != CT_FLOATING_CON &&
431 current->parent->orientation != search_orientation)
432 current = current->parent;
434 /* get the default percentage */
435 int children = con_num_children(current->parent);
437 LOG("ins. %d children\n", children);
438 double percentage = 1.0 / children;
439 LOG("default percentage = %f\n", percentage);
441 orientation_t orientation = current->parent->orientation;
443 if ((orientation == HORIZ &&
444 (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
445 (orientation == VERT &&
446 (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
447 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
448 (orientation == HORIZ ? "horizontal" : "vertical"));
449 return sstrdup("{\"sucess\": false}");
452 if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
453 other = TAILQ_PREV(current, nodes_head, nodes);
455 other = TAILQ_NEXT(current, nodes);
457 if (other == TAILQ_END(workspaces)) {
458 LOG("No other container in this direction found, cannot resize.\n");
459 return sstrdup("{\"sucess\": false}");
461 LOG("other->percent = %f\n", other->percent);
462 LOG("current->percent before = %f\n", current->percent);
463 if (current->percent == 0.0)
464 current->percent = percentage;
465 if (other->percent == 0.0)
466 other->percent = percentage;
467 double new_current_percent = current->percent + ((double)ppt / 100.0);
468 double new_other_percent = other->percent - ((double)ppt / 100.0);
469 LOG("new_current_percent = %f\n", new_current_percent);
470 LOG("new_other_percent = %f\n", new_other_percent);
471 /* Ensure that the new percentages are positive and greater than
472 * 0.05 to have a reasonable minimum size. */
473 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
474 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
475 current->percent += ((double)ppt / 100.0);
476 other->percent -= ((double)ppt / 100.0);
477 LOG("current->percent after = %f\n", current->percent);
478 LOG("other->percent after = %f\n", other->percent);
480 LOG("Not resizing, already at minimum size\n");
486 // XXX: default reply for now, make this a better reply
487 return sstrdup("{\"success\": true}");
491 * Implementation of 'border normal|none|1pixel|toggle'.
494 char *cmd_border(Match *current_match, char *border_style_str) {
495 DLOG("border style should be changed to %s\n", border_style_str);
500 TAILQ_FOREACH(current, &owindows, owindows) {
501 DLOG("matching: %p / %s\n", current->con, current->con->name);
502 int border_style = current->con->border_style;
503 if (strcmp(border_style_str, "toggle") == 0) {
507 if (strcmp(border_style_str, "normal") == 0)
508 border_style = BS_NORMAL;
509 else if (strcmp(border_style_str, "none") == 0)
510 border_style = BS_NONE;
511 else if (strcmp(border_style_str, "1pixel") == 0)
512 border_style = BS_1PIXEL;
514 ELOG("BUG: called with border_style=%s\n", border_style_str);
515 return sstrdup("{\"sucess\": false}");
518 con_set_border_style(current->con, border_style);
523 // XXX: default reply for now, make this a better reply
524 return sstrdup("{\"success\": true}");
528 * Implementation of 'nop <comment>'.
531 char *cmd_nop(Match *current_match, char *comment) {
532 LOG("-------------------------------------------------\n");
533 LOG(" NOP: %s\n", comment);
534 LOG("-------------------------------------------------\n");
540 * Implementation of 'append_layout <path>'.
543 char *cmd_append_layout(Match *current_match, char *path) {
544 LOG("Appending layout \"%s\"\n", path);
545 tree_append_json(path);
548 // XXX: default reply for now, make this a better reply
549 return sstrdup("{\"success\": true}");
553 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
556 char *cmd_workspace(Match *current_match, char *which) {
559 DLOG("which=%s\n", which);
561 if (strcmp(which, "next") == 0)
562 ws = workspace_next();
563 else if (strcmp(which, "prev") == 0)
564 ws = workspace_prev();
565 else if (strcmp(which, "next_on_output") == 0)
566 ws = workspace_next_on_output();
567 else if (strcmp(which, "prev_on_output") == 0)
568 ws = workspace_prev_on_output();
570 ELOG("BUG: called with which=%s\n", which);
571 return sstrdup("{\"sucess\": false}");
577 // XXX: default reply for now, make this a better reply
578 return sstrdup("{\"success\": true}");
582 * Implementation of 'workspace back_and_forth'.
585 char *cmd_workspace_back_and_forth(Match *current_match) {
586 workspace_back_and_forth();
589 // XXX: default reply for now, make this a better reply
590 return sstrdup("{\"success\": true}");
594 * Implementation of 'workspace <name>'
597 char *cmd_workspace_name(Match *current_match, char *name) {
598 if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
599 LOG("You cannot switch to the i3 internal workspaces.\n");
600 return sstrdup("{\"sucess\": false}");
603 DLOG("should switch to workspace %s\n", name);
605 Con *ws = con_get_workspace(focused);
607 /* Check if the command wants to switch to the current workspace */
608 if (strcmp(ws->name, name) == 0) {
609 DLOG("This workspace is already focused.\n");
610 if (config.workspace_auto_back_and_forth) {
611 workspace_back_and_forth();
614 return sstrdup("{\"sucess\": false}");
617 workspace_show_by_name(name);
621 // XXX: default reply for now, make this a better reply
622 return sstrdup("{\"success\": true}");
626 * Implementation of 'mark <mark>'
629 char *cmd_mark(Match *current_match, char *mark) {
630 DLOG("Clearing all windows which have that mark first\n");
633 TAILQ_FOREACH(con, &all_cons, all_cons) {
634 if (con->mark && strcmp(con->mark, mark) == 0)
638 DLOG("marking window with str %s\n", mark);
643 TAILQ_FOREACH(current, &owindows, owindows) {
644 DLOG("matching: %p / %s\n", current->con, current->con->name);
645 current->con->mark = sstrdup(mark);
650 // XXX: default reply for now, make this a better reply
651 return sstrdup("{\"success\": true}");
655 * Implementation of 'mode <string>'.
658 char *cmd_mode(Match *current_match, char *mode) {
659 DLOG("mode=%s\n", mode);
662 // XXX: default reply for now, make this a better reply
663 return sstrdup("{\"success\": true}");
667 * Implementation of 'move [window|container] [to] output <str>'.
670 char *cmd_move_con_to_output(Match *current_match, char *name) {
673 DLOG("should move window to output %s\n", name);
678 Output *current_output = NULL;
681 // TODO: fix the handling of criteria
682 TAILQ_FOREACH(current, &owindows, owindows)
683 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
685 assert(current_output != NULL);
687 // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
688 if (strcasecmp(name, "up") == 0)
689 output = get_output_next(D_UP, current_output);
690 else if (strcasecmp(name, "down") == 0)
691 output = get_output_next(D_DOWN, current_output);
692 else if (strcasecmp(name, "left") == 0)
693 output = get_output_next(D_LEFT, current_output);
694 else if (strcasecmp(name, "right") == 0)
695 output = get_output_next(D_RIGHT, current_output);
697 output = get_output_by_name(name);
700 LOG("No such output found.\n");
701 return sstrdup("{\"sucess\": false}");
704 /* get visible workspace on output */
706 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
708 return sstrdup("{\"sucess\": false}");
710 TAILQ_FOREACH(current, &owindows, owindows) {
711 DLOG("matching: %p / %s\n", current->con, current->con->name);
712 con_move_to_workspace(current->con, ws, true, false);
717 // XXX: default reply for now, make this a better reply
718 return sstrdup("{\"success\": true}");
722 * Implementation of 'floating enable|disable|toggle'
725 char *cmd_floating(Match *current_match, char *floating_mode) {
728 DLOG("floating_mode=%s\n", floating_mode);
732 TAILQ_FOREACH(current, &owindows, owindows) {
733 DLOG("matching: %p / %s\n", current->con, current->con->name);
734 if (strcmp(floating_mode, "toggle") == 0) {
735 DLOG("should toggle mode\n");
736 toggle_floating_mode(current->con, false);
738 DLOG("should switch mode to %s\n", floating_mode);
739 if (strcmp(floating_mode, "enable") == 0) {
740 floating_enable(current->con, false);
742 floating_disable(current->con, false);
749 // XXX: default reply for now, make this a better reply
750 return sstrdup("{\"success\": true}");
754 * Implementation of 'move workspace to [output] <str>'.
757 char *cmd_move_workspace_to_output(Match *current_match, char *name) {
758 DLOG("should move workspace to output %s\n", name);
763 TAILQ_FOREACH(current, &owindows, owindows) {
764 Output *current_output = get_output_containing(current->con->rect.x,
765 current->con->rect.y);
766 Output *output = get_output_from_string(current_output, name);
768 LOG("No such output\n");
769 return sstrdup("{\"sucess\": false}");
772 Con *content = output_get_content(output->con);
773 LOG("got output %p with content %p\n", output, content);
775 Con *ws = con_get_workspace(current->con);
776 LOG("should move workspace %p / %s\n", ws, ws->name);
777 if (con_num_children(ws->parent) == 1) {
778 LOG("Not moving workspace \"%s\", it is the only workspace on its output.\n", ws->name);
781 bool workspace_was_visible = workspace_is_visible(ws);
782 Con *old_content = ws->parent;
784 if (workspace_was_visible) {
785 /* The workspace which we just detached was visible, so focus
786 * the next one in the focus-stack. */
787 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
788 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
789 workspace_show(focus_ws);
791 con_attach(ws, content, false);
792 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
793 if (workspace_was_visible) {
794 /* Focus the moved workspace on the destination output. */
801 // XXX: default reply for now, make this a better reply
802 return sstrdup("{\"success\": true}");
806 * Implementation of 'split v|h|vertical|horizontal'.
809 char *cmd_split(Match *current_match, char *direction) {
810 /* TODO: use matches */
811 LOG("splitting in direction %c\n", direction[0]);
812 tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
816 // XXX: default reply for now, make this a better reply
817 return sstrdup("{\"success\": true}");
821 * Implementaiton of 'kill [window|client]'.
824 char *cmd_kill(Match *current_match, char *kill_mode_str) {
825 if (kill_mode_str == NULL)
826 kill_mode_str = "window";
829 DLOG("kill_mode=%s\n", kill_mode_str);
832 if (strcmp(kill_mode_str, "window") == 0)
833 kill_mode = KILL_WINDOW;
834 else if (strcmp(kill_mode_str, "client") == 0)
835 kill_mode = KILL_CLIENT;
837 ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
838 return sstrdup("{\"sucess\": false}");
841 /* check if the match is empty, not if the result is empty */
842 if (match_is_empty(current_match))
843 tree_close_con(kill_mode);
845 TAILQ_FOREACH(current, &owindows, owindows) {
846 DLOG("matching: %p / %s\n", current->con, current->con->name);
847 tree_close(current->con, kill_mode, false, false);
853 // XXX: default reply for now, make this a better reply
854 return sstrdup("{\"success\": true}");
858 * Implementation of 'exec [--no-startup-id] <command>'.
861 char *cmd_exec(Match *current_match, char *nosn, char *command) {
862 bool no_startup_id = (nosn != NULL);
864 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
865 start_application(command, no_startup_id);
867 // XXX: default reply for now, make this a better reply
868 return sstrdup("{\"success\": true}");
872 * Implementation of 'focus left|right|up|down'.
875 char *cmd_focus_direction(Match *current_match, char *direction) {
877 focused->type != CT_WORKSPACE &&
878 focused->fullscreen_mode != CF_NONE) {
879 LOG("Cannot change focus while in fullscreen mode.\n");
880 return sstrdup("{\"sucess\": false}");
883 DLOG("direction = *%s*\n", direction);
885 if (strcmp(direction, "left") == 0)
886 tree_next('p', HORIZ);
887 else if (strcmp(direction, "right") == 0)
888 tree_next('n', HORIZ);
889 else if (strcmp(direction, "up") == 0)
890 tree_next('p', VERT);
891 else if (strcmp(direction, "down") == 0)
892 tree_next('n', VERT);
894 ELOG("Invalid focus direction (%s)\n", direction);
895 return sstrdup("{\"sucess\": false}");
900 // XXX: default reply for now, make this a better reply
901 return sstrdup("{\"success\": true}");
905 * Implementation of 'focus tiling|floating|mode_toggle'.
908 char *cmd_focus_window_mode(Match *current_match, char *window_mode) {
910 focused->type != CT_WORKSPACE &&
911 focused->fullscreen_mode != CF_NONE) {
912 LOG("Cannot change focus while in fullscreen mode.\n");
913 return sstrdup("{\"sucess\": false}");
916 DLOG("window_mode = %s\n", window_mode);
918 Con *ws = con_get_workspace(focused);
921 if (strcmp(window_mode, "mode_toggle") == 0) {
922 current = TAILQ_FIRST(&(ws->focus_head));
923 if (current != NULL && current->type == CT_FLOATING_CON)
924 window_mode = "tiling";
925 else window_mode = "floating";
927 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
928 if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
929 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
932 con_focus(con_descend_focused(current));
939 // XXX: default reply for now, make this a better reply
940 return sstrdup("{\"success\": true}");
944 * Implementation of 'focus parent|child'.
947 char *cmd_focus_level(Match *current_match, char *level) {
949 focused->type != CT_WORKSPACE &&
950 focused->fullscreen_mode != CF_NONE) {
951 LOG("Cannot change focus while in fullscreen mode.\n");
952 return sstrdup("{\"sucess\": false}");
955 DLOG("level = %s\n", level);
957 if (strcmp(level, "parent") == 0)
963 // XXX: default reply for now, make this a better reply
964 return sstrdup("{\"success\": true}");
968 * Implementation of 'focus'.
971 char *cmd_focus(Match *current_match) {
972 DLOG("current_match = %p\n", current_match);
974 focused->type != CT_WORKSPACE &&
975 focused->fullscreen_mode != CF_NONE) {
976 LOG("Cannot change focus while in fullscreen mode.\n");
977 return sstrdup("{\"sucess\": false}");
982 if (match_is_empty(current_match)) {
983 ELOG("You have to specify which window/container should be focused.\n");
984 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
987 sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
988 "specify which window/container should be focused\"}");
993 TAILQ_FOREACH(current, &owindows, owindows) {
994 Con *ws = con_get_workspace(current->con);
995 /* If no workspace could be found, this was a dock window.
996 * Just skip it, you cannot focus dock windows. */
1000 /* If the container is not on the current workspace,
1001 * workspace_show() will switch to a different workspace and (if
1002 * enabled) trigger a mouse pointer warp to the currently focused
1003 * container (!) on the target workspace.
1005 * Therefore, before calling workspace_show(), we make sure that
1006 * 'current' will be focused on the workspace. However, we cannot
1007 * just con_focus(current) because then the pointer will not be
1008 * warped at all (the code thinks we are already there).
1010 * So we focus 'current' to make it the currently focused window of
1011 * the target workspace, then revert focus. */
1012 Con *currently_focused = focused;
1013 con_focus(current->con);
1014 con_focus(currently_focused);
1016 /* Now switch to the workspace, then focus */
1018 LOG("focusing %p / %s\n", current->con, current->con->name);
1019 con_focus(current->con);
1024 LOG("WARNING: Your criteria for the focus command matches %d containers, "
1025 "while only exactly one container can be focused at a time.\n", count);
1029 // XXX: default reply for now, make this a better reply
1030 return sstrdup("{\"success\": true}");
1034 * Implementation of 'fullscreen [global]'.
1037 char *cmd_fullscreen(Match *current_match, char *fullscreen_mode) {
1038 if (fullscreen_mode == NULL)
1039 fullscreen_mode = "output";
1040 DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1045 TAILQ_FOREACH(current, &owindows, owindows) {
1046 printf("matching: %p / %s\n", current->con, current->con->name);
1047 con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1052 // XXX: default reply for now, make this a better reply
1053 return sstrdup("{\"success\": true}");
1057 * Implementation of 'move <direction> [<pixels> [px]]'.
1060 char *cmd_move_direction(Match *current_match, char *direction, char *move_px) {
1061 // 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
1062 int px = atoi(move_px);
1064 /* TODO: make 'move' work with criteria. */
1065 DLOG("moving in direction %s, px %s\n", direction, move_px);
1066 if (con_is_floating(focused)) {
1067 DLOG("floating move with %d pixels\n", px);
1068 Rect newrect = focused->parent->rect;
1069 if (strcmp(direction, "left") == 0) {
1071 } else if (strcmp(direction, "right") == 0) {
1073 } else if (strcmp(direction, "up") == 0) {
1075 } else if (strcmp(direction, "down") == 0) {
1078 floating_reposition(focused->parent, newrect);
1080 tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1081 (strcmp(direction, "left") == 0 ? D_LEFT :
1082 (strcmp(direction, "up") == 0 ? D_UP :
1088 // XXX: default reply for now, make this a better reply
1089 return sstrdup("{\"success\": true}");
1093 * Implementation of 'layout default|stacked|stacking|tabbed'.
1096 char *cmd_layout(Match *current_match, char *layout_str) {
1097 if (strcmp(layout_str, "stacking") == 0)
1098 layout_str = "stacked";
1099 DLOG("changing layout to %s\n", layout_str);
1101 int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1102 (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1105 /* check if the match is empty, not if the result is empty */
1106 if (match_is_empty(current_match))
1107 con_set_layout(focused->parent, layout);
1109 TAILQ_FOREACH(current, &owindows, owindows) {
1110 DLOG("matching: %p / %s\n", current->con, current->con->name);
1111 con_set_layout(current->con, layout);
1117 // XXX: default reply for now, make this a better reply
1118 return sstrdup("{\"success\": true}");
1122 * Implementaiton of 'exit'.
1125 char *cmd_exit(Match *current_match) {
1126 LOG("Exiting due to user command.\n");
1133 * Implementaiton of 'reload'.
1136 char *cmd_reload(Match *current_match) {
1138 kill_configerror_nagbar(false);
1139 load_configuration(conn, NULL, true);
1141 /* Send an IPC event just in case the ws names have changed */
1142 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1144 // XXX: default reply for now, make this a better reply
1145 return sstrdup("{\"success\": true}");
1149 * Implementaiton of 'restart'.
1152 char *cmd_restart(Match *current_match) {
1153 LOG("restarting i3\n");
1156 // XXX: default reply for now, make this a better reply
1157 return sstrdup("{\"success\": true}");
1161 * Implementaiton of 'open'.
1164 char *cmd_open(Match *current_match) {
1165 LOG("opening new container\n");
1166 Con *con = tree_open_con(NULL, NULL);
1169 sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
1177 * Implementation of 'focus output <output>'.
1180 char *cmd_focus_output(Match *current_match, char *name) {
1183 DLOG("name = %s\n", name);
1187 /* get the output */
1188 Output *current_output = NULL;
1191 TAILQ_FOREACH(current, &owindows, owindows)
1192 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1193 assert(current_output != NULL);
1195 output = get_output_from_string(current_output, name);
1198 LOG("No such output found.\n");
1199 return sstrdup("{\"sucess\": false}");
1202 /* get visible workspace on output */
1204 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1206 return sstrdup("{\"sucess\": false}");
1211 // XXX: default reply for now, make this a better reply
1212 return sstrdup("{\"success\": true}");
1216 * Implementation of 'move scratchpad'.
1219 char *cmd_move_scratchpad(Match *current_match) {
1220 DLOG("should move window to scratchpad\n");
1225 TAILQ_FOREACH(current, &owindows, owindows) {
1226 DLOG("matching: %p / %s\n", current->con, current->con->name);
1227 scratchpad_move(current->con);
1232 // XXX: default reply for now, make this a better reply
1233 return sstrdup("{\"success\": true}");
1237 * Implementation of 'scratchpad show'.
1240 char *cmd_scratchpad_show(Match *current_match) {
1241 DLOG("should show scratchpad window\n");
1244 if (match_is_empty(current_match)) {
1245 scratchpad_show(NULL);
1247 TAILQ_FOREACH(current, &owindows, owindows) {
1248 DLOG("matching: %p / %s\n", current->con, current->con->name);
1249 scratchpad_show(current->con);
1255 // XXX: default reply for now, make this a better reply
1256 return sstrdup("{\"success\": true}");