1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 02:32:36 +01:00

input: move emulated pad handling to config class

This commit is contained in:
Megamouse 2023-05-22 23:10:13 +02:00
parent f4b346a6f8
commit e584653b72
6 changed files with 97 additions and 173 deletions

View File

@ -674,18 +674,12 @@ static void ds3_input_to_pad(const u32 port_no, be_t<u16>& digital_buttons, be_t
}
const auto& cfg = ::at32(g_cfg_gem.players, port_no);
for (const Button& button : pad->m_buttons)
{
if (!button.m_pressed)
continue;
for (const auto& btn : cfg->find_button(button.m_offset, button.m_outKeyCode))
cfg->handle_input(pad, true, [&](gem_btn btn, u16 value, bool pressed)
{
if (!btn)
continue;
if (!pressed)
return;
switch (btn->btn_id())
switch (btn)
{
case gem_btn::start:
digital_buttons |= CELL_GEM_CTRL_START;
@ -710,15 +704,14 @@ static void ds3_input_to_pad(const u32 port_no, be_t<u16>& digital_buttons, be_t
break;
case gem_btn::t:
digital_buttons |= CELL_GEM_CTRL_T;
analog_t = std::max<u16>(analog_t, button.m_value);
analog_t = std::max<u16>(analog_t, value);
break;
case gem_btn::x_axis:
case gem_btn::y_axis:
case gem_btn::count:
break;
}
}
}
});
}
constexpr u16 ds3_max_x = 255;
@ -730,36 +723,12 @@ static inline void ds3_get_stick_values(u32 port_no, const std::shared_ptr<Pad>&
y_pos = 0;
const auto& cfg = ::at32(g_cfg_gem.players, port_no);
std::function<void(u32 offset, u32 keycode, u16 value, bool check_axis)> handle_input;
handle_input = [&](u32 offset, u32 keycode, u16 value, bool check_axis)
{
const auto& btns = cfg->find_button(offset, keycode);
if (btns.empty())
cfg->handle_input(pad, true, [&](gem_btn btn, u16 value, bool pressed)
{
if (check_axis)
{
switch (offset)
{
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y:
handle_input(offset, static_cast<u32>(axis_direction::both), value, false);
break;
default:
break;
}
}
return;
}
if (!pressed)
return;
for (const auto& btn : btns)
{
if (!btn)
continue;
switch (btn->btn_id())
switch (btn)
{
case gem_btn::x_axis:
x_pos = value;
@ -770,13 +739,7 @@ static inline void ds3_get_stick_values(u32 port_no, const std::shared_ptr<Pad>&
default:
break;
}
}
};
for (const AnalogStick& stick : pad->m_sticks)
{
handle_input(stick.m_offset, get_axis_keycode(stick.m_offset, stick.m_value), stick.m_value, true);
}
});
}
template <typename T>

View File

@ -97,24 +97,16 @@ void usb_device_buzz::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpoint*/
for (u32 i = m_first_controller, index = 0; i <= m_last_controller; i++, index++)
{
const auto& pad = pads[i];
const cfg_buzzer* cfg = g_cfg_buzz.players[i];
if (!(pad->m_port_status & CELL_PAD_STATUS_CONNECTED))
{
continue;
}
for (const Button& button : pad->m_buttons)
{
if (!button.m_pressed)
continue;
for (const auto& btn : cfg->find_button(button.m_offset, button.m_outKeyCode))
const auto& cfg = g_cfg_buzz.players[i];
cfg->handle_input(pad, true, [&buf, &index](buzz_btn btn, u16 value, bool pressed)
{
if (!btn)
continue;
switch (btn->btn_id())
switch (btn)
{
case buzz_btn::red:
buf[2 + (0 + 5 * index) / 8] |= 1 << ((0 + 5 * index) % 8); // Red
@ -134,7 +126,6 @@ void usb_device_buzz::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpoint*/
case buzz_btn::count:
break;
}
}
}
});
}
}

View File

@ -140,42 +140,19 @@ void usb_device_ghltar::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpoint
std::lock_guard lock(pad::g_pad_mutex);
const auto handler = pad::get_current_handler();
const auto& pad = ::at32(handler->GetPads(), m_controller_index);
const auto& cfg = ::at32(g_cfg_ghltar.players, m_controller_index);
if (!(pad->m_port_status & CELL_PAD_STATUS_CONNECTED))
{
return;
}
std::function<void(u32 offset, u32 keycode, u16 value, bool check_axis)> handle_input;
handle_input = [&](u32 offset, u32 keycode, u16 value, bool check_axis)
{
const auto& btns = cfg->find_button(offset, keycode);
if (btns.empty())
const auto& cfg = ::at32(g_cfg_ghltar.players, m_controller_index);
cfg->handle_input(pad, true, [&buf](ghltar_btn btn, u16 value, bool pressed)
{
if (check_axis)
{
switch (offset)
{
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y:
handle_input(offset, static_cast<u32>(axis_direction::both), value, false);
break;
default:
break;
}
}
return;
}
if (!pressed)
return;
for (const auto& btn : btns)
{
if (!btn)
continue;
switch (btn->btn_id())
switch (btn)
{
case ghltar_btn::w1:
buf[0] += 0x01; // W1
@ -229,19 +206,5 @@ void usb_device_ghltar::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpoint
case ghltar_btn::count:
break;
}
}
};
for (const Button& button : pad->m_buttons)
{
if (button.m_pressed)
{
handle_input(button.m_offset, button.m_outKeyCode, button.m_value, true);
}
}
for (const AnalogStick& stick : pad->m_sticks)
{
handle_input(stick.m_offset, get_axis_keycode(stick.m_offset, stick.m_value), stick.m_value, true);
}
});
}

View File

@ -154,40 +154,17 @@ void usb_device_turntable::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpo
const auto handler = pad::get_current_handler();
const auto& pads = handler->GetPads();
const auto& pad = ::at32(pads, m_controller_index);
const auto& cfg = ::at32(g_cfg_turntable.players, m_controller_index);
if (!(pad->m_port_status & CELL_PAD_STATUS_CONNECTED))
return;
std::function<void(u32 offset, u32 keycode, u16 value, bool check_axis)> handle_input;
handle_input = [&](u32 offset, u32 keycode, u16 value, bool check_axis)
{
const auto& btns = cfg->find_button(offset, keycode);
if (btns.empty())
const auto& cfg = ::at32(g_cfg_turntable.players, m_controller_index);
cfg->handle_input(pad, true, [&buf](turntable_btn btn, u16 value, bool pressed)
{
if (check_axis)
{
switch (offset)
{
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y:
handle_input(offset, static_cast<u32>(axis_direction::both), value, false);
break;
default:
break;
}
}
return;
}
if (!pressed)
return;
for (const auto& btn : btns)
{
if (!btn)
continue;
switch (btn->btn_id())
switch (btn)
{
case turntable_btn::blue:
buf[0] |= 0x01; // Square Button
@ -306,19 +283,5 @@ void usb_device_turntable::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpo
case turntable_btn::count:
break;
}
}
};
for (const Button& button : pad->m_buttons)
{
if (button.m_pressed)
{
handle_input(button.m_offset, button.m_outKeyCode, button.m_value, true);
}
}
for (const AnalogStick& stick : pad->m_sticks)
{
handle_input(stick.m_offset, get_axis_keycode(stick.m_offset, stick.m_value), stick.m_value, true);
}
});
}

View File

@ -107,6 +107,56 @@ struct emulated_pad_config : cfg::node
init_button(static_cast<cfg_pad_btn<T>*>(n));
}
}
void handle_input(const std::function<void(T, u16, bool)>& func, u32 offset, u32 keycode, u16 value, bool pressed, bool check_axis) const
{
const auto& btns = find_button(offset, keycode);
if (btns.empty())
{
if (check_axis)
{
switch (offset)
{
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X:
case CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y:
handle_input(func, offset, static_cast<u32>(axis_direction::both), value, pressed, false);
break;
default:
break;
}
}
return;
}
for (const auto& btn : btns)
{
if (btn && func)
{
func(btn->btn_id(), value, pressed);
}
}
}
void handle_input(std::shared_ptr<Pad> pad, bool press_only, const std::function<void(T, u16, bool)>& func) const
{
if (!pad)
return;
for (const Button& button : pad->m_buttons)
{
if (button.m_pressed || !press_only)
{
handle_input(func, button.m_offset, button.m_outKeyCode, button.m_value, button.m_pressed, true);
}
}
for (const AnalogStick& stick : pad->m_sticks)
{
handle_input(func, stick.m_offset, get_axis_keycode(stick.m_offset, stick.m_value), stick.m_value, true, true);
}
}
};
template <typename T>

View File

@ -196,91 +196,85 @@ void usb_device_usio::translate_input()
return;
}
const auto& cfg = ::at32(g_cfg_usio.players, pad_number);
const std::size_t offset = (player * 8ULL);
for (const Button& button : pad->m_buttons)
{
for (const auto& btn : cfg->find_button(button.m_offset, button.m_outKeyCode))
const auto& cfg = ::at32(g_cfg_usio.players, pad_number);
cfg->handle_input(pad, false, [&](usio_btn btn, u16 value, bool pressed)
{
if (!btn)
continue;
switch (btn->btn_id())
switch (btn)
{
case usio_btn::test:
if (player != 0) break;
if (button.m_pressed && !test_key_pressed) // Solve the need to hold the Test key
if (pressed && !test_key_pressed) // Solve the need to hold the Test key
test_on = !test_on;
test_key_pressed = button.m_pressed;
test_key_pressed = pressed;
break;
case usio_btn::coin:
if (player != 0) break;
if (button.m_pressed && !coin_key_pressed) // Ensure only one coin is inserted each time the Coin key is pressed
if (pressed && !coin_key_pressed) // Ensure only one coin is inserted each time the Coin key is pressed
coin_counter++;
coin_key_pressed = button.m_pressed;
coin_key_pressed = pressed;
break;
case usio_btn::enter:
if (player == 0 && button.m_pressed)
if (player == 0 && pressed)
test_keys |= 0x200; // Enter
break;
case usio_btn::up:
if (player == 0 && button.m_pressed)
if (player == 0 && pressed)
test_keys |= 0x2000; // Up
break;
case usio_btn::down:
if (player == 0 && button.m_pressed)
if (player == 0 && pressed)
test_keys |= 0x1000; // Down
break;
case usio_btn::service:
if (player == 0 && button.m_pressed)
if (player == 0 && pressed)
test_keys |= 0x4000; // Service
break;
case usio_btn::strong_hit_side_left:
// Strong hit side left
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 32 + offset, &c_big_hit, sizeof(u16));
break;
case usio_btn::strong_hit_center_right:
// Strong hit center right
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 36 + offset, &c_big_hit, sizeof(u16));
break;
case usio_btn::strong_hit_side_right:
// Strong hit side right
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 38 + offset, &c_big_hit, sizeof(u16));
break;
case usio_btn::strong_hit_center_left:
// Strong hit center left
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 34 + offset, &c_big_hit, sizeof(u16));
break;
case usio_btn::small_hit_center_left:
// Small hit center left
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 34 + offset, &c_small_hit, sizeof(u16));
break;
case usio_btn::small_hit_center_right:
// Small hit center right
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 36 + offset, &c_small_hit, sizeof(u16));
break;
case usio_btn::small_hit_side_left:
// Small hit side left
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 32 + offset, &c_small_hit, sizeof(u16));
break;
case usio_btn::small_hit_side_right:
// Small hit side right
if (button.m_pressed)
if (pressed)
std::memcpy(input_buf.data() + 38 + offset, &c_small_hit, sizeof(u16));
break;
case usio_btn::count:
break;
}
}
}
});
};
translate_from_pad(0, 0);