From 27fb1707444b7767e569bb43cad5ab6402b8224b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 25 Jul 2015 20:12:45 +0200 Subject: [PATCH] screen_getbycoord: fallback to nearest screen This removes the handling of negative x/y from `screen_coord_in_screen`, which was added in 0f840d2e and does not make sense in screen_coord_in_screen. Ref: https://github.com/blueyed/awesome/commit/50982fc0827a7a1868e23fc7124db1a38adfe275#commitcomment-12358053. Ref: https://github.com/awesomeWM/awesome/pull/331. --- objects/screen.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/objects/screen.c b/objects/screen.c index 5f7ab390..eeb8385f 100644 --- a/objects/screen.c +++ b/objects/screen.c @@ -35,6 +35,7 @@ #include "objects/client.h" #include "objects/drawin.h" +#include #include #include @@ -297,8 +298,19 @@ screen_getbycoord(int x, int y) if(screen_coord_in_screen(*s, x, y)) return *s; - /* No screen found, let's be creative. */ - return globalconf.screens.tab[0]; + /* No screen found, find nearest screen. */ + screen_t * nearest_screen = globalconf.screens.tab[0]; + int nearest_dist = INT_MAX; + foreach(s, globalconf.screens) + { + int dist = sqrt(pow((*s)->geometry.x - x, 2) + pow((*s)->geometry.y - y, 2)); + if( dist < nearest_dist ) + { + nearest_dist = dist; + nearest_screen = (*s); + } + } + return nearest_screen; } /** Are the given coordinates in a given screen? @@ -310,8 +322,8 @@ screen_getbycoord(int x, int y) bool screen_coord_in_screen(screen_t *s, int x, int y) { - return (x < 0 || (x >= s->geometry.x && x < s->geometry.x + s->geometry.width)) - && (y < 0 || (y >= s->geometry.y && y < s->geometry.y + s->geometry.height)); + return (x >= s->geometry.x && x < s->geometry.x + s->geometry.width) + && (y >= s->geometry.y && y < s->geometry.y + s->geometry.height); } /** Get screens info.