foxygit / doom Log in
commit 10b75388bc1eb42972a427a32407db157d665be6
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sat Dec 6 00:31:40 2014 -0500
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sat Dec 6 00:31:40 2014 -0500

    Track when the window is focused for mouse grab.

    We release mouse grab when the window loses focus, but the
    SDL_GetAppState() function no longer exists in SDL2. Use window
    events to update the screenvisible and window_focused variables.
---
 src/i_video.c | 55 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 28 insertions(+), 27 deletions(-)

diff --git a/src/i_video.c b/src/i_video.c
index 5d51308c..51e910f3 100644
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -232,7 +232,7 @@ static grabmouse_callback_t grabmouse_callback = NULL;

 static byte *disk_image = NULL;
 static byte *saved_background;
-static boolean window_focused;
+static boolean window_focused = true;

 // Empty mouse cursor

@@ -329,31 +329,6 @@ void I_DisplayFPSDots(boolean dots_on)
     display_fps_dots = dots_on;
 }

-// Update the value of window_focused when we get a focus event
-//
-// We try to make ourselves be well-behaved: the grab on the mouse
-// is removed if we lose focus (such as a popup window appearing),
-// and we dont move the mouse around if we aren't focused either.
-
-#if 0
-// SDL2-TODO
-static void UpdateFocus(void)
-{
-    Uint8 state;
-
-    state = SDL_GetAppState();
-
-    // We should have input (keyboard) focus and be visible
-    // (not minimized)
-
-    window_focused = (state & SDL_APPINPUTFOCUS) && (state & SDL_APPACTIVE);
-
-    // Should the screen be grabbed?
-
-    screenvisible = (state & SDL_APPACTIVE) != 0;
-}
-#endif
-
 // Show or hide the mouse cursor. We have to use different techniques
 // depending on the OS.

@@ -709,6 +684,33 @@ static void HandleWindowEvent(SDL_WindowEvent *event)
             last_resize_time = SDL_GetTicks();
             break;

+        // Don't render the screen when the window is minimized:
+
+        case SDL_WINDOWEVENT_MINIMIZED:
+            screenvisible = false;
+            break;
+
+        case SDL_WINDOWEVENT_MAXIMIZED:
+        case SDL_WINDOWEVENT_RESTORED:
+            screenvisible = true;
+            break;
+
+        // Update the value of window_focused when we get a focus event
+        //
+        // We try to make ourselves be well-behaved: the grab on the mouse
+        // is removed if we lose focus (such as a popup window appearing),
+        // and we dont move the mouse around if we aren't focused either.
+
+        case SDL_WINDOWEVENT_ENTER:
+        case SDL_WINDOWEVENT_FOCUS_GAINED:
+            window_focused = true;
+            break;
+
+        case SDL_WINDOWEVENT_LEAVE:
+        case SDL_WINDOWEVENT_FOCUS_LOST:
+            window_focused = false;
+            break;
+
         default:
             break;
     }
@@ -1806,7 +1808,6 @@ static void SetVideoMode(screen_mode_t *mode, int w, int h)
         // In windowed mode, the window can be resized while the game is
         // running.  This feature is disabled on OS X, as it adds an ugly
         // scroll handle to the corner of the screen.
-
         flags |= SDL_WINDOW_RESIZABLE;
     }