From 1e8e4d3e7ff0110346f43b8f32c2eca00959b501 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Fri, 14 Sep 2018 20:18:04 +0300 Subject: [PATCH] Introduce direction / orientation / position conversion functions --- include/data.h | 2 ++ include/move.h | 3 --- include/util.h | 12 ++++++++++++ src/util.c | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/include/data.h b/include/data.h index 8cf387900..b8c31c527 100644 --- a/include/data.h +++ b/include/data.h @@ -59,6 +59,8 @@ typedef enum { D_LEFT, typedef enum { NO_ORIENTATION = 0, HORIZ, VERT } orientation_t; +typedef enum { BEFORE, + AFTER } position_t; typedef enum { BS_NORMAL = 0, BS_NONE = 1, BS_PIXEL = 2 } border_style_t; diff --git a/include/move.h b/include/move.h index 96a2cef79..830488b03 100644 --- a/include/move.h +++ b/include/move.h @@ -17,9 +17,6 @@ */ void tree_move(Con *con, direction_t direction); -typedef enum { BEFORE, - AFTER } position_t; - /** * This function detaches 'con' from its parent and inserts it either before or * after 'target'. diff --git a/include/util.h b/include/util.h index 7a2b30838..ad7195ee5 100644 --- a/include/util.h +++ b/include/util.h @@ -181,3 +181,15 @@ ssize_t slurp(const char *path, char **buf); * */ orientation_t orientation_from_direction(direction_t direction); + +/** + * Convert a direction to its corresponding position. + * + */ +position_t position_from_direction(direction_t direction); + +/** + * Convert orientation and position to the corresponding direction. + * + */ +direction_t direction_from_orientation_position(orientation_t orientation, position_t position); diff --git a/src/util.c b/src/util.c index 812aad371..8c84a436b 100644 --- a/src/util.c +++ b/src/util.c @@ -518,3 +518,23 @@ ssize_t slurp(const char *path, char **buf) { orientation_t orientation_from_direction(direction_t direction) { return (direction == D_LEFT || direction == D_RIGHT) ? HORIZ : VERT; } + +/* + * Convert a direction to its corresponding position. + * + */ +position_t position_from_direction(direction_t direction) { + return (direction == D_LEFT || direction == D_UP) ? BEFORE : AFTER; +} + +/* + * Convert orientation and position to the corresponding direction. + * + */ +direction_t direction_from_orientation_position(orientation_t orientation, position_t position) { + if (orientation == HORIZ) { + return position == BEFORE ? D_LEFT : D_RIGHT; + } else { + return position == BEFORE ? D_UP : D_DOWN; + } +}