From e09861f73fa21f95b58cd0ecec4647d585f91326 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Sat, 17 Mar 2018 17:42:49 +0200 Subject: [PATCH] contained_by_output: return output and rename to output_containing_rect --- include/randr.h | 9 ++++----- src/floating.c | 2 +- src/randr.c | 26 ++++++++++++++++++-------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/randr.h b/include/randr.h index bfbfd5a9..d365a9e4 100644 --- a/include/randr.h +++ b/include/randr.h @@ -95,15 +95,14 @@ Output *get_output_containing(unsigned int x, unsigned int y); */ Output *get_output_with_dimensions(Rect rect); -/* - * In contained_by_output, we check if any active output contains part of the container. +/** + * In output_containing_rect, we check if any active output contains part of the container. * We do this by checking if the output rect is intersected by the Rect. * This is the 2-dimensional counterpart of get_output_containing. - * Since we don't actually need the outputs intersected by the given Rect (There could - * be many), we just return true or false for convenience. + * Returns the output with the maximum intersecting area. * */ -bool contained_by_output(Rect rect); +Output *output_containing_rect(Rect rect); /** * Gets the output which is the next one in the given direction. diff --git a/src/floating.c b/src/floating.c index e958153d..29b9cab3 100644 --- a/src/floating.c +++ b/src/floating.c @@ -867,7 +867,7 @@ drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_ void floating_reposition(Con *con, Rect newrect) { /* Sanity check: Are the new coordinates on any output? If not, we * ignore that request. */ - if (!contained_by_output(newrect)) { + if (!output_containing_rect(newrect)) { ELOG("No output found at destination coordinates. Not repositioning.\n"); return; } diff --git a/src/randr.c b/src/randr.c index 85add08f..efdd0352 100644 --- a/src/randr.c +++ b/src/randr.c @@ -136,27 +136,37 @@ Output *get_output_with_dimensions(Rect rect) { } /* - * In contained_by_output, we check if any active output contains part of the container. + * In output_containing_rect, we check if any active output contains part of the container. * We do this by checking if the output rect is intersected by the Rect. * This is the 2-dimensional counterpart of get_output_containing. - * Since we don't actually need the outputs intersected by the given Rect (There could - * be many), we just return true or false for convenience. + * Returns the output with the maximum intersecting area. * */ -bool contained_by_output(Rect rect) { +Output *output_containing_rect(Rect rect) { Output *output; int lx = rect.x, uy = rect.y; int rx = rect.x + rect.width, by = rect.y + rect.height; + long max_area = 0; + Output *result = NULL; TAILQ_FOREACH(output, &outputs, outputs) { if (!output->active) continue; + int lx_o = (int)output->rect.x, uy_o = (int)output->rect.y; + int rx_o = (int)(output->rect.x + output->rect.width), by_o = (int)(output->rect.y + output->rect.height); DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n", rect.x, rect.y, output->rect.x, output->rect.y, output->rect.width, output->rect.height); - if (rx >= (int)output->rect.x && lx <= (int)(output->rect.x + output->rect.width) && - by >= (int)output->rect.y && uy <= (int)(output->rect.y + output->rect.height)) - return true; + int left = max(lx, lx_o); + int right = min(rx, rx_o); + int bottom = min(by, by_o); + int top = max(uy, uy_o); + if (left < right && bottom > top) { + long area = (right - left) * (bottom - top); + if (area > max_area) { + result = output; + } + } } - return false; + return result; } /* -- 2.39.5