[fix] cleanups suggested by Sway community

master
wil 8 years ago
parent a62048f15d
commit 97f70987d7

@ -1,4 +1,3 @@
# sway [![](https://api.travis-ci.org/SirCmpwn/sway.svg)](https://travis-ci.org/SirCmpwn/sway) [![Donate with fosspay](https://drewdevault.com/donate/static/donate-with-fosspay.png)](https://drewdevault.com/donate?project=4) # sway [![](https://api.travis-ci.org/SirCmpwn/sway.svg)](https://travis-ci.org/SirCmpwn/sway) [![Donate with fosspay](https://drewdevault.com/donate/static/donate-with-fosspay.png)](https://drewdevault.com/donate?project=4)
"**S**irCmpwn's **Way**land compositor" is a **work in progress** "**S**irCmpwn's **Way**land compositor" is a **work in progress**
@ -11,10 +10,6 @@ irc.freenode.net).
[More screenshots](https://github.com/SirCmpwn/sway/wiki/Screenshots-of-Sway) [More screenshots](https://github.com/SirCmpwn/sway/wiki/Screenshots-of-Sway)
This fork attempts to add
[Awesome](http://awesomewm.org)/[Monad](http://xmonad.org) style auto layouts
to Sway. For us lazies that just want things to happen automatically!
## Release Signatures ## Release Signatures
Releases are signed with [B22DA89A](http://pgp.mit.edu/pks/lookup?op=vindex&search=0x52CB6609B22DA89A) Releases are signed with [B22DA89A](http://pgp.mit.edu/pks/lookup?op=vindex&search=0x52CB6609B22DA89A)

@ -158,12 +158,12 @@ struct sway_container {
/** /**
* Number of master views in auto layouts. * Number of master views in auto layouts.
*/ */
uint32_t nb_master; size_t nb_master;
/** /**
* Number of slave groups (e.g. columns) in auto layouts. * Number of slave groups (e.g. columns) in auto layouts.
*/ */
uint32_t nb_slave_groups; size_t nb_slave_groups;
}; };
enum visibility_mask { enum visibility_mask {

@ -75,7 +75,5 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ..
*/ */
enum swayc_layouts default_layout(swayc_t *output); enum swayc_layouts default_layout(swayc_t *output);
inline bool is_auto_layout(enum swayc_layouts layout) { bool is_auto_layout(enum swayc_layouts layout);
return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST);
}
#endif #endif

@ -64,11 +64,18 @@ struct cmd_results *cmd_layout(int argc, char **argv) {
} else if (strcasecmp(argv[0], "auto_bot") == 0) { } else if (strcasecmp(argv[0], "auto_bot") == 0) {
swayc_change_layout(parent, L_AUTO_BOTTOM); swayc_change_layout(parent, L_AUTO_BOTTOM);
} else if (strcasecmp(argv[0], "incnmaster") == 0) { } else if (strcasecmp(argv[0], "incnmaster") == 0) {
if ((error = checkarg(argc, "layout incnmaster", const char *name = "layout incnmaster";
if ((error = checkarg(argc, name,
EXPECTED_EQUAL_TO, 2))) { EXPECTED_EQUAL_TO, 2))) {
return error; return error;
} }
int inc = (int) strtol(argv[1], NULL, 10); char *end;
int inc = (int) strtol(argv[1], &end, 10);
if (*end) {
return cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(argument must be an integer)", name);
}
swayc_t *container = get_focused_view(swayc_active_workspace()); swayc_t *container = get_focused_view(swayc_active_workspace());
if (container && inc && if (container && inc &&
is_auto_layout(container->parent->layout) && is_auto_layout(container->parent->layout) &&
@ -83,11 +90,18 @@ struct cmd_results *cmd_layout(int argc, char **argv) {
container->parent->nb_master += inc; container->parent->nb_master += inc;
} }
} else if ((strcasecmp(argv[0], "incncol") == 0) && argc ==2) { } else if ((strcasecmp(argv[0], "incncol") == 0) && argc ==2) {
if ((error = checkarg(argc, "layout incncol", const char *name = "layout incncol";
if ((error = checkarg(argc, name,
EXPECTED_EQUAL_TO, 2))) { EXPECTED_EQUAL_TO, 2))) {
return error; return error;
} }
int inc = (int) strtol(argv[1], NULL, 10); char *end;
int inc = (int) strtol(argv[1], &end, 10);
if (*end) {
return cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(argument must be an integer)", name);
}
swayc_t *container = get_focused_view(swayc_active_workspace()); swayc_t *container = get_focused_view(swayc_active_workspace());
if (container && inc && is_auto_layout(container->parent->layout) && if (container && inc && is_auto_layout(container->parent->layout) &&
((int)container->parent->nb_slave_groups + inc >= 1)) { ((int)container->parent->nb_slave_groups + inc >= 1)) {

@ -66,7 +66,7 @@ static bool resize_floating(int amount, bool use_width) {
* Return the number of children in the slave groups. This corresponds to the children * Return the number of children in the slave groups. This corresponds to the children
* that are not members of the master group. * that are not members of the master group.
*/ */
static inline uint_fast32_t slave_count(swayc_t *container) { static inline size_t auto_slave_count(swayc_t *container) {
return container->children->length - container->nb_master; return container->children->length - container->nb_master;
} }
@ -75,13 +75,13 @@ static inline uint_fast32_t slave_count(swayc_t *container) {
* given the index of a container's child, return the index of the first child of the group * given the index of a container's child, return the index of the first child of the group
* which index is a member of. * which index is a member of.
*/ */
static int group_start_index(swayc_t *container, int index) { static int auto_group_start_index(swayc_t *container, int index) {
if ((index < 0) || (! is_auto_layout(container->layout)) || if (index < 0 || ! is_auto_layout(container->layout)
((uint_fast32_t) index < container->nb_master)) { || (size_t) index < container->nb_master) {
return 0; return 0;
} else { } else {
uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups;
uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; size_t remainder = auto_slave_count(container) % container->nb_slave_groups;
int start_idx; int start_idx;
int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master;
if (index < idx2) { if (index < idx2) {
@ -98,16 +98,16 @@ static int group_start_index(swayc_t *container, int index) {
* that follows the one which index is a member of. * that follows the one which index is a member of.
* This makes the function usable to walk through the groups in a container. * This makes the function usable to walk through the groups in a container.
*/ */
static int group_end_index(swayc_t *container, int index) { static int auto_group_end_index(swayc_t *container, int index) {
if (index < 0 || ! is_auto_layout(container->layout)) { if (index < 0 || ! is_auto_layout(container->layout)) {
return container->children->length; return container->children->length;
} else { } else {
int nxt_idx; int nxt_idx;
if ((uint_fast32_t)index < container->nb_master) { if ((size_t)index < container->nb_master) {
nxt_idx = container->nb_master; nxt_idx = container->nb_master;
} else { } else {
uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups;
uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; size_t remainder = auto_slave_count(container) % container->nb_slave_groups;
int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master;
if (index < idx2) { if (index < idx2) {
nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master;
@ -122,30 +122,30 @@ static int group_end_index(swayc_t *container, int index) {
/** /**
* Return the combined number of master and slave groups in the container. * Return the combined number of master and slave groups in the container.
*/ */
static inline uint_fast32_t group_count(swayc_t *container) { static inline size_t auto_group_count(swayc_t *container) {
return MIN(container->nb_slave_groups, slave_count(container)) + (container->nb_master ? 1 : 0); return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0);
} }
/** /**
* return the index of the Group containing <index>th child of <container>. * return the index of the Group containing <index>th child of <container>.
* The index is the order of the group along the container's major axis (starting at 0). * The index is the order of the group along the container's major axis (starting at 0).
*/ */
static uint_fast32_t group_index(swayc_t *container, int index) { static size_t auto_group_index(swayc_t *container, int index) {
if (index < 0) { if (index < 0) {
return 0; return 0;
} }
bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP);
int nb_slaves = slave_count(container); int nb_slaves = auto_slave_count(container);
if ((uint_fast32_t) index < container->nb_master) { if ((size_t) index < container->nb_master) {
if (master_first || nb_slaves <= 0) { if (master_first || nb_slaves <= 0) {
return 0; return 0;
} else { } else {
return MIN(container->nb_slave_groups, nb_slaves); return MIN(container->nb_slave_groups, nb_slaves);
} }
} else { } else {
uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups; size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups;
uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups; size_t remainder = auto_slave_count(container) % container->nb_slave_groups;
uint_fast32_t grp_idx; size_t grp_idx;
int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master;
if (index < idx2) { if (index < idx2) {
grp_idx = (index - container->nb_master) / grp_sz; grp_idx = (index - container->nb_master) / grp_sz;
@ -161,16 +161,16 @@ static bool resize_tiled(int amount, bool use_width) {
swayc_t *parent = container->parent; swayc_t *parent = container->parent;
int idx_focused = 0; int idx_focused = 0;
bool use_major = false; bool use_major = false;
uint_fast32_t nb_before = 0; size_t nb_before = 0;
uint_fast32_t nb_after = 0; size_t nb_after = 0;
// 1. Identify a container ancestor that will allow the focused child to grow in the requested // 1. Identify a container ancestor that will allow the focused child to grow in the requested
// direction. // direction.
while (container->parent) { while (container->parent) {
parent = container->parent; parent = container->parent;
if ((parent->children && parent->children->length > 1) && if ((parent->children && parent->children->length > 1)
(is_auto_layout(parent->layout) || (use_width ? parent->layout == L_HORIZ : && (is_auto_layout(parent->layout)
parent->layout == L_VERT))) { || (use_width ? parent->layout == L_HORIZ : parent->layout == L_VERT))) {
// check if container has siblings that can provide/absorb the space needed for // check if container has siblings that can provide/absorb the space needed for
// the resize operation. // the resize operation.
use_major = use_width use_major = use_width
@ -185,15 +185,15 @@ static bool resize_tiled(int amount, bool use_width) {
continue; continue;
} }
if (use_major) { if (use_major) {
nb_before = group_index(parent, idx_focused); nb_before = auto_group_index(parent, idx_focused);
nb_after = group_count(parent) - nb_before - 1; nb_after = auto_group_count(parent) - nb_before - 1;
} else { } else {
nb_before = idx_focused - group_start_index(parent, idx_focused); nb_before = idx_focused - auto_group_start_index(parent, idx_focused);
nb_after = group_end_index(parent, idx_focused) - idx_focused - 1; nb_after = auto_group_end_index(parent, idx_focused) - idx_focused - 1;
sway_log(L_DEBUG, "+++ focused: %d, start: %d, end: %d, before: %d, after: %d", sway_log(L_DEBUG, "+++ focused: %d, start: %d, end: %d, before: %d, after: %d",
idx_focused, idx_focused,
(int)group_start_index(parent, idx_focused), (int)auto_group_start_index(parent, idx_focused),
(int)group_end_index(parent, idx_focused), (int)auto_group_end_index(parent, idx_focused),
(int)nb_before, (int)nb_after); (int)nb_before, (int)nb_after);
} }
@ -206,14 +206,14 @@ static bool resize_tiled(int amount, bool use_width) {
if (parent == &root_container) { if (parent == &root_container) {
return true; return true;
} }
sway_log(L_DEBUG, "Found the proper parent: %p. It has %" PRIuFAST32 " before conts, and %" sway_log(L_DEBUG, "Found the proper parent: %p. It has %zu before conts, "
PRIuFAST32 " after conts", parent, nb_before, nb_after); "and %zu after conts", parent, nb_before, nb_after);
// 2. Ensure that the resize operation will not make one of the resized containers drop // 2. Ensure that the resize operation will not make one of the resized containers drop
// below the "sane" size threshold. // below the "sane" size threshold.
bool valid = true; bool valid = true;
swayc_t *focused = parent->children->items[idx_focused]; swayc_t *focused = parent->children->items[idx_focused];
int start = use_major ? 0 : group_start_index(parent, idx_focused); int start = use_major ? 0 : auto_group_start_index(parent, idx_focused);
int end = use_major ? parent->children->length : group_end_index(parent, idx_focused); int end = use_major ? parent->children->length : auto_group_end_index(parent, idx_focused);
sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end); sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end);
for (int i = start; i < end; ) { for (int i = start; i < end; ) {
swayc_t *sibling = parent->children->items[i]; swayc_t *sibling = parent->children->items[i];
@ -235,13 +235,13 @@ static bool resize_tiled(int amount, bool use_width) {
sway_log(L_DEBUG, "Container size no longer sane"); sway_log(L_DEBUG, "Container size no longer sane");
break; break;
} }
i = use_major ? group_end_index(parent, i) : (i + 1); i = use_major ? auto_group_end_index(parent, i) : (i + 1);
sway_log(L_DEBUG, "+++++ check %i", i); sway_log(L_DEBUG, "+++++ check %i", i);
} }
// 3. Apply the size change // 3. Apply the size change
if (valid) { if (valid) {
for (int i = start; i < end; ) { for (int i = start; i < end; ) {
int next_i = use_major ? group_end_index(parent, i) : (i + 1); int next_i = use_major ? auto_group_end_index(parent, i) : (i + 1);
swayc_t *sibling = parent->children->items[i]; swayc_t *sibling = parent->children->items[i];
double pixels = amount; double pixels = amount;
bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y;

@ -253,8 +253,7 @@ void swap_geometry(swayc_t *a, swayc_t *b) {
void move_container(swayc_t *container, enum movement_direction dir) { void move_container(swayc_t *container, enum movement_direction dir) {
enum swayc_layouts layout = L_NONE; enum swayc_layouts layout = L_NONE;
swayc_t *parent = container->parent; swayc_t *parent = container->parent;
if (container->is_floating if (container->is_floating || (container->type != C_VIEW && container->type != C_CONTAINER)) {
|| (container->type != C_VIEW && container->type != C_CONTAINER)) {
return; return;
} }
if (dir == MOVE_UP || dir == MOVE_DOWN) { if (dir == MOVE_UP || dir == MOVE_DOWN) {
@ -323,14 +322,14 @@ void move_container(swayc_t *container, enum movement_direction dir) {
// if move command makes container change from master to slave // if move command makes container change from master to slave
// (or the contrary), reset its geometry an the one of the replaced item. // (or the contrary), reset its geometry an the one of the replaced item.
if (parent->nb_master && if (parent->nb_master &&
(uint_fast32_t) parent->children->length > parent->nb_master) { (size_t) parent->children->length > parent->nb_master) {
swayc_t *swap_geom = NULL; swayc_t *swap_geom = NULL;
// if child is being promoted/demoted, it will swap geometry // if child is being promoted/demoted, it will swap geometry
// with the sibling being demoted/promoted. // with the sibling being demoted/promoted.
if ((dir == MOVE_NEXT && desired == 0) if ((dir == MOVE_NEXT && desired == 0)
|| (dir == MOVE_PREV && (uint_fast32_t) desired == parent->nb_master - 1)) { || (dir == MOVE_PREV && (size_t) desired == parent->nb_master - 1)) {
swap_geom = parent->children->items[parent->nb_master - 1]; swap_geom = parent->children->items[parent->nb_master - 1];
} else if ((dir == MOVE_NEXT && (uint_fast32_t) desired == parent->nb_master) } else if ((dir == MOVE_NEXT && (size_t) desired == parent->nb_master)
|| (dir == MOVE_PREV && desired == parent->children->length - 1)) { || (dir == MOVE_PREV && desired == parent->children->length - 1)) {
swap_geom = parent->children->items[parent->nb_master]; swap_geom = parent->children->items[parent->nb_master];
} }
@ -822,6 +821,10 @@ void update_geometry(swayc_t *container) {
} }
} }
bool is_auto_layout(enum swayc_layouts layout) {
return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST);
}
/** /**
* Layout application prototypes * Layout application prototypes
*/ */
@ -1164,8 +1167,8 @@ void apply_auto_layout(swayc_t *container, const double x, const double y,
// a single slave group (containing slave 1 and 2). The master // a single slave group (containing slave 1 and 2). The master
// group and slave group are layed out using L_VERT. // group and slave group are layed out using L_VERT.
uint_fast32_t nb_slaves = container->children->length - container->nb_master; size_t nb_slaves = container->children->length - container->nb_master;
uint_fast32_t nb_groups = (container->nb_master > 0 ? 1 : 0) + size_t nb_groups = (container->nb_master > 0 ? 1 : 0) +
MIN(container->nb_slave_groups, nb_slaves); MIN(container->nb_slave_groups, nb_slaves);
// the target dimension of the container along the "major" axis, each // the target dimension of the container along the "major" axis, each
@ -1188,7 +1191,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y,
switch (group_layout) { switch (group_layout) {
default: default:
sway_log(L_ERROR, "Unknown layout type (%d) used in %s()", sway_log(L_DEBUG, "Unknown layout type (%d) used in %s()",
group_layout, __func__); group_layout, __func__);
/* fall through */ /* fall through */
case L_VERT: case L_VERT:
@ -1216,7 +1219,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y,
* layout. */ * layout. */
double old_group_dim[nb_groups]; double old_group_dim[nb_groups];
double old_dim = 0; double old_dim = 0;
uint_fast32_t group = 0; size_t group = 0;
for (int i = 0; i < container->children->length;) { for (int i = 0; i < container->children->length;) {
swayc_t *child = container->children->items[i]; swayc_t *child = container->children->items[i];
double *dim = group_layout == L_HORIZ ? &child->height : &child->width; double *dim = group_layout == L_HORIZ ? &child->height : &child->width;
@ -1252,7 +1255,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y,
for (group = 0; group < nb_groups; ++group) { for (group = 0; group < nb_groups; ++group) {
// column to include next by increasing position. // column to include next by increasing position.
uint_fast32_t layout_group = master_first ? group : (group + 1) % nb_groups; size_t layout_group = master_first ? group : (group + 1) % nb_groups;
// adjusted size of the group // adjusted size of the group
group_dim = old_group_dim[layout_group] * scale; group_dim = old_group_dim[layout_group] * scale;
@ -1270,8 +1273,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y,
if (group == nb_groups - 1) { if (group == nb_groups - 1) {
group_dim = pos_maj + dim_maj - pos; // remaining width group_dim = pos_maj + dim_maj - pos; // remaining width
} }
sway_log(L_DEBUG, "Arranging container %p column %" PRIuFAST32 sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)",
", children [%d,%d[ (%fx%f+%f,%f)",
container, group, start, end, *group_w, *group_h, *group_x, *group_y); container, group, start, end, *group_w, *group_h, *group_x, *group_y);
switch (group_layout) { switch (group_layout) {
default: default:

Loading…
Cancel
Save