Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input: export additional info for joysticks inputs #797

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions es-core/src/InputConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "InputConfig.h"

#include "Log.h"
#include "utils/StringUtil.h"
#include <pugixml/src/pugixml.hpp>

//some util functions
Expand Down Expand Up @@ -39,19 +40,10 @@ InputType stringToInputType(const std::string& type)
}


std::string toLower(std::string str)
{
for(unsigned int i = 0; i < str.length(); i++)
{
str[i] = (char)tolower(str[i]);
}

return str;
}
//end util functions

InputConfig::InputConfig(int deviceId, const std::string& deviceName, const std::string& deviceGUID) : mDeviceId(deviceId), mDeviceName(deviceName), mDeviceGUID(deviceGUID)
{
mVendorId = 0;
mProductId = 0;
}

void InputConfig::clear()
Expand All @@ -66,19 +58,19 @@ bool InputConfig::isConfigured()

void InputConfig::mapInput(const std::string& name, Input input)
{
mNameMap[toLower(name)] = input;
mNameMap[Utils::String::toLower(name)] = input;
}

void InputConfig::unmapInput(const std::string& name)
{
auto it = mNameMap.find(toLower(name));
auto it = mNameMap.find(Utils::String::toLower(name));
if(it != mNameMap.cend())
mNameMap.erase(it);
}

bool InputConfig::getInputByName(const std::string& name, Input* result)
{
auto it = mNameMap.find(toLower(name));
auto it = mNameMap.find(Utils::String::toLower(name));
if(it != mNameMap.cend())
{
*result = it->second;
Expand Down Expand Up @@ -188,7 +180,7 @@ void InputConfig::loadFromXML(pugi::xml_node& node)
if(value == 0)
LOG(LogWarning) << "WARNING: InputConfig value is 0 for " << type << " " << id << "!\n";

mNameMap[toLower(name)] = Input(mDeviceId, typeEnum, id, value, true);
mNameMap[Utils::String::toLower(name)] = Input(mDeviceId, typeEnum, id, value, true);
}
}

Expand All @@ -210,6 +202,11 @@ void InputConfig::writeToXML(pugi::xml_node& parent)
{
cfg.append_attribute("type") = "joystick";
cfg.append_attribute("deviceName") = mDeviceName.c_str();
if(mVendorId && mProductId)
{
cfg.append_attribute("vendorId") = mVendorId;
cfg.append_attribute("productId") = mProductId;
}
}

cfg.append_attribute("deviceGUID") = mDeviceGUID.c_str();
Expand Down
10 changes: 9 additions & 1 deletion es-core/src/InputConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ class InputConfig
void mapInput(const std::string& name, Input input);
void unmapInput(const std::string& name); // unmap all Inputs mapped to this name

inline int getDeviceId() const { return mDeviceId; };
inline int getDeviceId() const { return mDeviceId; }
inline const std::string& getDeviceName() { return mDeviceName; }
inline const std::string& getDeviceGUIDString() { return mDeviceGUID; }
inline const unsigned short getVendorId() { return mVendorId; }
inline const unsigned short getProductId() { return mProductId; }

inline void setVendorId(unsigned short vendorID) { mVendorId = vendorID; }
inline void setProductId(unsigned short productID) { mProductId = productID; }

//Returns true if Input is mapped to this name, false otherwise.
bool isMappedTo(const std::string& name, Input input);
Expand All @@ -127,6 +132,9 @@ class InputConfig
const int mDeviceId;
const std::string mDeviceName;
const std::string mDeviceGUID;

unsigned short mVendorId;
unsigned short mProductId;
};

#endif // ES_CORE_INPUT_CONFIG_H
5 changes: 5 additions & 0 deletions es-core/src/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ void InputManager::addJoystickByDeviceIndex(int id)

// create the InputConfig
mInputConfigs[joyId] = new InputConfig(joyId, SDL_JoystickName(joy), guid);

// add Vendor and Product IDs
mInputConfigs[joyId]->setVendorId(SDL_JoystickGetVendor(joy));
mInputConfigs[joyId]->setProductId(SDL_JoystickGetProduct(joy));

if(!loadInputConfig(mInputConfigs[joyId]))
{
LOG(LogInfo) << "Added unconfigured joystick " << SDL_JoystickName(joy) << " (GUID: " << guid << ", instance ID: " << joyId << ", device index: " << id << ").";
Expand Down