Skip to content

Commit

Permalink
Merge branch 'sdl_errors' into 'master'
Browse files Browse the repository at this point in the history
Handle and log some controller related SDL errors (#7728)

Closes #7728

See merge request OpenMW/openmw!3669
  • Loading branch information
AnyOldName3 committed Dec 22, 2023
2 parents f11c667 + 78a0e0e commit 8cafcf7
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions apps/openmw/mwinput/controllermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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] });
}
Expand Down

0 comments on commit 8cafcf7

Please sign in to comment.