Replaced Joystick:saveGamepadMapping with love.joystick.saveGamepadMappings, which saves all gamepad mappings from both recently-used joysticks and recently-modified mappings. saveGamepadMappings returns a string of newline-separated mappings and takes an optional filename argument which writes it to a file.

This commit is contained in:
Alex Szpakowski
2014-06-05 00:25:34 -03:00
parent 10e0d4b900
commit ddb9997ead
6 changed files with 60 additions and 45 deletions
+25 -14
View File
@@ -161,6 +161,9 @@ love::joystick::Joystick *JoystickModule::addJoystick(int deviceindex)
}
}
if (joystick->isGamepad())
recentGamepadGUIDs[joystick->getGUID()] = true;
activeSticks.push_back(joystick);
return joystick;
}
@@ -257,6 +260,9 @@ bool JoystickModule::setGamepadMapping(const std::string &guid, Joystick::Gamepa
// 1 == added, 0 == updated, -1 == error.
int status = SDL_GameControllerAddMapping(mapstr.c_str());
if (status != -1)
recentGamepadGUIDs[guid] = true;
// FIXME: massive hack until missing APIs are added to SDL 2:
// https://bugzilla.libsdl.org/show_bug.cgi?id=1975
if (status == 1)
@@ -498,10 +504,11 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
if (SDL_GameControllerAddMapping(mapping.c_str()) != -1)
{
success = true;
std::string guid = mapping.substr(0, mapping.find_first_of(','));
recentGamepadGUIDs[guid] = true;
// FIXME: massive hack until missing APIs are added to SDL 2:
// https://bugzilla.libsdl.org/show_bug.cgi?id=1975
std::string guid = mapping.substr(0, mapping.find_first_of(','));
checkGamepads(guid);
}
}
@@ -510,26 +517,30 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
throw love::Exception("Invalid gamepad mappings.");
}
std::string JoystickModule::saveGamepadMapping(const std::string &pguid)
std::string JoystickModule::saveGamepadMappings()
{
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(pguid.c_str());
std::string mappings;
std::string mapping;
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
for (const auto &g : recentGamepadGUIDs)
{
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(g.first.c_str());
if (sdlmapping == nullptr)
throw love::Exception("The specified Joystick GUID string has no gamepad mapping.");
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
if (sdlmapping == nullptr)
continue;
mapping = sdlmapping;
SDL_free(sdlmapping);
std::string mapping = sdlmapping;
SDL_free(sdlmapping);
if (mapping.find_last_of(',') != mapping.size() - 1)
mapping += ",";
if (mapping.find_last_of(',') != mapping.length() - 1)
mapping += ",";
// Matches SDL_GameControllerAddMappingsFromRW.
mapping += "platform:" + std::string(SDL_GetPlatform()) + ",";
// Matches SDL_GameControllerAddMappingsFromRW.
mapping += "platform:" + std::string(SDL_GetPlatform()) + ",\n";
mappings += mapping;
}
return mapping;
return mappings;
}
} // sdl
+6 -1
View File
@@ -28,6 +28,7 @@
#include <string>
#include <vector>
#include <list>
#include <map>
namespace love
{
@@ -57,7 +58,7 @@ public:
bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput);
Joystick::JoystickInput getGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput);
void loadGamepadMappings(const std::string &mappings);
std::string saveGamepadMapping(const std::string &pguid);
std::string saveGamepadMappings();
private:
@@ -76,6 +77,10 @@ private:
// Persistent list of all Joysticks which have been connected at some point.
std::list<Joystick *> joysticks;
// Persistent map indicating GUIDs for Gamepads which have been connected or
// modified at some point.
std::map<std::string, bool> recentGamepadGUIDs;
}; // JoystickModule
} // sdl