SDL: x11: Properly check for XInput2 support before using it.

From 82f54af6177f3d50be1db42a528027ada6ea0a20 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 11 Oct 2023 00:50:53 -0400
Subject: [PATCH] x11: Properly check for XInput2 support before using it.

This specifically fixes a crash in X11_WarpMouseInternal if XInput2 was
missing at runtime, but also cleans up a few other existing checks.

Fixes #8378.
---
 src/video/x11/SDL_x11mouse.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)

diff --git a/src/video/x11/SDL_x11mouse.c b/src/video/x11/SDL_x11mouse.c
index 51eda30f32b9..7edef6a84201 100644
--- a/src/video/x11/SDL_x11mouse.c
+++ b/src/video/x11/SDL_x11mouse.c
@@ -330,13 +330,16 @@ static void X11_WarpMouseInternal(Window xwindow, float x, float y)
     Display *display = videodata->display;
 #ifdef SDL_VIDEO_DRIVER_X11_XINPUT2
     int deviceid = 0;
-    /* It seems XIWarpPointer() doesn't work correctly on multi-head setups:
-     * https://developer.blender.org/rB165caafb99c6846e53d11c4e966990aaffc06cea
-     */
-    if (ScreenCount(display) == 1) {
-        X11_XIGetClientPointer(display, None, &deviceid);
+    if (X11_Xinput2IsInitialized()) {
+        /* It seems XIWarpPointer() doesn't work correctly on multi-head setups:
+         * https://developer.blender.org/rB165caafb99c6846e53d11c4e966990aaffc06cea
+         */
+        if (ScreenCount(display) == 1) {
+            X11_XIGetClientPointer(display, None, &deviceid);
+        }
     }
     if (deviceid != 0) {
+        SDL_assert(SDL_X11_HAVE_XINPUT2);
         X11_XIWarpPointer(display, deviceid, None, xwindow, 0.0, 0.0, 0, 0, x, y);
     } else
 #endif
@@ -370,12 +373,7 @@ static int X11_WarpMouseGlobal(float x, float y)
 
 static int X11_SetRelativeMouseMode(SDL_bool enabled)
 {
-#ifdef SDL_VIDEO_DRIVER_X11_XINPUT2
-    if (X11_Xinput2IsInitialized()) {
-        return 0;
-    }
-#endif
-    return SDL_Unsupported();
+    return X11_Xinput2IsInitialized() ? 0 : SDL_Unsupported();
 }
 
 static int X11_CaptureMouse(SDL_Window *window)
@@ -413,12 +411,9 @@ static Uint32 X11_GetGlobalMouseState(float *x, float *y)
 
     /* !!! FIXME: should we XSync() here first? */
 
-#ifndef SDL_VIDEO_DRIVER_X11_XINPUT2
-    videodata->global_mouse_changed = SDL_TRUE;
-#else
-    if (!SDL_X11_HAVE_XINPUT2)
+    if (!X11_Xinput2IsInitialized()) {
         videodata->global_mouse_changed = SDL_TRUE;
-#endif
+    }
 
     /* check if we have this cached since XInput last saw the mouse move. */
     /* !!! FIXME: can we just calculate this from XInput's events? */