Merge pull request #1668 from acrisci/split-containers

Basic split containers
This commit is contained in:
Drew DeVault 2018-04-02 20:44:56 -04:00 committed by GitHub
commit d3eaf6468c
Failed to generate hash of commit
11 changed files with 376 additions and 95 deletions

View file

@ -72,7 +72,7 @@ struct sway_container *seat_get_focus_inactive(struct sway_seat *seat,
struct sway_container *container);
struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
enum sway_container_type type);
struct sway_container *container, enum sway_container_type type);
void seat_apply_config(struct sway_seat *seat, struct seat_config *seat_config);

View file

@ -91,37 +91,61 @@ struct sway_container {
} events;
};
// TODO make private and use the container-specific create functions
struct sway_container *container_create(enum sway_container_type type);
const char *container_type_to_str(enum sway_container_type type);
// TODO only one container create function and pass the type?
struct sway_container *container_output_create(
struct sway_output *sway_output);
struct sway_container *container_workspace_create(
struct sway_container *output, const char *name);
/**
* Create a new container container. A container container can be a a child of
* a workspace container or another container container.
*/
struct sway_container *container_container_create();
/**
* Create a new output. Outputs are children of the root container and have no
* order in the tree structure.
*/
struct sway_container *container_output_create(struct sway_output *sway_output);
/**
* Create a new workspace container. Workspaces are children of an output
* container and are ordered alphabetically by name.
*/
struct sway_container *container_workspace_create(struct sway_container *output, const char *name);
/*
* Create a new view container. A view can be a child of a workspace container
* or a container container and are rendered in the order and structure of
* how they are attached to the tree.
*/
// TODO view containers should be created in a detached state.
struct sway_container *container_view_create(
struct sway_container *sibling, struct sway_view *sway_view);
struct sway_container *container_output_destroy(struct sway_container *output);
// TODO don't return the parent on destroy
struct sway_container *container_destroy(struct sway_container *container);
struct sway_container *container_workspace_destroy(
struct sway_container *workspace);
struct sway_container *container_view_destroy(struct sway_container *view);
struct sway_container *container_destroy(struct sway_container *cont);
struct sway_container *container_workspace_destroy(struct sway_container *container);
struct sway_container *container_output_destroy(struct sway_container *container);
struct sway_container *container_view_destroy(struct sway_container *container);
// TODO move to layout.c
struct sway_container *container_set_layout(struct sway_container *container,
enum sway_container_layout layout);
// TODO rename to container_descendants_for_each()
void container_descendants(struct sway_container *root,
enum sway_container_type type,
void (*func)(struct sway_container *item, void *data), void *data);
/**
* Finds a container based on test criteria. Returns the first container that
* passes the test.
* Search a container's descendants a container based on test criteria. Returns
* the first container that passes the test.
*/
struct sway_container *container_find(struct sway_container *container,
bool (*test)(struct sway_container *view, void *data), void *data);
@ -129,18 +153,21 @@ struct sway_container *container_find(struct sway_container *container,
/**
* Finds a parent container with the given struct sway_containerype.
*/
// TODO rename to container_parent_of_type()
struct sway_container *container_parent(struct sway_container *container,
enum sway_container_type type);
/**
* Find a container at the given coordinates.
* Find a container at the given coordinates. Returns the the surface and
* surface-local coordinates of the given layout coordinates if the container
* is a view and the view contains a surface at those coordinates.
*/
struct sway_container *container_at(struct sway_container *parent,
struct sway_container *container_at(struct sway_container *container,
double lx, double ly, struct wlr_surface **surface,
double *sx, double *sy);
/**
* Apply the function for each child of the container breadth first.
* Apply the function for each descendant of the container breadth first.
*/
void container_for_each_descendant_bfs(struct sway_container *container,
void (*f)(struct sway_container *container, void *data), void *data);
@ -151,7 +178,16 @@ void container_for_each_descendant_bfs(struct sway_container *container,
void container_for_each_descendant_dfs(struct sway_container *container,
void (*f)(struct sway_container *container, void *data), void *data);
bool container_has_anscestor(struct sway_container *descendant,
/**
* Returns true if the given container is an ancestor of this container.
*/
bool container_has_anscestor(struct sway_container *container,
struct sway_container *anscestor);
/**
* Returns true if the given container is a child descendant of this container.
*/
bool container_has_child(struct sway_container *con,
struct sway_container *child);
#endif

View file

@ -29,31 +29,43 @@ struct sway_root {
void layout_init(void);
// TODO move to tree.h
void container_add_child(struct sway_container *parent,
struct sway_container *child);
// TODO move to tree.h
struct sway_container *container_add_sibling(struct sway_container *parent,
struct sway_container *child);
// TODO move to tree.h
struct sway_container *container_remove_child(struct sway_container *child);
// TODO PRIVATE in tree.h
struct sway_container *container_reap_empty(struct sway_container *container);
// TODO move to tree.h
void container_move_to(struct sway_container* container,
struct sway_container* destination);
void container_move(struct sway_container *container,
enum movement_direction dir, int move_amt);
// TODO move to output.c
enum sway_container_layout container_get_default_layout(
struct sway_container *output);
// TODO move to output.c
void container_sort_workspaces(struct sway_container *output);
void arrange_windows(struct sway_container *container,
double width, double height);
// TODO move to container.h
struct sway_container *container_get_in_direction(struct sway_container
*container, struct sway_seat *seat, enum movement_direction dir);
// TODO move to tree.h
struct sway_container *container_split(struct sway_container *child,
enum sway_container_layout layout);
#endif

View file

@ -164,6 +164,10 @@ static struct cmd_handler command_handlers[] = {
{ "layout", cmd_layout },
{ "move", cmd_move },
{ "reload", cmd_reload },
{ "split", cmd_split },
{ "splith", cmd_splith },
{ "splitt", cmd_splitt },
{ "splitv", cmd_splitv },
};
static int handler_compare(const void *_a, const void *_b) {

View file

@ -3,21 +3,28 @@
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/tree/view.h"
#include "sway/tree/container.h"
#include "sway/commands.h"
struct cmd_results *cmd_kill(int argc, char **argv) {
enum sway_container_type type = config->handler_context.current_container->type;
if (type != C_VIEW && type != C_CONTAINER) {
struct sway_container *con =
config->handler_context.current_container;
switch (con->type) {
case C_ROOT:
case C_OUTPUT:
case C_WORKSPACE:
case C_TYPES:
return cmd_results_new(CMD_INVALID, NULL,
"Can only kill views and containers with this command");
}
// TODO close arbitrary containers without a view
struct sway_view *view =
config->handler_context.current_container->sway_view;
if (view) {
view_close(view);
break;
case C_CONTAINER:
con = container_destroy(con);
arrange_windows(con, -1, -1);
break;
case C_VIEW:
view_close(con->sway_view);
break;
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);

76
sway/commands/split.c Normal file
View file

@ -0,0 +1,76 @@
#include <string.h>
#include <strings.h>
#include "sway/commands.h"
#include "sway/tree/container.h"
#include "sway/tree/layout.h"
#include "sway/tree/view.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "log.h"
static struct cmd_results *do_split(int layout) {
struct sway_container *con = config->handler_context.current_container;
struct sway_container *parent = container_split(con, layout);
arrange_windows(parent, -1, -1);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
struct cmd_results *cmd_split(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
do_split(L_VERT);
} else if (strcasecmp(argv[0], "h") == 0 ||
strcasecmp(argv[0], "horizontal") == 0) {
do_split(L_HORIZ);
} else if (strcasecmp(argv[0], "t") == 0 ||
strcasecmp(argv[0], "toggle") == 0) {
struct sway_container *focused =
config->handler_context.current_container;
if (focused->parent->layout == L_VERT) {
do_split(L_HORIZ);
} else {
do_split(L_VERT);
}
} else {
error = cmd_results_new(CMD_FAILURE, "split",
"Invalid split command (expected either horizontal or vertical).");
return error;
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
struct cmd_results *cmd_splitv(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "splitv", EXPECTED_EQUAL_TO, 0))) {
return error;
}
return do_split(L_VERT);
}
struct cmd_results *cmd_splith(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "splitv", EXPECTED_EQUAL_TO, 0))) {
return error;
}
return do_split(L_HORIZ);
}
struct cmd_results *cmd_splitt(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "splitv", EXPECTED_EQUAL_TO, 0))) {
return error;
}
struct sway_container *con = config->handler_context.current_container;
if (con->parent->layout == L_VERT) {
return do_split(L_HORIZ);
} else {
return do_split(L_VERT);
}
}

View file

@ -1,4 +1,5 @@
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_xcursor_manager.h>
@ -35,31 +36,89 @@ void seat_destroy(struct sway_seat *seat) {
wlr_seat_destroy(seat->wlr_seat);
}
static struct sway_seat_container *seat_container_from_container(
struct sway_seat *seat, struct sway_container *con);
static void seat_container_destroy(struct sway_seat_container *seat_con) {
struct sway_container *con = seat_con->container;
struct sway_container *child = NULL;
if (con->children != NULL) {
for (int i = 0; i < con->children->length; ++i) {
child = con->children->items[i];
struct sway_seat_container *seat_child =
seat_container_from_container(seat_con->seat, child);
seat_container_destroy(seat_child);
}
}
wl_list_remove(&seat_con->destroy.link);
wl_list_remove(&seat_con->link);
free(seat_con);
}
static void seat_send_focus(struct sway_seat *seat,
struct sway_container *con) {
if (con->type != C_VIEW) {
return;
}
struct sway_view *view = con->sway_view;
if (view->type == SWAY_XWAYLAND_VIEW) {
struct wlr_xwayland *xwayland =
seat->input->server->xwayland;
wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
}
view_set_activated(view, true);
struct wlr_keyboard *keyboard =
wlr_seat_get_keyboard(seat->wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
view->surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(
seat->wlr_seat, view->surface, NULL, 0, NULL);
}
}
static void handle_seat_container_destroy(struct wl_listener *listener,
void *data) {
struct sway_seat_container *seat_con =
wl_container_of(listener, seat_con, destroy);
struct sway_seat *seat = seat_con->seat;
struct sway_container *con = seat_con->container;
struct sway_container *parent = con->parent;
struct sway_container *focus = seat_get_focus(seat);
bool is_focus = (seat_get_focus(seat) == con);
bool set_focus =
focus != NULL &&
(focus == con || container_has_child(con, focus)) &&
con->type != C_WORKSPACE;
wl_list_remove(&seat_con->link);
seat_container_destroy(seat_con);
if (is_focus) {
// pick next focus
seat_set_focus(seat, NULL);
struct sway_container *next =
seat_get_focus_inactive(seat, con->parent);
if (next == NULL) {
next = con->parent;
if (set_focus) {
struct sway_container *next_focus = NULL;
while (next_focus == NULL) {
next_focus = seat_get_focus_by_type(seat, parent, C_VIEW);
if (next_focus == NULL && parent->type == C_WORKSPACE) {
next_focus = parent;
break;
}
parent = parent->parent;
}
// the structure change might have caused it to move up to the top of
// the focus stack without sending focus notifications to the view
if (seat_get_focus(seat) == next_focus) {
seat_send_focus(seat, next_focus);
} else {
seat_set_focus(seat, next_focus);
}
seat_set_focus(seat, next);
}
wl_list_remove(&seat_con->destroy.link);
free(seat_con);
}
static struct sway_seat_container *seat_container_from_container(
@ -310,23 +369,7 @@ void seat_set_focus_warp(struct sway_seat *seat,
wl_list_insert(&seat->focus_stack, &seat_con->link);
if (container->type == C_VIEW) {
struct sway_view *view = container->sway_view;
view_set_activated(view, true);
if (view->type == SWAY_XWAYLAND_VIEW) {
struct wlr_xwayland *xwayland =
seat->input->server->xwayland;
wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
}
struct wlr_keyboard *keyboard =
wlr_seat_get_keyboard(seat->wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
view->surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(
seat->wlr_seat, view->surface, NULL, 0, NULL);
}
seat_send_focus(seat, container);
}
}
@ -378,17 +421,31 @@ void seat_set_focus(struct sway_seat *seat,
struct sway_container *seat_get_focus_inactive(struct sway_seat *seat,
struct sway_container *container) {
return seat_get_focus_by_type(seat, container, C_TYPES);
}
struct sway_container *sway_seat_get_focus(struct sway_seat *seat) {
if (!seat->has_focus) {
return NULL;
}
return seat_get_focus_inactive(seat, &root_container);
}
struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
struct sway_container *container, enum sway_container_type type) {
struct sway_seat_container *current = NULL;
struct sway_container *parent = NULL;
wl_list_for_each(current, &seat->focus_stack, link) {
parent = current->container->parent;
if (current->container == container) {
if (current->container == container &&
(type == C_TYPES || container->type == type)) {
return current->container;
}
while (parent) {
if (parent == container) {
if (parent == container && (type == C_TYPES ||
current->container->type == type)) {
return current->container;
}
parent = parent->parent;
@ -405,17 +462,6 @@ struct sway_container *seat_get_focus(struct sway_seat *seat) {
return seat_get_focus_inactive(seat, &root_container);
}
struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
enum sway_container_type type) {
struct sway_container *focus =
seat_get_focus_inactive(seat, &root_container);
if (focus->type == type) {
return focus;
}
return container_parent(focus, type);
}
void seat_apply_config(struct sway_seat *seat,
struct seat_config *seat_config) {
struct sway_seat_device *seat_device = NULL;

View file

@ -118,7 +118,7 @@ void run_as_ipc_client(char *command, char *socket_path) {
static void log_env() {
const char *log_vars[] = {
"PATH",
"LD_LOAD_PATH",
"LD_LIBRARY_PATH",
"LD_PRELOAD_PATH",
"LD_LIBRARY_PATH",
"SWAY_CURSOR_THEME",

View file

@ -19,6 +19,7 @@ sway_sources = files(
'commands/input.c',
'commands/layout.c',
'commands/mode.c',
'commands/split.c',
'commands/move.c',
'commands/seat.c',
'commands/seat/attach.c',

View file

@ -55,7 +55,7 @@ static void notify_new_container(struct sway_container *container) {
ipc_event_window(container, "new");
}
static struct sway_container *container_create(enum sway_container_type type) {
struct sway_container *container_create(enum sway_container_type type) {
// next id starts at 1 because 0 is assigned to root_container in layout.c
static size_t next_id = 1;
struct sway_container *c = calloc(1, sizeof(struct sway_container));
@ -76,7 +76,7 @@ static struct sway_container *container_create(enum sway_container_type type) {
return c;
}
struct sway_container *container_destroy(struct sway_container *cont) {
static struct sway_container *_container_destroy(struct sway_container *cont) {
if (cont == NULL) {
return NULL;
}
@ -84,13 +84,14 @@ struct sway_container *container_destroy(struct sway_container *cont) {
wl_signal_emit(&cont->events.destroy, cont);
struct sway_container *parent = cont->parent;
if (cont->children) {
if (cont->children != NULL) {
// remove children until there are no more, container_destroy calls
// container_remove_child, which removes child from this container
while (cont->children->length) {
container_destroy(cont->children->items[0]);
while (cont->children != NULL && cont->children->length != 0) {
struct sway_container *child = cont->children->items[0];
container_remove_child(child);
container_destroy(child);
}
list_free(cont->children);
}
if (cont->marks) {
list_foreach(cont->marks, free);
@ -102,10 +103,19 @@ struct sway_container *container_destroy(struct sway_container *cont) {
if (cont->name) {
free(cont->name);
}
list_free(cont->children);
cont->children = NULL;
free(cont);
return parent;
}
struct sway_container *container_destroy(struct sway_container *cont) {
struct sway_container *parent = _container_destroy(cont);
parent = container_reap_empty(parent);
arrange_windows(&root_container, -1, -1);
return parent;
}
struct sway_container *container_output_create(
struct sway_output *sway_output) {
struct wlr_box size;
@ -413,3 +423,17 @@ bool container_has_anscestor(struct sway_container *descendant,
}
return false;
}
static bool find_child_func(struct sway_container *con, void *data) {
struct sway_container *child = data;
return con == child;
}
bool container_has_child(struct sway_container *con,
struct sway_container *child) {
if (child == NULL || child->type == C_VIEW ||
child->children->length == 0) {
return false;
}
return container_find(con, find_child_func, child);
}

View file

@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
@ -103,7 +104,7 @@ void container_add_child(struct sway_container *parent,
}
struct sway_container *container_reap_empty(struct sway_container *container) {
if (!sway_assert(container, "reaping null container")) {
if (container == NULL) {
return NULL;
}
wlr_log(L_DEBUG, "Reaping %p %s '%s'", container,
@ -137,7 +138,7 @@ struct sway_container *container_remove_child(struct sway_container *child) {
}
}
child->parent = NULL;
return container_reap_empty(parent);
return parent;
}
void container_move_to(struct sway_container *container,
@ -348,8 +349,14 @@ static void apply_horiz_layout(struct sway_container *container,
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, width, scale);
view_configure(child->sway_view, child_x, y, child->width,
child->height);
if (child->type == C_VIEW) {
view_configure(child->sway_view, child_x, y, child->width,
child->height);
} else {
child->x = child_x;
child->y = y;
}
if (i == end - 1) {
double remaining_width = x + width - child_x;
@ -400,8 +407,13 @@ void apply_vert_layout(struct sway_container *container,
wlr_log(L_DEBUG,
"Calculating arrangement for %p:%d (will scale %f by %f)",
child, child->type, height, scale);
view_configure(child->sway_view, x, child_y, child->width,
child->height);
if (child->type == C_VIEW) {
view_configure(child->sway_view, x, child_y, child->width,
child->height);
} else {
child->x = x;
child->y = child_y;
}
if (i == end - 1) {
double remaining_height = y + height - child_y;
@ -533,9 +545,9 @@ static struct sway_container *sway_output_from_wlr(struct wlr_output *output) {
return NULL;
}
static struct sway_container *get_swayc_in_direction_under(
struct sway_container *container, enum movement_direction dir,
struct sway_seat *seat, struct sway_container *limit) {
struct sway_container *container_get_in_direction(
struct sway_container *container, struct sway_seat *seat,
enum movement_direction dir) {
if (dir == MOVE_CHILD) {
return seat_get_focus_inactive(seat, container);
}
@ -567,7 +579,6 @@ static struct sway_container *get_swayc_in_direction_under(
struct sway_container *wrap_candidate = NULL;
while (true) {
// Test if we can even make a difference here
bool can_move = false;
int desired;
int idx = index_child(container);
@ -597,7 +608,7 @@ static struct sway_container *get_swayc_in_direction_under(
}
if (next->children && next->children->length) {
// TODO consider floating children as well
return seat_get_focus_inactive(seat, next);
return seat_get_focus_by_type(seat, next, C_VIEW);
} else {
return next;
}
@ -627,21 +638,23 @@ static struct sway_container *get_swayc_in_direction_under(
wrap_candidate = parent->children->items[0];
}
if (config->force_focus_wrapping) {
return wrap_candidate;
return seat_get_focus_by_type(seat,
wrap_candidate, C_VIEW);
}
}
} else {
wlr_log(L_DEBUG,
"cont %d-%p dir %i sibling %d: %p", idx,
container, dir, desired, parent->children->items[desired]);
return parent->children->items[desired];
return seat_get_focus_by_type(seat,
parent->children->items[desired], C_VIEW);
}
}
if (!can_move) {
container = parent;
parent = parent->parent;
if (!parent || container == limit) {
if (!parent) {
// wrapping is the last chance
return wrap_candidate;
}
@ -649,8 +662,70 @@ static struct sway_container *get_swayc_in_direction_under(
}
}
struct sway_container *container_get_in_direction(
struct sway_container *container, struct sway_seat *seat,
enum movement_direction dir) {
return get_swayc_in_direction_under(container, dir, seat, NULL);
struct sway_container *container_replace_child(struct sway_container *child,
struct sway_container *new_child) {
struct sway_container *parent = child->parent;
if (parent == NULL) {
return NULL;
}
int i = index_child(child);
// TODO floating
parent->children->items[i] = new_child;
new_child->parent = parent;
child->parent = NULL;
// Set geometry for new child
new_child->x = child->x;
new_child->y = child->y;
new_child->width = child->width;
new_child->height = child->height;
// reset geometry for child
child->width = 0;
child->height = 0;
return parent;
}
struct sway_container *container_split(struct sway_container *child,
enum sway_container_layout layout) {
// TODO floating: cannot split a floating container
if (!sway_assert(child, "child cannot be null")) {
return NULL;
}
struct sway_container *cont = container_create(C_CONTAINER);
wlr_log(L_DEBUG, "creating container %p around %p", cont, child);
cont->prev_layout = L_NONE;
cont->width = child->width;
cont->height = child->height;
cont->x = child->x;
cont->y = child->y;
if (child->type == C_WORKSPACE) {
struct sway_seat *seat = input_manager_get_default_seat(input_manager);
struct sway_container *workspace = child;
bool set_focus = (seat_get_focus(seat) == workspace);
while (workspace->children->length) {
struct sway_container *ws_child = workspace->children->items[0];
container_remove_child(ws_child);
container_add_child(cont, ws_child);
}
container_add_child(workspace, cont);
container_set_layout(workspace, layout);
if (set_focus) {
seat_set_focus(seat, cont);
}
} else {
cont->layout = layout;
container_replace_child(child, cont);
container_add_child(cont, child);
}
return cont;
}