2023-10-01 04:12:25 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com> and contributors.
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
#include "digital_controller.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "host.h"
|
2021-01-23 16:52:52 +00:00
|
|
|
#include "system.h"
|
2023-10-01 04:12:25 +00:00
|
|
|
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/state_wrapper.h"
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2023-10-01 04:12:25 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/bitutils.h"
|
|
|
|
|
2023-09-03 04:30:26 +00:00
|
|
|
DigitalController::DigitalController(u32 index) : Controller(index)
|
|
|
|
{
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
|
|
|
|
DigitalController::~DigitalController() = default;
|
|
|
|
|
2019-12-14 14:17:43 +00:00
|
|
|
ControllerType DigitalController::GetType() const
|
|
|
|
{
|
|
|
|
return ControllerType::DigitalController;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float DigitalController::GetBindState(u32 index) const
|
2021-04-03 17:51:08 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (index < static_cast<u32>(Button::Count))
|
|
|
|
{
|
|
|
|
return static_cast<float>(((m_button_state >> index) & 1u) ^ 1u);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2021-04-03 17:51:08 +00:00
|
|
|
}
|
2019-12-14 14:31:07 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void DigitalController::SetBindState(u32 index, float value)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (index >= static_cast<u32>(Button::Count))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const bool pressed = (value >= 0.5f);
|
|
|
|
const u16 bit = u16(1) << static_cast<u8>(index);
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::unique_ptr<DigitalController> DigitalController::Create(u32 index)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return std::make_unique<DigitalController>(index);
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
2019-12-08 14:46:04 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static const Controller::ControllerBindingInfo s_binding_info[] = {
|
|
|
|
#define BUTTON(name, display_name, button, genb) \
|
2019-12-08 14:46:04 +00:00
|
|
|
{ \
|
2023-01-15 04:00:51 +00:00
|
|
|
name, display_name, static_cast<u32>(button), InputBindingInfo::Type::Button, genb \
|
2019-12-08 14:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:15:53 +00:00
|
|
|
// clang-format off
|
|
|
|
BUTTON("Up", TRANSLATE_NOOP("DigitalController", "D-Pad Up"), DigitalController::Button::Up, GenericInputBinding::DPadUp),
|
|
|
|
BUTTON("Right", TRANSLATE_NOOP("DigitalController", "D-Pad Right"), DigitalController::Button::Right, GenericInputBinding::DPadRight),
|
|
|
|
BUTTON("Down", TRANSLATE_NOOP("DigitalController", "D-Pad Down"), DigitalController::Button::Down, GenericInputBinding::DPadDown),
|
|
|
|
BUTTON("Left", TRANSLATE_NOOP("DigitalController", "D-Pad Left"), DigitalController::Button::Left, GenericInputBinding::DPadLeft),
|
|
|
|
BUTTON("Triangle", TRANSLATE_NOOP("DigitalController", "Triangle"), DigitalController::Button::Triangle, GenericInputBinding::Triangle),
|
|
|
|
BUTTON("Circle", TRANSLATE_NOOP("DigitalController", "Circle"), DigitalController::Button::Circle, GenericInputBinding::Circle),
|
|
|
|
BUTTON("Cross", TRANSLATE_NOOP("DigitalController", "Cross"), DigitalController::Button::Cross, GenericInputBinding::Cross),
|
|
|
|
BUTTON("Square", TRANSLATE_NOOP("DigitalController", "Square"), DigitalController::Button::Square, GenericInputBinding::Square),
|
|
|
|
BUTTON("Select", TRANSLATE_NOOP("DigitalController", "Select"), DigitalController::Button::Select, GenericInputBinding::Select),
|
|
|
|
BUTTON("Start", TRANSLATE_NOOP("DigitalController", "Start"), DigitalController::Button::Start, GenericInputBinding::Start),
|
|
|
|
BUTTON("L1", TRANSLATE_NOOP("DigitalController", "L1"), DigitalController::Button::L1, GenericInputBinding::L1),
|
|
|
|
BUTTON("R1", TRANSLATE_NOOP("DigitalController", "R1"), DigitalController::Button::R1, GenericInputBinding::R1),
|
|
|
|
BUTTON("L2", TRANSLATE_NOOP("DigitalController", "L2"), DigitalController::Button::L2, GenericInputBinding::L2),
|
|
|
|
BUTTON("R2", TRANSLATE_NOOP("DigitalController", "R2"), DigitalController::Button::R2, GenericInputBinding::R2),
|
2023-09-03 04:30:26 +00:00
|
|
|
// clang-format on
|
2019-12-08 14:46:04 +00:00
|
|
|
|
|
|
|
#undef BUTTON
|
2022-07-11 13:03:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const SettingInfo s_settings[] = {
|
|
|
|
{SettingInfo::Type::Boolean, "ForcePopnControllerMode",
|
2023-08-19 13:43:37 +00:00
|
|
|
TRANSLATE_NOOP("DigitalController", "Force Pop'n Controller Mode"),
|
2023-09-03 04:30:26 +00:00
|
|
|
TRANSLATE_NOOP("DigitalController", "Forces the Digital Controller to act as a Pop'n Controller."), "false", nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, 0.0f}};
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
const Controller::ControllerInfo DigitalController::INFO = {ControllerType::DigitalController,
|
|
|
|
"DigitalController",
|
2023-08-19 13:43:37 +00:00
|
|
|
TRANSLATE_NOOP("ControllerType", "Digital Controller"),
|
2022-07-11 13:03:29 +00:00
|
|
|
s_binding_info,
|
|
|
|
s_settings,
|
|
|
|
Controller::VibrationCapabilities::NoVibration};
|
|
|
|
|
|
|
|
void DigitalController::LoadSettings(SettingsInterface& si, const char* section)
|
|
|
|
{
|
|
|
|
Controller::LoadSettings(si, section);
|
|
|
|
m_popn_controller_mode = si.GetBoolValue(section, "ForcePopnControllerMode", false);
|
2020-12-10 07:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|