1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-17 10:21:11 +00:00

Merge branch 'sdl_errors' into 'master'

Handle and log some controller related SDL errors (#7728)

Closes #7728

See merge request OpenMW/openmw!3669
This commit is contained in:
AnyOldName3 2023-12-22 14:57:21 +00:00
commit 8cafcf702f

View File

@ -34,16 +34,27 @@ namespace MWInput
{ {
if (!controllerBindingsFile.empty()) if (!controllerBindingsFile.empty())
{ {
SDL_GameControllerAddMappingsFromFile(Files::pathToUnicodeString(controllerBindingsFile).c_str()); const int result
= SDL_GameControllerAddMappingsFromFile(Files::pathToUnicodeString(controllerBindingsFile).c_str());
if (result < 0)
Log(Debug::Error) << "Failed to add game controller mappings from file \"" << controllerBindingsFile
<< "\": " << SDL_GetError();
} }
if (!userControllerBindingsFile.empty()) if (!userControllerBindingsFile.empty())
{ {
SDL_GameControllerAddMappingsFromFile(Files::pathToUnicodeString(userControllerBindingsFile).c_str()); const int result
= SDL_GameControllerAddMappingsFromFile(Files::pathToUnicodeString(userControllerBindingsFile).c_str());
if (result < 0)
Log(Debug::Error) << "Failed to add game controller mappings from user file \""
<< userControllerBindingsFile << "\": " << SDL_GetError();
} }
// Open all presently connected sticks // Open all presently connected sticks
int numSticks = SDL_NumJoysticks(); const int numSticks = SDL_NumJoysticks();
if (numSticks < 0)
Log(Debug::Error) << "Failed to get number of joysticks: " << SDL_GetError();
for (int i = 0; i < numSticks; i++) for (int i = 0; i < numSticks; i++)
{ {
if (SDL_IsGameController(i)) if (SDL_IsGameController(i))
@ -52,11 +63,17 @@ namespace MWInput
evt.which = i; evt.which = i;
static const int fakeDeviceID = 1; static const int fakeDeviceID = 1;
ControllerManager::controllerAdded(fakeDeviceID, evt); ControllerManager::controllerAdded(fakeDeviceID, evt);
Log(Debug::Info) << "Detected game controller: " << SDL_GameControllerNameForIndex(i); if (const char* name = SDL_GameControllerNameForIndex(i))
Log(Debug::Info) << "Detected game controller: " << name;
else
Log(Debug::Warning) << "Detected game controller without a name: " << SDL_GetError();
} }
else else
{ {
Log(Debug::Info) << "Detected unusable controller: " << SDL_JoystickNameForIndex(i); if (const char* name = SDL_JoystickNameForIndex(i))
Log(Debug::Info) << "Detected unusable controller: " << name;
else
Log(Debug::Warning) << "Detected unusable controller without a name: " << SDL_GetError();
} }
} }
@ -336,8 +353,11 @@ namespace MWInput
return; return;
if (!SDL_GameControllerHasSensor(cntrl, SDL_SENSOR_GYRO)) if (!SDL_GameControllerHasSensor(cntrl, SDL_SENSOR_GYRO))
return; return;
if (SDL_GameControllerSetSensorEnabled(cntrl, SDL_SENSOR_GYRO, SDL_TRUE) < 0) if (const int result = SDL_GameControllerSetSensorEnabled(cntrl, SDL_SENSOR_GYRO, SDL_TRUE); result < 0)
{
Log(Debug::Error) << "Failed to enable game controller sensor: " << SDL_GetError();
return; return;
}
mGyroAvailable = true; mGyroAvailable = true;
#endif #endif
} }
@ -353,7 +373,11 @@ namespace MWInput
#if SDL_VERSION_ATLEAST(2, 0, 14) #if SDL_VERSION_ATLEAST(2, 0, 14)
SDL_GameController* cntrl = mBindingsManager->getControllerOrNull(); SDL_GameController* cntrl = mBindingsManager->getControllerOrNull();
if (cntrl && mGyroAvailable) if (cntrl && mGyroAvailable)
SDL_GameControllerGetSensorData(cntrl, SDL_SENSOR_GYRO, gyro, 3); {
const int result = SDL_GameControllerGetSensorData(cntrl, SDL_SENSOR_GYRO, gyro, 3);
if (result < 0)
Log(Debug::Error) << "Failed to get game controller sensor data: " << SDL_GetError();
}
#endif #endif
return std::array<float, 3>({ gyro[0], gyro[1], gyro[2] }); return std::array<float, 3>({ gyro[0], gyro[1], gyro[2] });
} }