basic split containers

This commit is contained in:
Tony Crisci 2018-03-31 00:44:17 -04:00
parent 122b96abed
commit 7706d83160
10 changed files with 245 additions and 51 deletions

View file

@ -74,7 +74,7 @@ struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat,
struct sway_container *container);
struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat,
enum sway_container_type type);
struct sway_container *container, enum sway_container_type type);
void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config);

View file

@ -87,6 +87,8 @@ struct sway_container {
} events;
};
struct sway_container *container_create(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);

View file

@ -54,4 +54,7 @@ void arrange_windows(struct sway_container *container,
struct sway_container *container_get_in_direction(struct sway_container
*container, struct sway_seat *seat, enum movement_direction dir);
struct sway_container *container_split(struct sway_container *child,
enum sway_container_layout layout);
#endif

View file

@ -163,6 +163,10 @@ static struct cmd_handler command_handlers[] = {
{ "kill", cmd_kill },
{ "layout", cmd_layout },
{ "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,30 @@
#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:
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);
con = container_reap_empty(con);
arrange_windows(con, -1, -1);
break;
case C_VIEW:
view_close(con->sway_view);
break;
default:
break;
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);

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

@ -0,0 +1,107 @@
#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 argc, char **argv, int layout) {
char *name = layout == L_VERT ? "splitv" :
layout == L_HORIZ ? "splith" : "split";
struct cmd_results *error = NULL;
if (config->reading) {
return cmd_results_new(CMD_FAILURE, name,
"Can't be used in config file.");
}
if (!config->active) {
return cmd_results_new(CMD_FAILURE, name,
"Can only be used when sway is running.");
}
if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) {
return error;
}
struct sway_container *focused = config->handler_context.current_container;
// TODO floating: dont split
/* Case that focus is on an workspace with 0/1 children.change its layout */
if (focused->type == C_WORKSPACE && focused->children->length <= 1) {
wlr_log(L_DEBUG, "changing workspace layout");
container_set_layout(focused, layout);
} else if (focused->type != C_WORKSPACE &&
focused->parent->children->length == 1) {
/* Case of no siblings. change parent layout */
wlr_log(L_DEBUG, "changing container layout");
container_set_layout(focused->parent, layout);
} else {
// regular case where new split container is build around focused
// container or in case of workspace, container inherits its children
wlr_log(L_DEBUG,
"Adding new container around current focused container");
wlr_log(L_INFO, "FOCUSED SIZE: %.f %.f",
focused->width, focused->height);
struct sway_container *parent = container_split(focused, layout);
arrange_windows(parent, -1, -1);
}
// TODO borders: update borders
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
struct cmd_results *cmd_split(int argc, char **argv) {
struct cmd_results *error = NULL;
if (config->reading) {
return cmd_results_new(CMD_FAILURE, "split",
"Can't be used in config file.");
}
if (!config->active) {
return cmd_results_new(CMD_FAILURE, "split",
"Can only be used when sway is running.");
}
if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
_do_split(argc - 1, argv + 1, L_VERT);
} else if (strcasecmp(argv[0], "h") == 0 ||
strcasecmp(argv[0], "horizontal") == 0) {
_do_split(argc - 1, argv + 1, 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(argc - 1, argv + 1, L_HORIZ);
} else {
_do_split(argc - 1, argv + 1, 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) {
return _do_split(argc, argv, L_VERT);
}
struct cmd_results *cmd_splith(int argc, char **argv) {
return _do_split(argc, argv, L_HORIZ);
}
struct cmd_results *cmd_splitt(int argc, char **argv) {
struct sway_container *focused = config->handler_context.current_container;
if (focused->parent->layout == L_VERT) {
return _do_split(argc, argv, L_HORIZ);
} else {
return _do_split(argc, argv, 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>
@ -378,24 +379,7 @@ void sway_seat_set_focus(struct sway_seat *seat,
}
struct sway_container *sway_seat_get_focus_inactive(struct sway_seat *seat, struct sway_container *container) {
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) {
return current->container;
}
while (parent) {
if (parent == container) {
return current->container;
}
parent = parent->parent;
}
}
return NULL;
return sway_seat_get_focus_by_type(seat, container, C_TYPES);
}
struct sway_container *sway_seat_get_focus(struct sway_seat *seat) {
@ -406,13 +390,25 @@ struct sway_container *sway_seat_get_focus(struct sway_seat *seat) {
}
struct sway_container *sway_seat_get_focus_by_type(struct sway_seat *seat,
enum sway_container_type type) {
struct sway_container *focus = sway_seat_get_focus_inactive(seat, &root_container);
if (focus->type == type) {
return focus;
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) {
return current->container;
}
while (parent) {
if (parent == container && (type == C_TYPES || current->container->type == type)) {
return current->container;
}
parent = parent->parent;
}
}
return container_parent(focus, type);
return NULL;
}
void sway_seat_set_config(struct sway_seat *seat,

View file

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

View file

@ -38,7 +38,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));
@ -66,13 +66,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) {
while (cont->children->length != 0) {
container_destroy(cont->children->items[0]);
}
list_free(cont->children);
cont->children = NULL;
}
if (cont->marks) {
list_foreach(cont->marks, free);

View file

@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
@ -100,6 +101,8 @@ void container_add_child(struct sway_container *parent,
parent, parent->type, parent->width, parent->height);
list_add(parent->children, child);
child->parent = parent;
// TODO: set focus for this container?
sway_input_manager_set_focus(input_manager, child);
}
struct sway_container *container_reap_empty(struct sway_container *container) {
@ -135,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,
@ -322,7 +325,12 @@ 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_set_position(child->sway_view, child_x, y);
if (child->type == C_VIEW) {
view_set_position(child->sway_view, child_x, y);
} else {
child->x = child_x;
child->y = y;
}
if (i == end - 1) {
double remaining_width = x + width - child_x;
@ -505,9 +513,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 sway_seat_get_focus_inactive(seat, container);
}
@ -559,7 +567,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);
@ -589,7 +596,7 @@ static struct sway_container *get_swayc_in_direction_under(
}
if (next->children && next->children->length) {
// TODO consider floating children as well
return sway_seat_get_focus_inactive(seat, next);
return sway_seat_get_focus_by_type(seat, next, C_VIEW);
} else {
return next;
}
@ -619,21 +626,22 @@ static struct sway_container *get_swayc_in_direction_under(
wrap_candidate = parent->children->items[0];
}
if (config->force_focus_wrapping) {
return wrap_candidate;
return sway_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 sway_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;
}
@ -641,8 +649,71 @@ 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->layout = layout;
cont->width = child->width;
cont->height = child->height;
cont->x = child->x;
cont->y = child->y;
/* Container inherits all of workspaces children, layout and whatnot */
if (child->type == C_WORKSPACE) {
struct sway_container *workspace = child;
// reorder focus
int i;
for (i = 0; i < workspace->children->length; ++i) {
((struct sway_container *)workspace->children->items[i])->parent =
cont;
}
// Swap children
list_t *tmp_list = workspace->children;
workspace->children = cont->children;
cont->children = tmp_list;
// add container to workspace chidren
container_add_child(workspace, cont);
// give them proper layouts
cont->layout = workspace->workspace_layout;
cont->prev_layout = workspace->prev_layout;
} else { // Or is built around container
container_replace_child(child, cont);
container_add_child(cont, child);
}
return cont;
}