Implement resizing tiled containers via cursor

* The OP_RESIZE seat operation has been renamed to OP_RESIZE_FLOATING,
and OP_RESIZE_TILING has been introduced.
* Similar to the above, seat_begin_resize and handle_resize_motion have
been renamed and tiling variants introduced.
* resize.c's resize_tiled has to be used, so container_resize_tiled has
been introduced in resize.c to allow external code to call it.
This commit is contained in:
Ryan Dwyer 2018-08-10 14:10:09 +10:00
parent 146cc0a441
commit b4a0363d17
10 changed files with 173 additions and 34 deletions

View file

@ -77,6 +77,15 @@ int list_seq_find(list_t *list, int compare(const void *item, const void *data),
return -1; return -1;
} }
int list_find(list_t *list, void *item) {
for (int i = 0; i < list->length; i++) {
if (list->items[i] == item) {
return i;
}
}
return -1;
}
void list_swap(list_t *list, int src, int dest) { void list_swap(list_t *list, int src, int dest) {
void *tmp = list->items[src]; void *tmp = list->items[src];
list->items[src] = list->items[dest]; list->items[src] = list->items[dest];

View file

@ -20,6 +20,7 @@ void list_qsort(list_t *list, int compare(const void *left, const void *right));
// Return index for first item in list that returns 0 for given compare // Return index for first item in list that returns 0 for given compare
// function or -1 if none matches. // function or -1 if none matches.
int list_seq_find(list_t *list, int compare(const void *item, const void *cmp_to), const void *cmp_to); int list_seq_find(list_t *list, int compare(const void *item, const void *cmp_to), const void *cmp_to);
int list_find(list_t *list, void *item);
// stable sort since qsort is not guaranteed to be stable // stable sort since qsort is not guaranteed to be stable
void list_stable_sort(list_t *list, int compare(const void *a, const void *b)); void list_stable_sort(list_t *list, int compare(const void *a, const void *b));
// swap two elements in a list // swap two elements in a list

View file

@ -1,6 +1,7 @@
#ifndef _SWAY_COMMANDS_H #ifndef _SWAY_COMMANDS_H
#define _SWAY_COMMANDS_H #define _SWAY_COMMANDS_H
#include <wlr/util/edges.h>
#include "config.h" #include "config.h"
typedef struct cmd_results *sway_cmd(int argc, char **argv); typedef struct cmd_results *sway_cmd(int argc, char **argv);
@ -84,6 +85,12 @@ char *cmd_results_to_json(struct cmd_results *results);
struct cmd_results *add_color(const char *name, struct cmd_results *add_color(const char *name,
char *buffer, const char *color); char *buffer, const char *color);
/**
* TODO: Move this function and its dependent functions to container.c.
*/
void container_resize_tiled(struct sway_container *parent, enum wlr_edges edge,
int amount);
sway_cmd cmd_assign; sway_cmd cmd_assign;
sway_cmd cmd_bar; sway_cmd cmd_bar;
sway_cmd cmd_bindcode; sway_cmd cmd_bindcode;

View file

@ -57,7 +57,8 @@ struct sway_seat {
enum { enum {
OP_NONE, OP_NONE,
OP_MOVE, OP_MOVE,
OP_RESIZE, OP_RESIZE_FLOATING,
OP_RESIZE_TILING,
} operation; } operation;
struct sway_container *op_container; struct sway_container *op_container;
@ -159,8 +160,11 @@ void drag_icon_update_position(struct sway_drag_icon *icon);
void seat_begin_move(struct sway_seat *seat, struct sway_container *con, void seat_begin_move(struct sway_seat *seat, struct sway_container *con,
uint32_t button); uint32_t button);
void seat_begin_resize(struct sway_seat *seat, struct sway_container *con, void seat_begin_resize_floating(struct sway_seat *seat,
uint32_t button, enum wlr_edges edge); struct sway_container *con, uint32_t button, enum wlr_edges edge);
void seat_begin_resize_tiling(struct sway_seat *seat,
struct sway_container *con, uint32_t button, enum wlr_edges edge);
void seat_end_mouse_operation(struct sway_seat *seat); void seat_end_mouse_operation(struct sway_seat *seat);

View file

@ -158,8 +158,8 @@ static int parallel_size(struct sway_container *c, enum resize_axis a) {
return normalize_axis(a) == RESIZE_AXIS_HORIZONTAL ? c->width : c->height; return normalize_axis(a) == RESIZE_AXIS_HORIZONTAL ? c->width : c->height;
} }
static void resize_tiled(int amount, enum resize_axis axis) { static void resize_tiled(struct sway_container *parent, int amount,
struct sway_container *parent = config->handler_context.current_container; enum resize_axis axis) {
struct sway_container *focused = parent; struct sway_container *focused = parent;
if (!parent) { if (!parent) {
return; return;
@ -297,6 +297,28 @@ static void resize_tiled(int amount, enum resize_axis axis) {
arrange_windows(parent->parent); arrange_windows(parent->parent);
} }
void container_resize_tiled(struct sway_container *parent,
enum wlr_edges edge, int amount) {
enum resize_axis axis = RESIZE_AXIS_INVALID;
switch (edge) {
case WLR_EDGE_TOP:
axis = RESIZE_AXIS_UP;
break;
case WLR_EDGE_RIGHT:
axis = RESIZE_AXIS_RIGHT;
break;
case WLR_EDGE_BOTTOM:
axis = RESIZE_AXIS_DOWN;
break;
case WLR_EDGE_LEFT:
axis = RESIZE_AXIS_LEFT;
break;
case WLR_EDGE_NONE:
break;
}
resize_tiled(parent, amount, axis);
}
/** /**
* Implement `resize <grow|shrink>` for a floating container. * Implement `resize <grow|shrink>` for a floating container.
*/ */
@ -398,7 +420,7 @@ static struct cmd_results *resize_adjust_tiled(enum resize_axis axis,
} }
} }
resize_tiled(amount->amount, axis); resize_tiled(current, amount->amount, axis);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }
@ -421,7 +443,8 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con,
} }
} }
if (width->unit == RESIZE_UNIT_PX) { if (width->unit == RESIZE_UNIT_PX) {
resize_tiled(width->amount - con->width, RESIZE_AXIS_HORIZONTAL); resize_tiled(con, width->amount - con->width,
RESIZE_AXIS_HORIZONTAL);
} }
} }
@ -439,7 +462,8 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con,
} }
} }
if (height->unit == RESIZE_UNIT_PX) { if (height->unit == RESIZE_UNIT_PX) {
resize_tiled(height->amount - con->height, RESIZE_AXIS_VERTICAL); resize_tiled(con, height->amount - con->height,
RESIZE_AXIS_HORIZONTAL);
} }
} }

View file

@ -331,7 +331,8 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
struct wlr_xdg_toplevel_resize_event *e = data; struct wlr_xdg_toplevel_resize_event *e = data;
struct sway_seat *seat = e->seat->seat->data; struct sway_seat *seat = e->seat->seat->data;
if (e->serial == seat->last_button_serial) { if (e->serial == seat->last_button_serial) {
seat_begin_resize(seat, view->swayc, seat->last_button, e->edges); seat_begin_resize_floating(seat, view->swayc,
seat->last_button, e->edges);
} }
} }

View file

@ -327,7 +327,8 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
struct wlr_xdg_toplevel_v6_resize_event *e = data; struct wlr_xdg_toplevel_v6_resize_event *e = data;
struct sway_seat *seat = e->seat->seat->data; struct sway_seat *seat = e->seat->seat->data;
if (e->serial == seat->last_button_serial) { if (e->serial == seat->last_button_serial) {
seat_begin_resize(seat, view->swayc, seat->last_button, e->edges); seat_begin_resize_floating(seat, view->swayc,
seat->last_button, e->edges);
} }
} }

View file

@ -432,7 +432,7 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
} }
struct wlr_xwayland_resize_event *e = data; struct wlr_xwayland_resize_event *e = data;
struct sway_seat *seat = input_manager_current_seat(input_manager); struct sway_seat *seat = input_manager_current_seat(input_manager);
seat_begin_resize(seat, view->swayc, seat->last_button, e->edges); seat_begin_resize_floating(seat, view->swayc, seat->last_button, e->edges);
} }
static void handle_set_title(struct wl_listener *listener, void *data) { static void handle_set_title(struct wl_listener *listener, void *data) {

View file

@ -12,6 +12,7 @@
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "config.h" #include "config.h"
#include "sway/commands.h"
#include "sway/desktop.h" #include "sway/desktop.h"
#include "sway/desktop/transaction.h" #include "sway/desktop/transaction.h"
#include "sway/input/cursor.h" #include "sway/input/cursor.h"
@ -136,6 +137,44 @@ static struct sway_container *container_at_coords(
return output->swayc; return output->swayc;
} }
/**
* Determine if the edge of the given container is on the edge of the
* workspace/output.
*/
static bool edge_is_external(struct sway_container *cont, enum wlr_edges edge) {
enum sway_container_layout layout = L_NONE;
switch (edge) {
case WLR_EDGE_TOP:
case WLR_EDGE_BOTTOM:
layout = L_VERT;
break;
case WLR_EDGE_LEFT:
case WLR_EDGE_RIGHT:
layout = L_HORIZ;
break;
case WLR_EDGE_NONE:
sway_assert(false, "Never reached");
return false;
}
// Iterate the parents until we find one with the layout we want,
// then check if the child has siblings between it and the edge.
while (cont->type != C_OUTPUT) {
if (cont->parent->layout == layout) {
int index = list_find(cont->parent->children, cont);
if (index > 0 && (edge == WLR_EDGE_LEFT || edge == WLR_EDGE_TOP)) {
return false;
}
if (index < cont->parent->children->length - 1 &&
(edge == WLR_EDGE_RIGHT || edge == WLR_EDGE_BOTTOM)) {
return false;
}
}
cont = cont->parent;
}
return true;
}
static enum wlr_edges find_resize_edge(struct sway_container *cont, static enum wlr_edges find_resize_edge(struct sway_container *cont,
struct sway_cursor *cursor) { struct sway_cursor *cursor) {
if (cont->type != C_VIEW) { if (cont->type != C_VIEW) {
@ -159,6 +198,11 @@ static enum wlr_edges find_resize_edge(struct sway_container *cont,
if (cursor->cursor->y >= cont->y + cont->height - view->border_thickness) { if (cursor->cursor->y >= cont->y + cont->height - view->border_thickness) {
edge |= WLR_EDGE_BOTTOM; edge |= WLR_EDGE_BOTTOM;
} }
if (edge && !container_is_floating(cont) && edge_is_external(cont, edge)) {
return WLR_EDGE_NONE;
}
return edge; return edge;
} }
@ -209,7 +253,7 @@ static void calculate_floating_constraints(struct sway_container *con,
} }
} }
static void handle_resize_motion(struct sway_seat *seat, static void handle_resize_floating_motion(struct sway_seat *seat,
struct sway_cursor *cursor) { struct sway_cursor *cursor) {
struct sway_container *con = seat->op_container; struct sway_container *con = seat->op_container;
enum wlr_edges edge = seat->op_resize_edge; enum wlr_edges edge = seat->op_resize_edge;
@ -301,6 +345,31 @@ static void handle_resize_motion(struct sway_seat *seat,
arrange_windows(con); arrange_windows(con);
} }
static void handle_resize_tiling_motion(struct sway_seat *seat,
struct sway_cursor *cursor) {
int amount = 0;
int moved_x = cursor->cursor->x - seat->op_ref_lx;
int moved_y = cursor->cursor->y - seat->op_ref_ly;
struct sway_container *con = seat->op_container;
switch (seat->op_resize_edge) {
case WLR_EDGE_TOP:
amount = (seat->op_ref_height - moved_y) - con->height;
break;
case WLR_EDGE_BOTTOM:
amount = (seat->op_ref_height + moved_y) - con->height;
break;
case WLR_EDGE_LEFT:
amount = (seat->op_ref_width - moved_x) - con->width;
break;
case WLR_EDGE_RIGHT:
amount = (seat->op_ref_width + moved_x) - con->width;
break;
case WLR_EDGE_NONE:
break;
}
container_resize_tiled(seat->op_container, seat->op_resize_edge, amount);
}
void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec, void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec,
bool allow_refocusing) { bool allow_refocusing) {
if (time_msec == 0) { if (time_msec == 0) {
@ -310,10 +379,18 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec,
struct sway_seat *seat = cursor->seat; struct sway_seat *seat = cursor->seat;
if (seat->operation != OP_NONE) { if (seat->operation != OP_NONE) {
if (seat->operation == OP_MOVE) { switch (seat->operation) {
case OP_MOVE:
handle_move_motion(seat, cursor); handle_move_motion(seat, cursor);
} else { break;
handle_resize_motion(seat, cursor); case OP_RESIZE_FLOATING:
handle_resize_floating_motion(seat, cursor);
break;
case OP_RESIZE_TILING:
handle_resize_tiling_motion(seat, cursor);
break;
case OP_NONE:
break;
} }
cursor->previous.x = cursor->cursor->x; cursor->previous.x = cursor->cursor->x;
cursor->previous.y = cursor->cursor->y; cursor->previous.y = cursor->cursor->y;
@ -375,8 +452,8 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec,
if (client != cursor->image_client) { if (client != cursor->image_client) {
cursor_set_image(cursor, "left_ptr", client); cursor_set_image(cursor, "left_ptr", client);
} }
} else if (c && container_is_floating(c)) { } else if (c) {
// Try a floating container's resize edge // Try a container's resize edge
enum wlr_edges edge = find_resize_edge(c, cursor); enum wlr_edges edge = find_resize_edge(c, cursor);
const char *image = edge == WLR_EDGE_NONE ? const char *image = edge == WLR_EDGE_NONE ?
"left_ptr" : wlr_xcursor_get_resize_name(edge); "left_ptr" : wlr_xcursor_get_resize_name(edge);
@ -467,7 +544,7 @@ static void dispatch_cursor_button_floating(struct sway_cursor *cursor,
edge |= cursor->cursor->y > floater->y + floater->height / 2 ? edge |= cursor->cursor->y > floater->y + floater->height / 2 ?
WLR_EDGE_BOTTOM : WLR_EDGE_TOP; WLR_EDGE_BOTTOM : WLR_EDGE_TOP;
} }
seat_begin_resize(seat, floater, button, edge); seat_begin_resize_floating(seat, floater, button, edge);
return; return;
} }
@ -592,6 +669,8 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
// TODO: do we want to pass on the event? // TODO: do we want to pass on the event?
} }
enum wlr_edges edge = cont ? find_resize_edge(cont, cursor) : WLR_EDGE_NONE;
if (surface && wlr_surface_is_layer_surface(surface)) { if (surface && wlr_surface_is_layer_surface(surface)) {
struct wlr_layer_surface *layer = struct wlr_layer_surface *layer =
wlr_layer_surface_from_wlr_surface(surface); wlr_layer_surface_from_wlr_surface(surface);
@ -599,6 +678,10 @@ void dispatch_cursor_button(struct sway_cursor *cursor,
seat_set_focus_layer(cursor->seat, layer); seat_set_focus_layer(cursor->seat, layer);
} }
seat_pointer_notify_button(cursor->seat, time_msec, button, state); seat_pointer_notify_button(cursor->seat, time_msec, button, state);
} else if (edge && button == BTN_LEFT &&
!container_is_floating(cont)) {
seat_set_focus(cursor->seat, cont);
seat_begin_resize_tiling(cursor->seat, cont, BTN_LEFT, edge);
} else if (cont && container_is_floating_or_child(cont)) { } else if (cont && container_is_floating_or_child(cont)) {
dispatch_cursor_button_floating(cursor, time_msec, button, state, dispatch_cursor_button_floating(cursor, time_msec, button, state,
surface, sx, sy, cont); surface, sx, sy, cont);

View file

@ -942,14 +942,14 @@ void seat_begin_move(struct sway_seat *seat, struct sway_container *con,
cursor_set_image(seat->cursor, "grab", NULL); cursor_set_image(seat->cursor, "grab", NULL);
} }
void seat_begin_resize(struct sway_seat *seat, struct sway_container *con, void seat_begin_resize_floating(struct sway_seat *seat,
uint32_t button, enum wlr_edges edge) { struct sway_container *con, uint32_t button, enum wlr_edges edge) {
if (!seat->cursor) { if (!seat->cursor) {
wlr_log(WLR_DEBUG, "Ignoring resize request due to no cursor device"); wlr_log(WLR_DEBUG, "Ignoring resize request due to no cursor device");
return; return;
} }
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat); struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat);
seat->operation = OP_RESIZE; seat->operation = OP_RESIZE_FLOATING;
seat->op_container = con; seat->op_container = con;
seat->op_resize_preserve_ratio = keyboard && seat->op_resize_preserve_ratio = keyboard &&
(wlr_keyboard_get_modifiers(keyboard) & WLR_MODIFIER_SHIFT); (wlr_keyboard_get_modifiers(keyboard) & WLR_MODIFIER_SHIFT);
@ -968,20 +968,29 @@ void seat_begin_resize(struct sway_seat *seat, struct sway_container *con,
cursor_set_image(seat->cursor, image, NULL); cursor_set_image(seat->cursor, image, NULL);
} }
void seat_begin_resize_tiling(struct sway_seat *seat,
struct sway_container *con, uint32_t button, enum wlr_edges edge) {
seat->operation = OP_RESIZE_TILING;
seat->op_container = con;
seat->op_resize_edge = edge;
seat->op_button = button;
seat->op_ref_lx = seat->cursor->cursor->x;
seat->op_ref_ly = seat->cursor->cursor->y;
seat->op_ref_con_lx = con->x;
seat->op_ref_con_ly = con->y;
seat->op_ref_width = con->width;
seat->op_ref_height = con->height;
const char *image = wlr_xcursor_get_resize_name(edge);
cursor_set_image(seat->cursor, image, NULL);
}
void seat_end_mouse_operation(struct sway_seat *seat) { void seat_end_mouse_operation(struct sway_seat *seat) {
switch (seat->operation) { if (seat->operation == OP_MOVE) {
case OP_MOVE: // We "move" the container to its own location so it discovers its
{ // output again.
// We "move" the container to its own location so it discovers its struct sway_container *con = seat->op_container;
// output again. container_floating_move_to(con, con->x, con->y);
struct sway_container *con = seat->op_container;
container_floating_move_to(con, con->x, con->y);
}
case OP_RESIZE:
// Don't need to do anything here.
break;
case OP_NONE:
break;
} }
seat->operation = OP_NONE; seat->operation = OP_NONE;
seat->op_container = NULL; seat->op_container = NULL;