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); \
47 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
58 if (strcasecmp(output_str,
"left") == 0) {
62 }
else if (strcasecmp(output_str,
"right") == 0) {
66 }
else if (strcasecmp(output_str,
"up") == 0) {
70 }
else if (strcasecmp(output_str,
"down") == 0) {
90 if (strcmp(ws->
name, name) != 0)
93 DLOG(
"This workspace is already focused.\n");
113 if (current == workspace) {
116 DLOG(
"Substituting workspace with back_and_forth, as it is focused.\n");
127 static pid_t migration_pid = -1;
134 static void nagbar_exited(EV_P_ ev_child *watcher,
int revents) {
135 ev_child_stop(EV_A_ watcher);
136 if (!WIFEXITED(watcher->rstatus)) {
137 fprintf(stderr,
"ERROR: i3-nagbar did not exit normally.\n");
141 int exitcode = WEXITSTATUS(watcher->rstatus);
142 printf(
"i3-nagbar process exited with status %d\n", exitcode);
144 fprintf(stderr,
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
153 #if EV_VERSION_MAJOR >= 4
159 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher,
int revent) {
160 if (migration_pid != -1) {
161 LOG(
"Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
162 kill(migration_pid, SIGKILL);
167 void cmd_MIGRATION_start_nagbar(
void) {
168 if (migration_pid != -1) {
169 fprintf(stderr,
"i3-nagbar already running.\n");
172 fprintf(stderr,
"Starting i3-nagbar, command parsing differs from expected output.\n");
173 ELOG(
"Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
174 ELOG(
"i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
175 ELOG(
"FYI: Your i3 version is " I3_VERSION
"\n");
176 migration_pid = fork();
177 if (migration_pid == -1) {
178 warn(
"Could not fork()");
183 if (migration_pid == 0) {
191 "You found a parsing error. Please, please, please, report it!",
202 ev_child *child =
smalloc(
sizeof(ev_child));
209 #if EV_VERSION_MAJOR >= 4
212 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
213 ev_cleanup_init(cleanup, nagbar_cleanup);
236 static owindows_head owindows;
271 DLOG(
"match specification finished, matching...\n");
274 struct owindows_head old = owindows;
283 DLOG(
"checking if con %p / %s matches\n", current->
con, current->
con->
name);
286 DLOG(
"matches container!\n");
291 DLOG(
"match by mark\n");
297 DLOG(
"matches window!\n");
300 DLOG(
"doesnt match\n");
317 DLOG(
"ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
319 if (strcmp(ctype,
"class") == 0) {
324 if (strcmp(ctype,
"instance") == 0) {
329 if (strcmp(ctype,
"window_role") == 0) {
334 if (strcmp(ctype,
"con_id") == 0) {
336 long parsed = strtol(cvalue, &end, 10);
337 if (parsed == LONG_MIN ||
338 parsed == LONG_MAX ||
340 (end && *end !=
'\0')) {
341 ELOG(
"Could not parse con id \"%s\"\n", cvalue);
349 if (strcmp(ctype,
"id") == 0) {
351 long parsed = strtol(cvalue, &end, 10);
352 if (parsed == LONG_MIN ||
353 parsed == LONG_MAX ||
355 (end && *end !=
'\0')) {
356 ELOG(
"Could not parse window id \"%s\"\n", cvalue);
364 if (strcmp(ctype,
"con_mark") == 0) {
369 if (strcmp(ctype,
"title") == 0) {
374 if (strcmp(ctype,
"urgent") == 0) {
375 if (strcasecmp(cvalue,
"latest") == 0 ||
376 strcasecmp(cvalue,
"newest") == 0 ||
377 strcasecmp(cvalue,
"recent") == 0 ||
378 strcasecmp(cvalue,
"last") == 0) {
380 }
else if (strcasecmp(cvalue,
"oldest") == 0 ||
381 strcasecmp(cvalue,
"first") == 0) {
387 ELOG(
"Unknown criterion: %s\n", ctype);
398 DLOG(
"which=%s\n", which);
414 if (strcmp(which,
"next") == 0)
416 else if (strcmp(which,
"prev") == 0)
418 else if (strcmp(which,
"next_on_output") == 0)
420 else if (strcmp(which,
"prev_on_output") == 0)
422 else if (strcmp(which,
"current") == 0)
425 ELOG(
"BUG: called with which=%s\n", which);
435 cmd_output->needs_tree_render =
true;
455 ystr(
"No workspace was previously active.");
467 cmd_output->needs_tree_render =
true;
477 if (strncasecmp(name,
"__i3_", strlen(
"__i3_")) == 0) {
478 LOG(
"You cannot switch to the i3 internal workspaces.\n");
489 ELOG(
"No windows match your criteria, cannot move.\n");
499 LOG(
"should move window to workspace %s\n", name);
512 cmd_output->needs_tree_render =
true;
534 LOG(
"should move window to workspace %s\n", which);
536 Con *output, *workspace = NULL;
539 long parsed_num = strtol(which, &endptr, 10);
540 if (parsed_num == LONG_MIN ||
541 parsed_num == LONG_MAX ||
544 LOG(
"Could not parse initial part of \"%s\" as a number.\n", which);
550 ystr(
"Could not parse number");
557 child->
num == parsed_num);
572 cmd_output->needs_tree_render =
true;
578 LOG(
"floating resize\n");
581 if (strcmp(direction,
"up") == 0) {
583 }
else if (strcmp(direction,
"down") == 0 || strcmp(direction,
"height") == 0) {
585 }
else if (strcmp(direction,
"left") == 0) {
595 if (memcmp(&old_rect, &(floating_con->
rect),
sizeof(
Rect)) == 0)
598 if (strcmp(direction,
"up") == 0) {
599 floating_con->
rect.
y -= px;
600 }
else if (strcmp(direction,
"left") == 0) {
601 floating_con->
rect.
x -= px;
606 LOG(
"tiling resize\n");
609 double percentage = 0;
612 current = current->
parent;
616 (strcmp(direction,
"left") == 0 || strcmp(direction,
"right") == 0 ?
HORIZ :
VERT);
620 current = current->
parent;
626 LOG(
"ins. %d children\n", children);
627 percentage = 1.0 / children;
628 LOG(
"default percentage = %f\n", percentage);
632 if ((orientation ==
HORIZ &&
633 (strcmp(direction,
"up") == 0 || strcmp(direction,
"down") == 0)) ||
634 (orientation ==
VERT &&
635 (strcmp(direction,
"left") == 0 || strcmp(direction,
"right") == 0))) {
636 LOG(
"You cannot resize in that direction. Your focus is in a %s split container currently.\n",
637 (orientation ==
HORIZ ?
"horizontal" :
"vertical"));
642 if (strcmp(direction,
"up") == 0 || strcmp(direction,
"left") == 0) {
643 other =
TAILQ_PREV(current, nodes_head, nodes);
648 LOG(
"No other container in this direction found, trying to look further up in the tree...\n");
649 current = current->
parent;
653 }
while (current->
type != CT_WORKSPACE &&
654 current->
type != CT_FLOATING_CON);
657 LOG(
"No other container in this direction found, trying to look further up in the tree...\n");
663 LOG(
"current->percent before = %f\n", current->
percent);
668 double new_current_percent = current->
percent + ((double)ppt / 100.0);
669 double new_other_percent = other->
percent - ((double)ppt / 100.0);
670 LOG(
"new_current_percent = %f\n", new_current_percent);
671 LOG(
"new_other_percent = %f\n", new_other_percent);
676 current->
percent += ((double)ppt / 100.0);
677 other->
percent -= ((double)ppt / 100.0);
678 LOG(
"current->percent after = %f\n", current->
percent);
679 LOG(
"other->percent after = %f\n", other->
percent);
681 LOG(
"Not resizing, already at minimum size\n");
688 LOG(
"width/height resize\n");
692 current = current->
parent;
696 (strcmp(direction,
"width") == 0 ?
HORIZ :
VERT);
698 while (current->
type != CT_WORKSPACE &&
699 current->
type != CT_FLOATING_CON &&
701 current = current->
parent;
705 LOG(
"ins. %d children\n", children);
706 double percentage = 1.0 / children;
707 LOG(
"default percentage = %f\n", percentage);
711 if ((orientation ==
HORIZ &&
712 strcmp(direction,
"height") == 0) ||
713 (orientation ==
VERT &&
714 strcmp(direction,
"width") == 0)) {
715 LOG(
"You cannot resize in that direction. Your focus is in a %s split container currently.\n",
716 (orientation ==
HORIZ ?
"horizontal" :
"vertical"));
722 LOG(
"This is the only container, cannot resize.\n");
730 LOG(
"child->percent = %f (child %p)\n", child->
percent, child);
735 double new_current_percent = current->
percent + ((double)ppt / 100.0);
736 double subtract_percent = ((double)ppt / 100.0) / (children - 1);
737 LOG(
"new_current_percent = %f\n", new_current_percent);
738 LOG(
"subtract_percent = %f\n", subtract_percent);
742 if (child == current)
745 LOG(
"Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->
percent - subtract_percent);
751 LOG(
"Not resizing, already at minimum size\n");
756 current->
percent += ((double)ppt / 100.0);
757 LOG(
"current->percent after = %f\n", current->
percent);
760 if (child == current)
762 child->
percent -= subtract_percent;
763 LOG(
"child->percent after (%p) = %f\n", child, child->
percent);
775 DLOG(
"resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
777 int px = atoi(resize_px);
778 int ppt = atoi(resize_ppt);
779 if (strcmp(way,
"shrink") == 0) {
792 if (strcmp(direction,
"width") == 0 ||
793 strcmp(direction,
"height") == 0) {
803 cmd_output->needs_tree_render =
true;
813 DLOG(
"border style should be changed to %s with border width %s\n", border_style_str, border_width);
822 int tmp_border_width = -1;
823 tmp_border_width = strtol(border_width, &end, 10);
824 if (end == border_width) {
826 tmp_border_width = -1;
828 if (strcmp(border_style_str,
"toggle") == 0) {
832 tmp_border_width = 2;
833 else if (border_style ==
BS_NONE)
834 tmp_border_width = 0;
836 tmp_border_width = 1;
838 if (strcmp(border_style_str,
"normal") == 0)
840 else if (strcmp(border_style_str,
"pixel") == 0)
842 else if (strcmp(border_style_str,
"1pixel") == 0){
844 tmp_border_width = 1;
845 }
else if (strcmp(border_style_str,
"none") == 0)
848 ELOG(
"BUG: called with border_style=%s\n", border_style_str);
856 cmd_output->needs_tree_render =
true;
866 LOG(
"-------------------------------------------------\n");
867 LOG(
" NOP: %s\n", comment);
868 LOG(
"-------------------------------------------------\n");
876 LOG(
"Appending layout \"%s\"\n", path);
879 cmd_output->needs_tree_render =
true;
891 DLOG(
"which=%s\n", which);
893 if (strcmp(which,
"next") == 0)
895 else if (strcmp(which,
"prev") == 0)
897 else if (strcmp(which,
"next_on_output") == 0)
899 else if (strcmp(which,
"prev_on_output") == 0)
902 ELOG(
"BUG: called with which=%s\n", which);
909 cmd_output->needs_tree_render =
true;
919 Con *output, *workspace = NULL;
922 long parsed_num = strtol(which, &endptr, 10);
923 if (parsed_num == LONG_MIN ||
924 parsed_num == LONG_MAX ||
927 LOG(
"Could not parse initial part of \"%s\" as a number.\n", which);
933 ystr(
"Could not parse number");
941 child->
num == parsed_num);
944 LOG(
"There is no workspace with number %ld, creating a new one.\n", parsed_num);
947 cmd_output->needs_tree_render =
true;
954 cmd_output->needs_tree_render =
true;
966 cmd_output->needs_tree_render =
true;
976 if (strncasecmp(name,
"__i3_", strlen(
"__i3_")) == 0) {
977 LOG(
"You cannot switch to the i3 internal workspaces.\n");
982 DLOG(
"should switch to workspace %s\n", name);
987 cmd_output->needs_tree_render =
true;
997 DLOG(
"Clearing all windows which have that mark first\n");
1001 if (con->
mark && strcmp(con->
mark, mark) == 0)
1005 DLOG(
"marking window with str %s\n", mark);
1015 cmd_output->needs_tree_render =
true;
1025 DLOG(
"mode=%s\n", mode);
1039 DLOG(
"should move window to output %s\n", name);
1044 Output *current_output = NULL;
1051 assert(current_output != NULL);
1054 if (strcasecmp(name,
"up") == 0)
1056 else if (strcasecmp(name,
"down") == 0)
1058 else if (strcasecmp(name,
"left") == 0)
1060 else if (strcasecmp(name,
"right") == 0)
1066 LOG(
"No such output found.\n");
1084 cmd_output->needs_tree_render =
true;
1096 DLOG(
"floating_mode=%s\n", floating_mode);
1102 if (strcmp(floating_mode,
"toggle") == 0) {
1103 DLOG(
"should toggle mode\n");
1106 DLOG(
"should switch mode to %s\n", floating_mode);
1107 if (strcmp(floating_mode,
"enable") == 0) {
1115 cmd_output->needs_tree_render =
true;
1125 DLOG(
"should move workspace to output %s\n", name);
1133 if (!current_output) {
1134 ELOG(
"Cannot get current output. This is a bug in i3.\n");
1140 ELOG(
"Could not get output from string \"%s\"\n", name);
1146 LOG(
"got output %p with content %p\n", output, content);
1148 Con *previously_visible_ws =
TAILQ_FIRST(&(content->nodes_head));
1149 LOG(
"Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->
name);
1152 LOG(
"should move workspace %p / %s\n", ws, ws->
name);
1156 LOG(
"Creating a new workspace to replace \"%s\" (last on its output).\n", ws->
name);
1159 bool used_assignment =
false;
1162 if (strcmp(assignment->
output, current_output->
name) != 0)
1166 Con *workspace = NULL, *out;
1169 !strcasecmp(child->name, assignment->
name));
1170 if (workspace != NULL)
1174 LOG(
"Creating workspace from assignment %s.\n", assignment->
name);
1176 used_assignment =
true;
1182 if (!used_assignment)
1186 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"init\"}");
1188 DLOG(
"Detaching\n");
1193 if (workspace_was_visible) {
1197 LOG(
"workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->
name);
1204 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1207 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"move\"}");
1208 if (workspace_was_visible) {
1219 if (ws != previously_visible_ws)
1225 CALL(previously_visible_ws, on_remove_child);
1230 cmd_output->needs_tree_render =
true;
1242 LOG(
"splitting in direction %c\n", direction[0]);
1252 cmd_output->needs_tree_render =
true;
1262 if (kill_mode_str == NULL)
1263 kill_mode_str =
"window";
1266 DLOG(
"kill_mode=%s\n", kill_mode_str);
1269 if (strcmp(kill_mode_str,
"window") == 0)
1271 else if (strcmp(kill_mode_str,
"client") == 0)
1274 ELOG(
"BUG: called with kill_mode=%s\n", kill_mode_str);
1289 cmd_output->needs_tree_render =
true;
1299 bool no_startup_id = (nosn != NULL);
1301 DLOG(
"should execute %s, no_startup_id = %d\n", command, no_startup_id);
1313 DLOG(
"direction = *%s*\n", direction);
1315 if (strcmp(direction,
"left") == 0)
1317 else if (strcmp(direction,
"right") == 0)
1319 else if (strcmp(direction,
"up") == 0)
1321 else if (strcmp(direction,
"down") == 0)
1324 ELOG(
"Invalid focus direction (%s)\n", direction);
1329 cmd_output->needs_tree_render =
true;
1339 DLOG(
"window_mode = %s\n", window_mode);
1344 if (strcmp(window_mode,
"mode_toggle") == 0) {
1346 if (current != NULL && current->
type == CT_FLOATING_CON)
1347 window_mode =
"tiling";
1348 else window_mode =
"floating";
1351 if ((strcmp(window_mode,
"floating") == 0 && current->
type != CT_FLOATING_CON) ||
1352 (strcmp(window_mode,
"tiling") == 0 && current->
type == CT_FLOATING_CON))
1360 cmd_output->needs_tree_render =
true;
1370 DLOG(
"level = %s\n", level);
1371 bool success =
false;
1375 if (strcmp(level,
"parent") == 0) {
1380 ELOG(
"'focus parent': Currently in fullscreen, not going up\n");
1387 cmd_output->needs_tree_render = success;
1400 ELOG(
"You have to specify which window/container should be focused.\n");
1401 ELOG(
"Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1407 ystr(
"You have to specify which window/container should be focused");
1424 LOG(
"Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1447 LOG(
"focusing %p / %s\n", current->
con, current->
con->
name);
1453 LOG(
"WARNING: Your criteria for the focus command matches %d containers, "
1454 "while only exactly one container can be focused at a time.\n", count);
1456 cmd_output->needs_tree_render =
true;
1466 if (fullscreen_mode == NULL)
1467 fullscreen_mode =
"output";
1468 DLOG(
"toggling fullscreen, mode = %s\n", fullscreen_mode);
1474 printf(
"matching: %p / %s\n", current->
con, current->
con->
name);
1478 cmd_output->needs_tree_render =
true;
1489 int px = atoi(move_px);
1492 DLOG(
"moving in direction %s, px %s\n", direction, move_px);
1494 DLOG(
"floating move with %d pixels\n", px);
1496 if (strcmp(direction,
"left") == 0) {
1498 }
else if (strcmp(direction,
"right") == 0) {
1500 }
else if (strcmp(direction,
"up") == 0) {
1502 }
else if (strcmp(direction,
"down") == 0) {
1508 (strcmp(direction,
"left") == 0 ?
D_LEFT :
1509 (strcmp(direction,
"up") == 0 ?
D_UP :
1511 cmd_output->needs_tree_render =
true;
1523 if (strcmp(layout_str,
"stacking") == 0)
1524 layout_str =
"stacked";
1528 if (strcmp(layout_str,
"default") == 0)
1530 else if (strcmp(layout_str,
"stacked") == 0)
1532 else if (strcmp(layout_str,
"tabbed") == 0)
1534 else if (strcmp(layout_str,
"splitv") == 0)
1536 else if (strcmp(layout_str,
"splith") == 0)
1539 ELOG(
"Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1543 DLOG(
"changing layout to %s (%d)\n", layout_str, layout);
1555 cmd_output->needs_tree_render =
true;
1567 if (toggle_mode == NULL)
1568 toggle_mode =
"default";
1570 DLOG(
"toggling layout (mode = %s)\n", toggle_mode);
1582 cmd_output->needs_tree_render =
true;
1592 LOG(
"Exiting due to user command.\n");
1593 xcb_disconnect(
conn);
1610 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"reload\"}");
1621 LOG(
"restarting i3\n");
1633 LOG(
"opening new container\n");
1642 y(integer, (
long int)con);
1645 cmd_output->needs_tree_render =
true;
1655 DLOG(
"name = %s\n", name);
1660 Output *current_output = NULL;
1665 assert(current_output != NULL);
1670 LOG(
"No such output found.\n");
1685 cmd_output->needs_tree_render =
true;
1700 ELOG(
"Cannot change position. The window/container is not floating\n");
1705 ystr(
"Cannot change position. The window/container is not floating.");
1710 if (strcmp(method,
"absolute") == 0) {
1714 DLOG(
"moving to absolute position %d %d\n", x, y);
1716 cmd_output->needs_tree_render =
true;
1719 if (strcmp(method,
"position") == 0) {
1722 DLOG(
"moving to position %d %d\n", x, y);
1740 ELOG(
"Cannot change position. The window/container is not floating\n");
1745 ystr(
"Cannot change position. The window/container is not floating.");
1749 if (strcmp(method,
"absolute") == 0) {
1752 DLOG(
"moving to absolute center\n");
1757 cmd_output->needs_tree_render =
true;
1760 if (strcmp(method,
"position") == 0) {
1764 DLOG(
"moving to center\n");
1780 DLOG(
"should move window to scratchpad\n");
1790 cmd_output->needs_tree_render =
true;
1800 DLOG(
"should show scratchpad window\n");
1812 cmd_output->needs_tree_render =
true;
1823 LOG(
"Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1825 LOG(
"Renaming current workspace to \"%s\"\n", new_name);
1832 !strcasecmp(child->name, old_name));
1845 ystr(
"Old workspace not found");
1850 Con *check_dest = NULL;
1853 !strcasecmp(child->name, new_name));
1855 if (check_dest != NULL) {
1863 ystr(
"New workspace already exists");
1871 char *endptr = NULL;
1872 long parsed_num = strtol(new_name, &endptr, 10);
1873 if (parsed_num == LONG_MIN ||
1874 parsed_num == LONG_MAX ||
1877 workspace->
num = -1;
1878 else workspace->
num = parsed_num;
1879 LOG(
"num = %d\n", workspace->
num);
1889 cmd_output->needs_tree_render =
true;
1892 ipc_send_event(
"workspace", I3_IPC_EVENT_WORKSPACE,
"{\"change\":\"rename\"}");