Commit e261bd4205dc24a497d7288c52639072472c2dba

Sam Lantinga 2020-02-12T12:26:27

Improved fix for bug 4748 - Calling WIN_UpdateClipCursor() / WIN_UpdateClipCursorForWindows() on WIN_PumpEvents() causes beeping and choppy mouse cursor movement, right-click doesn't work

diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index c2fd85c..237d380 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -909,7 +909,7 @@ WIN_UpdateClipCursor(SDL_Window *window)
 {
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
     SDL_Mouse *mouse = SDL_GetMouse();
-    RECT rect;
+    RECT rect, clipped_rect;
 
     if (data->in_title_click || data->focus_click_pending) {
         return;
@@ -918,38 +918,43 @@ WIN_UpdateClipCursor(SDL_Window *window)
         data->skip_update_clipcursor = SDL_FALSE;
         return;
     }
+    if (!GetClipCursor(&clipped_rect)) {
+        return;
+    }
 
     if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
         if (mouse->relative_mode && !mouse->relative_mode_warp) {
-            LONG cx, cy;
-            GetWindowRect(data->hwnd, &rect);
+            if (GetWindowRect(data->hwnd, &rect)) {
+                LONG cx, cy;
 
-            cx = (rect.left + rect.right) / 2;
-            cy = (rect.top + rect.bottom) / 2;
+                cx = (rect.left + rect.right) / 2;
+                cy = (rect.top + rect.bottom) / 2;
 
-            /* Make an absurdly small clip rect */
-            rect.left = cx - 1;
-            rect.right = cx + 1;
-            rect.top = cy - 1;
-            rect.bottom = cy + 1;
+                /* Make an absurdly small clip rect */
+                rect.left = cx - 1;
+                rect.right = cx + 1;
+                rect.top = cy - 1;
+                rect.bottom = cy + 1;
 
-            if (ClipCursor(&rect)) {
-                data->cursor_clipped_rect = rect;
+                if (SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
+                    if (ClipCursor(&rect)) {
+                        data->cursor_clipped_rect = rect;
+                    }
+                }
             }
         } else {
-            RECT clipped_rect;
             if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
                 ClientToScreen(data->hwnd, (LPPOINT) & rect);
                 ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
-                if (!GetClipCursor(&clipped_rect) || SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
+                if (SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
                     if (ClipCursor(&rect)) {
                         data->cursor_clipped_rect = rect;
                     }
                 }
             }
         }
-    } else if (GetClipCursor(&rect) && SDL_memcmp(&rect, &data->cursor_clipped_rect, sizeof(rect)) == 0) {
+    } else if (SDL_memcmp(&clipped_rect, &data->cursor_clipped_rect, sizeof(clipped_rect)) == 0) {
         ClipCursor(NULL);
         SDL_zero(data->cursor_clipped_rect);
     }