2023-09-20 06:56:12 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-12-08 14:51:52 +00:00
|
|
|
#include "controller.h"
|
2019-12-15 11:58:27 +00:00
|
|
|
#include "analog_controller.h"
|
2020-11-13 07:36:36 +00:00
|
|
|
#include "analog_joystick.h"
|
2019-12-08 14:51:52 +00:00
|
|
|
#include "digital_controller.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "fmt/format.h"
|
|
|
|
#include "guncon.h"
|
2023-05-02 11:46:06 +00:00
|
|
|
#include "host.h"
|
2020-05-30 04:33:55 +00:00
|
|
|
#include "negcon.h"
|
2020-06-30 14:33:45 +00:00
|
|
|
#include "playstation_mouse.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/state_wrapper.h"
|
2019-12-08 14:51:52 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static const Controller::ControllerInfo s_none_info = {ControllerType::None,
|
|
|
|
"None",
|
2023-08-19 13:43:37 +00:00
|
|
|
TRANSLATE_NOOP("ControllerType", "Not Connected"),
|
2023-11-26 10:30:10 +00:00
|
|
|
nullptr,
|
2023-09-20 07:10:41 +00:00
|
|
|
{},
|
|
|
|
{},
|
2022-07-11 13:03:29 +00:00
|
|
|
Controller::VibrationCapabilities::NoVibration};
|
|
|
|
|
|
|
|
static const Controller::ControllerInfo* s_controller_info[] = {
|
|
|
|
&s_none_info, &DigitalController::INFO, &AnalogController::INFO, &AnalogJoystick::INFO, &NeGcon::INFO,
|
|
|
|
&GunCon::INFO, &PlayStationMouse::INFO,
|
|
|
|
};
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
Controller::Controller(u32 index) : m_index(index)
|
|
|
|
{
|
|
|
|
}
|
2019-12-08 14:51:52 +00:00
|
|
|
|
|
|
|
Controller::~Controller() = default;
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
void Controller::Reset()
|
|
|
|
{
|
|
|
|
}
|
2019-12-08 14:51:52 +00:00
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
bool Controller::DoState(StateWrapper& sw, bool apply_input_state)
|
2019-12-08 14:51:52 +00:00
|
|
|
{
|
|
|
|
return !sw.HasError();
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
void Controller::ResetTransferState()
|
|
|
|
{
|
|
|
|
}
|
2019-12-08 14:51:52 +00:00
|
|
|
|
|
|
|
bool Controller::Transfer(const u8 data_in, u8* data_out)
|
|
|
|
{
|
|
|
|
*data_out = 0xFF;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float Controller::GetBindState(u32 index) const
|
2021-04-03 17:51:08 +00:00
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
void Controller::SetBindState(u32 index, float value)
|
|
|
|
{
|
|
|
|
}
|
2019-12-08 15:06:58 +00:00
|
|
|
|
2020-12-06 05:47:00 +00:00
|
|
|
u32 Controller::GetButtonStateBits() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-09 04:10:42 +00:00
|
|
|
bool Controller::InAnalogMode() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:54:16 +00:00
|
|
|
std::optional<u32> Controller::GetAnalogInputBytes() const
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
void Controller::LoadSettings(SettingsInterface& si, const char* section)
|
2020-06-30 14:35:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
std::unique_ptr<Controller> Controller::Create(ControllerType type, u32 index)
|
2019-12-08 14:51:52 +00:00
|
|
|
{
|
2019-12-14 13:29:26 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ControllerType::DigitalController:
|
2022-07-11 13:03:29 +00:00
|
|
|
return DigitalController::Create(index);
|
2019-12-14 13:29:26 +00:00
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
case ControllerType::AnalogController:
|
2020-07-31 07:09:18 +00:00
|
|
|
return AnalogController::Create(index);
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2020-11-13 07:36:36 +00:00
|
|
|
case ControllerType::AnalogJoystick:
|
|
|
|
return AnalogJoystick::Create(index);
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
case ControllerType::GunCon:
|
|
|
|
return GunCon::Create(index);
|
2020-04-25 15:18:42 +00:00
|
|
|
|
2020-04-29 03:59:49 +00:00
|
|
|
case ControllerType::PlayStationMouse:
|
2022-07-11 13:03:29 +00:00
|
|
|
return PlayStationMouse::Create(index);
|
2020-04-29 03:59:49 +00:00
|
|
|
|
2020-05-30 04:33:55 +00:00
|
|
|
case ControllerType::NeGcon:
|
2022-07-11 13:03:29 +00:00
|
|
|
return NeGcon::Create(index);
|
2020-05-30 04:33:55 +00:00
|
|
|
|
2019-12-14 13:29:26 +00:00
|
|
|
case ControllerType::None:
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
2019-12-08 14:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
const char* Controller::GetDefaultPadType(u32 pad)
|
2019-12-14 14:31:07 +00:00
|
|
|
{
|
2022-08-29 10:34:12 +00:00
|
|
|
return Settings::GetControllerTypeName((pad == 0) ? Settings::DEFAULT_CONTROLLER_1_TYPE :
|
|
|
|
Settings::DEFAULT_CONTROLLER_2_TYPE);
|
2019-12-14 14:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
const Controller::ControllerInfo* Controller::GetControllerInfo(ControllerType type)
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
for (const ControllerInfo* info : s_controller_info)
|
2020-01-02 06:10:42 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (type == info->type)
|
|
|
|
return info;
|
2020-01-02 06:10:42 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2020-01-02 06:10:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
const Controller::ControllerInfo* Controller::GetControllerInfo(const std::string_view& name)
|
2020-01-02 06:10:42 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
for (const ControllerInfo* info : s_controller_info)
|
2020-01-02 06:10:42 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (name == info->name)
|
|
|
|
return info;
|
2020-01-02 06:10:42 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2020-01-02 06:10:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> Controller::GetControllerTypeNames()
|
2020-04-14 06:34:39 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> ret;
|
|
|
|
for (const ControllerInfo* info : s_controller_info)
|
2023-11-30 04:21:44 +00:00
|
|
|
ret.emplace_back(info->name, Host::TranslateToString("ControllerType", info->display_name));
|
2020-05-30 04:33:55 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
return ret;
|
2020-04-14 06:34:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::optional<u32> Controller::GetBindIndex(ControllerType type, const std::string_view& bind_name)
|
|
|
|
{
|
|
|
|
const ControllerInfo* info = GetControllerInfo(type);
|
|
|
|
if (!info)
|
|
|
|
return std::nullopt;
|
2020-05-30 04:33:55 +00:00
|
|
|
|
2023-09-20 07:10:41 +00:00
|
|
|
for (u32 i = 0; i < static_cast<u32>(info->bindings.size()); i++)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
if (bind_name == info->bindings[i].name)
|
|
|
|
return i;
|
2019-12-14 13:29:26 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
return std::nullopt;
|
2019-12-08 14:51:52 +00:00
|
|
|
}
|
2020-06-30 14:33:45 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
Controller::VibrationCapabilities Controller::GetControllerVibrationCapabilities(const std::string_view& type)
|
2020-06-30 14:33:45 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
const ControllerInfo* info = GetControllerInfo(type);
|
|
|
|
return info ? info->vibration_caps : VibrationCapabilities::NoVibration;
|
|
|
|
}
|
2020-06-30 14:33:45 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::tuple<u32, u32> Controller::ConvertPadToPortAndSlot(u32 index)
|
|
|
|
{
|
|
|
|
if (index > 4) // [5,6,7]
|
|
|
|
return std::make_tuple(1, index - 4); // 2B,2C,2D
|
|
|
|
else if (index > 1) // [2,3,4]
|
|
|
|
return std::make_tuple(0, index - 1); // 1B,1C,1D
|
|
|
|
else // [0,1]
|
|
|
|
return std::make_tuple(index, 0); // 1A,2A
|
|
|
|
}
|
2020-11-13 07:36:36 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
u32 Controller::ConvertPortAndSlotToPad(u32 port, u32 slot)
|
|
|
|
{
|
|
|
|
if (slot == 0)
|
|
|
|
return port;
|
|
|
|
else if (port == 0) // slot=[0,1]
|
|
|
|
return slot + 1; // 2,3,4
|
|
|
|
else
|
|
|
|
return slot + 4; // 5,6,7
|
|
|
|
}
|
2020-06-30 14:35:13 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool Controller::PadIsMultitapSlot(u32 index)
|
|
|
|
{
|
|
|
|
return (index >= 2);
|
|
|
|
}
|
2020-11-01 06:52:00 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool Controller::PortAndSlotIsMultitap(u32 port, u32 slot)
|
|
|
|
{
|
|
|
|
return (slot != 0);
|
|
|
|
}
|
2020-12-27 14:34:49 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string Controller::GetSettingsSection(u32 pad)
|
|
|
|
{
|
|
|
|
return fmt::format("Pad{}", pad + 1u);
|
2020-06-30 14:33:45 +00:00
|
|
|
}
|
2022-10-21 11:14:27 +00:00
|
|
|
|
|
|
|
bool Controller::InCircularDeadzone(float deadzone, float pos_x, float pos_y)
|
|
|
|
{
|
|
|
|
if (pos_x == 0.0f && pos_y == 0.0f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Compute the angle at the given position in the stick's square bounding box.
|
|
|
|
const float theta = std::atan2(pos_y, pos_x);
|
|
|
|
|
|
|
|
// Compute the position that the edge of the circle would be at, given the angle.
|
|
|
|
const float dz_x = std::cos(theta) * deadzone;
|
|
|
|
const float dz_y = std::sin(theta) * deadzone;
|
|
|
|
|
|
|
|
// We're in the deadzone if our position is less than the circle edge.
|
|
|
|
const bool in_x = (pos_x < 0.0f) ? (pos_x > dz_x) : (pos_x <= dz_x);
|
|
|
|
const bool in_y = (pos_y < 0.0f) ? (pos_y > dz_y) : (pos_y <= dz_y);
|
|
|
|
return (in_x && in_y);
|
|
|
|
}
|