From 85871cb666a8adf8221b0b64049697a9d5ade686 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Wed, 14 Feb 2018 14:10:31 -0500 Subject: [PATCH 1/4] add wlr_output_layout_adjacent_output --- include/wlr/types/wlr_output_layout.h | 9 +++++ include/wlr/util/direction.h | 11 ++++++ types/wlr_output_layout.c | 50 +++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 include/wlr/util/direction.h diff --git a/include/wlr/types/wlr_output_layout.h b/include/wlr/types/wlr_output_layout.h index 7dd2b16a..ea96c6b8 100644 --- a/include/wlr/types/wlr_output_layout.h +++ b/include/wlr/types/wlr_output_layout.h @@ -5,6 +5,7 @@ #include #include #include +#include struct wlr_output_layout_state; @@ -96,4 +97,12 @@ void wlr_output_layout_add_auto(struct wlr_output_layout *layout, struct wlr_output *wlr_output_layout_get_center_output( struct wlr_output_layout *layout); +/** + * Get the closest adjacent output to the reference output from the reference + * point in the given direction. + */ +struct wlr_output *wlr_output_layout_adjacent_output( + struct wlr_output_layout *layout, enum wlr_direction direction, + struct wlr_output *reference, double ref_x, double ref_y); + #endif diff --git a/include/wlr/util/direction.h b/include/wlr/util/direction.h new file mode 100644 index 00000000..b5f3a589 --- /dev/null +++ b/include/wlr/util/direction.h @@ -0,0 +1,11 @@ +#ifndef WLR_UTIL_DIRECTION_H +#define WLR_UTIL_DIRECTION_H + +enum wlr_direction { + WLR_DIRECTION_UP = 0, + WLR_DIRECTION_DOWN = 1, + WLR_DIRECTION_LEFT = 2, + WLR_DIRECTION_RIGHT = 4, +}; + +#endif diff --git a/types/wlr_output_layout.c b/types/wlr_output_layout.c index db0389bd..24982e98 100644 --- a/types/wlr_output_layout.c +++ b/types/wlr_output_layout.c @@ -421,3 +421,53 @@ struct wlr_output *wlr_output_layout_get_center_output( return wlr_output_layout_output_at(layout, dest_x, dest_y); } + + +struct wlr_output *wlr_output_layout_adjacent_output( + struct wlr_output_layout *layout, enum wlr_direction direction, + struct wlr_output *reference, double ref_x, double ref_y) { + // XXX should we allow reference to be NULL? + assert(reference); + + struct wlr_box *ref_box = wlr_output_layout_get_box(layout, reference); + + double min_distance = DBL_MAX; + struct wlr_output *closest_output = NULL; + struct wlr_output_layout_output *l_output; + wl_list_for_each(l_output, &layout->outputs, link) { + if (reference != NULL && reference == l_output->output) { + continue; + } + struct wlr_box *box = wlr_output_layout_output_get_box(l_output); + + bool match = false; + // test to make sure this output is in the given direction + if (direction | WLR_DIRECTION_LEFT) { + match = box->x + box->width <= ref_box->x || match; + } + if (direction | WLR_DIRECTION_RIGHT) { + match = box->x >= ref_box->x + ref_box->width || match; + } + if (direction | WLR_DIRECTION_UP) { + match = box->y + box->height <= ref_box->y; + } + if (direction | WLR_DIRECTION_DOWN) { + match = box->y >= ref_box->y + ref_box->height; + } + if (!match) { + continue; + } + + // calculate distance from the given reference point + double x, y; + wlr_output_layout_closest_point(layout, l_output->output, + ref_x, ref_y, &x, &y); + double distance = + (x - ref_x) * (x - ref_x) + (y - ref_y) * (y - ref_y); + if (distance < min_distance) { + min_distance = distance; + closest_output = l_output->output; + } + } + return closest_output; +} From 9933b7ad955204208ae6e72b23c6de8bea1e436c Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 17 Feb 2018 17:54:57 -0500 Subject: [PATCH 2/4] fix direction determination --- types/wlr_output_layout.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/wlr_output_layout.c b/types/wlr_output_layout.c index 24982e98..b1d0a130 100644 --- a/types/wlr_output_layout.c +++ b/types/wlr_output_layout.c @@ -442,17 +442,17 @@ struct wlr_output *wlr_output_layout_adjacent_output( bool match = false; // test to make sure this output is in the given direction - if (direction | WLR_DIRECTION_LEFT) { + if (direction & WLR_DIRECTION_LEFT) { match = box->x + box->width <= ref_box->x || match; } - if (direction | WLR_DIRECTION_RIGHT) { + if (direction & WLR_DIRECTION_RIGHT) { match = box->x >= ref_box->x + ref_box->width || match; } - if (direction | WLR_DIRECTION_UP) { - match = box->y + box->height <= ref_box->y; + if (direction & WLR_DIRECTION_UP) { + match = box->y + box->height <= ref_box->y || match; } - if (direction | WLR_DIRECTION_DOWN) { - match = box->y >= ref_box->y + ref_box->height; + if (direction & WLR_DIRECTION_DOWN) { + match = box->y >= ref_box->y + ref_box->height || match; } if (!match) { continue; From 9a8808f8cf514076561c714293568aae4709a95a Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 17 Feb 2018 18:48:50 -0500 Subject: [PATCH 3/4] move direction to wlr_output_layout.h --- include/wlr/types/wlr_output_layout.h | 8 +++++++- include/wlr/util/direction.h | 11 ----------- 2 files changed, 7 insertions(+), 12 deletions(-) delete mode 100644 include/wlr/util/direction.h diff --git a/include/wlr/types/wlr_output_layout.h b/include/wlr/types/wlr_output_layout.h index ea96c6b8..4d10720e 100644 --- a/include/wlr/types/wlr_output_layout.h +++ b/include/wlr/types/wlr_output_layout.h @@ -5,7 +5,6 @@ #include #include #include -#include struct wlr_output_layout_state; @@ -97,6 +96,13 @@ void wlr_output_layout_add_auto(struct wlr_output_layout *layout, struct wlr_output *wlr_output_layout_get_center_output( struct wlr_output_layout *layout); +enum wlr_direction { + WLR_DIRECTION_UP = 0, + WLR_DIRECTION_DOWN = 1, + WLR_DIRECTION_LEFT = 2, + WLR_DIRECTION_RIGHT = 4, +}; + /** * Get the closest adjacent output to the reference output from the reference * point in the given direction. diff --git a/include/wlr/util/direction.h b/include/wlr/util/direction.h deleted file mode 100644 index b5f3a589..00000000 --- a/include/wlr/util/direction.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef WLR_UTIL_DIRECTION_H -#define WLR_UTIL_DIRECTION_H - -enum wlr_direction { - WLR_DIRECTION_UP = 0, - WLR_DIRECTION_DOWN = 1, - WLR_DIRECTION_LEFT = 2, - WLR_DIRECTION_RIGHT = 4, -}; - -#endif From c951000198ecdbe974a89cab4ca9e394fddf565e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 18 Feb 2018 11:21:08 -0500 Subject: [PATCH 4/4] Remove XXX comment --- types/wlr_output_layout.c | 1 - 1 file changed, 1 deletion(-) diff --git a/types/wlr_output_layout.c b/types/wlr_output_layout.c index b1d0a130..c305f04d 100644 --- a/types/wlr_output_layout.c +++ b/types/wlr_output_layout.c @@ -426,7 +426,6 @@ struct wlr_output *wlr_output_layout_get_center_output( struct wlr_output *wlr_output_layout_adjacent_output( struct wlr_output_layout *layout, enum wlr_direction direction, struct wlr_output *reference, double ref_x, double ref_y) { - // XXX should we allow reference to be NULL? assert(reference); struct wlr_box *ref_box = wlr_output_layout_get_box(layout, reference);