1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 23:09:42 +00:00

Handle and log some controller related SDL errors

SDL_GameControllerNameForIndex may return nullptr indicating an error which
causes a crash when passed to log.
This commit is contained in:
elsid 2023-12-21 02:19:14 +01:00
parent 78a8f9d692
commit 78a0e0eb3b
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625

View file

@ -34,16 +34,27 @@ namespace MWInput
{
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())
{
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
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++)
{
if (SDL_IsGameController(i))
@ -52,11 +63,17 @@ namespace MWInput
evt.which = i;
static const int fakeDeviceID = 1;
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
{
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;
if (!SDL_GameControllerHasSensor(cntrl, SDL_SENSOR_GYRO))
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;
}
mGyroAvailable = true;
#endif
}
@ -353,7 +373,11 @@ namespace MWInput
#if SDL_VERSION_ATLEAST(2, 0, 14)
SDL_GameController* cntrl = mBindingsManager->getControllerOrNull();
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
return std::array<float, 3>({ gyro[0], gyro[1], gyro[2] });
}