From 36f572d2f6353f57367e8a20842b84fb26fdd344 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Tue, 18 May 2021 00:51:35 -0400
Subject: [PATCH] Add a function to translate event coordinates to screen

Fixes issue 56
---
 src/conio/x_events.c | 53 +++++++++++++++++++++++++++++---------------
 1 file changed, 35 insertions(+), 18 deletions(-)

diff --git a/src/conio/x_events.c b/src/conio/x_events.c
index 79a6df5977..ce385934de 100644
--- a/src/conio/x_events.c
+++ b/src/conio/x_events.c
@@ -669,6 +669,35 @@ static void expose_rect(int x, int y, int width, int height)
 	bitmap_drv_request_some_pixels(sx, sy, ex-sx+1, ey-sy+1);
 }
 
+static bool
+xlat_mouse_xy(int *x, int *y)
+{
+	int xoff, yoff;
+
+	xoff = (x11_window_width - xim->width) / 2;
+	if (xoff < 0)
+		xoff = 0;
+	yoff = (x11_window_height - xim->height) / 2;
+	if (yoff < 0)
+		yoff = 0;
+
+	if (*x < xoff)
+		return false;
+	if (*y < yoff)
+		return false;
+	*x -= xoff;
+	*y -= yoff;
+	if (*x >= xim->width)
+		return false;
+	if (*y >= xim->height)
+		return false;
+	*x *= x_cvstat.scrnwidth;
+	*y *= x_cvstat.scrnheight;
+	*x /= xim->width;
+	*y /= xim->height;
+	return true;
+}
+
 static int x11_event(XEvent *ev)
 {
 	if (x11.XFilterEvent(ev, win))
@@ -803,16 +832,12 @@ static int x11_event(XEvent *ev)
 		case MotionNotify:
 			{
 				XMotionEvent *me = (XMotionEvent *)ev;
+				if (!xlat_mouse_xy(&me->x, &me->y))
+					break;
 				int x_res = me->x;
 				int y_res = me->y;
 
-				x_res /= x_cvstat.scaling;
-				y_res /= x_cvstat.scaling;
-				y_res /= x_cvstat.vmultiplier;
-				me->x /= x_cvstat.scaling;
 				me->x /= x_cvstat.charwidth;
-				me->y /= x_cvstat.scaling;
-				me->y /= x_cvstat.vmultiplier;
 				me->y /= x_cvstat.charheight;
 				me->x++;
 				me->y++;
@@ -830,16 +855,12 @@ static int x11_event(XEvent *ev)
 		case ButtonRelease:
 			{
 				XButtonEvent *be = (XButtonEvent *)ev;
+				if (!xlat_mouse_xy(&be->x, &be->y))
+					break;
 				int x_res = be->x;
 				int y_res = be->y;
 
-				x_res /= x_cvstat.scaling;
-				y_res /= x_cvstat.scaling;
-				y_res /= x_cvstat.vmultiplier;
-				be->x/=x_cvstat.scaling;
 				be->x/=x_cvstat.charwidth;
-				be->y/=x_cvstat.scaling;
-				be->y/=x_cvstat.vmultiplier;
 				be->y/=x_cvstat.charheight;
 				be->x++;
 				be->y++;
@@ -859,16 +880,12 @@ static int x11_event(XEvent *ev)
 		case ButtonPress:
 			{
 				XButtonEvent *be = (XButtonEvent *)ev;
+				if (!xlat_mouse_xy(&be->x, &be->y))
+					break;
 				int x_res = be->x;
 				int y_res = be->y;
 
-				x_res /= x_cvstat.scaling;
-				y_res /= x_cvstat.scaling;
-				y_res /= x_cvstat.vmultiplier;
-				be->x/=x_cvstat.scaling;
 				be->x/=x_cvstat.charwidth;
-				be->y/=x_cvstat.scaling;
-				be->y/=x_cvstat.vmultiplier;
 				be->y/=x_cvstat.charheight;
 				be->x++;
 				be->y++;
-- 
GitLab