-#undef I3__FILE__
-#define I3__FILE__ "commands.c"
/*
* vim:ts=4:sw=4:expandtab
*
* commands.c: all command functions (see commands_parser.c)
*
*/
+#include "all.h"
+
+#include <stdint.h>
#include <float.h>
#include <stdarg.h>
#include <sanitizer/lsan_interface.h>
#endif
-#include "all.h"
#include "shmlog.h"
// Macros to make the YAJL API a bit easier to use.
return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
-/*
- * Returns the output containing the given container.
- */
-static Output *get_output_of_con(Con *con) {
- Con *output_con = con_get_output(con);
- Output *output = get_output_by_name(output_con->name);
- assert(output != NULL);
-
- return output;
-}
-
/*
* Checks whether we switched to a new workspace and returns false in that case,
* signaling that further workspace switching should be done by the calling function
*/
typedef struct owindow {
Con *con;
- TAILQ_ENTRY(owindow) owindows;
+
+ TAILQ_ENTRY(owindow)
+ owindows;
} owindow;
typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
}
/*
- * Implementation of 'resize set <px> [px] <px> [px]'.
+ * Implementation of 'resize set <width> [px | ppt] <height> [px | ppt]'.
*
*/
-void cmd_resize_set(I3_CMD, long cwidth, long cheight) {
- DLOG("resizing to %ldx%ld px\n", cwidth, cheight);
+void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, const char *mode_height) {
+ DLOG("resizing to %ld %s x %ld %s\n", cwidth, mode_width, cheight, mode_height);
if (cwidth <= 0 || cheight <= 0) {
- ELOG("Resize failed: dimensions cannot be negative (was %ldx%ld)\n", cwidth, cheight);
+ ELOG("Resize failed: dimensions cannot be negative (was %ld %s x %ld %s)\n", cwidth, mode_width, cheight, mode_height);
return;
}
TAILQ_FOREACH(current, &owindows, owindows) {
Con *floating_con;
if ((floating_con = con_inside_floating(current->con))) {
+ Con *output = con_get_output(floating_con);
+ if (mode_width && strcmp(mode_width, "ppt") == 0) {
+ cwidth = output->rect.width * ((double)cwidth / 100.0);
+ }
+ if (mode_height && strcmp(mode_height, "ppt") == 0) {
+ cheight = output->rect.height * ((double)cheight / 100.0);
+ }
floating_resize(floating_con, cwidth, cheight);
} else {
ELOG("Resize failed: %p not a floating container\n", current->con);
/* Make sure we allow paths like '~/.i3/layout.json' */
path = resolve_tilde(path);
- json_content_t content = json_determine_content(path);
+ char *buf = NULL;
+ ssize_t len;
+ if ((len = slurp(path, &buf)) < 0) {
+ /* slurp already logged an error. */
+ goto out;
+ }
+
+ if (!json_validate(buf, len)) {
+ ELOG("Could not parse \"%s\" as JSON, not loading.\n", path);
+ yerror("Could not parse \"%s\" as JSON.", path);
+ goto out;
+ }
+
+ json_content_t content = json_determine_content(buf, len);
LOG("JSON content = %d\n", content);
if (content == JSON_CONTENT_UNKNOWN) {
ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
yerror("Could not determine the contents of \"%s\".", path);
- free(path);
- return;
+ goto out;
}
Con *parent = focused;
}
DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
char *errormsg = NULL;
- tree_append_json(parent, path, &errormsg);
+ tree_append_json(parent, buf, len, &errormsg);
if (errormsg != NULL) {
yerror(errormsg);
free(errormsg);
if (content == JSON_CONTENT_WORKSPACE)
ipc_send_workspace_event("restored", parent, NULL);
- free(path);
cmd_output->needs_tree_render = true;
+out:
+ free(path);
+ free(buf);
}
/*
cmd_output->needs_tree_render = true;
return;
}
- if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name))
+ if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name)) {
+ ysuccess(true);
return;
+ }
workspace_show(workspace);
cmd_output->needs_tree_render = true;
}
DLOG("should switch to workspace %s\n", name);
- if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name))
+ if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name)) {
+ ysuccess(true);
return;
+ }
workspace_show_by_name(name);
cmd_output->needs_tree_render = true;
TAILQ_FOREACH(current, &owindows, owindows) {
DLOG("matching: %p / %s\n", current->con, current->con->name);
- Output *current_output = get_output_of_con(current->con);
- assert(current_output != NULL);
-
- Output *output = get_output_from_string(current_output, name);
- if (output == NULL) {
- ELOG("Could not find output \"%s\", skipping.\n", name);
- had_error = true;
- continue;
- }
-
- Con *ws = NULL;
- GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
- if (ws == NULL) {
- ELOG("Could not find a visible workspace on output %p.\n", output);
- had_error = true;
- continue;
- }
-
- con_move_to_workspace(current->con, ws, true, false, false);
+ had_error |= !con_move_to_output_name(current->con, name, true);
}
cmd_output->needs_tree_render = true;
owindow *current;
TAILQ_FOREACH(current, &owindows, owindows) {
Con *ws = con_get_workspace(current->con);
+ if (con_is_internal(ws)) {
+ continue;
+ }
+
bool success = workspace_move_to_output(ws, name);
if (!success) {
ELOG("Failed to move workspace to output.\n");
void cmd_layout(I3_CMD, const char *layout_str) {
HANDLE_EMPTY_MATCH;
- if (strcmp(layout_str, "stacking") == 0)
- layout_str = "stacked";
layout_t layout;
- /* default is a special case which will be handled in con_set_layout(). */
- if (strcmp(layout_str, "default") == 0)
- layout = L_DEFAULT;
- else if (strcmp(layout_str, "stacked") == 0)
- layout = L_STACKED;
- else if (strcmp(layout_str, "tabbed") == 0)
- layout = L_TABBED;
- else if (strcmp(layout_str, "splitv") == 0)
- layout = L_SPLITV;
- else if (strcmp(layout_str, "splith") == 0)
- layout = L_SPLITH;
- else {
+ if (!layout_from_name(layout_str, &layout)) {
ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
return;
}
#ifdef I3_ASAN_ENABLED
__lsan_do_leak_check();
#endif
- ipc_shutdown();
+ ipc_shutdown(SHUTDOWN_REASON_EXIT);
unlink(config.ipc_socket_path);
xcb_disconnect(conn);
exit(0);
*/
void cmd_restart(I3_CMD) {
LOG("restarting i3\n");
- ipc_shutdown();
+ ipc_shutdown(SHUTDOWN_REASON_RESTART);
unlink(config.ipc_socket_path);
/* We need to call this manually since atexit handlers don’t get called
* when exec()ing */
ystr("success");
y(bool, true);
ystr("id");
- y(integer, (long int)con);
+ y(integer, (uintptr_t)con);
y(map_close);
cmd_output->needs_tree_render = true;
Output *output;
TAILQ_FOREACH(current, &owindows, owindows)
- current_output = get_output_of_con(current->con);
+ current_output = get_output_for_con(current->con);
assert(current_output != NULL);
output = get_output_from_string(current_output, name);
ysuccess(true);
}
+/*
+ * Implementation of 'swap [container] [with] id|con_id|mark <arg>'.
+ *
+ */
+void cmd_swap(I3_CMD, const char *mode, const char *arg) {
+ HANDLE_EMPTY_MATCH;
+
+ owindow *match = TAILQ_FIRST(&owindows);
+ if (match == NULL) {
+ DLOG("No match found for swapping.\n");
+ return;
+ }
+
+ Con *con;
+ if (strcmp(mode, "id") == 0) {
+ long target;
+ if (!parse_long(arg, &target, 0)) {
+ yerror("Failed to parse %s into a window id.\n", arg);
+ return;
+ }
+
+ con = con_by_window_id(target);
+ } else if (strcmp(mode, "con_id") == 0) {
+ long target;
+ if (!parse_long(arg, &target, 0)) {
+ yerror("Failed to parse %s into a container id.\n", arg);
+ return;
+ }
+
+ con = con_by_con_id(target);
+ } else if (strcmp(mode, "mark") == 0) {
+ con = con_by_mark(arg);
+ } else {
+ yerror("Unhandled swap mode \"%s\". This is a bug.\n", mode);
+ return;
+ }
+
+ if (con == NULL) {
+ yerror("Could not find container for %s = %s\n", mode, arg);
+ return;
+ }
+
+ if (match != TAILQ_LAST(&owindows, owindows_head)) {
+ DLOG("More than one container matched the swap command, only using the first one.");
+ }
+
+ if (match->con == NULL) {
+ DLOG("Match %p has no container.\n", match);
+ ysuccess(false);
+ return;
+ }
+
+ DLOG("Swapping %p with %p.\n", match->con, con);
+ bool result = con_swap(match->con, con);
+
+ cmd_output->needs_tree_render = true;
+ ysuccess(result);
+}
+
/*
* Implementation of 'title_format <format>'
*