aboutsummaryrefslogtreecommitdiff
path: root/sway/commands
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/bind.c48
-rw-r--r--sway/commands/swap.c80
2 files changed, 110 insertions, 18 deletions
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index cbabb07b..c6b3368a 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -83,20 +83,26 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
binding->keys = create_list();
binding->modifiers = 0;
binding->release = false;
+ binding->locked = false;
binding->bindcode = false;
- // Handle --release
- if (strcmp("--release", argv[0]) == 0) {
- if (argc >= 3) {
+ // Handle --release and --locked
+ while (argc > 0) {
+ if (strcmp("--release", argv[0]) == 0) {
binding->release = true;
- argv++;
- argc--;
+ } else if (strcmp("--locked", argv[0]) == 0) {
+ binding->locked = true;
} else {
- free_sway_binding(binding);
- return cmd_results_new(CMD_FAILURE, "bindsym",
- "Invalid bindsym command "
- "(expected more than 2 arguments, got %d)", argc);
+ break;
}
+ argv++;
+ argc--;
+ }
+ if (argc < 2) {
+ free_sway_binding(binding);
+ return cmd_results_new(CMD_FAILURE, "bindsym",
+ "Invalid bindsym command "
+ "(expected at least 2 non-option arguments, got %d)", argc);
}
binding->command = join_args(argv + 1, argc - 1);
@@ -176,20 +182,26 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
binding->keys = create_list();
binding->modifiers = 0;
binding->release = false;
+ binding->locked = false;
binding->bindcode = true;
- // Handle --release
- if (strcmp("--release", argv[0]) == 0) {
- if (argc >= 3) {
+ // Handle --release and --locked
+ while (argc > 0) {
+ if (strcmp("--release", argv[0]) == 0) {
binding->release = true;
- argv++;
- argc--;
+ } else if (strcmp("--locked", argv[0]) == 0) {
+ binding->locked = true;
} else {
- free_sway_binding(binding);
- return cmd_results_new(CMD_FAILURE, "bindcode",
- "Invalid bindcode command "
- "(expected more than 2 arguments, got %d)", argc);
+ break;
}
+ argv++;
+ argc--;
+ }
+ if (argc < 2) {
+ free_sway_binding(binding);
+ return cmd_results_new(CMD_FAILURE, "bindcode",
+ "Invalid bindcode command "
+ "(expected at least 2 non-option arguments, got %d)", argc);
}
binding->command = join_args(argv + 1, argc - 1);
diff --git a/sway/commands/swap.c b/sway/commands/swap.c
new file mode 100644
index 00000000..e925ad33
--- /dev/null
+++ b/sway/commands/swap.c
@@ -0,0 +1,80 @@
+#include <strings.h>
+#include <wlr/util/log.h>
+#include "sway/commands.h"
+#include "sway/tree/layout.h"
+#include "sway/tree/view.h"
+#include "stringop.h"
+
+static const char* EXPECTED_SYNTAX =
+ "Expected 'swap container with id|con_id|mark <arg>'";
+
+static bool test_con_id(struct sway_container *container, void *con_id) {
+ return container->id == (size_t)con_id;
+}
+
+static bool test_id(struct sway_container *container, void *id) {
+ xcb_window_t *wid = id;
+ return (container->type == C_VIEW
+ && container->sway_view->type == SWAY_VIEW_XWAYLAND
+ && container->sway_view->wlr_xwayland_surface->window_id == *wid);
+}
+
+static bool test_mark(struct sway_container *container, void *mark) {
+ if (container->type == C_VIEW && container->sway_view->marks->length) {
+ return !list_seq_find(container->sway_view->marks,
+ (int (*)(const void *, const void *))strcmp, mark);
+ }
+ return false;
+}
+
+struct cmd_results *cmd_swap(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "swap", EXPECTED_AT_LEAST, 4))) {
+ return error;
+ }
+
+ if (strcasecmp(argv[0], "container") || strcasecmp(argv[1], "with")) {
+ return cmd_results_new(CMD_INVALID, "swap", EXPECTED_SYNTAX);
+ }
+
+ struct sway_container *current = config->handler_context.current_container;
+ struct sway_container *other;
+
+ char *value = join_args(argv + 3, argc - 3);
+ if (strcasecmp(argv[2], "id") == 0) {
+ xcb_window_t id = strtol(value, NULL, 0);
+ other = container_find(&root_container, test_id, (void *)&id);
+ } else if (strcasecmp(argv[2], "con_id") == 0) {
+ size_t con_id = atoi(value);
+ other = container_find(&root_container, test_con_id, (void *)con_id);
+ } else if (strcasecmp(argv[2], "mark") == 0) {
+ other = container_find(&root_container, test_mark, (void *)value);
+ } else {
+ free(value);
+ return cmd_results_new(CMD_INVALID, "swap", EXPECTED_SYNTAX);
+ }
+
+ if (!other) {
+ error = cmd_results_new(CMD_FAILURE, "swap",
+ "Failed to find %s '%s'", argv[2], value);
+ } else if (current->type < C_CONTAINER || other->type < C_CONTAINER) {
+ error = cmd_results_new(CMD_FAILURE, "swap",
+ "Can only swap with containers and views");
+ } else if (container_has_anscestor(current, other)
+ || container_has_anscestor(other, current)) {
+ error = cmd_results_new(CMD_FAILURE, "swap",
+ "Cannot swap ancestor and descendant");
+ } else if (current->layout == L_FLOATING || other->layout == L_FLOATING) {
+ error = cmd_results_new(CMD_FAILURE, "swap",
+ "Swapping with floating containers is not supported");
+ }
+
+ free(value);
+
+ if (error) {
+ return error;
+ }
+
+ container_swap(current, other);
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}