Commit 15b3794f119190d39a453676d9668c8326190a65

Sam Lantinga 2018-08-26T10:34:23

Only reset the clip rect if it's currently the rect we previously clipped. This prevents us from clearing the clip rect globally when another application has set it. There's also an experimental change to regularly update the clip rect for a window defensively, in case someone else has reset it. It works well, but I don't know if it's cheap enough to call as frequently as it would be called now, and might have other undesirable side effects. Also fixed whitespace and SDL coding style

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 801a55b..ae6fee9 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -1543,8 +1543,9 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
 	// Clear minimized if not on windows, only windows handles it at create rather than FinishWindowCreation,
 	// but it's important or window focus will get broken on windows!
 #if !defined(__WIN32__)
-	if ( window->flags & SDL_WINDOW_MINIMIZED )
+	if (window->flags & SDL_WINDOW_MINIMIZED) {
 		window->flags &= ~SDL_WINDOW_MINIMIZED;
+    }
 #endif
 
 #if __WINRT__ && (NTDDI_VERSION < NTDDI_WIN10)
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 5b15b92..8dcd151 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -228,9 +228,7 @@ WIN_CheckWParamMouseButton(SDL_bool bwParamMousePressed, SDL_bool bSDLMousePress
         /* Ignore the button click for activation */
         if (!bwParamMousePressed) {
             data->focus_click_pending &= ~SDL_BUTTON(button);
-            if (!data->focus_click_pending) {
-                WIN_UpdateClipCursor(data->window);
-            }
+            WIN_UpdateClipCursor(data->window);
         }
         if (WIN_ShouldIgnoreFocusClick()) {
             return;
@@ -401,6 +399,11 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
     }
 #endif /* WMMSG_DEBUG */
 
+    /* Update the clipping rect in case someone else has stolen it */
+    /* FIXME: Is this function cheap enough to call this frequently?
+    WIN_UpdateClipCursor(data->window);
+    */
+
     if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
         return 0;
 
@@ -465,6 +468,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
                 SDL_ToggleModState(KMOD_CAPS, (GetKeyState(VK_CAPITAL) & 0x0001) != 0);
                 SDL_ToggleModState(KMOD_NUM, (GetKeyState(VK_NUMLOCK) & 0x0001) != 0);
             } else {
+                RECT rect;
+
                 data->in_window_deactivation = SDL_TRUE;
 
                 if (SDL_GetKeyboardFocus() == data->window) {
@@ -472,7 +477,11 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
                     WIN_ResetDeadKeys();
                 }
 
-                ClipCursor(NULL);
+                if (GetClipCursor(&rect) && SDL_memcmp(&rect, &data->cursor_clipped_rect, sizeof(rect) == 0)) {
+SDL_Log("Windows deactivate, ClipCursor(NULL)\n");
+                    ClipCursor(NULL);
+                    SDL_zero(data->cursor_clipped_rect);
+                }
 
                 data->in_window_deactivation = SDL_FALSE;
             }
@@ -653,7 +662,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
         break;
 
     case WM_UNICHAR:
-        if ( wParam == UNICODE_NOCHAR ) {
+        if (wParam == UNICODE_NOCHAR) {
             returnCode = 1;
             break;
         }
@@ -661,8 +670,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
     case WM_CHAR:
         {
             char text[5];
-            if ( WIN_ConvertUTF32toUTF8( (UINT32)wParam, text ) ) {
-                SDL_SendKeyboardText( text );
+            if (WIN_ConvertUTF32toUTF8((UINT32)wParam, text)) {
+                SDL_SendKeyboardText(text);
             }
         }
         returnCode = 0;
@@ -1099,9 +1108,9 @@ IsWin10FCUorNewer(void)
             SDL_zero(info);
             info.dwOSVersionInfoSize = sizeof(info);
             if (getVersionPtr(&info) == 0) { /* STATUS_SUCCESS == 0 */
-                if (   (info.dwMajorVersion == 10 && info.dwMinorVersion == 0 && info.dwBuildNumber >= 16299)
-                    || (info.dwMajorVersion == 10 && info.dwMinorVersion > 0)
-                    || (info.dwMajorVersion > 10) )
+                if ((info.dwMajorVersion == 10 && info.dwMinorVersion == 0 && info.dwBuildNumber >= 16299) ||
+                    (info.dwMajorVersion == 10 && info.dwMinorVersion > 0) ||
+                    (info.dwMajorVersion > 10))
                 {
                     return SDL_TRUE;
                 }
diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index 753b736..ba5ef05 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -98,9 +98,10 @@ GetWindowStyle(SDL_Window * window)
             style |= STYLE_RESIZABLE;
         }
 
-		/* Need to set initialize minimize style, or when we call ShowWindow with WS_MINIMIZE it will activate a random window */
-		if ( window->flags & SDL_WINDOW_MINIMIZED )
-			style |= WS_MINIMIZE;
+        /* Need to set initialize minimize style, or when we call ShowWindow with WS_MINIMIZE it will activate a random window */
+        if (window->flags & SDL_WINDOW_MINIMIZED) {
+            style |= WS_MINIMIZE;
+        }
     }
     return style;
 }
@@ -334,8 +335,9 @@ WIN_CreateWindow(_THIS, SDL_Window * window)
     /* Inform Windows of the frame change so we can respond to WM_NCCALCSIZE */
     SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
 
-	if ( window->flags & SDL_WINDOW_MINIMIZED )
-		ShowWindow( hwnd, SW_SHOWMINNOACTIVE );
+    if (window->flags & SDL_WINDOW_MINIMIZED) {
+        ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
+    }
 
     if (!(window->flags & SDL_WINDOW_OPENGL)) {
         return 0;
@@ -409,13 +411,11 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
             SDL_sscanf(hint, "%p", (void**)&otherWindow);
 
             /* Do some error checking on the pointer */
-            if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
-            {
+            if (otherWindow != NULL && otherWindow->magic == &_this->window_magic) {
                 /* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
-                if (otherWindow->flags & SDL_WINDOW_OPENGL)
-                {
+                if (otherWindow->flags & SDL_WINDOW_OPENGL) {
                     window->flags |= SDL_WINDOW_OPENGL;
-                    if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
+                    if (!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
                         return -1;
                     }
                 }
@@ -548,17 +548,17 @@ WIN_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *b
 void
 WIN_ShowWindow(_THIS, SDL_Window * window)
 {
-	DWORD style;
-	HWND hwnd;
-	int nCmdShow;
-	
-	hwnd = ( (SDL_WindowData *)window->driverdata )->hwnd;
-	nCmdShow = SW_SHOW;
-	style = GetWindowLong(hwnd, GWL_EXSTYLE);
-	if ( style & WS_EX_NOACTIVATE )
-		nCmdShow = SW_SHOWNOACTIVATE;
-	
-    ShowWindow(hwnd, nCmdShow );
+    DWORD style;
+    HWND hwnd;
+    int nCmdShow;
+    
+    hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
+    nCmdShow = SW_SHOW;
+    style = GetWindowLong(hwnd, GWL_EXSTYLE);
+    if (style & WS_EX_NOACTIVATE) {
+        nCmdShow = SW_SHOWNOACTIVATE;
+    }
+    ShowWindow(hwnd, nCmdShow);
 }
 
 void
@@ -902,6 +902,7 @@ WIN_UpdateClipCursor(SDL_Window *window)
 {
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
     SDL_Mouse *mouse = SDL_GetMouse();
+    RECT rect;
 
     if (data->focus_click_pending) {
         return;
@@ -911,7 +912,6 @@ WIN_UpdateClipCursor(SDL_Window *window)
         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
         if (mouse->relative_mode && !mouse->relative_mode_warp) {
             LONG cx, cy;
-            RECT rect;
             GetWindowRect(data->hwnd, &rect);
 
             cx = (rect.left + rect.right) / 2;
@@ -923,17 +923,21 @@ WIN_UpdateClipCursor(SDL_Window *window)
             rect.top = cy - 1;
             rect.bottom = cy + 1;
 
-            ClipCursor(&rect);
+            if (ClipCursor(&rect)) {
+                data->cursor_clipped_rect = rect;
+            }
         } else {
-            RECT rect;
             if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
                 ClientToScreen(data->hwnd, (LPPOINT) & rect);
                 ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
-                ClipCursor(&rect);
+                if (ClipCursor(&rect)) {
+                    data->cursor_clipped_rect = rect;
+                }
             }
         }
-    } else {
+    } else if (GetClipCursor(&rect) && SDL_memcmp(&rect, &data->cursor_clipped_rect, sizeof(rect)) == 0) {
         ClipCursor(NULL);
+        SDL_zero(data->cursor_clipped_rect);
     }
 }
 
diff --git a/src/video/windows/SDL_windowswindow.h b/src/video/windows/SDL_windowswindow.h
index 0ed8759..0dacff6 100644
--- a/src/video/windows/SDL_windowswindow.h
+++ b/src/video/windows/SDL_windowswindow.h
@@ -46,6 +46,7 @@ typedef struct
     Uint8 focus_click_pending;
     SDL_bool windowed_mode_was_maximized;
     SDL_bool in_window_deactivation;
+    RECT cursor_clipped_rect;
     struct SDL_VideoData *videodata;
 #if SDL_VIDEO_OPENGL_EGL  
     EGLSurface egl_surface;