input: fix minor data race

While usually not exposed to the user, there was a slight chance that user input was read in a dirty state.
This became apparent during usage of the new pressure sensitivity button
This commit is contained in:
Megamouse 2021-08-08 18:50:37 +02:00
parent 3fa9d23627
commit f0d51899c1
7 changed files with 69 additions and 52 deletions

View file

@ -160,18 +160,21 @@ void pad_thread::Init()
input_log.error("Failed to bind device %s to handler %s", g_cfg_input.player[i]->device.to_string(), handler_type);
nullpad->bindPadToDevice(m_pads[i], g_cfg_input.player[i]->device.to_string());
}
m_pads_interface[i] = std::make_shared<Pad>(CELL_PAD_STATUS_DISCONNECTED, pad_settings[i].device_capability, pad_settings[i].device_type);
*m_pads_interface[i] = *m_pads[i];
}
}
void pad_thread::SetRumble(const u32 pad, u8 largeMotor, bool smallMotor)
{
if (pad > m_pads.size())
if (pad > m_pads_interface.size())
return;
if (m_pads[pad]->m_vibrateMotors.size() >= 2)
if (m_pads_interface[pad]->m_vibrateMotors.size() >= 2)
{
m_pads[pad]->m_vibrateMotors[0].m_value = largeMotor;
m_pads[pad]->m_vibrateMotors[1].m_value = smallMotor ? 255 : 0;
m_pads_interface[pad]->m_vibrateMotors[0].m_value = largeMotor;
m_pads_interface[pad]->m_vibrateMotors[1].m_value = smallMotor ? 255 : 0;
}
}
@ -206,69 +209,74 @@ void pad_thread::ThreadFunc()
u32 connected_devices = 0;
// Copy public pad data - which might have been changed - to internal pads
{
std::lock_guard lock(pad::g_pad_mutex);
for (usz i = 0; i < m_pads.size(); i++)
{
*m_pads[i] = *m_pads_interface[i];
}
}
for (auto& cur_pad_handler : handlers)
{
cur_pad_handler.second->ThreadProc();
connected_devices += cur_pad_handler.second->connected_devices;
}
m_info.now_connect = connected_devices + num_ldd_pad;
// The following section is only reached when a dialog was closed and the pads are still intercepted.
// As long as any of the listed buttons is pressed, cellPadGetData will ignore all input (needed for Hotline Miami).
// ignore_input was added because if we keep the pads intercepted, then some games will enter the menu due to unexpected system interception (tested with Ninja Gaiden Sigma).
if (!(m_info.system_info & CELL_PAD_INFO_INTERCEPTED) && m_info.ignore_input)
// Copy new internal pad data back to public pads
{
std::lock_guard lock(pad::g_pad_mutex);
m_info.now_connect = connected_devices + num_ldd_pad;
// The input_ignored section is only reached when a dialog was closed and the pads are still intercepted.
// As long as any of the listed buttons is pressed, cellPadGetData will ignore all input (needed for Hotline Miami).
// ignore_input was added because if we keep the pads intercepted, then some games will enter the menu due to unexpected system interception (tested with Ninja Gaiden Sigma).
const bool input_ignored = m_info.ignore_input && !(m_info.system_info & CELL_PAD_INFO_INTERCEPTED);
bool any_button_pressed = false;
for (const auto& pad : m_pads)
for (usz i = 0; i < m_pads.size(); i++)
{
const auto& pad = m_pads[i];
// I guess this is the best place to add pressure sensitivity without too much code duplication.
if (pad->m_port_status & CELL_PAD_STATUS_CONNECTED)
{
for (const auto& button : pad->m_buttons)
const bool adjust_pressure = pad->m_pressure_intensity_button_index >= 0 && pad->m_buttons[pad->m_pressure_intensity_button_index].m_pressed;
for (auto& button : pad->m_buttons)
{
if (button.m_pressed && (
button.m_outKeyCode == CELL_PAD_CTRL_CROSS ||
button.m_outKeyCode == CELL_PAD_CTRL_CIRCLE ||
button.m_outKeyCode == CELL_PAD_CTRL_TRIANGLE ||
button.m_outKeyCode == CELL_PAD_CTRL_SQUARE ||
button.m_outKeyCode == CELL_PAD_CTRL_START ||
button.m_outKeyCode == CELL_PAD_CTRL_SELECT))
if (button.m_pressed)
{
any_button_pressed = true;
break;
if (button.m_outKeyCode == CELL_PAD_CTRL_CROSS ||
button.m_outKeyCode == CELL_PAD_CTRL_CIRCLE ||
button.m_outKeyCode == CELL_PAD_CTRL_TRIANGLE ||
button.m_outKeyCode == CELL_PAD_CTRL_SQUARE ||
button.m_outKeyCode == CELL_PAD_CTRL_START ||
button.m_outKeyCode == CELL_PAD_CTRL_SELECT)
{
any_button_pressed = true;
}
if (adjust_pressure)
{
button.m_value = pad->m_pressure_intensity;
}
}
}
if (any_button_pressed)
{
break;
}
}
*m_pads_interface[i] = *pad;
}
if (!any_button_pressed)
if (input_ignored && !any_button_pressed)
{
m_info.ignore_input = false;
}
}
// I guess this is the best place to add pressure sensitivity without too much code duplication.
for (const auto& pad : m_pads)
{
if ((pad->m_port_status & CELL_PAD_STATUS_CONNECTED) &&
pad->m_pressure_intensity_button_index >= 0 && pad->m_buttons[pad->m_pressure_intensity_button_index].m_pressed)
{
for (auto& button : pad->m_buttons)
{
if (button.m_pressed)
{
button.m_value = pad->m_pressure_intensity;
}
}
}
}
std::this_thread::sleep_for(1ms);
}
}
@ -295,6 +303,8 @@ void pad_thread::InitLddPad(u32 handle)
50
);
*m_pads_interface[handle] = *m_pads[handle];
num_ldd_pad++;
}
@ -315,6 +325,10 @@ s32 pad_thread::AddLddPad()
void pad_thread::UnregisterLddPad(u32 handle)
{
ensure(handle < m_pads.size());
m_pads[handle]->ldd = false;
m_pads_interface[handle]->ldd = false;
num_ldd_pad--;
}