Input: Fallback in FindKeyCode to prevent crashes on incompatible config

This commit is contained in:
Unknown 2017-11-28 23:24:22 +01:00 committed by Ivan
parent 6702c14b88
commit 5810b29298
3 changed files with 66 additions and 44 deletions

View File

@ -333,21 +333,54 @@ protected:
}
// Search an unordered map for a string value and return found keycode
int FindKeyCode(std::unordered_map<u32, std::string> map, const std::string& name)
int FindKeyCode(std::unordered_map<u32, std::string> map, const cfg::string& name, bool fallback = true)
{
std::string def = name.def;
std::string nam = name.to_string();
int def_code = -1;
for (auto it = map.begin(); it != map.end(); ++it)
{
if (it->second == name) return it->first;
if (it->second == nam)
return it->first;
if (fallback && it->second == def)
def_code = it->first;
}
return -1;
if (fallback)
{
LOG_ERROR(HLE, "int FindKeyCode for [name = %s] returned with [def_code = %d] for [def = %s]", nam, def_code, def);
if (def_code < 0)
def_code = 0;
}
return def_code;
};
long FindKeyCode(std::unordered_map<u64, std::string> map, const std::string& name)
long FindKeyCode(std::unordered_map<u64, std::string> map, const cfg::string& name, bool fallback = true)
{
std::string def = name.def;
std::string nam = name.to_string();
int def_code = -1;
for (auto it = map.begin(); it != map.end(); ++it)
{
if (it->second == name) return it->first;
if (it->second == nam)
return it->first;
if (fallback && it->second == def)
def_code = it->first;
}
return -1;
if (fallback)
{
LOG_ERROR(HLE, "long FindKeyCode for [name = %s] returned with [def_code = %d] for [def = %s]", nam, def_code, def);
if (def_code < 0)
def_code = 0;
}
return def_code;
};
// Get normalized trigger value based on the range defined by a threshold

View File

@ -650,27 +650,21 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
std::unordered_map<int, bool> axis_orientations;
int i = 0; // increment to know the axis location (17-24). Be careful if you ever add more find_key() calls in here (BUTTON_COUNT = 17)
auto find_key = [&](const std::string& name)
auto find_key = [&](const cfg::string& name)
{
int key = FindKeyCode(button_list, name);
if (key < 0)
{
key = FindKeyCode(axis_list, name);
int key = FindKeyCode(axis_list, name, false);
if (key >= 0)
{
axis_orientations.emplace(i, false);
}
}
if (key < 0)
{
key = FindKeyCode(rev_axis_list, name);
key = FindKeyCode(rev_axis_list, name, false);
if (key >= 0)
{
axis_orientations.emplace(i, true);
}
}
if (key < 0)
key = std::stoi(name);
key = FindKeyCode(button_list, name);
i++;
return key;

View File

@ -143,18 +143,13 @@ bool mm_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::s
joy_device->device_name = device;
joy_device->device_id = id;
auto find_key = [=](const std::string& name)
auto find_key = [=](const cfg::string& name)
{
long key = FindKeyCode(button_list, name);
long key = FindKeyCode(button_list, name, false);
if (key < 0)
key = FindKeyCode(pov_list, name);
key = FindKeyCode(pov_list, name, false);
if (key < 0)
key = FindKeyCode(axis_list, name);
if (key < 0)
{
LOG_ERROR(HLE, "mmjoystick FindKey(%s) returned value %d", name, key);
key = -1;
}
return key;
};