mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-01-31 20:41:45 +01:00
Input: Fallback in FindKeyCode to prevent crashes on incompatible config
This commit is contained in:
parent
6702c14b88
commit
5810b29298
@ -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
|
||||
|
@ -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);
|
||||
int key = FindKeyCode(axis_list, name, false);
|
||||
if (key >= 0)
|
||||
axis_orientations.emplace(i, false);
|
||||
|
||||
if (key < 0)
|
||||
{
|
||||
key = FindKeyCode(axis_list, name);
|
||||
key = FindKeyCode(rev_axis_list, name, false);
|
||||
if (key >= 0)
|
||||
{
|
||||
axis_orientations.emplace(i, false);
|
||||
}
|
||||
}
|
||||
if (key < 0)
|
||||
{
|
||||
key = FindKeyCode(rev_axis_list, name);
|
||||
if (key >= 0)
|
||||
{
|
||||
axis_orientations.emplace(i, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (key < 0)
|
||||
key = std::stoi(name);
|
||||
key = FindKeyCode(button_list, name);
|
||||
|
||||
i++;
|
||||
return key;
|
||||
@ -685,22 +679,22 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
|
||||
);
|
||||
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.triangle), CELL_PAD_CTRL_TRIANGLE);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.circle), CELL_PAD_CTRL_CIRCLE);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.cross), CELL_PAD_CTRL_CROSS);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.square), CELL_PAD_CTRL_SQUARE);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.l2), CELL_PAD_CTRL_L2);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.r2), CELL_PAD_CTRL_R2);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.l1), CELL_PAD_CTRL_L1);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.r1), CELL_PAD_CTRL_R1);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.start), CELL_PAD_CTRL_START);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.select), CELL_PAD_CTRL_SELECT);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.l3), CELL_PAD_CTRL_L3);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.r3), CELL_PAD_CTRL_R3);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.ps), 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.up), CELL_PAD_CTRL_UP);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.down), CELL_PAD_CTRL_DOWN);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.left), CELL_PAD_CTRL_LEFT);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.right), CELL_PAD_CTRL_RIGHT);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.circle), CELL_PAD_CTRL_CIRCLE);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.cross), CELL_PAD_CTRL_CROSS);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.square), CELL_PAD_CTRL_SQUARE);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.l2), CELL_PAD_CTRL_L2);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.r2), CELL_PAD_CTRL_R2);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.l1), CELL_PAD_CTRL_L1);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.r1), CELL_PAD_CTRL_R1);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.start), CELL_PAD_CTRL_START);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.select), CELL_PAD_CTRL_SELECT);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.l3), CELL_PAD_CTRL_L3);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.r3), CELL_PAD_CTRL_R3);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.ps), 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.up), CELL_PAD_CTRL_UP);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.down), CELL_PAD_CTRL_DOWN);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.left), CELL_PAD_CTRL_LEFT);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.right), CELL_PAD_CTRL_RIGHT);
|
||||
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x0); // Reserved
|
||||
|
||||
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X, find_key(m_pad_config.ls_left), find_key(m_pad_config.ls_right));
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user