[PATCH] Allow mouse warp only when mouse is in Hatari window & it has focus

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


User's mouse shouldn't be moved if mouse has left Hatari window, or
Hatari window has lost keyboard focus.

Both of these events need to be checked because SDL doesn't report
mouse leave event on all activities where Hatari window disappears
from user's view (e.g. switching to another workspace, one without
Hatari).
---
 src/main.c | 53 ++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/src/main.c b/src/main.c
index e1a6f191..78c01e37 100644
--- a/src/main.c
+++ b/src/main.c
@@ -80,7 +80,10 @@ static int nVBLSlowdown = 1;		  /* host VBL wait multiplier */
 
 static bool bEmulationActive = true;      /* Run emulation when started */
 static bool bAccurateDelays;              /* Host system has an accurate SDL_Delay()? */
+
 static bool bIgnoreNextMouseMotion = false;  /* Next mouse motion will be ignored (needed after SDL_WarpMouse) */
+static bool bAllowMouseWarp = true;       /* disabled when Hatari window loses mouse pointer / key focus */
+
 
 /*-----------------------------------------------------------------------*/
 /**
@@ -435,12 +438,17 @@ static void Main_CheckForAccurateDelays(void)
  * Set mouse pointer to new x,y coordinates and set flag to ignore
  * the mouse event that is generated by SDL_WarpMouse().
  *
- * Skip the request is it's not position restore and mouse warping is disabled.
+ * Skip the request if:
+ * - it's not position restore and mouse warping is disabled, or
+ * - mouse warp disable due to mouse leaving Hatari window or focus loss
+ *   (i.e. user isn't interacting with it)
  */
 void Main_WarpMouse(int x, int y, bool restore)
 {
 	if (!(restore || ConfigureParams.Screen.bMouseWarp))
 		return;
+	if (!bAllowMouseWarp)
+		return;
 #if WITH_SDL2
 	SDL_WarpMouseInWindow(sdlWindow, x, y);
 #else
@@ -635,23 +643,34 @@ void Main_EventHandler(void)
 			break;
 
 		 case SDL_WINDOWEVENT:
-			if (event.window.event == SDL_WINDOWEVENT_EXPOSED
-			    && !ConfigureParams.Screen.bUseSdlRenderer)
-			{
-				/* Hack: Redraw screen here when going into
-				 * fullscreen mode without SDL renderer */
-				sdlscrn = SDL_GetWindowSurface(sdlWindow);
-				Screen_SetFullUpdate();
-				Statusbar_Init(sdlscrn);
-			}
-			/* Note: any changes here should most likely be done
-			 * also in sdlgui.c::SDLGui_DoDialog()
-			 */
-			if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED
-			    || event.window.event == SDL_WINDOWEVENT_RESTORED
-			    || event.window.event == SDL_WINDOWEVENT_EXPOSED)
-			{
+			Log_Printf(LOG_DEBUG, "Window event: 0x%x\n", event.window.event);
+			switch(event.window.event) {
+			case SDL_WINDOWEVENT_EXPOSED:
+				if (!ConfigureParams.Screen.bUseSdlRenderer)
+				{
+					/* Hack: Redraw screen here when going into
+					 * fullscreen mode without SDL renderer */
+					sdlscrn = SDL_GetWindowSurface(sdlWindow);
+					Screen_SetFullUpdate();
+					Statusbar_Init(sdlscrn);
+				}
+				/* pass-through */
+			case SDL_WINDOWEVENT_RESTORED:
+			case SDL_WINDOWEVENT_SIZE_CHANGED:
+				/* Note: any changes here should most likely
+				 * be done also in sdlgui.c::SDLGui_DoDialog()
+				 */
 				SDL_UpdateRect(sdlscrn, 0, 0, 0, 0);
+				break;
+				/* mouse & keyboard focus */
+			case SDL_WINDOWEVENT_ENTER:
+			case SDL_WINDOWEVENT_FOCUS_GAINED:
+				bAllowMouseWarp = true;
+				break;
+			case SDL_WINDOWEVENT_LEAVE:
+			case SDL_WINDOWEVENT_FOCUS_LOST:
+				bAllowMouseWarp = false;
+				break;
 			}
 			bContinueProcessing = true;
 			break;
-- 
2.20.1


--------------95D09324768CA530ED2C6472--



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/