aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/input/cursor.h6
-rw-r--r--include/sway/input/seat.h83
-rw-r--r--include/sway/output.h8
-rw-r--r--include/sway/tree/container.h2
-rw-r--r--sway/desktop/output.c2
-rw-r--r--sway/desktop/render.c27
-rw-r--r--sway/desktop/transaction.c2
-rw-r--r--sway/desktop/xdg_shell.c4
-rw-r--r--sway/desktop/xdg_shell_v6.c4
-rw-r--r--sway/desktop/xwayland.c4
-rw-r--r--sway/input/cursor.c396
-rw-r--r--sway/input/seat.c236
-rw-r--r--sway/input/seatop_down.c77
-rw-r--r--sway/input/seatop_move_floating.c65
-rw-r--r--sway/input/seatop_move_tiling.c335
-rw-r--r--sway/input/seatop_resize_floating.c199
-rw-r--r--sway/input/seatop_resize_tiling.c92
-rw-r--r--sway/meson.build5
-rw-r--r--sway/tree/container.c23
19 files changed, 924 insertions, 646 deletions
diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h
index 4636bf6b..9f699dcd 100644
--- a/include/sway/input/cursor.h
+++ b/include/sway/input/cursor.h
@@ -50,6 +50,12 @@ struct sway_cursor {
50 size_t pressed_button_count; 50 size_t pressed_button_count;
51}; 51};
52 52
53struct sway_node;
54
55struct sway_node *node_at_coords(
56 struct sway_seat *seat, double lx, double ly,
57 struct wlr_surface **surface, double *sx, double *sy);
58
53void sway_cursor_destroy(struct sway_cursor *cursor); 59void sway_cursor_destroy(struct sway_cursor *cursor);
54struct sway_cursor *sway_cursor_create(struct sway_seat *seat); 60struct sway_cursor *sway_cursor_create(struct sway_seat *seat);
55 61
diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h
index a3c20346..ce8abde9 100644
--- a/include/sway/input/seat.h
+++ b/include/sway/input/seat.h
@@ -6,6 +6,17 @@
6#include <wlr/util/edges.h> 6#include <wlr/util/edges.h>
7#include "sway/input/input-manager.h" 7#include "sway/input/input-manager.h"
8 8
9struct sway_seat;
10
11struct sway_seatop_impl {
12 void (*motion)(struct sway_seat *seat, uint32_t time_msec);
13 void (*finish)(struct sway_seat *seat);
14 void (*abort)(struct sway_seat *seat);
15 void (*unref)(struct sway_seat *seat, struct sway_container *con);
16 void (*render)(struct sway_seat *seat, struct sway_output *output,
17 pixman_region32_t *damage);
18};
19
9struct sway_seat_device { 20struct sway_seat_device {
10 struct sway_seat *sway_seat; 21 struct sway_seat *sway_seat;
11 struct sway_input_device *input_device; 22 struct sway_input_device *input_device;
@@ -35,16 +46,6 @@ struct sway_drag_icon {
35 struct wl_listener destroy; 46 struct wl_listener destroy;
36}; 47};
37 48
38enum sway_seat_operation {
39 OP_NONE,
40 OP_DOWN,
41 OP_MOVE_FLOATING,
42 OP_MOVE_TILING_THRESHOLD,
43 OP_MOVE_TILING,
44 OP_RESIZE_FLOATING,
45 OP_RESIZE_TILING,
46};
47
48struct sway_seat { 49struct sway_seat {
49 struct wlr_seat *wlr_seat; 50 struct wlr_seat *wlr_seat;
50 struct sway_cursor *cursor; 51 struct sway_cursor *cursor;
@@ -64,19 +65,10 @@ struct sway_seat {
64 int32_t touch_id; 65 int32_t touch_id;
65 double touch_x, touch_y; 66 double touch_x, touch_y;
66 67
67 // Operations (drag and resize) 68 // Seat operations (drag and resize)
68 enum sway_seat_operation operation; 69 const struct sway_seatop_impl *seatop_impl;
69 struct sway_container *op_container; 70 void *seatop_data;
70 struct sway_node *op_target_node; // target for tiling move 71 uint32_t seatop_button;
71 enum wlr_edges op_target_edge;
72 struct wlr_box op_drop_box;
73 enum wlr_edges op_resize_edge;
74 uint32_t op_button;
75 bool op_resize_preserve_ratio;
76 double op_ref_lx, op_ref_ly; // cursor's x/y at start of op
77 double op_ref_width, op_ref_height; // container's size at start of op
78 double op_ref_con_lx, op_ref_con_ly; // container's x/y at start of op
79 bool op_moved; // if the mouse moved during a down op
80 72
81 uint32_t last_button; 73 uint32_t last_button;
82 uint32_t last_button_serial; 74 uint32_t last_button_serial;
@@ -181,32 +173,59 @@ bool seat_is_input_allowed(struct sway_seat *seat, struct wlr_surface *surface);
181 173
182void drag_icon_update_position(struct sway_drag_icon *icon); 174void drag_icon_update_position(struct sway_drag_icon *icon);
183 175
184void seat_begin_down(struct sway_seat *seat, struct sway_container *con, 176void seatop_begin_down(struct sway_seat *seat,
185 uint32_t button, double sx, double sy); 177 struct sway_container *con, uint32_t button, int sx, int sy);
186 178
187void seat_begin_move_floating(struct sway_seat *seat, 179void seatop_begin_move_floating(struct sway_seat *seat,
188 struct sway_container *con, uint32_t button); 180 struct sway_container *con, uint32_t button);
189 181
190void seat_begin_move_tiling_threshold(struct sway_seat *seat, 182void seatop_begin_move_tiling_threshold(struct sway_seat *seat,
191 struct sway_container *con, uint32_t button); 183 struct sway_container *con, uint32_t button);
192 184
193void seat_begin_move_tiling(struct sway_seat *seat, 185void seatop_begin_move_tiling(struct sway_seat *seat,
194 struct sway_container *con, uint32_t button); 186 struct sway_container *con, uint32_t button);
195 187
196void seat_begin_resize_floating(struct sway_seat *seat, 188void seatop_begin_resize_floating(struct sway_seat *seat,
197 struct sway_container *con, uint32_t button, enum wlr_edges edge); 189 struct sway_container *con, uint32_t button, enum wlr_edges edge);
198 190
199void seat_begin_resize_tiling(struct sway_seat *seat, 191void seatop_begin_resize_tiling(struct sway_seat *seat,
200 struct sway_container *con, uint32_t button, enum wlr_edges edge); 192 struct sway_container *con, uint32_t button, enum wlr_edges edge);
201 193
202struct sway_container *seat_get_focus_inactive_floating(struct sway_seat *seat, 194struct sway_container *seat_get_focus_inactive_floating(struct sway_seat *seat,
203 struct sway_workspace *workspace); 195 struct sway_workspace *workspace);
204 196
205void seat_end_mouse_operation(struct sway_seat *seat);
206
207void seat_pointer_notify_button(struct sway_seat *seat, uint32_t time_msec, 197void seat_pointer_notify_button(struct sway_seat *seat, uint32_t time_msec,
208 uint32_t button, enum wlr_button_state state); 198 uint32_t button, enum wlr_button_state state);
209 199
210void seat_consider_warp_to_focus(struct sway_seat *seat); 200void seat_consider_warp_to_focus(struct sway_seat *seat);
211 201
202bool seat_doing_seatop(struct sway_seat *seat);
203
204void seatop_motion(struct sway_seat *seat, uint32_t time_msec);
205
206/**
207 * End a seatop and apply the affects.
208 */
209void seatop_finish(struct sway_seat *seat);
210
211/**
212 * End a seatop without applying the affects.
213 */
214void seatop_abort(struct sway_seat *seat);
215
216/**
217 * Instructs the seatop implementation to drop any references to the given
218 * container (eg. because the container is destroying).
219 * The seatop may choose to abort itself in response to this.
220 */
221void seatop_unref(struct sway_seat *seat, struct sway_container *con);
222
223/**
224 * Instructs a seatop to render anything that it needs to render
225 * (eg. dropzone for move-tiling)
226 */
227void seatop_render(struct sway_seat *seat, struct sway_output *output,
228 pixman_region32_t *damage);
229
230
212#endif 231#endif
diff --git a/include/sway/output.h b/include/sway/output.h
index f7c5ceba..bdf9614d 100644
--- a/include/sway/output.h
+++ b/include/sway/output.h
@@ -146,4 +146,12 @@ enum sway_container_layout output_get_default_layout(
146 146
147void output_add_listeners(struct sway_output *output); 147void output_add_listeners(struct sway_output *output);
148 148
149void render_rect(struct wlr_output *wlr_output,
150 pixman_region32_t *output_damage, const struct wlr_box *_box,
151 float color[static 4]);
152
153void premultiply_alpha(float color[4], float opacity);
154
155void scale_box(struct wlr_box *box, float scale);
156
149#endif 157#endif
diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h
index 1d0a0ad1..9a432cb2 100644
--- a/include/sway/tree/container.h
+++ b/include/sway/tree/container.h
@@ -333,4 +333,6 @@ void container_add_mark(struct sway_container *container, char *mark);
333 333
334void container_update_marks_textures(struct sway_container *container); 334void container_update_marks_textures(struct sway_container *container);
335 335
336void container_raise_floating(struct sway_container *con);
337
336#endif 338#endif
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index 79ad7faa..04c9b4f6 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -300,7 +300,7 @@ static int scale_length(int length, int offset, float scale) {
300 return round((offset + length) * scale) - round(offset * scale); 300 return round((offset + length) * scale) - round(offset * scale);
301} 301}
302 302
303static void scale_box(struct wlr_box *box, float scale) { 303void scale_box(struct wlr_box *box, float scale) {
304 box->width = scale_length(box->width, box->x, scale); 304 box->width = scale_length(box->width, box->x, scale);
305 box->height = scale_length(box->height, box->y, scale); 305 box->height = scale_length(box->height, box->y, scale);
306 box->x = round(box->x * scale); 306 box->x = round(box->x * scale);
diff --git a/sway/desktop/render.c b/sway/desktop/render.c
index 6c9fe23c..a38c6a07 100644
--- a/sway/desktop/render.c
+++ b/sway/desktop/render.c
@@ -49,13 +49,6 @@ static int scale_length(int length, int offset, float scale) {
49 return round((offset + length) * scale) - round(offset * scale); 49 return round((offset + length) * scale) - round(offset * scale);
50} 50}
51 51
52static void scale_box(struct wlr_box *box, float scale) {
53 box->width = scale_length(box->width, box->x, scale);
54 box->height = scale_length(box->height, box->y, scale);
55 box->x = round(box->x * scale);
56 box->y = round(box->y * scale);
57}
58
59static void scissor_output(struct wlr_output *wlr_output, 52static void scissor_output(struct wlr_output *wlr_output,
60 pixman_box32_t *rect) { 53 pixman_box32_t *rect) {
61 struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); 54 struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend);
@@ -164,7 +157,7 @@ static void render_drag_icons(struct sway_output *output,
164 157
165// _box.x and .y are expected to be layout-local 158// _box.x and .y are expected to be layout-local
166// _box.width and .height are expected to be output-buffer-local 159// _box.width and .height are expected to be output-buffer-local
167static void render_rect(struct wlr_output *wlr_output, 160void render_rect(struct wlr_output *wlr_output,
168 pixman_region32_t *output_damage, const struct wlr_box *_box, 161 pixman_region32_t *output_damage, const struct wlr_box *_box,
169 float color[static 4]) { 162 float color[static 4]) {
170 struct wlr_renderer *renderer = 163 struct wlr_renderer *renderer =
@@ -197,7 +190,7 @@ damage_finish:
197 pixman_region32_fini(&damage); 190 pixman_region32_fini(&damage);
198} 191}
199 192
200static void premultiply_alpha(float color[4], float opacity) { 193void premultiply_alpha(float color[4], float opacity) {
201 color[3] *= opacity; 194 color[3] *= opacity;
202 color[0] *= color[3]; 195 color[0] *= color[3];
203 color[1] *= color[3]; 196 color[1] *= color[3];
@@ -949,21 +942,11 @@ static void render_floating(struct sway_output *soutput,
949 } 942 }
950} 943}
951 944
952static void render_dropzones(struct sway_output *output, 945static void render_seatops(struct sway_output *output,
953 pixman_region32_t *damage) { 946 pixman_region32_t *damage) {
954 struct sway_seat *seat; 947 struct sway_seat *seat;
955 wl_list_for_each(seat, &server.input->seats, link) { 948 wl_list_for_each(seat, &server.input->seats, link) {
956 if (seat->operation == OP_MOVE_TILING && seat->op_target_node 949 seatop_render(seat, output, damage);
957 && node_get_output(seat->op_target_node) == output) {
958 float color[4];
959 memcpy(&color, config->border_colors.focused.indicator,
960 sizeof(float) * 4);
961 premultiply_alpha(color, 0.5);
962 struct wlr_box box;
963 memcpy(&box, &seat->op_drop_box, sizeof(struct wlr_box));
964 scale_box(&box, output->wlr_output->scale);
965 render_rect(output->wlr_output, damage, &box, color);
966 }
967 } 950 }
968} 951}
969 952
@@ -1060,7 +1043,7 @@ void output_render(struct sway_output *output, struct timespec *when,
1060 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]); 1043 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
1061 } 1044 }
1062 1045
1063 render_dropzones(output, damage); 1046 render_seatops(output, damage);
1064 1047
1065 struct sway_seat *seat = input_manager_current_seat(); 1048 struct sway_seat *seat = input_manager_current_seat();
1066 struct sway_container *focus = seat_get_focused_container(seat); 1049 struct sway_container *focus = seat_get_focused_container(seat);
diff --git a/sway/desktop/transaction.c b/sway/desktop/transaction.c
index f46938e2..1cdd7c6d 100644
--- a/sway/desktop/transaction.c
+++ b/sway/desktop/transaction.c
@@ -299,7 +299,7 @@ static void transaction_apply(struct sway_transaction *transaction) {
299 if (root->outputs->length) { 299 if (root->outputs->length) {
300 struct sway_seat *seat; 300 struct sway_seat *seat;
301 wl_list_for_each(seat, &server.input->seats, link) { 301 wl_list_for_each(seat, &server.input->seats, link) {
302 if (seat->operation == OP_NONE) { 302 if (!seat_doing_seatop(seat)) {
303 cursor_rebase(seat->cursor); 303 cursor_rebase(seat->cursor);
304 } 304 }
305 } 305 }
diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c
index 801dcee0..f05e156f 100644
--- a/sway/desktop/xdg_shell.c
+++ b/sway/desktop/xdg_shell.c
@@ -360,7 +360,7 @@ static void handle_request_move(struct wl_listener *listener, void *data) {
360 struct wlr_xdg_toplevel_move_event *e = data; 360 struct wlr_xdg_toplevel_move_event *e = data;
361 struct sway_seat *seat = e->seat->seat->data; 361 struct sway_seat *seat = e->seat->seat->data;
362 if (e->serial == seat->last_button_serial) { 362 if (e->serial == seat->last_button_serial) {
363 seat_begin_move_floating(seat, view->container, seat->last_button); 363 seatop_begin_move_floating(seat, view->container, seat->last_button);
364 } 364 }
365} 365}
366 366
@@ -374,7 +374,7 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
374 struct wlr_xdg_toplevel_resize_event *e = data; 374 struct wlr_xdg_toplevel_resize_event *e = data;
375 struct sway_seat *seat = e->seat->seat->data; 375 struct sway_seat *seat = e->seat->seat->data;
376 if (e->serial == seat->last_button_serial) { 376 if (e->serial == seat->last_button_serial) {
377 seat_begin_resize_floating(seat, view->container, 377 seatop_begin_resize_floating(seat, view->container,
378 seat->last_button, e->edges); 378 seat->last_button, e->edges);
379 } 379 }
380} 380}
diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c
index 4bc83b8e..9f6741c8 100644
--- a/sway/desktop/xdg_shell_v6.c
+++ b/sway/desktop/xdg_shell_v6.c
@@ -357,7 +357,7 @@ static void handle_request_move(struct wl_listener *listener, void *data) {
357 struct wlr_xdg_toplevel_v6_move_event *e = data; 357 struct wlr_xdg_toplevel_v6_move_event *e = data;
358 struct sway_seat *seat = e->seat->seat->data; 358 struct sway_seat *seat = e->seat->seat->data;
359 if (e->serial == seat->last_button_serial) { 359 if (e->serial == seat->last_button_serial) {
360 seat_begin_move_floating(seat, view->container, seat->last_button); 360 seatop_begin_move_floating(seat, view->container, seat->last_button);
361 } 361 }
362} 362}
363 363
@@ -371,7 +371,7 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
371 struct wlr_xdg_toplevel_v6_resize_event *e = data; 371 struct wlr_xdg_toplevel_v6_resize_event *e = data;
372 struct sway_seat *seat = e->seat->seat->data; 372 struct sway_seat *seat = e->seat->seat->data;
373 if (e->serial == seat->last_button_serial) { 373 if (e->serial == seat->last_button_serial) {
374 seat_begin_resize_floating(seat, view->container, 374 seatop_begin_resize_floating(seat, view->container,
375 seat->last_button, e->edges); 375 seat->last_button, e->edges);
376 } 376 }
377} 377}
diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c
index 1838ad32..080f6c41 100644
--- a/sway/desktop/xwayland.c
+++ b/sway/desktop/xwayland.c
@@ -470,7 +470,7 @@ static void handle_request_move(struct wl_listener *listener, void *data) {
470 return; 470 return;
471 } 471 }
472 struct sway_seat *seat = input_manager_current_seat(); 472 struct sway_seat *seat = input_manager_current_seat();
473 seat_begin_move_floating(seat, view->container, seat->last_button); 473 seatop_begin_move_floating(seat, view->container, seat->last_button);
474} 474}
475 475
476static void handle_request_resize(struct wl_listener *listener, void *data) { 476static void handle_request_resize(struct wl_listener *listener, void *data) {
@@ -486,7 +486,7 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
486 } 486 }
487 struct wlr_xwayland_resize_event *e = data; 487 struct wlr_xwayland_resize_event *e = data;
488 struct sway_seat *seat = input_manager_current_seat(); 488 struct sway_seat *seat = input_manager_current_seat();
489 seat_begin_resize_floating(seat, view->container, 489 seatop_begin_resize_floating(seat, view->container,
490 seat->last_button, e->edges); 490 seat->last_button, e->edges);
491} 491}
492 492
diff --git a/sway/input/cursor.c b/sway/input/cursor.c
index 9af7ef57..06294b8b 100644
--- a/sway/input/cursor.c
+++ b/sway/input/cursor.c
@@ -27,10 +27,6 @@
27#include "sway/tree/workspace.h" 27#include "sway/tree/workspace.h"
28#include "wlr-layer-shell-unstable-v1-protocol.h" 28#include "wlr-layer-shell-unstable-v1-protocol.h"
29 29
30// When doing a tiling drag, this is the thickness of the dropzone
31// when dragging to the edge of a layout container.
32#define DROP_LAYOUT_BORDER 30
33
34static uint32_t get_current_time_msec(void) { 30static uint32_t get_current_time_msec(void) {
35 struct timespec now; 31 struct timespec now;
36 clock_gettime(CLOCK_MONOTONIC, &now); 32 clock_gettime(CLOCK_MONOTONIC, &now);
@@ -59,7 +55,7 @@ static struct wlr_surface *layer_surface_at(struct sway_output *output,
59 * Returns the node at the cursor's position. If there is a surface at that 55 * Returns the node at the cursor's position. If there is a surface at that
60 * location, it is stored in **surface (it may not be a view). 56 * location, it is stored in **surface (it may not be a view).
61 */ 57 */
62static struct sway_node *node_at_coords( 58struct sway_node *node_at_coords(
63 struct sway_seat *seat, double lx, double ly, 59 struct sway_seat *seat, double lx, double ly,
64 struct wlr_surface **surface, double *sx, double *sy) { 60 struct wlr_surface **surface, double *sx, double *sy) {
65 // check for unmanaged views first 61 // check for unmanaged views first
@@ -226,347 +222,6 @@ static enum wlr_edges find_resize_edge(struct sway_container *cont,
226 return edge; 222 return edge;
227} 223}
228 224
229static void handle_down_motion(struct sway_seat *seat,
230 struct sway_cursor *cursor, uint32_t time_msec) {
231 struct sway_container *con = seat->op_container;
232 if (seat_is_input_allowed(seat, con->view->surface)) {
233 double moved_x = cursor->cursor->x - seat->op_ref_lx;
234 double moved_y = cursor->cursor->y - seat->op_ref_ly;
235 double sx = seat->op_ref_con_lx + moved_x;
236 double sy = seat->op_ref_con_ly + moved_y;
237 wlr_seat_pointer_notify_motion(seat->wlr_seat, time_msec, sx, sy);
238 }
239 seat->op_moved = true;
240}
241
242static void handle_move_floating_motion(struct sway_seat *seat,
243 struct sway_cursor *cursor) {
244 struct sway_container *con = seat->op_container;
245 desktop_damage_whole_container(con);
246 container_floating_translate(con,
247 cursor->cursor->x - cursor->previous.x,
248 cursor->cursor->y - cursor->previous.y);
249 desktop_damage_whole_container(con);
250}
251
252static void resize_box(struct wlr_box *box, enum wlr_edges edge,
253 int thickness) {
254 switch (edge) {
255 case WLR_EDGE_TOP:
256 box->height = thickness;
257 break;
258 case WLR_EDGE_LEFT:
259 box->width = thickness;
260 break;
261 case WLR_EDGE_RIGHT:
262 box->x = box->x + box->width - thickness;
263 box->width = thickness;
264 break;
265 case WLR_EDGE_BOTTOM:
266 box->y = box->y + box->height - thickness;
267 box->height = thickness;
268 break;
269 case WLR_EDGE_NONE:
270 box->x += thickness;
271 box->y += thickness;
272 box->width -= thickness * 2;
273 box->height -= thickness * 2;
274 break;
275 }
276}
277
278static void handle_move_tiling_motion(struct sway_seat *seat,
279 struct sway_cursor *cursor) {
280 struct wlr_surface *surface = NULL;
281 double sx, sy;
282 struct sway_node *node = node_at_coords(seat,
283 cursor->cursor->x, cursor->cursor->y, &surface, &sx, &sy);
284 // Damage the old location
285 desktop_damage_box(&seat->op_drop_box);
286
287 if (!node) {
288 // Eg. hovered over a layer surface such as swaybar
289 seat->op_target_node = NULL;
290 seat->op_target_edge = WLR_EDGE_NONE;
291 return;
292 }
293
294 if (node->type == N_WORKSPACE) {
295 // Emtpy workspace
296 seat->op_target_node = node;
297 seat->op_target_edge = WLR_EDGE_NONE;
298 workspace_get_box(node->sway_workspace, &seat->op_drop_box);
299 desktop_damage_box(&seat->op_drop_box);
300 return;
301 }
302
303 // Deny moving within own workspace if this is the only child
304 struct sway_container *con = node->sway_container;
305 if (workspace_num_tiling_views(seat->op_container->workspace) == 1 &&
306 con->workspace == seat->op_container->workspace) {
307 seat->op_target_node = NULL;
308 seat->op_target_edge = WLR_EDGE_NONE;
309 return;
310 }
311
312 // Traverse the ancestors, trying to find a layout container perpendicular
313 // to the edge. Eg. close to the top or bottom of a horiz layout.
314 while (con) {
315 enum wlr_edges edge = WLR_EDGE_NONE;
316 enum sway_container_layout layout = container_parent_layout(con);
317 struct wlr_box parent;
318 con->parent ? container_get_box(con->parent, &parent) :
319 workspace_get_box(con->workspace, &parent);
320 if (layout == L_HORIZ || layout == L_TABBED) {
321 if (cursor->cursor->y < parent.y + DROP_LAYOUT_BORDER) {
322 edge = WLR_EDGE_TOP;
323 } else if (cursor->cursor->y > parent.y + parent.height
324 - DROP_LAYOUT_BORDER) {
325 edge = WLR_EDGE_BOTTOM;
326 }
327 } else if (layout == L_VERT || layout == L_STACKED) {
328 if (cursor->cursor->x < parent.x + DROP_LAYOUT_BORDER) {
329 edge = WLR_EDGE_LEFT;
330 } else if (cursor->cursor->x > parent.x + parent.width
331 - DROP_LAYOUT_BORDER) {
332 edge = WLR_EDGE_RIGHT;
333 }
334 }
335 if (edge) {
336 seat->op_target_node = node_get_parent(&con->node);
337 seat->op_target_edge = edge;
338 node_get_box(seat->op_target_node, &seat->op_drop_box);
339 resize_box(&seat->op_drop_box, edge, DROP_LAYOUT_BORDER);
340 desktop_damage_box(&seat->op_drop_box);
341 return;
342 }
343 con = con->parent;
344 }
345
346 // Use the hovered view - but we must be over the actual surface
347 con = node->sway_container;
348 if (!con->view->surface || node == &seat->op_container->node) {
349 seat->op_target_node = NULL;
350 seat->op_target_edge = WLR_EDGE_NONE;
351 return;
352 }
353
354 // Find the closest edge
355 size_t thickness = fmin(con->content_width, con->content_height) * 0.3;
356 size_t closest_dist = INT_MAX;
357 size_t dist;
358 seat->op_target_edge = WLR_EDGE_NONE;
359 if ((dist = cursor->cursor->y - con->y) < closest_dist) {
360 closest_dist = dist;
361 seat->op_target_edge = WLR_EDGE_TOP;
362 }
363 if ((dist = cursor->cursor->x - con->x) < closest_dist) {
364 closest_dist = dist;
365 seat->op_target_edge = WLR_EDGE_LEFT;
366 }
367 if ((dist = con->x + con->width - cursor->cursor->x) < closest_dist) {
368 closest_dist = dist;
369 seat->op_target_edge = WLR_EDGE_RIGHT;
370 }
371 if ((dist = con->y + con->height - cursor->cursor->y) < closest_dist) {
372 closest_dist = dist;
373 seat->op_target_edge = WLR_EDGE_BOTTOM;
374 }
375
376 if (closest_dist > thickness) {
377 seat->op_target_edge = WLR_EDGE_NONE;
378 }
379
380 seat->op_target_node = node;
381 seat->op_drop_box.x = con->content_x;
382 seat->op_drop_box.y = con->content_y;
383 seat->op_drop_box.width = con->content_width;
384 seat->op_drop_box.height = con->content_height;
385 resize_box(&seat->op_drop_box, seat->op_target_edge, thickness);
386 desktop_damage_box(&seat->op_drop_box);
387}
388
389static void handle_move_tiling_threshold_motion(struct sway_seat *seat,
390 struct sway_cursor *cursor) {
391 double cx = seat->cursor->cursor->x;
392 double cy = seat->cursor->cursor->y;
393 double sx = seat->op_ref_lx;
394 double sy = seat->op_ref_ly;
395
396 // Get the scaled threshold for the output. Even if the operation goes
397 // across multiple outputs of varying scales, just use the scale for the
398 // output that the cursor is currently on for simplicity.
399 struct wlr_output *wlr_output = wlr_output_layout_output_at(
400 root->output_layout, cx, cy);
401 double output_scale = wlr_output ? wlr_output->scale : 1;
402 double threshold = config->tiling_drag_threshold * output_scale;
403 threshold *= threshold;
404
405 // If the threshold has been exceeded, start the actual drag
406 if ((cx - sx) * (cx - sx) + (cy - sy) * (cy - sy) > threshold) {
407 seat->operation = OP_MOVE_TILING;
408 cursor_set_image(cursor, "grab", NULL);
409 handle_move_tiling_motion(seat, cursor);
410 }
411}
412
413static void calculate_floating_constraints(struct sway_container *con,
414 int *min_width, int *max_width, int *min_height, int *max_height) {
415 if (config->floating_minimum_width == -1) { // no minimum
416 *min_width = 0;
417 } else if (config->floating_minimum_width == 0) { // automatic
418 *min_width = 75;
419 } else {
420 *min_width = config->floating_minimum_width;
421 }
422
423 if (config->floating_minimum_height == -1) { // no minimum
424 *min_height = 0;
425 } else if (config->floating_minimum_height == 0) { // automatic
426 *min_height = 50;
427 } else {
428 *min_height = config->floating_minimum_height;
429 }
430
431 if (config->floating_maximum_width == -1) { // no maximum
432 *max_width = INT_MAX;
433 } else if (config->floating_maximum_width == 0) { // automatic
434 *max_width = con->workspace->width;
435 } else {
436 *max_width = config->floating_maximum_width;
437 }
438
439 if (config->floating_maximum_height == -1) { // no maximum
440 *max_height = INT_MAX;
441 } else if (config->floating_maximum_height == 0) { // automatic
442 *max_height = con->workspace->height;
443 } else {
444 *max_height = config->floating_maximum_height;
445 }
446}
447
448static void handle_resize_floating_motion(struct sway_seat *seat,
449 struct sway_cursor *cursor) {
450 struct sway_container *con = seat->op_container;
451 enum wlr_edges edge = seat->op_resize_edge;
452
453 // The amount the mouse has moved since the start of the resize operation
454 // Positive is down/right
455 double mouse_move_x = cursor->cursor->x - seat->op_ref_lx;
456 double mouse_move_y = cursor->cursor->y - seat->op_ref_ly;
457
458 if (edge == WLR_EDGE_TOP || edge == WLR_EDGE_BOTTOM) {
459 mouse_move_x = 0;
460 }
461 if (edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT) {
462 mouse_move_y = 0;
463 }
464
465 double grow_width = edge & WLR_EDGE_LEFT ? -mouse_move_x : mouse_move_x;
466 double grow_height = edge & WLR_EDGE_TOP ? -mouse_move_y : mouse_move_y;
467
468 if (seat->op_resize_preserve_ratio) {
469 double x_multiplier = grow_width / seat->op_ref_width;
470 double y_multiplier = grow_height / seat->op_ref_height;
471 double max_multiplier = fmax(x_multiplier, y_multiplier);
472 grow_width = seat->op_ref_width * max_multiplier;
473 grow_height = seat->op_ref_height * max_multiplier;
474 }
475
476 // Determine new width/height, and accommodate for floating min/max values
477 double width = seat->op_ref_width + grow_width;
478 double height = seat->op_ref_height + grow_height;
479 int min_width, max_width, min_height, max_height;
480 calculate_floating_constraints(con, &min_width, &max_width,
481 &min_height, &max_height);
482 width = fmax(min_width, fmin(width, max_width));
483 height = fmax(min_height, fmin(height, max_height));
484
485 // Apply the view's min/max size
486 if (con->view) {
487 double view_min_width, view_max_width, view_min_height, view_max_height;
488 view_get_constraints(con->view, &view_min_width, &view_max_width,
489 &view_min_height, &view_max_height);
490 width = fmax(view_min_width, fmin(width, view_max_width));
491 height = fmax(view_min_height, fmin(height, view_max_height));
492 }
493
494 // Recalculate these, in case we hit a min/max limit
495 grow_width = width - seat->op_ref_width;
496 grow_height = height - seat->op_ref_height;
497
498 // Determine grow x/y values - these are relative to the container's x/y at
499 // the start of the resize operation.
500 double grow_x = 0, grow_y = 0;
501 if (edge & WLR_EDGE_LEFT) {
502 grow_x = -grow_width;
503 } else if (edge & WLR_EDGE_RIGHT) {
504 grow_x = 0;
505 } else {
506 grow_x = -grow_width / 2;
507 }
508 if (edge & WLR_EDGE_TOP) {
509 grow_y = -grow_height;
510 } else if (edge & WLR_EDGE_BOTTOM) {
511 grow_y = 0;
512 } else {
513 grow_y = -grow_height / 2;
514 }
515
516 // Determine the amounts we need to bump everything relative to the current
517 // size.
518 int relative_grow_width = width - con->width;
519 int relative_grow_height = height - con->height;
520 int relative_grow_x = (seat->op_ref_con_lx + grow_x) - con->x;
521 int relative_grow_y = (seat->op_ref_con_ly + grow_y) - con->y;
522
523 // Actually resize stuff
524 con->x += relative_grow_x;
525 con->y += relative_grow_y;
526 con->width += relative_grow_width;
527 con->height += relative_grow_height;
528
529 con->content_x += relative_grow_x;
530 con->content_y += relative_grow_y;
531 con->content_width += relative_grow_width;
532 con->content_height += relative_grow_height;
533
534 arrange_container(con);
535}
536
537static void handle_resize_tiling_motion(struct sway_seat *seat,
538 struct sway_cursor *cursor) {
539 int amount_x = 0;
540 int amount_y = 0;
541 int moved_x = cursor->cursor->x - seat->op_ref_lx;
542 int moved_y = cursor->cursor->y - seat->op_ref_ly;
543 enum wlr_edges edge_x = WLR_EDGE_NONE;
544 enum wlr_edges edge_y = WLR_EDGE_NONE;
545 struct sway_container *con = seat->op_container;
546
547 if (seat->op_resize_edge & WLR_EDGE_TOP) {
548 amount_y = (seat->op_ref_height - moved_y) - con->height;
549 edge_y = WLR_EDGE_TOP;
550 } else if (seat->op_resize_edge & WLR_EDGE_BOTTOM) {
551 amount_y = (seat->op_ref_height + moved_y) - con->height;
552 edge_y = WLR_EDGE_BOTTOM;
553 }
554 if (seat->op_resize_edge & WLR_EDGE_LEFT) {
555 amount_x = (seat->op_ref_width - moved_x) - con->width;
556 edge_x = WLR_EDGE_LEFT;
557 } else if (seat->op_resize_edge & WLR_EDGE_RIGHT) {
558 amount_x = (seat->op_ref_width + moved_x) - con->width;
559 edge_x = WLR_EDGE_RIGHT;
560 }
561
562 if (amount_x != 0) {
563 container_resize_tiled(seat->op_container, edge_x, amount_x);
564 }
565 if (amount_y != 0) {
566 container_resize_tiled(seat->op_container, edge_y, amount_y);
567 }
568}
569
570static void cursor_do_rebase(struct sway_cursor *cursor, uint32_t time_msec, 225static void cursor_do_rebase(struct sway_cursor *cursor, uint32_t time_msec,
571 struct sway_node *node, struct wlr_surface *surface, 226 struct sway_node *node, struct wlr_surface *surface,
572 double sx, double sy) { 227 double sx, double sy) {
@@ -669,29 +324,8 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor,
669 struct sway_seat *seat = cursor->seat; 324 struct sway_seat *seat = cursor->seat;
670 struct wlr_seat *wlr_seat = seat->wlr_seat; 325 struct wlr_seat *wlr_seat = seat->wlr_seat;
671 326
672 if (seat->operation != OP_NONE) { 327 if (seat_doing_seatop(seat)) {
673 switch (seat->operation) { 328 seatop_motion(seat, time_msec);
674 case OP_DOWN:
675 handle_down_motion(seat, cursor, time_msec);
676 break;
677 case OP_MOVE_FLOATING:
678 handle_move_floating_motion(seat, cursor);
679 break;
680 case OP_MOVE_TILING_THRESHOLD:
681 handle_move_tiling_threshold_motion(seat, cursor);
682 break;
683 case OP_MOVE_TILING:
684 handle_move_tiling_motion(seat, cursor);
685 break;
686 case OP_RESIZE_FLOATING:
687 handle_resize_floating_motion(seat, cursor);
688 break;
689 case OP_RESIZE_TILING:
690 handle_resize_tiling_motion(seat, cursor);
691 break;
692 case OP_NONE:
693 break;
694 }
695 cursor->previous.x = cursor->cursor->x; 329 cursor->previous.x = cursor->cursor->x;
696 cursor->previous.y = cursor->cursor->y; 330 cursor->previous.y = cursor->cursor->y;
697 return; 331 return;
@@ -868,9 +502,9 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
868 struct sway_seat *seat = cursor->seat; 502 struct sway_seat *seat = cursor->seat;
869 503
870 // Handle existing seat operation 504 // Handle existing seat operation
871 if (cursor->seat->operation != OP_NONE) { 505 if (seat_doing_seatop(seat)) {
872 if (button == cursor->seat->op_button && state == WLR_BUTTON_RELEASED) { 506 if (button == seat->seatop_button && state == WLR_BUTTON_RELEASED) {
873 seat_end_mouse_operation(seat); 507 seatop_finish(seat);
874 seat_pointer_notify_button(seat, time_msec, button, state); 508 seat_pointer_notify_button(seat, time_msec, button, state);
875 } 509 }
876 if (state == WLR_BUTTON_PRESSED) { 510 if (state == WLR_BUTTON_PRESSED) {
@@ -943,7 +577,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
943 if (cont && resize_edge && button == BTN_LEFT && 577 if (cont && resize_edge && button == BTN_LEFT &&
944 state == WLR_BUTTON_PRESSED && !is_floating) { 578 state == WLR_BUTTON_PRESSED && !is_floating) {
945 seat_set_focus_container(seat, cont); 579 seat_set_focus_container(seat, cont);
946 seat_begin_resize_tiling(seat, cont, button, edge); 580 seatop_begin_resize_tiling(seat, cont, button, edge);
947 return; 581 return;
948 } 582 }
949 583
@@ -973,7 +607,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
973 } 607 }
974 cursor_set_image(seat->cursor, image, NULL); 608 cursor_set_image(seat->cursor, image, NULL);
975 seat_set_focus_container(seat, cont); 609 seat_set_focus_container(seat, cont);
976 seat_begin_resize_tiling(seat, cont, button, edge); 610 seatop_begin_resize_tiling(seat, cont, button, edge);
977 return; 611 return;
978 } 612 }
979 } 613 }
@@ -988,7 +622,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
988 cont = cont->parent; 622 cont = cont->parent;
989 } 623 }
990 seat_set_focus_container(seat, cont); 624 seat_set_focus_container(seat, cont);
991 seat_begin_move_floating(seat, cont, button); 625 seatop_begin_move_floating(seat, cont, button);
992 return; 626 return;
993 } 627 }
994 } 628 }
@@ -998,7 +632,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
998 state == WLR_BUTTON_PRESSED) { 632 state == WLR_BUTTON_PRESSED) {
999 // Via border 633 // Via border
1000 if (button == BTN_LEFT && resize_edge != WLR_EDGE_NONE) { 634 if (button == BTN_LEFT && resize_edge != WLR_EDGE_NONE) {
1001 seat_begin_resize_floating(seat, cont, button, resize_edge); 635 seatop_begin_resize_floating(seat, cont, button, resize_edge);
1002 return; 636 return;
1003 } 637 }
1004 638
@@ -1015,7 +649,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
1015 WLR_EDGE_RIGHT : WLR_EDGE_LEFT; 649 WLR_EDGE_RIGHT : WLR_EDGE_LEFT;
1016 edge |= cursor->cursor->y > floater->y + floater->height / 2 ? 650 edge |= cursor->cursor->y > floater->y + floater->height / 2 ?
1017 WLR_EDGE_BOTTOM : WLR_EDGE_TOP; 651 WLR_EDGE_BOTTOM : WLR_EDGE_TOP;
1018 seat_begin_resize_floating(seat, floater, button, edge); 652 seatop_begin_resize_floating(seat, floater, button, edge);
1019 return; 653 return;
1020 } 654 }
1021 } 655 }
@@ -1035,9 +669,9 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
1035 669
1036 // If moving a container by it's title bar, use a threshold for the drag 670 // If moving a container by it's title bar, use a threshold for the drag
1037 if (!mod_pressed && config->tiling_drag_threshold > 0) { 671 if (!mod_pressed && config->tiling_drag_threshold > 0) {
1038 seat_begin_move_tiling_threshold(seat, cont, button); 672 seatop_begin_move_tiling_threshold(seat, cont, button);
1039 } else { 673 } else {
1040 seat_begin_move_tiling(seat, cont, button); 674 seatop_begin_move_tiling(seat, cont, button);
1041 } 675 }
1042 return; 676 return;
1043 } 677 }
@@ -1046,7 +680,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
1046 if (surface && cont && state == WLR_BUTTON_PRESSED) { 680 if (surface && cont && state == WLR_BUTTON_PRESSED) {
1047 seat_set_focus_container(seat, cont); 681 seat_set_focus_container(seat, cont);
1048 seat_pointer_notify_button(seat, time_msec, button, state); 682 seat_pointer_notify_button(seat, time_msec, button, state);
1049 seat_begin_down(seat, cont, button, sx, sy); 683 seatop_begin_down(seat, cont, button, sx, sy);
1050 return; 684 return;
1051 } 685 }
1052 686
@@ -1350,7 +984,7 @@ static void handle_request_set_cursor(struct wl_listener *listener,
1350 void *data) { 984 void *data) {
1351 struct sway_cursor *cursor = 985 struct sway_cursor *cursor =
1352 wl_container_of(listener, cursor, request_set_cursor); 986 wl_container_of(listener, cursor, request_set_cursor);
1353 if (cursor->seat->operation != OP_NONE) { 987 if (seat_doing_seatop(cursor->seat)) {
1354 return; 988 return;
1355 } 989 }
1356 struct wlr_seat_pointer_request_set_cursor_event *event = data; 990 struct wlr_seat_pointer_request_set_cursor_event *event = data;
diff --git a/sway/input/seat.c b/sway/input/seat.c
index a8df5b99..7bd889d7 100644
--- a/sway/input/seat.c
+++ b/sway/input/seat.c
@@ -308,7 +308,7 @@ static void handle_new_drag_icon(struct wl_listener *listener, void *data) {
308 wl_list_insert(&root->drag_icons, &icon->link); 308 wl_list_insert(&root->drag_icons, &icon->link);
309 309
310 drag_icon_update_position(icon); 310 drag_icon_update_position(icon);
311 seat_end_mouse_operation(seat); 311 seatop_abort(seat);
312} 312}
313 313
314static void collect_focus_iter(struct sway_node *node, void *data) { 314static void collect_focus_iter(struct sway_node *node, void *data) {
@@ -625,18 +625,6 @@ static int handle_urgent_timeout(void *data) {
625 return 0; 625 return 0;
626} 626}
627 627
628static void container_raise_floating(struct sway_container *con) {
629 // Bring container to front by putting it at the end of the floating list.
630 struct sway_container *floater = con;
631 while (floater->parent) {
632 floater = floater->parent;
633 }
634 if (container_is_floating(floater)) {
635 list_move_to_end(floater->workspace->floating, floater);
636 node_set_dirty(&floater->workspace->node);
637 }
638}
639
640static void set_workspace(struct sway_seat *seat, 628static void set_workspace(struct sway_seat *seat,
641 struct sway_workspace *new_ws) { 629 struct sway_workspace *new_ws) {
642 if (seat->workspace == new_ws) { 630 if (seat->workspace == new_ws) {
@@ -1025,187 +1013,6 @@ struct seat_config *seat_get_config_by_name(const char *name) {
1025 return NULL; 1013 return NULL;
1026} 1014}
1027 1015
1028void seat_begin_down(struct sway_seat *seat, struct sway_container *con,
1029 uint32_t button, double sx, double sy) {
1030 seat->operation = OP_DOWN;
1031 seat->op_container = con;
1032 seat->op_button = button;
1033 seat->op_ref_lx = seat->cursor->cursor->x;
1034 seat->op_ref_ly = seat->cursor->cursor->y;
1035 seat->op_ref_con_lx = sx;
1036 seat->op_ref_con_ly = sy;
1037 seat->op_moved = false;
1038
1039 container_raise_floating(con);
1040}
1041
1042void seat_begin_move_floating(struct sway_seat *seat,
1043 struct sway_container *con, uint32_t button) {
1044 if (!seat->cursor) {
1045 wlr_log(WLR_DEBUG, "Ignoring move request due to no cursor device");
1046 return;
1047 }
1048 seat->operation = OP_MOVE_FLOATING;
1049 seat->op_container = con;
1050 seat->op_button = button;
1051
1052 container_raise_floating(con);
1053
1054 cursor_set_image(seat->cursor, "grab", NULL);
1055}
1056
1057void seat_begin_move_tiling_threshold(struct sway_seat *seat,
1058 struct sway_container *con, uint32_t button) {
1059 seat->operation = OP_MOVE_TILING_THRESHOLD;
1060 seat->op_container = con;
1061 seat->op_button = button;
1062 seat->op_target_node = NULL;
1063 seat->op_target_edge = 0;
1064 seat->op_ref_lx = seat->cursor->cursor->x;
1065 seat->op_ref_ly = seat->cursor->cursor->y;
1066}
1067
1068void seat_begin_move_tiling(struct sway_seat *seat,
1069 struct sway_container *con, uint32_t button) {
1070 seat->operation = OP_MOVE_TILING;
1071 seat->op_container = con;
1072 seat->op_button = button;
1073 seat->op_target_node = NULL;
1074 seat->op_target_edge = 0;
1075 cursor_set_image(seat->cursor, "grab", NULL);
1076}
1077
1078void seat_begin_resize_floating(struct sway_seat *seat,
1079 struct sway_container *con, uint32_t button, enum wlr_edges edge) {
1080 if (!seat->cursor) {
1081 wlr_log(WLR_DEBUG, "Ignoring resize request due to no cursor device");
1082 return;
1083 }
1084 struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat);
1085 seat->operation = OP_RESIZE_FLOATING;
1086 seat->op_container = con;
1087 seat->op_resize_preserve_ratio = keyboard &&
1088 (wlr_keyboard_get_modifiers(keyboard) & WLR_MODIFIER_SHIFT);
1089 seat->op_resize_edge = edge == WLR_EDGE_NONE ?
1090 WLR_EDGE_BOTTOM | WLR_EDGE_RIGHT : edge;
1091 seat->op_button = button;
1092 seat->op_ref_lx = seat->cursor->cursor->x;
1093 seat->op_ref_ly = seat->cursor->cursor->y;
1094 seat->op_ref_con_lx = con->x;
1095 seat->op_ref_con_ly = con->y;
1096 seat->op_ref_width = con->width;
1097 seat->op_ref_height = con->height;
1098
1099 container_raise_floating(con);
1100
1101 const char *image = edge == WLR_EDGE_NONE ?
1102 "se-resize" : wlr_xcursor_get_resize_name(edge);
1103 cursor_set_image(seat->cursor, image, NULL);
1104}
1105
1106void seat_begin_resize_tiling(struct sway_seat *seat,
1107 struct sway_container *con, uint32_t button, enum wlr_edges edge) {
1108 seat->operation = OP_RESIZE_TILING;
1109 seat->op_container = con;
1110 seat->op_resize_edge = edge;
1111 seat->op_button = button;
1112 seat->op_ref_lx = seat->cursor->cursor->x;
1113 seat->op_ref_ly = seat->cursor->cursor->y;
1114 seat->op_ref_con_lx = con->x;
1115 seat->op_ref_con_ly = con->y;
1116 seat->op_ref_width = con->width;
1117 seat->op_ref_height = con->height;
1118}
1119
1120static bool is_parallel(enum sway_container_layout layout,
1121 enum wlr_edges edge) {
1122 bool layout_is_horiz = layout == L_HORIZ || layout == L_TABBED;
1123 bool edge_is_horiz = edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT;
1124 return layout_is_horiz == edge_is_horiz;
1125}
1126
1127static void seat_end_move_tiling(struct sway_seat *seat) {
1128 struct sway_container *con = seat->op_container;
1129 struct sway_container *old_parent = con->parent;
1130 struct sway_workspace *old_ws = con->workspace;
1131 struct sway_node *target_node = seat->op_target_node;
1132 struct sway_workspace *new_ws = target_node->type == N_WORKSPACE ?
1133 target_node->sway_workspace : target_node->sway_container->workspace;
1134 enum wlr_edges edge = seat->op_target_edge;
1135 int after = edge != WLR_EDGE_TOP && edge != WLR_EDGE_LEFT;
1136
1137 container_detach(con);
1138
1139 // Moving container into empty workspace
1140 if (target_node->type == N_WORKSPACE && edge == WLR_EDGE_NONE) {
1141 workspace_add_tiling(new_ws, con);
1142 } else if (target_node->type == N_CONTAINER) {
1143 // Moving container before/after another
1144 struct sway_container *target = target_node->sway_container;
1145 enum sway_container_layout layout = container_parent_layout(target);
1146 if (edge && !is_parallel(layout, edge)) {
1147 enum sway_container_layout new_layout = edge == WLR_EDGE_TOP ||
1148 edge == WLR_EDGE_BOTTOM ? L_VERT : L_HORIZ;
1149 container_split(target, new_layout);
1150 }
1151 container_add_sibling(target, con, after);
1152 } else {
1153 // Target is a workspace which requires splitting
1154 enum sway_container_layout new_layout = edge == WLR_EDGE_TOP ||
1155 edge == WLR_EDGE_BOTTOM ? L_VERT : L_HORIZ;
1156 workspace_split(new_ws, new_layout);
1157 workspace_insert_tiling(new_ws, con, after);
1158 }
1159
1160 if (old_parent) {
1161 container_reap_empty(old_parent);
1162 }
1163
1164 // This is a bit dirty, but we'll set the dimensions to that of a sibling.
1165 // I don't think there's any other way to make it consistent without
1166 // changing how we auto-size containers.
1167 list_t *siblings = container_get_siblings(con);
1168 if (siblings->length > 1) {
1169 int index = list_find(siblings, con);
1170 struct sway_container *sibling = index == 0 ?
1171 siblings->items[1] : siblings->items[index - 1];
1172 con->width = sibling->width;
1173 con->height = sibling->height;
1174 }
1175
1176 arrange_workspace(old_ws);
1177 if (new_ws != old_ws) {
1178 arrange_workspace(new_ws);
1179 }
1180}
1181
1182void seat_end_mouse_operation(struct sway_seat *seat) {
1183 enum sway_seat_operation operation = seat->operation;
1184 if (seat->operation == OP_MOVE_FLOATING) {
1185 // We "move" the container to its own location so it discovers its
1186 // output again.
1187 struct sway_container *con = seat->op_container;
1188 container_floating_move_to(con, con->x, con->y);
1189 } else if (seat->operation == OP_MOVE_TILING && seat->op_target_node) {
1190 seat_end_move_tiling(seat);
1191 }
1192 seat->operation = OP_NONE;
1193 seat->op_container = NULL;
1194 if (operation == OP_DOWN) {
1195 // Set the cursor's previous coords to the x/y at the start of the
1196 // operation, so the container change will be detected if using
1197 // focus_follows_mouse and the cursor moved off the original container
1198 // during the operation.
1199 seat->cursor->previous.x = seat->op_ref_lx;
1200 seat->cursor->previous.y = seat->op_ref_ly;
1201 if (seat->op_moved) {
1202 cursor_send_pointer_motion(seat->cursor, 0);
1203 }
1204 } else {
1205 cursor_set_image(seat->cursor, "left_ptr", NULL);
1206 }
1207}
1208
1209void seat_pointer_notify_button(struct sway_seat *seat, uint32_t time_msec, 1016void seat_pointer_notify_button(struct sway_seat *seat, uint32_t time_msec,
1210 uint32_t button, enum wlr_button_state state) { 1017 uint32_t button, enum wlr_button_state state) {
1211 seat->last_button = button; 1018 seat->last_button = button;
@@ -1238,3 +1045,44 @@ void seat_consider_warp_to_focus(struct sway_seat *seat) {
1238 wl_event_source_timer_update(seat->cursor->hide_source, cursor_get_timeout(seat->cursor)); 1045 wl_event_source_timer_update(seat->cursor->hide_source, cursor_get_timeout(seat->cursor));
1239 } 1046 }
1240} 1047}
1048
1049bool seat_doing_seatop(struct sway_seat *seat) {
1050 return seat->seatop_impl != NULL;
1051}
1052
1053void seatop_unref(struct sway_seat *seat, struct sway_container *con) {
1054 if (seat->seatop_impl && seat->seatop_impl->unref) {
1055 seat->seatop_impl->unref(seat, con);
1056 }
1057}
1058
1059void seatop_motion(struct sway_seat *seat, uint32_t time_msec) {
1060 if (seat->seatop_impl && seat->seatop_impl->motion) {
1061 seat->seatop_impl->motion(seat, time_msec);
1062 }
1063}
1064
1065void seatop_finish(struct sway_seat *seat) {
1066 if (seat->seatop_impl && seat->seatop_impl->finish) {
1067 seat->seatop_impl->finish(seat);
1068 }
1069 free(seat->seatop_data);
1070 seat->seatop_data = NULL;
1071 seat->seatop_impl = NULL;
1072}
1073
1074void seatop_abort(struct sway_seat *seat) {
1075 if (seat->seatop_impl && seat->seatop_impl->abort) {
1076 seat->seatop_impl->abort(seat);
1077 }
1078 free(seat->seatop_data);
1079 seat->seatop_data = NULL;
1080 seat->seatop_impl = NULL;
1081}
1082
1083void seatop_render(struct sway_seat *seat, struct sway_output *output,
1084 pixman_region32_t *damage) {
1085 if (seat->seatop_impl && seat->seatop_impl->render) {
1086 seat->seatop_impl->render(seat, output, damage);
1087 }
1088}
diff --git a/sway/input/seatop_down.c b/sway/input/seatop_down.c
new file mode 100644
index 00000000..ad11c5ca
--- /dev/null
+++ b/sway/input/seatop_down.c
@@ -0,0 +1,77 @@
1#define _POSIX_C_SOURCE 200809L
2#include <wlr/types/wlr_cursor.h>
3#include "sway/input/cursor.h"
4#include "sway/input/seat.h"
5#include "sway/tree/view.h"
6
7struct seatop_down_event {
8 struct sway_container *con;
9 double ref_lx, ref_ly; // cursor's x/y at start of op
10 double ref_con_lx, ref_con_ly; // container's x/y at start of op
11 bool moved;
12};
13
14static void handle_motion(struct sway_seat *seat, uint32_t time_msec) {
15 struct seatop_down_event *e = seat->seatop_data;
16 struct sway_container *con = e->con;
17 if (seat_is_input_allowed(seat, con->view->surface)) {
18 double moved_x = seat->cursor->cursor->x - e->ref_lx;
19 double moved_y = seat->cursor->cursor->y - e->ref_ly;
20 double sx = e->ref_con_lx + moved_x;
21 double sy = e->ref_con_ly + moved_y;
22 wlr_seat_pointer_notify_motion(seat->wlr_seat, time_msec, sx, sy);
23 }
24 e->moved = true;
25}
26
27static void handle_finish(struct sway_seat *seat) {
28 struct seatop_down_event *e = seat->seatop_data;
29 // Set the cursor's previous coords to the x/y at the start of the
30 // operation, so the container change will be detected if using
31 // focus_follows_mouse and the cursor moved off the original container
32 // during the operation.
33 seat->cursor->previous.x = e->ref_lx;
34 seat->cursor->previous.y = e->ref_ly;
35 if (e->moved) {
36 cursor_send_pointer_motion(seat->cursor, 0);
37 }
38}
39
40static void handle_abort(struct sway_seat *seat) {
41 cursor_set_image(seat->cursor, "left_ptr", NULL);
42}
43
44static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
45 struct seatop_down_event *e = seat->seatop_data;
46 if (e->con == con) {
47 seatop_abort(seat);
48 }
49}
50
51static const struct sway_seatop_impl seatop_impl = {
52 .motion = handle_motion,
53 .finish = handle_finish,
54 .abort = handle_abort,
55 .unref = handle_unref,
56};
57
58void seatop_begin_down(struct sway_seat *seat,
59 struct sway_container *con, uint32_t button, int sx, int sy) {
60 seatop_abort(seat);
61
62 struct seatop_down_event *e =
63 calloc(1, sizeof(struct seatop_down_event));
64 if (!e) {
65 return;
66 }
67 e->con = con;
68 e->ref_lx = seat->cursor->cursor->x;
69 e->ref_ly = seat->cursor->cursor->y;
70 e->ref_con_lx = sx;
71 e->ref_con_ly = sy;
72 e->moved = false;
73
74 seat->seatop_impl = &seatop_impl;
75 seat->seatop_data = e;
76 seat->seatop_button = button;
77}
diff --git a/sway/input/seatop_move_floating.c b/sway/input/seatop_move_floating.c
new file mode 100644
index 00000000..08e3a5a4
--- /dev/null
+++ b/sway/input/seatop_move_floating.c
@@ -0,0 +1,65 @@
1#define _POSIX_C_SOURCE 200809L
2#include <wlr/types/wlr_cursor.h>
3#include "sway/desktop.h"
4#include "sway/input/cursor.h"
5#include "sway/input/seat.h"
6
7struct seatop_move_floating_event {
8 struct sway_container *con;
9};
10
11static void handle_motion(struct sway_seat *seat, uint32_t time_msec) {
12 struct seatop_move_floating_event *e = seat->seatop_data;
13 desktop_damage_whole_container(e->con);
14 container_floating_translate(e->con,
15 seat->cursor->cursor->x - seat->cursor->previous.x,
16 seat->cursor->cursor->y - seat->cursor->previous.y);
17 desktop_damage_whole_container(e->con);
18}
19
20static void handle_finish(struct sway_seat *seat) {
21 struct seatop_move_floating_event *e = seat->seatop_data;
22
23 // We "move" the container to its own location
24 // so it discovers its output again.
25 container_floating_move_to(e->con, e->con->x, e->con->y);
26 cursor_set_image(seat->cursor, "left_ptr", NULL);
27}
28
29static void handle_abort(struct sway_seat *seat) {
30 cursor_set_image(seat->cursor, "left_ptr", NULL);
31}
32
33static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
34 struct seatop_move_floating_event *e = seat->seatop_data;
35 if (e->con == con) {
36 seatop_abort(seat);
37 }
38}
39
40static const struct sway_seatop_impl seatop_impl = {
41 .motion = handle_motion,
42 .finish = handle_finish,
43 .abort = handle_abort,
44 .unref = handle_unref,
45};
46
47void seatop_begin_move_floating(struct sway_seat *seat,
48 struct sway_container *con, uint32_t button) {
49 seatop_abort(seat);
50
51 struct seatop_move_floating_event *e =
52 calloc(1, sizeof(struct seatop_move_floating_event));
53 if (!e) {
54 return;
55 }
56 e->con = con;
57
58 seat->seatop_impl = &seatop_impl;
59 seat->seatop_data = e;
60 seat->seatop_button = button;
61
62 container_raise_floating(con);
63
64 cursor_set_image(seat->cursor, "grab", NULL);
65}
diff --git a/sway/input/seatop_move_tiling.c b/sway/input/seatop_move_tiling.c
new file mode 100644
index 00000000..8b541f80
--- /dev/null
+++ b/sway/input/seatop_move_tiling.c
@@ -0,0 +1,335 @@
1#define _POSIX_C_SOURCE 200809L
2#include <limits.h>
3#include <wlr/types/wlr_cursor.h>
4#include <wlr/util/edges.h>
5#include "sway/desktop.h"
6#include "sway/input/cursor.h"
7#include "sway/input/seat.h"
8#include "sway/output.h"
9#include "sway/tree/arrange.h"
10#include "sway/tree/node.h"
11#include "sway/tree/view.h"
12#include "sway/tree/workspace.h"
13
14// Thickness of the dropzone when dragging to the edge of a layout container
15#define DROP_LAYOUT_BORDER 30
16
17struct seatop_move_tiling_event {
18 struct sway_container *con;
19 struct sway_node *target_node;
20 enum wlr_edges target_edge;
21 struct wlr_box drop_box;
22 double ref_lx, ref_ly; // cursor's x/y at start of op
23 bool threshold_reached;
24};
25
26static void handle_render(struct sway_seat *seat,
27 struct sway_output *output, pixman_region32_t *damage) {
28 struct seatop_move_tiling_event *e = seat->seatop_data;
29 if (!e->threshold_reached) {
30 return;
31 }
32 if (e->target_node && node_get_output(e->target_node) == output) {
33 float color[4];
34 memcpy(&color, config->border_colors.focused.indicator,
35 sizeof(float) * 4);
36 premultiply_alpha(color, 0.5);
37 struct wlr_box box;
38 memcpy(&box, &e->drop_box, sizeof(struct wlr_box));
39 scale_box(&box, output->wlr_output->scale);
40 render_rect(output->wlr_output, damage, &box, color);
41 }
42}
43
44static void handle_motion_prethreshold(struct sway_seat *seat) {
45 struct seatop_move_tiling_event *e = seat->seatop_data;
46 double cx = seat->cursor->cursor->x;
47 double cy = seat->cursor->cursor->y;
48 double sx = e->ref_lx;
49 double sy = e->ref_ly;
50
51 // Get the scaled threshold for the output. Even if the operation goes
52 // across multiple outputs of varying scales, just use the scale for the
53 // output that the cursor is currently on for simplicity.
54 struct wlr_output *wlr_output = wlr_output_layout_output_at(
55 root->output_layout, cx, cy);
56 double output_scale = wlr_output ? wlr_output->scale : 1;
57 double threshold = config->tiling_drag_threshold * output_scale;
58 threshold *= threshold;
59
60 // If the threshold has been exceeded, start the actual drag
61 if ((cx - sx) * (cx - sx) + (cy - sy) * (cy - sy) > threshold) {
62 e->threshold_reached = true;
63 cursor_set_image(seat->cursor, "grab", NULL);
64 }
65}
66
67static void resize_box(struct wlr_box *box, enum wlr_edges edge,
68 int thickness) {
69 switch (edge) {
70 case WLR_EDGE_TOP:
71 box->height = thickness;
72 break;
73 case WLR_EDGE_LEFT:
74 box->width = thickness;
75 break;
76 case WLR_EDGE_RIGHT:
77 box->x = box->x + box->width - thickness;
78 box->width = thickness;
79 break;
80 case WLR_EDGE_BOTTOM:
81 box->y = box->y + box->height - thickness;
82 box->height = thickness;
83 break;
84 case WLR_EDGE_NONE:
85 box->x += thickness;
86 box->y += thickness;
87 box->width -= thickness * 2;
88 box->height -= thickness * 2;
89 break;
90 }
91}
92
93static void handle_motion_postthreshold(struct sway_seat *seat) {
94 struct seatop_move_tiling_event *e = seat->seatop_data;
95 struct wlr_surface *surface = NULL;
96 double sx, sy;
97 struct sway_cursor *cursor = seat->cursor;
98 struct sway_node *node = node_at_coords(seat,
99 cursor->cursor->x, cursor->cursor->y, &surface, &sx, &sy);
100 // Damage the old location
101 desktop_damage_box(&e->drop_box);
102
103 if (!node) {
104 // Eg. hovered over a layer surface such as swaybar
105 e->target_node = NULL;
106 e->target_edge = WLR_EDGE_NONE;
107 return;
108 }
109
110 if (node->type == N_WORKSPACE) {
111 // Emtpy workspace
112 e->target_node = node;
113 e->target_edge = WLR_EDGE_NONE;
114 workspace_get_box(node->sway_workspace, &e->drop_box);
115 desktop_damage_box(&e->drop_box);
116 return;
117 }
118
119 // Deny moving within own workspace if this is the only child
120 struct sway_container *con = node->sway_container;
121 if (workspace_num_tiling_views(e->con->workspace) == 1 &&
122 con->workspace == e->con->workspace) {
123 e->target_node = NULL;
124 e->target_edge = WLR_EDGE_NONE;
125 return;
126 }
127
128 // Traverse the ancestors, trying to find a layout container perpendicular
129 // to the edge. Eg. close to the top or bottom of a horiz layout.
130 while (con) {
131 enum wlr_edges edge = WLR_EDGE_NONE;
132 enum sway_container_layout layout = container_parent_layout(con);
133 struct wlr_box parent;
134 con->parent ? container_get_box(con->parent, &parent) :
135 workspace_get_box(con->workspace, &parent);
136 if (layout == L_HORIZ || layout == L_TABBED) {
137 if (cursor->cursor->y < parent.y + DROP_LAYOUT_BORDER) {
138 edge = WLR_EDGE_TOP;
139 } else if (cursor->cursor->y > parent.y + parent.height
140 - DROP_LAYOUT_BORDER) {
141 edge = WLR_EDGE_BOTTOM;
142 }
143 } else if (layout == L_VERT || layout == L_STACKED) {
144 if (cursor->cursor->x < parent.x + DROP_LAYOUT_BORDER) {
145 edge = WLR_EDGE_LEFT;
146 } else if (cursor->cursor->x > parent.x + parent.width
147 - DROP_LAYOUT_BORDER) {
148 edge = WLR_EDGE_RIGHT;
149 }
150 }
151 if (edge) {
152 e->target_node = node_get_parent(&con->node);
153 e->target_edge = edge;
154 node_get_box(e->target_node, &e->drop_box);
155 resize_box(&e->drop_box, edge, DROP_LAYOUT_BORDER);
156 desktop_damage_box(&e->drop_box);
157 return;
158 }
159 con = con->parent;
160 }
161
162 // Use the hovered view - but we must be over the actual surface
163 con = node->sway_container;
164 if (!con->view->surface || node == &e->con->node) {
165 e->target_node = NULL;
166 e->target_edge = WLR_EDGE_NONE;
167 return;
168 }
169
170 // Find the closest edge
171 size_t thickness = fmin(con->content_width, con->content_height) * 0.3;
172 size_t closest_dist = INT_MAX;
173 size_t dist;
174 e->target_edge = WLR_EDGE_NONE;
175 if ((dist = cursor->cursor->y - con->y) < closest_dist) {
176 closest_dist = dist;
177 e->target_edge = WLR_EDGE_TOP;
178 }
179 if ((dist = cursor->cursor->x - con->x) < closest_dist) {
180 closest_dist = dist;
181 e->target_edge = WLR_EDGE_LEFT;
182 }
183 if ((dist = con->x + con->width - cursor->cursor->x) < closest_dist) {
184 closest_dist = dist;
185 e->target_edge = WLR_EDGE_RIGHT;
186 }
187 if ((dist = con->y + con->height - cursor->cursor->y) < closest_dist) {
188 closest_dist = dist;
189 e->target_edge = WLR_EDGE_BOTTOM;
190 }
191
192 if (closest_dist > thickness) {
193 e->target_edge = WLR_EDGE_NONE;
194 }
195
196 e->target_node = node;
197 e->drop_box.x = con->content_x;
198 e->drop_box.y = con->content_y;
199 e->drop_box.width = con->content_width;
200 e->drop_box.height = con->content_height;
201 resize_box(&e->drop_box, e->target_edge, thickness);
202 desktop_damage_box(&e->drop_box);
203}
204
205static void handle_motion(struct sway_seat *seat, uint32_t time_msec) {
206 struct seatop_move_tiling_event *e = seat->seatop_data;
207 if (e->threshold_reached) {
208 handle_motion_postthreshold(seat);
209 } else {
210 handle_motion_prethreshold(seat);
211 }
212}
213
214static void handle_abort(struct sway_seat *seat) {
215 cursor_set_image(seat->cursor, "left_ptr", NULL);
216}
217
218static bool is_parallel(enum sway_container_layout layout,
219 enum wlr_edges edge) {
220 bool layout_is_horiz = layout == L_HORIZ || layout == L_TABBED;
221 bool edge_is_horiz = edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT;
222 return layout_is_horiz == edge_is_horiz;
223}
224
225static void handle_finish(struct sway_seat *seat) {
226 struct seatop_move_tiling_event *e = seat->seatop_data;
227
228 if (!e->target_node) {
229 handle_abort(seat);
230 return;
231 }
232
233 struct sway_container *con = e->con;
234 struct sway_container *old_parent = con->parent;
235 struct sway_workspace *old_ws = con->workspace;
236 struct sway_node *target_node = e->target_node;
237 struct sway_workspace *new_ws = target_node->type == N_WORKSPACE ?
238 target_node->sway_workspace : target_node->sway_container->workspace;
239 enum wlr_edges edge = e->target_edge;
240 int after = edge != WLR_EDGE_TOP && edge != WLR_EDGE_LEFT;
241
242 container_detach(con);
243
244 // Moving container into empty workspace
245 if (target_node->type == N_WORKSPACE && edge == WLR_EDGE_NONE) {
246 workspace_add_tiling(new_ws, con);
247 } else if (target_node->type == N_CONTAINER) {
248 // Moving container before/after another
249 struct sway_container *target = target_node->sway_container;
250 enum sway_container_layout layout = container_parent_layout(target);
251 if (edge && !is_parallel(layout, edge)) {
252 enum sway_container_layout new_layout = edge == WLR_EDGE_TOP ||
253 edge == WLR_EDGE_BOTTOM ? L_VERT : L_HORIZ;
254 container_split(target, new_layout);
255 }
256 container_add_sibling(target, con, after);
257 } else {
258 // Target is a workspace which requires splitting
259 enum sway_container_layout new_layout = edge == WLR_EDGE_TOP ||
260 edge == WLR_EDGE_BOTTOM ? L_VERT : L_HORIZ;
261 workspace_split(new_ws, new_layout);
262 workspace_insert_tiling(new_ws, con, after);
263 }
264
265 if (old_parent) {
266 container_reap_empty(old_parent);
267 }
268
269 // This is a bit dirty, but we'll set the dimensions to that of a sibling.
270 // I don't think there's any other way to make it consistent without
271 // changing how we auto-size containers.
272 list_t *siblings = container_get_siblings(con);
273 if (siblings->length > 1) {
274 int index = list_find(siblings, con);
275 struct sway_container *sibling = index == 0 ?
276 siblings->items[1] : siblings->items[index - 1];
277 con->width = sibling->width;
278 con->height = sibling->height;
279 }
280
281 arrange_workspace(old_ws);
282 if (new_ws != old_ws) {
283 arrange_workspace(new_ws);
284 }
285
286 cursor_set_image(seat->cursor, "left_ptr", NULL);
287}
288
289static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
290 struct seatop_move_tiling_event *e = seat->seatop_data;
291 if (e->target_node == &con->node) { // Drop target
292 e->target_node = NULL;
293 }
294 if (e->con == con) { // The container being moved
295 seatop_abort(seat);
296 }
297}
298
299static const struct sway_seatop_impl seatop_impl = {
300 .motion = handle_motion,
301 .finish = handle_finish,
302 .abort = handle_abort,
303 .unref = handle_unref,
304 .render = handle_render,
305};
306
307void seatop_begin_move_tiling_threshold(struct sway_seat *seat,
308 struct sway_container *con, uint32_t button) {
309 seatop_abort(seat);
310
311 struct seatop_move_tiling_event *e =
312 calloc(1, sizeof(struct seatop_move_tiling_event));
313 if (!e) {
314 return;
315 }
316 e->con = con;
317 e->ref_lx = seat->cursor->cursor->x;
318 e->ref_ly = seat->cursor->cursor->y;
319
320 seat->seatop_impl = &seatop_impl;
321 seat->seatop_data = e;
322 seat->seatop_button = button;
323
324 container_raise_floating(con);
325}
326
327void seatop_begin_move_tiling(struct sway_seat *seat,
328 struct sway_container *con, uint32_t button) {
329 seatop_begin_move_tiling_threshold(seat, con, button);
330 struct seatop_move_tiling_event *e = seat->seatop_data;
331 if (e) {
332 e->threshold_reached = true;
333 cursor_set_image(seat->cursor, "grab", NULL);
334 }
335}
diff --git a/sway/input/seatop_resize_floating.c b/sway/input/seatop_resize_floating.c
new file mode 100644
index 00000000..12851b40
--- /dev/null
+++ b/sway/input/seatop_resize_floating.c
@@ -0,0 +1,199 @@
1#define _POSIX_C_SOURCE 200809L
2#include <limits.h>
3#include <wlr/types/wlr_cursor.h>
4#include <wlr/types/wlr_xcursor_manager.h>
5#include "sway/input/cursor.h"
6#include "sway/input/seat.h"
7#include "sway/tree/arrange.h"
8#include "sway/tree/view.h"
9#include "sway/tree/workspace.h"
10
11struct seatop_resize_floating_event {
12 struct sway_container *con;
13 enum wlr_edges edge;
14 bool preserve_ratio;
15 double ref_lx, ref_ly; // cursor's x/y at start of op
16 double ref_width, ref_height; // container's size at start of op
17 double ref_con_lx, ref_con_ly; // container's x/y at start of op
18};
19
20static void calculate_floating_constraints(struct sway_container *con,
21 int *min_width, int *max_width, int *min_height, int *max_height) {
22 if (config->floating_minimum_width == -1) { // no minimum
23 *min_width = 0;
24 } else if (config->floating_minimum_width == 0) { // automatic
25 *min_width = 75;
26 } else {
27 *min_width = config->floating_minimum_width;
28 }
29
30 if (config->floating_minimum_height == -1) { // no minimum
31 *min_height = 0;
32 } else if (config->floating_minimum_height == 0) { // automatic
33 *min_height = 50;
34 } else {
35 *min_height = config->floating_minimum_height;
36 }
37
38 if (config->floating_maximum_width == -1) { // no maximum
39 *max_width = INT_MAX;
40 } else if (config->floating_maximum_width == 0) { // automatic
41 *max_width = con->workspace->width;
42 } else {
43 *max_width = config->floating_maximum_width;
44 }
45
46 if (config->floating_maximum_height == -1) { // no maximum
47 *max_height = INT_MAX;
48 } else if (config->floating_maximum_height == 0) { // automatic
49 *max_height = con->workspace->height;
50 } else {
51 *max_height = config->floating_maximum_height;
52 }
53}
54
55static void handle_motion(struct sway_seat *seat, uint32_t time_msec) {
56 struct seatop_resize_floating_event *e = seat->seatop_data;
57 struct sway_container *con = e->con;
58 enum wlr_edges edge = e->edge;
59 struct sway_cursor *cursor = seat->cursor;
60
61 // The amount the mouse has moved since the start of the resize operation
62 // Positive is down/right
63 double mouse_move_x = cursor->cursor->x - e->ref_lx;
64 double mouse_move_y = cursor->cursor->y - e->ref_ly;
65
66 if (edge == WLR_EDGE_TOP || edge == WLR_EDGE_BOTTOM) {
67 mouse_move_x = 0;
68 }
69 if (edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT) {
70 mouse_move_y = 0;
71 }
72
73 double grow_width = edge & WLR_EDGE_LEFT ? -mouse_move_x : mouse_move_x;
74 double grow_height = edge & WLR_EDGE_TOP ? -mouse_move_y : mouse_move_y;
75
76 if (e->preserve_ratio) {
77 double x_multiplier = grow_width / e->ref_width;
78 double y_multiplier = grow_height / e->ref_height;
79 double max_multiplier = fmax(x_multiplier, y_multiplier);
80 grow_width = e->ref_width * max_multiplier;
81 grow_height = e->ref_height * max_multiplier;
82 }
83
84 // Determine new width/height, and accommodate for floating min/max values
85 double width = e->ref_width + grow_width;
86 double height = e->ref_height + grow_height;
87 int min_width, max_width, min_height, max_height;
88 calculate_floating_constraints(con, &min_width, &max_width,
89 &min_height, &max_height);
90 width = fmax(min_width, fmin(width, max_width));
91 height = fmax(min_height, fmin(height, max_height));
92
93 // Apply the view's min/max size
94 if (con->view) {
95 double view_min_width, view_max_width, view_min_height, view_max_height;
96 view_get_constraints(con->view, &view_min_width, &view_max_width,
97 &view_min_height, &view_max_height);
98 width = fmax(view_min_width, fmin(width, view_max_width));
99 height = fmax(view_min_height, fmin(height, view_max_height));
100 }
101
102 // Recalculate these, in case we hit a min/max limit
103 grow_width = width - e->ref_width;
104 grow_height = height - e->ref_height;
105
106 // Determine grow x/y values - these are relative to the container's x/y at
107 // the start of the resize operation.
108 double grow_x = 0, grow_y = 0;
109 if (edge & WLR_EDGE_LEFT) {
110 grow_x = -grow_width;
111 } else if (edge & WLR_EDGE_RIGHT) {
112 grow_x = 0;
113 } else {
114 grow_x = -grow_width / 2;
115 }
116 if (edge & WLR_EDGE_TOP) {
117 grow_y = -grow_height;
118 } else if (edge & WLR_EDGE_BOTTOM) {
119 grow_y = 0;
120 } else {
121 grow_y = -grow_height / 2;
122 }
123
124 // Determine the amounts we need to bump everything relative to the current
125 // size.
126 int relative_grow_width = width - con->width;
127 int relative_grow_height = height - con->height;
128 int relative_grow_x = (e->ref_con_lx + grow_x) - con->x;
129 int relative_grow_y = (e->ref_con_ly + grow_y) - con->y;
130
131 // Actually resize stuff
132 con->x += relative_grow_x;
133 con->y += relative_grow_y;
134 con->width += relative_grow_width;
135 con->height += relative_grow_height;
136
137 con->content_x += relative_grow_x;
138 con->content_y += relative_grow_y;
139 con->content_width += relative_grow_width;
140 con->content_height += relative_grow_height;
141
142 arrange_container(con);
143}
144
145static void handle_finish(struct sway_seat *seat) {
146 cursor_set_image(seat->cursor, "left_ptr", NULL);
147}
148
149static void handle_abort(struct sway_seat *seat) {
150 cursor_set_image(seat->cursor, "left_ptr", NULL);
151}
152
153static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
154 struct seatop_resize_floating_event *e = seat->seatop_data;
155 if (e->con == con) {
156 seatop_abort(seat);
157 }
158}
159
160static const struct sway_seatop_impl seatop_impl = {
161 .motion = handle_motion,
162 .finish = handle_finish,
163 .abort = handle_abort,
164 .unref = handle_unref,
165};
166
167void seatop_begin_resize_floating(struct sway_seat *seat,
168 struct sway_container *con, uint32_t button, enum wlr_edges edge) {
169 seatop_abort(seat);
170
171 struct seatop_resize_floating_event *e =
172 calloc(1, sizeof(struct seatop_resize_floating_event));
173 if (!e) {
174 return;
175 }
176 e->con = con;
177
178 struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat);
179 e->preserve_ratio = keyboard &&
180 (wlr_keyboard_get_modifiers(keyboard) & WLR_MODIFIER_SHIFT);
181
182 e->edge = edge == WLR_EDGE_NONE ? WLR_EDGE_BOTTOM | WLR_EDGE_RIGHT : edge;
183 e->ref_lx = seat->cursor->cursor->x;
184 e->ref_ly = seat->cursor->cursor->y;
185 e->ref_con_lx = con->x;
186 e->ref_con_ly = con->y;
187 e->ref_width = con->width;
188 e->ref_height = con->height;
189
190 seat->seatop_impl = &seatop_impl;
191 seat->seatop_data = e;
192 seat->seatop_button = button;
193
194 container_raise_floating(con);
195
196 const char *image = edge == WLR_EDGE_NONE ?
197 "se-resize" : wlr_xcursor_get_resize_name(edge);
198 cursor_set_image(seat->cursor, image, NULL);
199}
diff --git a/sway/input/seatop_resize_tiling.c b/sway/input/seatop_resize_tiling.c
new file mode 100644
index 00000000..30431f04
--- /dev/null
+++ b/sway/input/seatop_resize_tiling.c
@@ -0,0 +1,92 @@
1#define _POSIX_C_SOURCE 200809L
2#include <wlr/types/wlr_cursor.h>
3#include "sway/commands.h"
4#include "sway/input/cursor.h"
5#include "sway/input/seat.h"
6
7struct seatop_resize_tiling_event {
8 struct sway_container *con;
9 enum wlr_edges edge;
10 double ref_lx, ref_ly; // cursor's x/y at start of op
11 double ref_width, ref_height; // container's size at start of op
12 double ref_con_lx, ref_con_ly; // container's x/y at start of op
13};
14
15static void handle_motion(struct sway_seat *seat, uint32_t time_msec) {
16 struct seatop_resize_tiling_event *e = seat->seatop_data;
17 int amount_x = 0;
18 int amount_y = 0;
19 int moved_x = seat->cursor->cursor->x - e->ref_lx;
20 int moved_y = seat->cursor->cursor->y - e->ref_ly;
21 enum wlr_edges edge_x = WLR_EDGE_NONE;
22 enum wlr_edges edge_y = WLR_EDGE_NONE;
23 struct sway_container *con = e->con;
24
25 if (e->edge & WLR_EDGE_TOP) {
26 amount_y = (e->ref_height - moved_y) - con->height;
27 edge_y = WLR_EDGE_TOP;
28 } else if (e->edge & WLR_EDGE_BOTTOM) {
29 amount_y = (e->ref_height + moved_y) - con->height;
30 edge_y = WLR_EDGE_BOTTOM;
31 }
32 if (e->edge & WLR_EDGE_LEFT) {
33 amount_x = (e->ref_width - moved_x) - con->width;
34 edge_x = WLR_EDGE_LEFT;
35 } else if (e->edge & WLR_EDGE_RIGHT) {
36 amount_x = (e->ref_width + moved_x) - con->width;
37 edge_x = WLR_EDGE_RIGHT;
38 }
39
40 if (amount_x != 0) {
41 container_resize_tiled(e->con, edge_x, amount_x);
42 }
43 if (amount_y != 0) {
44 container_resize_tiled(e->con, edge_y, amount_y);
45 }
46}
47
48static void handle_finish(struct sway_seat *seat) {
49 cursor_set_image(seat->cursor, "left_ptr", NULL);
50}
51
52static void handle_abort(struct sway_seat *seat) {
53 cursor_set_image(seat->cursor, "left_ptr", NULL);
54}
55
56static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
57 struct seatop_resize_tiling_event *e = seat->seatop_data;
58 if (e->con == con) {
59 seatop_abort(seat);
60 }
61}
62
63static const struct sway_seatop_impl seatop_impl = {
64 .motion = handle_motion,
65 .finish = handle_finish,
66 .abort = handle_abort,
67 .unref = handle_unref,
68};
69
70void seatop_begin_resize_tiling(struct sway_seat *seat,
71 struct sway_container *con, uint32_t button, enum wlr_edges edge) {
72 seatop_abort(seat);
73
74 struct seatop_resize_tiling_event *e =
75 calloc(1, sizeof(struct seatop_resize_tiling_event));
76 if (!e) {
77 return;
78 }
79 e->con = con;
80 e->edge = edge;
81
82 e->ref_lx = seat->cursor->cursor->x;
83 e->ref_ly = seat->cursor->cursor->y;
84 e->ref_con_lx = con->x;
85 e->ref_con_ly = con->y;
86 e->ref_width = con->width;
87 e->ref_height = con->height;
88
89 seat->seatop_impl = &seatop_impl;
90 seat->seatop_data = e;
91 seat->seatop_button = button;
92}
diff --git a/sway/meson.build b/sway/meson.build
index 98676ce0..ff326f04 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -23,6 +23,11 @@ sway_sources = files(
23 23
24 'input/input-manager.c', 24 'input/input-manager.c',
25 'input/seat.c', 25 'input/seat.c',
26 'input/seatop_down.c',
27 'input/seatop_move_floating.c',
28 'input/seatop_move_tiling.c',
29 'input/seatop_resize_floating.c',
30 'input/seatop_resize_tiling.c',
26 'input/cursor.c', 31 'input/cursor.c',
27 'input/keyboard.c', 32 'input/keyboard.c',
28 33
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 99262356..d9c721f5 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -864,15 +864,7 @@ bool container_has_urgent_child(struct sway_container *container) {
864void container_end_mouse_operation(struct sway_container *container) { 864void container_end_mouse_operation(struct sway_container *container) {
865 struct sway_seat *seat; 865 struct sway_seat *seat;
866 wl_list_for_each(seat, &server.input->seats, link) { 866 wl_list_for_each(seat, &server.input->seats, link) {
867 if (seat->op_container == container) { 867 seatop_unref(seat, container);
868 seat->op_target_node = NULL; // ensure tiling move doesn't apply
869 seat_end_mouse_operation(seat);
870 }
871 // If the user is doing a tiling drag over this container,
872 // keep the operation active but unset the target container.
873 if (seat->op_target_node == &container->node) {
874 seat->op_target_node = NULL;
875 }
876 } 868 }
877} 869}
878 870
@@ -1384,3 +1376,16 @@ void container_update_marks_textures(struct sway_container *con) {
1384 &config->border_colors.urgent); 1376 &config->border_colors.urgent);
1385 container_damage_whole(con); 1377 container_damage_whole(con);
1386} 1378}
1379
1380void container_raise_floating(struct sway_container *con) {
1381 // Bring container to front by putting it at the end of the floating list.
1382 struct sway_container *floater = con;
1383 while (floater->parent) {
1384 floater = floater->parent;
1385 }
1386 if (container_is_floating(floater)) {
1387 list_move_to_end(floater->workspace->floating, floater);
1388 node_set_dirty(&floater->workspace->node);
1389 }
1390}
1391