summaryrefslogtreecommitdiffstats
path: root/sway/commands/move.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands/move.c')
-rw-r--r--sway/commands/move.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/sway/commands/move.c b/sway/commands/move.c
new file mode 100644
index 00000000..68f05b56
--- /dev/null
+++ b/sway/commands/move.c
@@ -0,0 +1,159 @@
1#include <string.h>
2#include <wlc/wlc.h>
3#include "commands.h"
4#include "container.h"
5#include "layout.h"
6#include "list.h"
7#include "output.h"
8#include "workspace.h"
9
10struct cmd_results *cmd_move(int argc, char **argv) {
11 struct cmd_results *error = NULL;
12 if (config->reading) return cmd_results_new(CMD_FAILURE, "move", "Can't be used in config file.");
13 if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) {
14 return error;
15 }
16 const char* expected_syntax = "Expected 'move <left|right|up|down>' or "
17 "'move <container|window> to workspace <name>' or "
18 "'move <container|window|workspace> to output <name|direction>' or "
19 "'move position mouse'";
20 swayc_t *view = get_focused_container(&root_container);
21
22 if (strcasecmp(argv[0], "left") == 0) {
23 move_container(view, MOVE_LEFT);
24 } else if (strcasecmp(argv[0], "right") == 0) {
25 move_container(view, MOVE_RIGHT);
26 } else if (strcasecmp(argv[0], "up") == 0) {
27 move_container(view, MOVE_UP);
28 } else if (strcasecmp(argv[0], "down") == 0) {
29 move_container(view, MOVE_DOWN);
30 } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) {
31 // "move container ...
32 if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) {
33 return error;
34 } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "workspace") == 0) {
35 // move container to workspace x
36 if (view->type == C_WORKSPACE) {
37 view = new_container(view, view->layout);
38 } if (view->type != C_CONTAINER && view->type != C_VIEW) {
39 return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
40 }
41
42 const char *ws_name = argv[3];
43 swayc_t *ws;
44 if (argc == 5 && strcasecmp(ws_name, "number") == 0) {
45 // move "container to workspace number x"
46 ws_name = argv[4];
47 ws = workspace_by_number(ws_name);
48 } else {
49 ws = workspace_by_name(ws_name);
50 }
51
52 if (ws == NULL) {
53 ws = workspace_create(ws_name);
54 }
55 move_container_to(view, get_focused_container(ws));
56 } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "output") == 0) {
57 // move container to output x
58 swayc_t *output = NULL;
59 struct wlc_point abs_pos;
60 get_absolute_center_position(view, &abs_pos);
61 if (view->type == C_WORKSPACE) {
62 view = new_container(view, view->layout);
63 } else if (view->type != C_CONTAINER && view->type != C_VIEW) {
64 return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
65 } else if (!(output = output_by_name(argv[3], &abs_pos))) {
66 return cmd_results_new(CMD_FAILURE, "move",
67 "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y);
68 }
69
70 swayc_t *container = get_focused_container(output);
71 if (container->is_floating) {
72 move_container_to(view, container->parent);
73 } else {
74 move_container_to(view, container);
75 }
76 } else {
77 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
78 }
79 } else if (strcasecmp(argv[0], "workspace") == 0) {
80 // move workspace (to output x)
81 swayc_t *output = NULL;
82 struct wlc_point abs_pos;
83 get_absolute_center_position(view, &abs_pos);
84 if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 4))) {
85 return error;
86 } else if (strcasecmp(argv[1], "to") != 0 || strcasecmp(argv[2], "output") != 0) {
87 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
88 } else if (!(output = output_by_name(argv[3], &abs_pos))) {
89 return cmd_results_new(CMD_FAILURE, "move workspace",
90 "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y);
91 }
92 if (view->type == C_WORKSPACE) {
93 // This probably means we're moving an empty workspace, but
94 // that's fine.
95 move_workspace_to(view, output);
96 } else {
97 swayc_t *workspace = swayc_parent_by_type(view, C_WORKSPACE);
98 move_workspace_to(workspace, output);
99 }
100 } else if (strcasecmp(argv[0], "scratchpad") == 0) {
101 // move scratchpad ...
102 if (view->type != C_CONTAINER && view->type != C_VIEW) {
103 return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views.");
104 }
105 swayc_t *view = get_focused_container(&root_container);
106 int i;
107 for (i = 0; i < scratchpad->length; i++) {
108 if (scratchpad->items[i] == view) {
109 hide_view_in_scratchpad(view);
110 sp_view = NULL;
111 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
112 }
113 }
114 list_add(scratchpad, view);
115 if (!view->is_floating) {
116 destroy_container(remove_child(view));
117 } else {
118 remove_child(view);
119 }
120 wlc_view_set_mask(view->handle, 0);
121 arrange_windows(swayc_active_workspace(), -1, -1);
122 swayc_t *focused = container_under_pointer();
123 if (focused == NULL) {
124 focused = swayc_active_workspace();
125 }
126 set_focused_container(focused);
127 } else if (strcasecmp(argv[0], "position") == 0) {
128 if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 2))) {
129 return error;
130 }
131 if (strcasecmp(argv[1], "mouse")) {
132 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
133 }
134
135 if (view->is_floating) {
136 swayc_t *output = swayc_parent_by_type(view, C_OUTPUT);
137 struct wlc_geometry g;
138 wlc_view_get_visible_geometry(view->handle, &g);
139 const struct wlc_size *size = wlc_output_get_resolution(output->handle);
140
141 struct wlc_point origin;
142 wlc_pointer_get_position(&origin);
143
144 int32_t x = origin.x - g.size.w / 2;
145 int32_t y = origin.y - g.size.h / 2;
146
147 uint32_t w = size->w - g.size.w;
148 uint32_t h = size->h - g.size.h;
149
150 view->x = g.origin.x = MIN((int32_t)w, MAX(x, 0));
151 view->y = g.origin.y = MIN((int32_t)h, MAX(y, 0));
152
153 wlc_view_set_geometry(view->handle, 0, &g);
154 }
155 } else {
156 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
157 }
158 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
159}