Remove enum movement_direction

There's no point having both movement_direction and wlr_direction. This
replaces the former with the latter.

As movement_direction also contained MOVE_PARENT and MOVE_CHILD items,
these are now checked specifically in the focus command and handled in
separate functions, just like the other focus variants.
This commit is contained in:
Ryan Dwyer 2018-10-30 23:27:49 +10:00
parent b90af33570
commit 7be309710d
7 changed files with 86 additions and 114 deletions

View file

@ -175,24 +175,3 @@ failed:
free(current);
return NULL;
}
bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out) {
switch (dir) {
case MOVE_UP:
*out = WLR_DIRECTION_UP;
break;
case MOVE_DOWN:
*out = WLR_DIRECTION_DOWN;
break;
case MOVE_LEFT:
*out = WLR_DIRECTION_LEFT;
break;
case MOVE_RIGHT:
*out = WLR_DIRECTION_RIGHT;
break;
default:
return false;
}
return true;
}

View file

@ -62,7 +62,7 @@ void output_begin_destroy(struct sway_output *output);
struct sway_output *output_from_wlr_output(struct wlr_output *output);
struct sway_output *output_get_in_direction(struct sway_output *reference,
enum movement_direction direction);
enum wlr_direction direction);
void output_add_workspace(struct sway_output *output,
struct sway_workspace *workspace);

View file

@ -36,7 +36,6 @@ struct sway_output;
struct sway_workspace;
struct sway_view;
enum movement_direction;
enum wlr_direction;
struct sway_container_state {
@ -287,8 +286,6 @@ void container_detach(struct sway_container *child);
void container_replace(struct sway_container *container,
struct sway_container *replacement);
bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out);
struct sway_container *container_split(struct sway_container *child,
enum sway_container_layout layout);

View file

@ -7,15 +7,6 @@
#include <wlr/types/wlr_output_layout.h>
#include <xkbcommon/xkbcommon.h>
enum movement_direction {
MOVE_LEFT,
MOVE_RIGHT,
MOVE_UP,
MOVE_DOWN,
MOVE_PARENT,
MOVE_CHILD,
};
/**
* Wrap i into the range [0, max[
*/
@ -77,6 +68,4 @@ bool parse_boolean(const char *boolean, bool current);
*/
char* resolve_path(const char* path);
bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out);
#endif

View file

@ -1,4 +1,5 @@
#include <strings.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/util/log.h>
#include "log.h"
#include "sway/commands.h"
@ -13,20 +14,16 @@
#include "stringop.h"
#include "util.h"
static bool parse_movement_direction(const char *name,
enum movement_direction *out) {
static bool parse_direction(const char *name,
enum wlr_direction *out) {
if (strcasecmp(name, "left") == 0) {
*out = MOVE_LEFT;
*out = WLR_DIRECTION_LEFT;
} else if (strcasecmp(name, "right") == 0) {
*out = MOVE_RIGHT;
*out = WLR_DIRECTION_RIGHT;
} else if (strcasecmp(name, "up") == 0) {
*out = MOVE_UP;
*out = WLR_DIRECTION_UP;
} else if (strcasecmp(name, "down") == 0) {
*out = MOVE_DOWN;
} else if (strcasecmp(name, "parent") == 0) {
*out = MOVE_PARENT;
} else if (strcasecmp(name, "child") == 0) {
*out = MOVE_CHILD;
*out = WLR_DIRECTION_DOWN;
} else {
return false;
}
@ -38,7 +35,7 @@ static bool parse_movement_direction(const char *name,
* Get node in the direction of newly entered output.
*/
static struct sway_node *get_node_in_output_direction(
struct sway_output *output, enum movement_direction dir) {
struct sway_output *output, enum wlr_direction dir) {
struct sway_seat *seat = config->handler_context.seat;
struct sway_workspace *ws = output_get_active_workspace(output);
if (ws->fullscreen) {
@ -48,7 +45,7 @@ static struct sway_node *get_node_in_output_direction(
if (ws->tiling->length > 0) {
switch (dir) {
case MOVE_LEFT:
case WLR_DIRECTION_LEFT:
if (ws->layout == L_HORIZ || ws->layout == L_TABBED) {
// get most right child of new output
container = ws->tiling->items[ws->tiling->length-1];
@ -56,7 +53,7 @@ static struct sway_node *get_node_in_output_direction(
container = seat_get_focus_inactive_tiling(seat, ws);
}
break;
case MOVE_RIGHT:
case WLR_DIRECTION_RIGHT:
if (ws->layout == L_HORIZ || ws->layout == L_TABBED) {
// get most left child of new output
container = ws->tiling->items[0];
@ -64,7 +61,7 @@ static struct sway_node *get_node_in_output_direction(
container = seat_get_focus_inactive_tiling(seat, ws);
}
break;
case MOVE_UP:
case WLR_DIRECTION_UP:
if (ws->layout == L_VERT || ws->layout == L_STACKED) {
// get most bottom child of new output
container = ws->tiling->items[ws->tiling->length-1];
@ -72,7 +69,7 @@ static struct sway_node *get_node_in_output_direction(
container = seat_get_focus_inactive_tiling(seat, ws);
}
break;
case MOVE_DOWN: {
case WLR_DIRECTION_DOWN:
if (ws->layout == L_VERT || ws->layout == L_STACKED) {
// get most top child of new output
container = ws->tiling->items[0];
@ -81,9 +78,6 @@ static struct sway_node *get_node_in_output_direction(
}
break;
}
default:
break;
}
}
if (container) {
@ -95,11 +89,8 @@ static struct sway_node *get_node_in_output_direction(
}
static struct sway_node *node_get_in_direction(struct sway_container *container,
struct sway_seat *seat, enum movement_direction dir) {
struct sway_seat *seat, enum wlr_direction dir) {
if (container->is_fullscreen) {
if (dir == MOVE_PARENT) {
return NULL;
}
// Fullscreen container with a direction - go straight to outputs
struct sway_output *output = container->workspace->output;
struct sway_output *new_output = output_get_in_direction(output, dir);
@ -108,9 +99,6 @@ static struct sway_node *node_get_in_direction(struct sway_container *container,
}
return get_node_in_output_direction(new_output, dir);
}
if (dir == MOVE_PARENT) {
return node_get_parent(&container->node);
}
struct sway_container *wrap_candidate = NULL;
struct sway_container *current = container;
@ -122,15 +110,15 @@ static struct sway_node *node_get_in_direction(struct sway_container *container,
container_parent_layout(current);
list_t *siblings = container_get_siblings(current);
if (dir == MOVE_LEFT || dir == MOVE_RIGHT) {
if (dir == WLR_DIRECTION_LEFT || dir == WLR_DIRECTION_RIGHT) {
if (parent_layout == L_HORIZ || parent_layout == L_TABBED) {
can_move = true;
desired = idx + (dir == MOVE_LEFT ? -1 : 1);
desired = idx + (dir == WLR_DIRECTION_LEFT ? -1 : 1);
}
} else {
if (parent_layout == L_VERT || parent_layout == L_STACKED) {
can_move = true;
desired = idx + (dir == MOVE_UP ? -1 : 1);
desired = idx + (dir == WLR_DIRECTION_UP ? -1 : 1);
}
}
@ -200,9 +188,8 @@ static struct cmd_results *focus_output(struct sway_seat *seat,
struct sway_output *output = output_by_name(identifier);
if (!output) {
enum movement_direction direction;
if (!parse_movement_direction(identifier, &direction) ||
direction == MOVE_PARENT || direction == MOVE_CHILD) {
enum wlr_direction direction;
if (!parse_direction(identifier, &direction)) {
free(identifier);
return cmd_results_new(CMD_INVALID, "focus",
"There is no output with that name");
@ -220,6 +207,31 @@ static struct cmd_results *focus_output(struct sway_seat *seat,
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
static struct cmd_results *focus_parent(void) {
struct sway_seat *seat = config->handler_context.seat;
struct sway_container *con = config->handler_context.container;
if (!con || con->is_fullscreen) {
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
struct sway_node *parent = node_get_parent(&con->node);
if (parent) {
seat_set_focus(seat, parent);
seat_consider_warp_to_focus(seat);
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
static struct cmd_results *focus_child(void) {
struct sway_seat *seat = config->handler_context.seat;
struct sway_node *node = config->handler_context.node;
struct sway_node *focus = seat_get_active_tiling_child(seat, node);
if (focus) {
seat_set_focus(seat, focus);
seat_consider_warp_to_focus(seat);
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
struct cmd_results *cmd_focus(int argc, char **argv) {
if (config->reading || !config->active) {
return cmd_results_new(CMD_DEFER, NULL, NULL);
@ -257,27 +269,21 @@ struct cmd_results *cmd_focus(int argc, char **argv) {
return focus_output(seat, argc, argv);
}
enum movement_direction direction = 0;
if (!parse_movement_direction(argv[0], &direction)) {
if (strcasecmp(argv[0], "parent") == 0) {
return focus_parent();
}
if (strcasecmp(argv[0], "child") == 0) {
return focus_child();
}
enum wlr_direction direction = 0;
if (!parse_direction(argv[0], &direction)) {
return cmd_results_new(CMD_INVALID, "focus",
"Expected 'focus <direction|parent|child|mode_toggle|floating|tiling>' "
"or 'focus output <direction|name>'");
}
if (direction == MOVE_CHILD) {
struct sway_node *focus = seat_get_active_tiling_child(seat, node);
if (focus) {
seat_set_focus(seat, focus);
seat_consider_warp_to_focus(seat);
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
if (node->type == N_WORKSPACE) {
if (direction == MOVE_PARENT) {
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
// Jump to the next output
struct sway_output *new_output =
output_get_in_direction(workspace->output, direction);

View file

@ -81,14 +81,14 @@ static struct sway_output *output_in_direction(const char *direction_string,
}
static bool is_parallel(enum sway_container_layout layout,
enum movement_direction dir) {
enum wlr_direction dir) {
switch (layout) {
case L_TABBED:
case L_HORIZ:
return dir == MOVE_LEFT || dir == MOVE_RIGHT;
return dir == WLR_DIRECTION_LEFT || dir == WLR_DIRECTION_RIGHT;
case L_STACKED:
case L_VERT:
return dir == MOVE_UP || dir == MOVE_DOWN;
return dir == WLR_DIRECTION_UP || dir == WLR_DIRECTION_DOWN;
default:
return false;
}
@ -115,7 +115,7 @@ static void workspace_focus_fullscreen(struct sway_workspace *workspace) {
static void container_move_to_container_from_direction(
struct sway_container *container, struct sway_container *destination,
enum movement_direction move_dir) {
enum wlr_direction move_dir) {
if (destination->view) {
if (destination->parent == container->parent &&
destination->workspace == container->workspace) {
@ -126,7 +126,8 @@ static void container_move_to_container_from_direction(
list_swap(siblings, container_index, destination_index);
} else {
wlr_log(WLR_DEBUG, "Promoting to sibling of cousin");
int offset = move_dir == MOVE_LEFT || move_dir == MOVE_UP;
int offset =
move_dir == WLR_DIRECTION_LEFT || move_dir == WLR_DIRECTION_UP;
int index = container_sibling_index(destination) + offset;
if (destination->parent) {
container_insert_child(destination->parent, container, index);
@ -141,7 +142,8 @@ static void container_move_to_container_from_direction(
if (is_parallel(destination->layout, move_dir)) {
wlr_log(WLR_DEBUG, "Reparenting container (parallel)");
int index = move_dir == MOVE_RIGHT || move_dir == MOVE_DOWN ?
int index =
move_dir == WLR_DIRECTION_RIGHT || move_dir == WLR_DIRECTION_DOWN ?
0 : destination->children->length;
container_insert_child(destination, container, index);
container->width = container->height = 0;
@ -164,10 +166,11 @@ static void container_move_to_container_from_direction(
static void container_move_to_workspace_from_direction(
struct sway_container *container, struct sway_workspace *workspace,
enum movement_direction move_dir) {
enum wlr_direction move_dir) {
if (is_parallel(workspace->layout, move_dir)) {
wlr_log(WLR_DEBUG, "Reparenting container (parallel)");
int index = move_dir == MOVE_RIGHT || move_dir == MOVE_DOWN ?
int index =
move_dir == WLR_DIRECTION_RIGHT || move_dir == WLR_DIRECTION_DOWN ?
0 : workspace->tiling->length;
workspace_insert_tiling(workspace, container, index);
return;
@ -258,28 +261,31 @@ static void container_move_to_container(struct sway_container *container,
* container, switches the layout of the workspace, and drops the child back in.
* In other words, rejigger it. */
static void workspace_rejigger(struct sway_workspace *ws,
struct sway_container *child, enum movement_direction move_dir) {
struct sway_container *child, enum wlr_direction move_dir) {
if (!child->parent && ws->tiling->length == 1) {
ws->layout =
move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT;
move_dir == WLR_DIRECTION_LEFT || move_dir == WLR_DIRECTION_RIGHT ?
L_HORIZ : L_VERT;
workspace_update_representation(ws);
return;
}
container_detach(child);
struct sway_container *new_parent = workspace_wrap_children(ws);
int index = move_dir == MOVE_LEFT || move_dir == MOVE_UP ? 0 : 1;
int index =
move_dir == WLR_DIRECTION_LEFT || move_dir == WLR_DIRECTION_UP ? 0 : 1;
workspace_insert_tiling(ws, child, index);
container_flatten(new_parent);
ws->layout =
move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT;
move_dir == WLR_DIRECTION_LEFT || move_dir == WLR_DIRECTION_RIGHT ?
L_HORIZ : L_VERT;
workspace_update_representation(ws);
child->width = child->height = 0;
}
// Returns true if moved
static bool container_move_in_direction(struct sway_container *container,
enum movement_direction move_dir) {
enum wlr_direction move_dir) {
// If moving a fullscreen view, only consider outputs
if (container->is_fullscreen) {
struct sway_output *new_output =
@ -305,7 +311,8 @@ static bool container_move_in_direction(struct sway_container *container,
// The below loop stops once we hit the workspace because current->parent
// is NULL for the topmost containers in a workspace.
struct sway_container *current = container;
int offs = move_dir == MOVE_LEFT || move_dir == MOVE_UP ? -1 : 1;
int offs =
move_dir == WLR_DIRECTION_LEFT || move_dir == WLR_DIRECTION_UP ? -1 : 1;
while (current) {
list_t *siblings = container_get_siblings(current);
@ -642,7 +649,7 @@ static struct cmd_results *cmd_move_workspace(int argc, char **argv) {
}
static struct cmd_results *cmd_move_in_direction(
enum movement_direction direction, int argc, char **argv) {
enum wlr_direction direction, int argc, char **argv) {
int move_amt = 10;
if (argc > 1) {
char *inv;
@ -666,22 +673,18 @@ static struct cmd_results *cmd_move_in_direction(
double lx = container->x;
double ly = container->y;
switch (direction) {
case MOVE_LEFT:
case WLR_DIRECTION_LEFT:
lx -= move_amt;
break;
case MOVE_RIGHT:
case WLR_DIRECTION_RIGHT:
lx += move_amt;
break;
case MOVE_UP:
case WLR_DIRECTION_UP:
ly -= move_amt;
break;
case MOVE_DOWN:
case WLR_DIRECTION_DOWN:
ly += move_amt;
break;
case MOVE_PARENT:
case MOVE_CHILD:
return cmd_results_new(CMD_FAILURE, "move",
"Cannot move floating container to parent or child");
}
container_floating_move_to(container, lx, ly);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
@ -850,13 +853,13 @@ struct cmd_results *cmd_move(int argc, char **argv) {
}
if (strcasecmp(argv[0], "left") == 0) {
return cmd_move_in_direction(MOVE_LEFT, argc, argv);
return cmd_move_in_direction(WLR_DIRECTION_LEFT, argc, argv);
} else if (strcasecmp(argv[0], "right") == 0) {
return cmd_move_in_direction(MOVE_RIGHT, argc, argv);
return cmd_move_in_direction(WLR_DIRECTION_RIGHT, argc, argv);
} else if (strcasecmp(argv[0], "up") == 0) {
return cmd_move_in_direction(MOVE_UP, argc, argv);
return cmd_move_in_direction(WLR_DIRECTION_UP, argc, argv);
} else if (strcasecmp(argv[0], "down") == 0) {
return cmd_move_in_direction(MOVE_DOWN, argc, argv);
return cmd_move_in_direction(WLR_DIRECTION_DOWN, argc, argv);
} else if ((strcasecmp(argv[0], "container") == 0
|| strcasecmp(argv[0], "window") == 0) ||
(strcasecmp(argv[0], "--no-auto-back-and-forth") && argc >= 2

View file

@ -274,16 +274,14 @@ struct sway_output *output_from_wlr_output(struct wlr_output *output) {
}
struct sway_output *output_get_in_direction(struct sway_output *reference,
enum movement_direction direction) {
enum wlr_direction wlr_dir = 0;
if (!sway_assert(sway_dir_to_wlr(direction, &wlr_dir),
"got invalid direction: %d", direction)) {
enum wlr_direction direction) {
if (!sway_assert(direction, "got invalid direction: %d", direction)) {
return NULL;
}
int lx = reference->wlr_output->lx + reference->width / 2;
int ly = reference->wlr_output->ly + reference->height / 2;
struct wlr_output *wlr_adjacent = wlr_output_layout_adjacent_output(
root->output_layout, wlr_dir, reference->wlr_output, lx, ly);
root->output_layout, direction, reference->wlr_output, lx, ly);
if (!wlr_adjacent) {
return NULL;
}