2 #define I3__FILE__ "commands.c"
18 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
19 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
20 #define ysuccess(success) do { \
32 #define HANDLE_EMPTY_MATCH do { \
33 if (match_is_empty(current_match)) { \
34 owindow *ow = smalloc(sizeof(owindow)); \
36 TAILQ_INIT(&owindows); \
37 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
48 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
59 if (strcasecmp(output_str,
"left") == 0) {
63 }
else if (strcasecmp(output_str,
"right") == 0) {
67 }
else if (strcasecmp(output_str,
"up") == 0) {
71 }
else if (strcasecmp(output_str,
"down") == 0) {
91 if (strcmp(ws->
name, name) != 0)
94 DLOG(
"This workspace is already focused.\n");
105 static pid_t migration_pid = -1;
112 static void nagbar_exited(EV_P_ ev_child *watcher,
int revents) {
113 ev_child_stop(EV_A_ watcher);
114 if (!WIFEXITED(watcher->rstatus)) {
115 fprintf(stderr,
"ERROR: i3-nagbar did not exit normally.\n");
119 int exitcode = WEXITSTATUS(watcher->rstatus);
120 printf(
"i3-nagbar process exited with status %d\n", exitcode);
122 fprintf(stderr,
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
131 #if EV_VERSION_MAJOR >= 4
137 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher,
int revent) {
138 if (migration_pid != -1) {
139 LOG(
"Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
140 kill(migration_pid, SIGKILL);
145 void cmd_MIGRATION_start_nagbar(
void) {
146 if (migration_pid != -1) {
147 fprintf(stderr,
"i3-nagbar already running.\n");
150 fprintf(stderr,
"Starting i3-nagbar, command parsing differs from expected output.\n");
151 ELOG(
"Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
152 ELOG(
"i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
153 ELOG(
"FYI: Your i3 version is " I3_VERSION
"\n");
154 migration_pid = fork();
155 if (migration_pid == -1) {
156 warn(
"Could not fork()");
161 if (migration_pid == 0) {
169 "You found a parsing error. Please, please, please, report it!",
180 ev_child *child =
smalloc(
sizeof(ev_child));
187 #if EV_VERSION_MAJOR >= 4
190 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
191 ev_cleanup_init(cleanup, nagbar_cleanup);
235 DLOG(
"match specification finished, matching...\n");
238 struct owindows_head old =
owindows;
247 DLOG(
"checking if con %p / %s matches\n", current->
con, current->
con->
name);
250 DLOG(
"matches container!\n");
255 DLOG(
"match by mark\n");
261 DLOG(
"matches window!\n");
264 DLOG(
"doesnt match\n");
281 DLOG(
"ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
283 if (strcmp(ctype,
"class") == 0) {
288 if (strcmp(ctype,
"instance") == 0) {
293 if (strcmp(ctype,
"window_role") == 0) {
298 if (strcmp(ctype,
"con_id") == 0) {
300 long parsed = strtol(cvalue, &end, 10);
301 if (parsed == LONG_MIN ||
302 parsed == LONG_MAX ||
304 (end && *end !=
'\0')) {
305 ELOG(
"Could not parse con id \"%s\"\n", cvalue);
313 if (strcmp(ctype,
"id") == 0) {
315 long parsed = strtol(cvalue, &end, 10);
316 if (parsed == LONG_MIN ||
317 parsed == LONG_MAX ||
319 (end && *end !=
'\0')) {
320 ELOG(
"Could not parse window id \"%s\"\n", cvalue);
328 if (strcmp(ctype,
"con_mark") == 0) {
333 if (strcmp(ctype,
"title") == 0) {
338 if (strcmp(ctype,
"urgent") == 0) {
339 if (strcasecmp(cvalue,
"latest") == 0 ||
340 strcasecmp(cvalue,
"newest") == 0 ||
341 strcasecmp(cvalue,
"recent") == 0 ||
342 strcasecmp(cvalue,
"last") == 0) {
344 }
else if (strcasecmp(cvalue,
"oldest") == 0 ||
345 strcasecmp(cvalue,
"first") == 0) {
351 ELOG(
"Unknown criterion: %s\n", ctype);
362 DLOG(
"which=%s\n", which);
377 if (strcmp(which,
"next") == 0)
379 else if (strcmp(which,
"prev") == 0)
381 else if (strcmp(which,
"next_on_output") == 0)
383 else if (strcmp(which,
"prev_on_output") == 0)
385 else if (strcmp(which,
"current") == 0)
388 ELOG(
"BUG: called with which=%s\n", which);
398 cmd_output->needs_tree_render =
true;
408 if (strncasecmp(name,
"__i3_", strlen(
"__i3_")) == 0) {
409 LOG(
"You cannot switch to the i3 internal workspaces.\n");
420 ELOG(
"No windows match your criteria, cannot move.\n");
426 ELOG(
"No window to move, you have focused a workspace.\n");
431 LOG(
"should move window to workspace %s\n", name);
442 cmd_output->needs_tree_render =
true;
463 LOG(
"should move window to workspace %s\n", which);
465 Con *output, *workspace = NULL;
468 long parsed_num = strtol(which, &endptr, 10);
469 if (parsed_num == LONG_MIN ||
470 parsed_num == LONG_MAX ||
473 LOG(
"Could not parse \"%s\" as a number.\n", which);
479 ystr(
"Could not parse number");
486 child->
num == parsed_num);
499 cmd_output->needs_tree_render =
true;
505 LOG(
"floating resize\n");
506 if (strcmp(direction,
"up") == 0) {
507 floating_con->
rect.
y -= px;
509 }
else if (strcmp(direction,
"down") == 0 || strcmp(direction,
"height") == 0) {
511 }
else if (strcmp(direction,
"left") == 0) {
512 floating_con->
rect.
x -= px;
520 LOG(
"tiling resize\n");
524 double percentage = 0;
527 current = current->
parent;
531 (strcmp(direction,
"left") == 0 || strcmp(direction,
"right") == 0 ?
HORIZ :
VERT);
535 current = current->
parent;
541 LOG(
"ins. %d children\n", children);
542 percentage = 1.0 / children;
543 LOG(
"default percentage = %f\n", percentage);
547 if ((orientation ==
HORIZ &&
548 (strcmp(direction,
"up") == 0 || strcmp(direction,
"down") == 0)) ||
549 (orientation ==
VERT &&
550 (strcmp(direction,
"left") == 0 || strcmp(direction,
"right") == 0))) {
551 LOG(
"You cannot resize in that direction. Your focus is in a %s split container currently.\n",
552 (orientation ==
HORIZ ?
"horizontal" :
"vertical"));
557 if (strcmp(direction,
"up") == 0 || strcmp(direction,
"left") == 0) {
558 other =
TAILQ_PREV(current, nodes_head, nodes);
563 LOG(
"No other container in this direction found, trying to look further up in the tree...\n");
564 current = current->
parent;
568 }
while (current->
type != CT_WORKSPACE &&
569 current->
type != CT_FLOATING_CON);
572 LOG(
"No other container in this direction found, trying to look further up in the tree...\n");
578 LOG(
"current->percent before = %f\n", current->
percent);
583 double new_current_percent = current->
percent + ((double)ppt / 100.0);
584 double new_other_percent = other->
percent - ((double)ppt / 100.0);
585 LOG(
"new_current_percent = %f\n", new_current_percent);
586 LOG(
"new_other_percent = %f\n", new_other_percent);
591 current->
percent += ((double)ppt / 100.0);
592 other->
percent -= ((double)ppt / 100.0);
593 LOG(
"current->percent after = %f\n", current->
percent);
594 LOG(
"other->percent after = %f\n", other->
percent);
596 LOG(
"Not resizing, already at minimum size\n");
603 LOG(
"width/height resize\n");
608 current = current->
parent;
612 (strcmp(direction,
"width") == 0 ?
HORIZ :
VERT);
614 while (current->
type != CT_WORKSPACE &&
615 current->
type != CT_FLOATING_CON &&
617 current = current->
parent;
621 LOG(
"ins. %d children\n", children);
622 double percentage = 1.0 / children;
623 LOG(
"default percentage = %f\n", percentage);
627 if ((orientation ==
HORIZ &&
628 strcmp(direction,
"height") == 0) ||
629 (orientation ==
VERT &&
630 strcmp(direction,
"width") == 0)) {
631 LOG(
"You cannot resize in that direction. Your focus is in a %s split container currently.\n",
632 (orientation ==
HORIZ ?
"horizontal" :
"vertical"));
638 LOG(
"This is the only container, cannot resize.\n");
646 LOG(
"child->percent = %f (child %p)\n", child->
percent, child);
651 double new_current_percent = current->
percent + ((double)ppt / 100.0);
652 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
653 LOG(
"new_current_percent = %f\n", new_current_percent);
654 LOG(
"subtract_percent = %f\n", subtract_percent);
658 if (child == current)
661 LOG(
"Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->
percent - subtract_percent);
667 LOG(
"Not resizing, already at minimum size\n");
672 current->
percent += ((double)ppt / 100.0);
673 LOG(
"current->percent after = %f\n", current->
percent);
676 if (child == current)
678 child->
percent -= subtract_percent;
679 LOG(
"child->percent after (%p) = %f\n", child, child->
percent);
691 DLOG(
"resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
693 int px = atoi(resize_px);
694 int ppt = atoi(resize_ppt);
695 if (strcmp(way,
"shrink") == 0) {
704 if (strcmp(direction,
"width") == 0 ||
705 strcmp(direction,
"height") == 0) {
714 cmd_output->needs_tree_render =
true;
724 DLOG(
"border style should be changed to %s\n", border_style_str);
732 if (strcmp(border_style_str,
"toggle") == 0) {
736 if (strcmp(border_style_str,
"normal") == 0)
738 else if (strcmp(border_style_str,
"none") == 0)
740 else if (strcmp(border_style_str,
"1pixel") == 0)
743 ELOG(
"BUG: called with border_style=%s\n", border_style_str);
751 cmd_output->needs_tree_render =
true;
761 LOG(
"-------------------------------------------------\n");
762 LOG(
" NOP: %s\n", comment);
763 LOG(
"-------------------------------------------------\n");
771 LOG(
"Appending layout \"%s\"\n", path);
774 cmd_output->needs_tree_render =
true;
786 DLOG(
"which=%s\n", which);
788 if (strcmp(which,
"next") == 0)
790 else if (strcmp(which,
"prev") == 0)
792 else if (strcmp(which,
"next_on_output") == 0)
794 else if (strcmp(which,
"prev_on_output") == 0)
797 ELOG(
"BUG: called with which=%s\n", which);
804 cmd_output->needs_tree_render =
true;
814 Con *output, *workspace = NULL;
817 long parsed_num = strtol(which, &endptr, 10);
818 if (parsed_num == LONG_MIN ||
819 parsed_num == LONG_MAX ||
822 LOG(
"Could not parse \"%s\" as a number.\n", which);
828 ystr(
"Could not parse number");
836 child->
num == parsed_num);
839 LOG(
"There is no workspace with number %ld, creating a new one.\n", parsed_num);
844 cmd_output->needs_tree_render =
true;
851 cmd_output->needs_tree_render =
true;
863 cmd_output->needs_tree_render =
true;
873 if (strncasecmp(name,
"__i3_", strlen(
"__i3_")) == 0) {
874 LOG(
"You cannot switch to the i3 internal workspaces.\n");
879 DLOG(
"should switch to workspace %s\n", name);
884 cmd_output->needs_tree_render =
true;
894 DLOG(
"Clearing all windows which have that mark first\n");
898 if (con->
mark && strcmp(con->
mark, mark) == 0)
902 DLOG(
"marking window with str %s\n", mark);
912 cmd_output->needs_tree_render =
true;
922 DLOG(
"mode=%s\n", mode);
936 DLOG(
"should move window to output %s\n", name);
941 Output *current_output = NULL;
948 assert(current_output != NULL);
951 if (strcasecmp(name,
"up") == 0)
953 else if (strcasecmp(name,
"down") == 0)
955 else if (strcasecmp(name,
"left") == 0)
957 else if (strcasecmp(name,
"right") == 0)
963 LOG(
"No such output found.\n");
981 cmd_output->needs_tree_render =
true;
993 DLOG(
"floating_mode=%s\n", floating_mode);
999 if (strcmp(floating_mode,
"toggle") == 0) {
1000 DLOG(
"should toggle mode\n");
1003 DLOG(
"should switch mode to %s\n", floating_mode);
1004 if (strcmp(floating_mode,
"enable") == 0) {
1012 cmd_output->needs_tree_render =
true;
1022 DLOG(
"should move workspace to output %s\n", name);
1030 if (!current_output) {
1031 ELOG(
"Cannot get current output. This is a bug in i3.\n");
1037 ELOG(
"Could not get output from string \"%s\"\n", name);
1043 LOG(
"got output %p with content %p\n", output, content);
1046 LOG(
"should move workspace %p / %s\n", ws, ws->
name);
1049 LOG(
"Creating a new workspace to replace \"%s\" (last on its output).\n", ws->
name);
1052 bool used_assignment =
false;
1055 if (strcmp(assignment->
output, current_output->
name) != 0)
1059 Con *workspace = NULL, *out;
1062 !strcasecmp(child->name, assignment->
name));
1063 if (workspace != NULL)
1067 LOG(
"Creating workspace from assignment %s.\n", assignment->
name);
1069 used_assignment =
true;
1075 if (!used_assignment)
1079 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"init\"}");
1086 if (workspace_was_visible) {
1090 LOG(
"workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->
name);
1097 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1100 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"move\"}");
1101 if (workspace_was_visible) {
1107 cmd_output->needs_tree_render =
true;
1119 LOG(
"splitting in direction %c\n", direction[0]);
1129 cmd_output->needs_tree_render =
true;
1139 if (kill_mode_str == NULL)
1140 kill_mode_str =
"window";
1143 DLOG(
"kill_mode=%s\n", kill_mode_str);
1146 if (strcmp(kill_mode_str,
"window") == 0)
1148 else if (strcmp(kill_mode_str,
"client") == 0)
1151 ELOG(
"BUG: called with kill_mode=%s\n", kill_mode_str);
1166 cmd_output->needs_tree_render =
true;
1176 bool no_startup_id = (nosn != NULL);
1178 DLOG(
"should execute %s, no_startup_id = %d\n", command, no_startup_id);
1193 LOG(
"Cannot change focus while in fullscreen mode.\n");
1198 DLOG(
"direction = *%s*\n", direction);
1200 if (strcmp(direction,
"left") == 0)
1202 else if (strcmp(direction,
"right") == 0)
1204 else if (strcmp(direction,
"up") == 0)
1206 else if (strcmp(direction,
"down") == 0)
1209 ELOG(
"Invalid focus direction (%s)\n", direction);
1214 cmd_output->needs_tree_render =
true;
1227 LOG(
"Cannot change focus while in fullscreen mode.\n");
1232 DLOG(
"window_mode = %s\n", window_mode);
1237 if (strcmp(window_mode,
"mode_toggle") == 0) {
1239 if (current != NULL && current->
type == CT_FLOATING_CON)
1240 window_mode =
"tiling";
1241 else window_mode =
"floating";
1244 if ((strcmp(window_mode,
"floating") == 0 && current->
type != CT_FLOATING_CON) ||
1245 (strcmp(window_mode,
"tiling") == 0 && current->
type == CT_FLOATING_CON))
1253 cmd_output->needs_tree_render =
true;
1263 DLOG(
"level = %s\n", level);
1264 bool success =
false;
1268 if (strcmp(level,
"parent") == 0) {
1273 ELOG(
"'focus parent': Currently in fullscreen, not going up\n");
1280 cmd_output->needs_tree_render = success;
1293 ELOG(
"You have to specify which window/container should be focused.\n");
1294 ELOG(
"Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1300 ystr(
"You have to specify which window/container should be focused");
1317 LOG(
"Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1340 LOG(
"focusing %p / %s\n", current->
con, current->
con->
name);
1346 LOG(
"WARNING: Your criteria for the focus command matches %d containers, "
1347 "while only exactly one container can be focused at a time.\n", count);
1349 cmd_output->needs_tree_render =
true;
1359 if (fullscreen_mode == NULL)
1360 fullscreen_mode =
"output";
1361 DLOG(
"toggling fullscreen, mode = %s\n", fullscreen_mode);
1367 printf(
"matching: %p / %s\n", current->
con, current->
con->
name);
1371 cmd_output->needs_tree_render =
true;
1382 int px = atoi(move_px);
1385 DLOG(
"moving in direction %s, px %s\n", direction, move_px);
1387 DLOG(
"floating move with %d pixels\n", px);
1389 if (strcmp(direction,
"left") == 0) {
1391 }
else if (strcmp(direction,
"right") == 0) {
1393 }
else if (strcmp(direction,
"up") == 0) {
1395 }
else if (strcmp(direction,
"down") == 0) {
1401 (strcmp(direction,
"left") == 0 ?
D_LEFT :
1402 (strcmp(direction,
"up") == 0 ?
D_UP :
1404 cmd_output->needs_tree_render =
true;
1416 if (strcmp(layout_str,
"stacking") == 0)
1417 layout_str =
"stacked";
1421 if (strcmp(layout_str,
"default") == 0)
1423 else if (strcmp(layout_str,
"stacked") == 0)
1425 else if (strcmp(layout_str,
"tabbed") == 0)
1427 else if (strcmp(layout_str,
"splitv") == 0)
1429 else if (strcmp(layout_str,
"splith") == 0)
1432 ELOG(
"Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1436 DLOG(
"changing layout to %s (%d)\n", layout_str, layout);
1448 cmd_output->needs_tree_render =
true;
1460 if (toggle_mode == NULL)
1461 toggle_mode =
"default";
1463 DLOG(
"toggling layout (mode = %s)\n", toggle_mode);
1475 cmd_output->needs_tree_render =
true;
1485 LOG(
"Exiting due to user command.\n");
1486 xcb_disconnect(
conn);
1503 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"reload\"}");
1514 LOG(
"restarting i3\n");
1526 LOG(
"opening new container\n");
1535 y(integer, (
long int)con);
1538 cmd_output->needs_tree_render =
true;
1548 DLOG(
"name = %s\n", name);
1553 Output *current_output = NULL;
1558 assert(current_output != NULL);
1563 LOG(
"No such output found.\n");
1578 cmd_output->needs_tree_render =
true;
1593 ELOG(
"Cannot change position. The window/container is not floating\n");
1598 ystr(
"Cannot change position. The window/container is not floating.");
1603 if (strcmp(method,
"absolute") == 0) {
1607 DLOG(
"moving to absolute position %d %d\n", x, y);
1609 cmd_output->needs_tree_render =
true;
1612 if (strcmp(method,
"position") == 0) {
1615 DLOG(
"moving to position %d %d\n", x, y);
1633 ELOG(
"Cannot change position. The window/container is not floating\n");
1638 ystr(
"Cannot change position. The window/container is not floating.");
1642 if (strcmp(method,
"absolute") == 0) {
1645 DLOG(
"moving to absolute center\n");
1650 cmd_output->needs_tree_render =
true;
1653 if (strcmp(method,
"position") == 0) {
1657 DLOG(
"moving to center\n");
1673 DLOG(
"should move window to scratchpad\n");
1683 cmd_output->needs_tree_render =
true;
1693 DLOG(
"should show scratchpad window\n");
1705 cmd_output->needs_tree_render =
true;
1715 LOG(
"Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1720 !strcasecmp(child->name, old_name));
1730 ystr(
"Old workspace not found");
1735 Con *check_dest = NULL;
1738 !strcasecmp(child->name, new_name));
1740 if (check_dest != NULL) {
1748 ystr(
"New workspace already exists");
1756 char *endptr = NULL;
1757 long parsed_num = strtol(new_name, &endptr, 10);
1758 if (parsed_num == LONG_MIN ||
1759 parsed_num == LONG_MAX ||
1762 workspace->
num = -1;
1763 else workspace->
num = parsed_num;
1764 LOG(
"num = %d\n", workspace->
num);
1774 cmd_output->needs_tree_render =
true;
1777 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"rename\"}");