2013-04-08 14:27:38 +00:00
|
|
|
#include "InputConfig.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2013-06-14 12:34:12 +00:00
|
|
|
#include "Log.h"
|
2017-11-10 19:16:42 +00:00
|
|
|
#include <pugixml/src/pugixml.hpp>
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
//some util functions
|
|
|
|
std::string inputTypeToString(InputType type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case TYPE_AXIS:
|
|
|
|
return "axis";
|
|
|
|
case TYPE_BUTTON:
|
|
|
|
return "button";
|
|
|
|
case TYPE_HAT:
|
|
|
|
return "hat";
|
|
|
|
case TYPE_KEY:
|
|
|
|
return "key";
|
2017-11-08 22:22:15 +00:00
|
|
|
case TYPE_CEC_BUTTON:
|
|
|
|
return "cec-button";
|
2013-04-11 22:27:27 +00:00
|
|
|
default:
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InputType stringToInputType(const std::string& type)
|
|
|
|
{
|
|
|
|
if(type == "axis")
|
|
|
|
return TYPE_AXIS;
|
|
|
|
if(type == "button")
|
|
|
|
return TYPE_BUTTON;
|
|
|
|
if(type == "hat")
|
|
|
|
return TYPE_HAT;
|
|
|
|
if(type == "key")
|
|
|
|
return TYPE_KEY;
|
2017-11-08 22:22:15 +00:00
|
|
|
if(type == "cec-button")
|
|
|
|
return TYPE_CEC_BUTTON;
|
2013-04-11 22:27:27 +00:00
|
|
|
return TYPE_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-08 14:27:38 +00:00
|
|
|
std::string toLower(std::string str)
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < str.length(); i++)
|
|
|
|
{
|
|
|
|
str[i] = tolower(str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2013-04-11 22:27:27 +00:00
|
|
|
//end util functions
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
InputConfig::InputConfig(int deviceId, const std::string& deviceName, const std::string& deviceGUID) : mDeviceId(deviceId), mDeviceName(deviceName), mDeviceGUID(deviceGUID)
|
2013-04-08 14:27:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputConfig::clear()
|
|
|
|
{
|
|
|
|
mNameMap.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
bool InputConfig::isConfigured()
|
|
|
|
{
|
|
|
|
return mNameMap.size() > 0;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:27:38 +00:00
|
|
|
void InputConfig::mapInput(const std::string& name, Input input)
|
|
|
|
{
|
|
|
|
mNameMap[toLower(name)] = input;
|
|
|
|
}
|
|
|
|
|
2014-04-13 02:09:54 +00:00
|
|
|
void InputConfig::unmapInput(const std::string& name)
|
|
|
|
{
|
|
|
|
auto it = mNameMap.find(toLower(name));
|
|
|
|
if(it != mNameMap.end())
|
|
|
|
mNameMap.erase(it);
|
|
|
|
}
|
|
|
|
|
2014-05-31 23:00:42 +00:00
|
|
|
bool InputConfig::getInputByName(const std::string& name, Input* result)
|
2013-04-08 14:27:38 +00:00
|
|
|
{
|
2014-05-31 23:00:42 +00:00
|
|
|
auto it = mNameMap.find(toLower(name));
|
|
|
|
if(it != mNameMap.end())
|
|
|
|
{
|
|
|
|
*result = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-04-08 14:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InputConfig::isMappedTo(const std::string& name, Input input)
|
|
|
|
{
|
2014-05-31 23:00:42 +00:00
|
|
|
Input comp;
|
|
|
|
if(!getInputByName(name, &comp))
|
|
|
|
return false;
|
|
|
|
|
2013-04-08 14:27:38 +00:00
|
|
|
if(comp.configured && comp.type == input.type && comp.id == input.id)
|
|
|
|
{
|
|
|
|
if(comp.type == TYPE_HAT)
|
|
|
|
{
|
|
|
|
return (input.value == 0 || input.value & comp.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(comp.type == TYPE_AXIS)
|
|
|
|
{
|
|
|
|
return input.value == 0 || comp.value == input.value;
|
|
|
|
}else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> InputConfig::getMappedTo(Input input)
|
|
|
|
{
|
|
|
|
std::vector<std::string> maps;
|
|
|
|
|
|
|
|
typedef std::map<std::string, Input>::iterator it_type;
|
|
|
|
for(it_type iterator = mNameMap.begin(); iterator != mNameMap.end(); iterator++)
|
|
|
|
{
|
|
|
|
Input chk = iterator->second;
|
|
|
|
|
|
|
|
if(!chk.configured)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(chk.device == input.device && chk.type == input.type && chk.id == input.id)
|
|
|
|
{
|
|
|
|
if(chk.type == TYPE_HAT)
|
|
|
|
{
|
|
|
|
if(input.value == 0 || input.value & chk.value)
|
|
|
|
{
|
|
|
|
maps.push_back(iterator->first);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input.type == TYPE_AXIS)
|
|
|
|
{
|
|
|
|
if(input.value == 0 || chk.value == input.value)
|
|
|
|
maps.push_back(iterator->first);
|
|
|
|
}else{
|
|
|
|
maps.push_back(iterator->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maps;
|
|
|
|
}
|
|
|
|
|
2017-11-10 19:16:42 +00:00
|
|
|
void InputConfig::loadFromXML(pugi::xml_node& node)
|
2013-04-11 22:27:27 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
clear();
|
2013-04-11 22:27:27 +00:00
|
|
|
|
|
|
|
for(pugi::xml_node input = node.child("input"); input; input = input.next_sibling("input"))
|
|
|
|
{
|
|
|
|
std::string name = input.attribute("name").as_string();
|
|
|
|
std::string type = input.attribute("type").as_string();
|
|
|
|
InputType typeEnum = stringToInputType(type);
|
|
|
|
|
|
|
|
if(typeEnum == TYPE_COUNT)
|
|
|
|
{
|
2013-06-14 12:34:12 +00:00
|
|
|
LOG(LogError) << "InputConfig load error - input of type \"" << type << "\" is invalid! Skipping input \"" << name << "\".\n";
|
2013-04-11 22:27:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int id = input.attribute("id").as_int();
|
|
|
|
int value = input.attribute("value").as_int();
|
|
|
|
|
|
|
|
if(value == 0)
|
2013-06-14 12:34:12 +00:00
|
|
|
LOG(LogWarning) << "WARNING: InputConfig value is 0 for " << type << " " << id << "!\n";
|
2013-04-11 22:27:27 +00:00
|
|
|
|
|
|
|
mNameMap[toLower(name)] = Input(mDeviceId, typeEnum, id, value, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 19:16:42 +00:00
|
|
|
void InputConfig::writeToXML(pugi::xml_node& parent)
|
2013-04-11 22:27:27 +00:00
|
|
|
{
|
|
|
|
pugi::xml_node cfg = parent.append_child("inputConfig");
|
|
|
|
|
|
|
|
if(mDeviceId == DEVICE_KEYBOARD)
|
2013-04-12 02:59:19 +00:00
|
|
|
{
|
2013-04-11 22:27:27 +00:00
|
|
|
cfg.append_attribute("type") = "keyboard";
|
2014-03-22 01:12:57 +00:00
|
|
|
cfg.append_attribute("deviceName") = "Keyboard";
|
2013-04-12 02:59:19 +00:00
|
|
|
}else{
|
2013-04-11 22:27:27 +00:00
|
|
|
cfg.append_attribute("type") = "joystick";
|
2013-08-18 17:17:24 +00:00
|
|
|
cfg.append_attribute("deviceName") = mDeviceName.c_str();
|
2013-04-12 02:59:19 +00:00
|
|
|
}
|
2013-04-11 22:27:27 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
cfg.append_attribute("deviceGUID") = mDeviceGUID.c_str();
|
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
typedef std::map<std::string, Input>::iterator it_type;
|
|
|
|
for(it_type iterator = mNameMap.begin(); iterator != mNameMap.end(); iterator++)
|
|
|
|
{
|
2013-06-14 12:34:12 +00:00
|
|
|
if(!iterator->second.configured)
|
|
|
|
continue;
|
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
pugi::xml_node input = cfg.append_child("input");
|
|
|
|
input.append_attribute("name") = iterator->first.c_str();
|
|
|
|
input.append_attribute("type") = inputTypeToString(iterator->second.type).c_str();
|
|
|
|
input.append_attribute("id").set_value(iterator->second.id);
|
|
|
|
input.append_attribute("value").set_value(iterator->second.value);
|
|
|
|
}
|
|
|
|
}
|