Browse Source

Add Joystick:getGamepadMappingString and love.joystick.getGamepadMappingString(guid). Resolves issue #1371.

Alex Szpakowski 6 years ago
parent
commit
33bc8aabfc

+ 1 - 0
src/modules/joystick/Joystick.h

@@ -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;
 

+ 6 - 1
src/modules/joystick/JoystickModule.h

@@ -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

+ 27 - 0
src/modules/joystick/sdl/Joystick.cpp

@@ -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;

+ 1 - 0
src/modules/joystick/sdl/Joystick.h

@@ -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;
 

+ 19 - 0
src/modules/joystick/sdl/JoystickModule.cpp

@@ -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;

+ 13 - 11
src/modules/joystick/sdl/JoystickModule.h

@@ -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;
-
-	bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput);
-	void loadGamepadMappings(const std::string &mappings);
-	std::string saveGamepadMappings();
+	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) override;
+	void loadGamepadMappings(const std::string &mappings) override;
+
+	std::string getGamepadMappingString(const std::string &guid) const override;
+	std::string saveGamepadMappings() override;
 
 private:
 

+ 12 - 0
src/modules/joystick/wrap_Joystick.cpp

@@ -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 },

+ 12 - 0
src/modules/joystick/wrap_JoystickModule.cpp

@@ -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 }
 };