2019-09-20 06:47:41 +00:00
|
|
|
#include "digital_controller.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/assert.h"
|
2020-08-23 12:47:49 +00:00
|
|
|
#include "host_interface.h"
|
2021-01-23 16:52:52 +00:00
|
|
|
#include "system.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/state_wrapper.h"
|
2019-09-20 06:47:41 +00:00
|
|
|
|
|
|
|
DigitalController::DigitalController() = default;
|
|
|
|
|
|
|
|
DigitalController::~DigitalController() = default;
|
|
|
|
|
2019-12-14 14:17:43 +00:00
|
|
|
ControllerType DigitalController::GetType() const
|
|
|
|
{
|
|
|
|
return ControllerType::DigitalController;
|
|
|
|
}
|
|
|
|
|
2019-12-14 14:31:07 +00:00
|
|
|
std::optional<s32> DigitalController::GetAxisCodeByName(std::string_view axis_name) const
|
|
|
|
{
|
|
|
|
return StaticGetAxisCodeByName(axis_name);
|
|
|
|
}
|
|
|
|
|
2019-12-14 14:17:43 +00:00
|
|
|
std::optional<s32> DigitalController::GetButtonCodeByName(std::string_view button_name) const
|
|
|
|
{
|
|
|
|
return StaticGetButtonCodeByName(button_name);
|
|
|
|
}
|
|
|
|
|
2020-04-25 15:18:15 +00:00
|
|
|
void DigitalController::Reset()
|
|
|
|
{
|
|
|
|
m_transfer_state = TransferState::Idle;
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
bool DigitalController::DoState(StateWrapper& sw, bool apply_input_state)
|
2020-04-25 15:18:15 +00:00
|
|
|
{
|
2020-12-16 14:09:32 +00:00
|
|
|
if (!Controller::DoState(sw, apply_input_state))
|
2020-04-25 15:18:15 +00:00
|
|
|
return false;
|
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
u16 button_state = m_button_state;
|
|
|
|
sw.Do(&button_state);
|
|
|
|
if (apply_input_state)
|
2021-03-27 03:42:02 +00:00
|
|
|
m_button_state = button_state;
|
2020-12-16 14:09:32 +00:00
|
|
|
|
2020-04-25 15:18:15 +00:00
|
|
|
sw.Do(&m_transfer_state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:51:08 +00:00
|
|
|
bool DigitalController::GetButtonState(s32 button_code) const
|
|
|
|
{
|
|
|
|
if (button_code < 0 || button_code >= static_cast<s32>(Button::Count))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const u16 bit = u16(1) << static_cast<u8>(button_code);
|
|
|
|
return ((m_button_state & bit) == 0);
|
|
|
|
}
|
2019-12-14 14:31:07 +00:00
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
void DigitalController::SetButtonState(Button button, bool pressed)
|
|
|
|
{
|
2021-01-23 16:52:52 +00:00
|
|
|
const u16 bit = u16(1) << static_cast<u8>(button);
|
2019-09-20 06:47:41 +00:00
|
|
|
if (pressed)
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
|
|
|
if (m_button_state & bit)
|
|
|
|
System::SetRunaheadReplayFlag();
|
|
|
|
|
|
|
|
m_button_state &= ~bit;
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
else
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
|
|
|
if (!(m_button_state & bit))
|
|
|
|
System::SetRunaheadReplayFlag();
|
|
|
|
|
|
|
|
m_button_state |= bit;
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 15:06:58 +00:00
|
|
|
void DigitalController::SetButtonState(s32 button_code, bool pressed)
|
|
|
|
{
|
|
|
|
if (button_code < 0 || button_code >= static_cast<s32>(Button::Count))
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetButtonState(static_cast<Button>(button_code), pressed);
|
|
|
|
}
|
|
|
|
|
2020-12-06 05:47:00 +00:00
|
|
|
u32 DigitalController::GetButtonStateBits() const
|
|
|
|
{
|
|
|
|
return m_button_state ^ 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
void DigitalController::ResetTransferState()
|
|
|
|
{
|
2019-10-26 12:33:23 +00:00
|
|
|
m_transfer_state = TransferState::Idle;
|
2019-09-29 15:07:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
bool DigitalController::Transfer(const u8 data_in, u8* data_out)
|
|
|
|
{
|
2019-10-26 12:33:23 +00:00
|
|
|
static constexpr u16 ID = 0x5A41;
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-10-26 12:33:23 +00:00
|
|
|
switch (m_transfer_state)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2019-10-26 12:33:23 +00:00
|
|
|
case TransferState::Idle:
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2021-02-21 23:20:31 +00:00
|
|
|
*data_out = 0xFF;
|
|
|
|
|
|
|
|
if (data_in == 0x01)
|
|
|
|
{
|
|
|
|
m_transfer_state = TransferState::Ready;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TransferState::Ready:
|
|
|
|
{
|
2019-10-26 12:33:23 +00:00
|
|
|
if (data_in == 0x42)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2019-10-26 12:33:23 +00:00
|
|
|
*data_out = Truncate8(ID);
|
|
|
|
m_transfer_state = TransferState::IDMSB;
|
|
|
|
return true;
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
2021-02-21 23:20:31 +00:00
|
|
|
|
|
|
|
*data_out = 0xFF;
|
|
|
|
return false;
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 12:33:23 +00:00
|
|
|
case TransferState::IDMSB:
|
|
|
|
{
|
|
|
|
*data_out = Truncate8(ID >> 8);
|
|
|
|
m_transfer_state = TransferState::ButtonsLSB;
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-10-26 12:33:23 +00:00
|
|
|
case TransferState::ButtonsLSB:
|
|
|
|
{
|
2020-12-10 07:39:11 +00:00
|
|
|
*data_out = Truncate8(m_button_state) & GetButtonsLSBMask();
|
2019-10-26 12:33:23 +00:00
|
|
|
m_transfer_state = TransferState::ButtonsMSB;
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-10-26 12:33:23 +00:00
|
|
|
case TransferState::ButtonsMSB:
|
|
|
|
*data_out = Truncate8(m_button_state >> 8);
|
|
|
|
m_transfer_state = TransferState::Idle;
|
|
|
|
return false;
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-10-26 12:33:23 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
UnreachableCode();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 13:20:24 +00:00
|
|
|
std::unique_ptr<DigitalController> DigitalController::Create()
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2019-12-14 13:20:24 +00:00
|
|
|
return std::make_unique<DigitalController>();
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
2019-12-08 14:46:04 +00:00
|
|
|
|
2019-12-14 14:31:07 +00:00
|
|
|
std::optional<s32> DigitalController::StaticGetAxisCodeByName(std::string_view button_name)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2019-12-14 14:17:43 +00:00
|
|
|
std::optional<s32> DigitalController::StaticGetButtonCodeByName(std::string_view button_name)
|
2019-12-08 14:46:04 +00:00
|
|
|
{
|
|
|
|
#define BUTTON(name) \
|
|
|
|
if (button_name == #name) \
|
|
|
|
{ \
|
|
|
|
return static_cast<s32>(ZeroExtend32(static_cast<u8>(Button::name))); \
|
|
|
|
}
|
|
|
|
|
|
|
|
BUTTON(Select);
|
|
|
|
BUTTON(L3);
|
|
|
|
BUTTON(R3);
|
|
|
|
BUTTON(Start);
|
|
|
|
BUTTON(Up);
|
|
|
|
BUTTON(Right);
|
|
|
|
BUTTON(Down);
|
|
|
|
BUTTON(Left);
|
|
|
|
BUTTON(L2);
|
|
|
|
BUTTON(R2);
|
|
|
|
BUTTON(L1);
|
|
|
|
BUTTON(R1);
|
|
|
|
BUTTON(Triangle);
|
|
|
|
BUTTON(Circle);
|
|
|
|
BUTTON(Cross);
|
|
|
|
BUTTON(Square);
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
#undef BUTTON
|
|
|
|
}
|
2020-01-02 06:10:42 +00:00
|
|
|
|
|
|
|
Controller::AxisList DigitalController::StaticGetAxisNames()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::ButtonList DigitalController::StaticGetButtonNames()
|
|
|
|
{
|
2020-08-23 12:47:49 +00:00
|
|
|
return {{TRANSLATABLE("DigitalController", "Up"), static_cast<s32>(Button::Up)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Down"), static_cast<s32>(Button::Down)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Left"), static_cast<s32>(Button::Left)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Right"), static_cast<s32>(Button::Right)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Select"), static_cast<s32>(Button::Select)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Start"), static_cast<s32>(Button::Start)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Triangle"), static_cast<s32>(Button::Triangle)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Cross"), static_cast<s32>(Button::Cross)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Circle"), static_cast<s32>(Button::Circle)},
|
|
|
|
{TRANSLATABLE("DigitalController", "Square"), static_cast<s32>(Button::Square)},
|
|
|
|
{TRANSLATABLE("DigitalController", "L1"), static_cast<s32>(Button::L1)},
|
|
|
|
{TRANSLATABLE("DigitalController", "L2"), static_cast<s32>(Button::L2)},
|
|
|
|
{TRANSLATABLE("DigitalController", "R1"), static_cast<s32>(Button::R1)},
|
|
|
|
{TRANSLATABLE("DigitalController", "R2"), static_cast<s32>(Button::R2)}};
|
2020-01-02 06:10:42 +00:00
|
|
|
}
|
2020-04-14 06:34:39 +00:00
|
|
|
|
|
|
|
u32 DigitalController::StaticGetVibrationMotorCount()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-10 07:39:11 +00:00
|
|
|
|
|
|
|
Controller::SettingList DigitalController::StaticGetSettings()
|
|
|
|
{
|
|
|
|
static constexpr std::array<SettingInfo, 1> settings = {
|
|
|
|
{{SettingInfo::Type::Boolean, "ForcePopnControllerMode",
|
|
|
|
TRANSLATABLE("DigitalController", "Force Pop'n Controller Mode"),
|
|
|
|
TRANSLATABLE("DigitalController", "Forces the Digital Controller to act as a Pop'n Controller."), "false"}}};
|
|
|
|
return SettingList(settings.begin(), settings.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DigitalController::LoadSettings(const char* section)
|
|
|
|
{
|
|
|
|
Controller::LoadSettings(section);
|
|
|
|
m_popn_controller_mode = g_host_interface->GetBoolSettingValue(section, "ForcePopnControllerMode", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 DigitalController::GetButtonsLSBMask() const
|
|
|
|
{
|
|
|
|
constexpr u8 popn_controller_mask =
|
2020-12-12 01:37:38 +00:00
|
|
|
static_cast<u8>(~(u8(1) << static_cast<u8>(Button::Right) | u8(1) << static_cast<u8>(Button::Down) |
|
|
|
|
u8(1) << static_cast<u8>(Button::Left)));
|
2020-12-10 07:39:11 +00:00
|
|
|
return m_popn_controller_mode ? popn_controller_mask : 0xFF;
|
|
|
|
}
|