mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Add Joystick:getGamepadMappingString and love.joystick.getGamepadMappingString(guid). Resolves issue #1371.
This commit is contained in:
@@ -153,6 +153,7 @@ public:
|
||||
virtual bool isGamepadDown(const std::vector<GamepadButton> &blist) const = 0;
|
||||
|
||||
virtual JoystickInput getGamepadMapping(const GamepadInput &input) const = 0;
|
||||
virtual std::string getGamepadMappingString() const = 0;
|
||||
|
||||
virtual void *getHandle() const = 0;
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
virtual ~JoystickModule() {}
|
||||
|
||||
// Implements Module.
|
||||
virtual ModuleType getModuleType() const { return M_JOYSTICK; }
|
||||
ModuleType getModuleType() const override { return M_JOYSTICK; }
|
||||
|
||||
/**
|
||||
* Adds a connected Joystick device and opens it for use.
|
||||
@@ -96,6 +96,11 @@ public:
|
||||
**/
|
||||
virtual std::string saveGamepadMappings() = 0;
|
||||
|
||||
/**
|
||||
* Gets the gamepad mapping string for the given GUID.
|
||||
**/
|
||||
virtual std::string getGamepadMappingString(const std::string &guid) const = 0;
|
||||
|
||||
}; // JoystickModule
|
||||
|
||||
} // joystick
|
||||
|
||||
@@ -301,6 +301,33 @@ Joystick::JoystickInput Joystick::getGamepadMapping(const GamepadInput &input) c
|
||||
return jinput;
|
||||
}
|
||||
|
||||
std::string Joystick::getGamepadMappingString() const
|
||||
{
|
||||
char *sdlmapping = nullptr;
|
||||
|
||||
if (controller != nullptr)
|
||||
sdlmapping = SDL_GameControllerMapping(controller);
|
||||
|
||||
if (sdlmapping == nullptr)
|
||||
{
|
||||
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(pguid.c_str());
|
||||
sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
|
||||
}
|
||||
|
||||
if (sdlmapping == nullptr)
|
||||
return "";
|
||||
|
||||
std::string mappingstr(sdlmapping);
|
||||
SDL_free(sdlmapping);
|
||||
|
||||
// Matches SDL_GameControllerAddMappingsFromRW.
|
||||
if (mappingstr.find_last_of(',') != mappingstr.length() - 1)
|
||||
mappingstr += ",";
|
||||
mappingstr += "platform:" + std::string(SDL_GetPlatform());
|
||||
|
||||
return mappingstr;
|
||||
}
|
||||
|
||||
void *Joystick::getHandle() const
|
||||
{
|
||||
return joyhandle;
|
||||
|
||||
@@ -68,6 +68,7 @@ public:
|
||||
bool isGamepadDown(const std::vector<GamepadButton> &blist) const override;
|
||||
|
||||
JoystickInput getGamepadMapping(const GamepadInput &input) const override;
|
||||
std::string getGamepadMappingString() const override;
|
||||
|
||||
void *getHandle() const override;
|
||||
|
||||
|
||||
@@ -451,6 +451,25 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
|
||||
throw love::Exception("Invalid gamepad mappings.");
|
||||
}
|
||||
|
||||
std::string JoystickModule::getGamepadMappingString(const std::string &guid) const
|
||||
{
|
||||
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(guid.c_str());
|
||||
|
||||
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
|
||||
if (sdlmapping == nullptr)
|
||||
return "";
|
||||
|
||||
std::string mapping(sdlmapping);
|
||||
SDL_free(sdlmapping);
|
||||
|
||||
// Matches SDL_GameControllerAddMappingsFromRW.
|
||||
if (mapping.find_last_of(',') != mapping.length() - 1)
|
||||
mapping += ",";
|
||||
mapping += "platform:" + std::string(SDL_GetPlatform());
|
||||
|
||||
return mapping;
|
||||
}
|
||||
|
||||
std::string JoystickModule::saveGamepadMappings()
|
||||
{
|
||||
std::string mappings;
|
||||
|
||||
@@ -45,19 +45,21 @@ public:
|
||||
virtual ~JoystickModule();
|
||||
|
||||
// Implements Module.
|
||||
const char *getName() const;
|
||||
const char *getName() const override;
|
||||
|
||||
// Implements JoystickModule.
|
||||
love::joystick::Joystick *addJoystick(int deviceindex);
|
||||
void removeJoystick(love::joystick::Joystick *joystick);
|
||||
love::joystick::Joystick *getJoystickFromID(int instanceid);
|
||||
love::joystick::Joystick *getJoystick(int joyindex);
|
||||
int getIndex(const love::joystick::Joystick *joystick);
|
||||
int getJoystickCount() const;
|
||||
love::joystick::Joystick *addJoystick(int deviceindex) override;
|
||||
void removeJoystick(love::joystick::Joystick *joystick) override;
|
||||
love::joystick::Joystick *getJoystickFromID(int instanceid) override;
|
||||
love::joystick::Joystick *getJoystick(int joyindex) override;
|
||||
int getIndex(const love::joystick::Joystick *joystick) override;
|
||||
int getJoystickCount() const override;
|
||||
|
||||
bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput);
|
||||
void loadGamepadMappings(const std::string &mappings);
|
||||
std::string saveGamepadMappings();
|
||||
bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput) override;
|
||||
void loadGamepadMappings(const std::string &mappings) override;
|
||||
|
||||
std::string getGamepadMappingString(const std::string &guid) const override;
|
||||
std::string saveGamepadMappings() override;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
@@ -292,6 +292,17 @@ int w_Joystick_getGamepadMapping(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_getGamepadMappingString(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
std::string mapping = j->getGamepadMappingString();
|
||||
if (mapping.empty())
|
||||
lua_pushnil(L);
|
||||
else
|
||||
luax_pushstring(L, mapping);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_isVibrationSupported(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
@@ -351,6 +362,7 @@ static const luaL_Reg w_Joystick_functions[] =
|
||||
{ "getGamepadAxis", w_Joystick_getGamepadAxis },
|
||||
{ "isGamepadDown", w_Joystick_isGamepadDown },
|
||||
{ "getGamepadMapping", w_Joystick_getGamepadMapping },
|
||||
{ "getGamepadMappingString", w_Joystick_getGamepadMappingString },
|
||||
|
||||
{ "isVibrationSupported", w_Joystick_isVibrationSupported },
|
||||
{ "setVibration", w_Joystick_setVibration },
|
||||
|
||||
@@ -160,6 +160,17 @@ int w_saveGamepadMappings(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getGamepadMappingString(lua_State *L)
|
||||
{
|
||||
const char *guid = luaL_checkstring(L, 1);
|
||||
std::string mapping = instance()->getGamepadMappingString(guid);
|
||||
if (mapping.empty())
|
||||
lua_pushnil(L);
|
||||
else
|
||||
luax_pushstring(L, mapping);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
@@ -168,6 +179,7 @@ static const luaL_Reg functions[] =
|
||||
{ "setGamepadMapping", w_setGamepadMapping },
|
||||
{ "loadGamepadMappings", w_loadGamepadMappings },
|
||||
{ "saveGamepadMappings", w_saveGamepadMappings },
|
||||
{ "getGamepadMappingString", w_getGamepadMappingString },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user