SDL: [SDL] Minimize number of system calls when handling WM_INPUT raw input messages.

From 157c3f8097403c08cc9747082de37276fb2ab28c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 21 May 2021 09:45:08 -0700
Subject: [PATCH] [SDL] Minimize number of system calls when handling WM_INPUT
 raw input messages.

Details:
Currently doing 4 system calls per WM_INPUT message, which can cause the thread handling the message loop to be swapped out several times:
* GetProp - to get window data from the window handle
* GetRawInputData - to retrieve the raw input data
* 2 calls to GetMessageExtraInfo - to ignore synthetic mouse events generated for touchscreens

In this change:
* Replaced GetProp by iterating the list of windows maintained by SDL (with a fallback to GetProp). Note that this will affect all messages and not just WM_INPUT
* only calling GetMessageExtraInfo if a touchscreen has been detected

Fix for https://jira.valve.org/browse/CSGO-4855
@saml
---
 build-scripts/config.guess            |  0
 build-scripts/config.sub              |  0
 src/video/windows/SDL_windowsevents.c | 27 ++++++++++++++++++++++++---
 3 files changed, 24 insertions(+), 3 deletions(-)
 mode change 100755 => 100644 build-scripts/config.guess
 mode change 100755 => 100644 build-scripts/config.sub

diff --git a/build-scripts/config.guess b/build-scripts/config.guess
old mode 100755
new mode 100644
diff --git a/build-scripts/config.sub b/build-scripts/config.sub
old mode 100755
new mode 100644
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 0fbe012aa..bb86108a3 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -428,6 +428,23 @@ static SDL_MOUSE_EVENT_SOURCE GetMouseMessageSource()
     return SDL_MOUSE_EVENT_SOURCE_MOUSE;
 }
 
+static SDL_WindowData *
+WIN_GetWindowDataFromHWND(HWND hwnd)
+{
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
+    SDL_Window *window;
+
+    if (_this) {
+        for (window = _this->windows; window; window = window->next) {
+            SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
+            if (data && data->hwnd == hwnd) {
+                return data;
+            }
+        }
+    }
+    return NULL;
+}
+
 LRESULT CALLBACK
 WIN_KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
 {
@@ -510,7 +527,11 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
     }
 
     /* Get the window data for the window */
-    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
+    data = WIN_GetWindowDataFromHWND(hwnd);
+    if (!data) {
+        /* Fallback */
+        data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
+    }
     if (!data) {
         return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
     }
@@ -693,8 +714,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
             /* Mouse data (ignoring synthetic mouse events generated for touchscreens) */
             if (inp.header.dwType == RIM_TYPEMOUSE) {
-                if (GetMouseMessageSource() == SDL_MOUSE_EVENT_SOURCE_TOUCH ||
-                    (GetMessageExtraInfo() & 0x82) == 0x82) {
+                if (SDL_GetNumTouchDevices() > 0 &&
+                    (GetMouseMessageSource() == SDL_MOUSE_EVENT_SOURCE_TOUCH || (GetMessageExtraInfo() & 0x82) == 0x82)) {
                     break;
                 }
                 if (isRelative) {