Quellcode durchsuchen

Support calling SteamAPI_InitEx() before SDL_Init(SDL_INIT_GAMEPAD)

Calling SteamAPI_InitEx() will set environment variables that SDL uses to properly support the Steam virtual gamepad. Make sure that we fall back to the real environment for the variables that Steam sets.
Sam Lantinga vor 2 Monaten
Ursprung
Commit
517a3d20e8

+ 3 - 0
include/SDL3/SDL_gamepad.h

@@ -48,6 +48,9 @@
  * SDL_INIT_GAMEPAD flag. This causes SDL to scan the system for gamepads, and
  * SDL_INIT_GAMEPAD flag. This causes SDL to scan the system for gamepads, and
  * load appropriate drivers.
  * load appropriate drivers.
  *
  *
+ * If you're using SDL gamepad support in a Steam game, you must call
+ * SteamAPI_InitEx() before calling SDL_Init().
+ *
  * If you would like to receive gamepad updates while the application is in
  * If you would like to receive gamepad updates while the application is in
  * the background, you should set the following hint before calling
  * the background, you should set the following hint before calling
  * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
  * SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS

+ 10 - 4
src/joystick/SDL_gamepad.c

@@ -33,6 +33,7 @@
 #include "hidapi/SDL_hidapi_nintendo.h"
 #include "hidapi/SDL_hidapi_nintendo.h"
 #include "hidapi/SDL_hidapi_sinput.h"
 #include "hidapi/SDL_hidapi_sinput.h"
 #include "../events/SDL_events_c.h"
 #include "../events/SDL_events_c.h"
+#include "../SDL_hints_c.h"
 
 
 #ifdef SDL_PLATFORM_WIN32
 #ifdef SDL_PLATFORM_WIN32
 #include "../core/windows/SDL_windows.h"
 #include "../core/windows/SDL_windows.h"
@@ -2616,7 +2617,11 @@ static int SDL_PrivateAddGamepadMapping(const char *mappingString, SDL_GamepadMa
                 }
                 }
 
 
             } else {
             } else {
-                value = SDL_GetHintBoolean(hint, default_value);
+                const char *hint_value = SDL_GetHint(hint);
+                if (!hint_value) {
+                    hint_value = SDL_getenv_unsafe(hint_value);
+                }
+                value = SDL_GetStringBoolean(hint_value, default_value);
                 if (negate) {
                 if (negate) {
                     value = !value;
                     value = !value;
                 }
                 }
@@ -3235,9 +3240,10 @@ bool SDL_ShouldIgnoreGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version
         }
         }
     }
     }
 
 
+    const char *hint = SDL_getenv_unsafe("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD");
+    bool allow_steam_virtual_gamepad = SDL_GetStringBoolean(hint, false);
 #ifdef SDL_PLATFORM_WIN32
 #ifdef SDL_PLATFORM_WIN32
-    if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", false) &&
-        WIN_IsWine()) {
+    if (allow_steam_virtual_gamepad && WIN_IsWine()) {
         // We are launched by Steam and running under Proton or Wine
         // We are launched by Steam and running under Proton or Wine
         // We can't tell whether this controller is a Steam Virtual Gamepad,
         // We can't tell whether this controller is a Steam Virtual Gamepad,
         // so assume that is doing the appropriate filtering of controllers
         // so assume that is doing the appropriate filtering of controllers
@@ -3247,7 +3253,7 @@ bool SDL_ShouldIgnoreGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version
 #endif // SDL_PLATFORM_WIN32
 #endif // SDL_PLATFORM_WIN32
 
 
     if (SDL_IsJoystickSteamVirtualGamepad(vendor_id, product_id, version)) {
     if (SDL_IsJoystickSteamVirtualGamepad(vendor_id, product_id, version)) {
-        return !SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", false);
+        return !allow_steam_virtual_gamepad;
     }
     }
 
 
     if (SDL_allowed_gamepads.num_included_entries > 0) {
     if (SDL_allowed_gamepads.num_included_entries > 0) {

+ 6 - 0
src/joystick/SDL_joystick.c

@@ -3961,9 +3961,15 @@ void SDL_LoadVIDPIDList(SDL_vidpid_list *list)
 
 
     if (list->included_hint_name) {
     if (list->included_hint_name) {
         included_list = SDL_GetHint(list->included_hint_name);
         included_list = SDL_GetHint(list->included_hint_name);
+        if (!included_list) {
+            included_list = SDL_getenv_unsafe(list->included_hint_name);
+        }
     }
     }
     if (list->excluded_hint_name) {
     if (list->excluded_hint_name) {
         excluded_list = SDL_GetHint(list->excluded_hint_name);
         excluded_list = SDL_GetHint(list->excluded_hint_name);
+        if (!excluded_list) {
+            excluded_list = SDL_getenv_unsafe(list->excluded_hint_name);
+        }
     }
     }
     SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);
     SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);
 }
 }

+ 2 - 4
src/joystick/SDL_steam_virtual_gamepad.c

@@ -33,8 +33,6 @@
 #include <sys/stat.h>
 #include <sys/stat.h>
 #endif
 #endif
 
 
-#define SDL_HINT_STEAM_VIRTUAL_GAMEPAD_INFO_FILE    "SteamVirtualGamepadInfo"
-
 static char *SDL_steam_virtual_gamepad_info_file SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
 static char *SDL_steam_virtual_gamepad_info_file SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
 static Uint64 SDL_steam_virtual_gamepad_info_file_mtime SDL_GUARDED_BY(SDL_joystick_lock) = 0;
 static Uint64 SDL_steam_virtual_gamepad_info_file_mtime SDL_GUARDED_BY(SDL_joystick_lock) = 0;
 static Uint64 SDL_steam_virtual_gamepad_info_check_time SDL_GUARDED_BY(SDL_joystick_lock) = 0;
 static Uint64 SDL_steam_virtual_gamepad_info_check_time SDL_GUARDED_BY(SDL_joystick_lock) = 0;
@@ -135,14 +133,14 @@ void SDL_InitSteamVirtualGamepadInfo(void)
         return;
         return;
     }
     }
 
 
-    file = SDL_GetHint(SDL_HINT_STEAM_VIRTUAL_GAMEPAD_INFO_FILE);
+    file = SDL_getenv_unsafe("SteamVirtualGamepadInfo");
     if (file && *file) {
     if (file && *file) {
 #ifdef SDL_PLATFORM_LINUX
 #ifdef SDL_PLATFORM_LINUX
         // Older versions of Wine will blacklist the Steam Virtual Gamepad if
         // Older versions of Wine will blacklist the Steam Virtual Gamepad if
         // it appears to have the real controller's VID/PID, so ignore this.
         // it appears to have the real controller's VID/PID, so ignore this.
         const char *exe = SDL_GetExeName();
         const char *exe = SDL_GetExeName();
         if (exe && SDL_strcmp(exe, "wine64-preloader") == 0) {
         if (exe && SDL_strcmp(exe, "wine64-preloader") == 0) {
-            SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Wine launched by Steam, ignoring %s", SDL_HINT_STEAM_VIRTUAL_GAMEPAD_INFO_FILE);
+            SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Wine launched by Steam, ignoring SteamVirtualGamepadInfo");
             return;
             return;
         }
         }
 #endif
 #endif