Browse Source

Add Joystick:getDeviceInfo(). Returns USB vendor id, product id, and product version numbers (consistent across operating systems).

Can be used to show different icons, etc. for different gamepads.
Alex Szpakowski 6 years ago
parent
commit
aca979c3ea

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

@@ -160,6 +160,8 @@ public:
 	virtual int getInstanceID() const = 0;
 	virtual int getID() const = 0;
 
+	virtual void getDeviceInfo(int &vendorID, int &productID, int &productVersion) const = 0;
+
 	virtual bool isVibrationSupported() = 0;
 	virtual bool setVibration(float left, float right, float duration = -1.0f) = 0;
 	virtual bool setVibration() = 0;

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

@@ -322,6 +322,24 @@ int Joystick::getID() const
 	return id;
 }
 
+void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion) const
+{
+#if SDL_VERSION_ATLEAST(2, 0, 6)
+	if (joyhandle != nullptr)
+	{
+		vendorID = SDL_JoystickGetVendor(joyhandle);
+		productID = SDL_JoystickGetProduct(joyhandle);
+		productVersion = SDL_JoystickGetProductVersion(joyhandle);
+	}
+	else
+#endif
+	{
+		vendorID = 0;
+		productID = 0;
+		productVersion = 0;
+	}
+}
+
 bool Joystick::checkCreateHaptic()
 {
 	if (!isConnected())

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

@@ -75,6 +75,8 @@ public:
 	int getInstanceID() const override;
 	int getID() const override;
 
+	void getDeviceInfo(int &vendorID, int &productID, int &productVersion) const override;
+
 	bool isVibrationSupported() override;
 	bool setVibration(float left, float right, float duration = -1.0f) override;
 	bool setVibration() override;

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

@@ -71,6 +71,20 @@ int w_Joystick_getGUID(lua_State *L)
 	return 1;
 }
 
+int w_Joystick_getDeviceInfo(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+
+	int vendorID = 0, productID = 0, productVersion = 0;
+	j->getDeviceInfo(vendorID, productID, productVersion);
+
+	lua_pushnumber(L, vendorID);
+	lua_pushnumber(L, productID);
+	lua_pushnumber(L, productVersion);
+
+	return 3;
+}
+
 int w_Joystick_getAxisCount(lua_State *L)
 {
 	Joystick *j = luax_checkjoystick(L, 1);
@@ -324,6 +338,7 @@ static const luaL_Reg w_Joystick_functions[] =
 	{ "getName", w_Joystick_getName },
 	{ "getID", w_Joystick_getID },
 	{ "getGUID", w_Joystick_getGUID },
+	{ "getDeviceInfo", w_Joystick_getDeviceInfo },
 	{ "getAxisCount", w_Joystick_getAxisCount },
 	{ "getButtonCount", w_Joystick_getButtonCount },
 	{ "getHatCount", w_Joystick_getHatCount },