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)
|
|
|
|
|
2020-04-29 03:59:49 +00:00
|
|
|
#include "playstation_mouse.h"
|
|
|
|
#include "gpu.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "host.h"
|
2020-04-29 03:59:49 +00:00
|
|
|
#include "system.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/state_wrapper.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
|
2023-11-26 10:30:10 +00:00
|
|
|
#include "IconsPromptFont.h"
|
|
|
|
|
2020-04-29 03:59:49 +00:00
|
|
|
#include <array>
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2020-04-29 03:59:49 +00:00
|
|
|
Log_SetChannel(PlayStationMouse);
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
static constexpr std::array<u8, static_cast<size_t>(PlayStationMouse::Binding::ButtonCount)> s_button_indices = {
|
|
|
|
{11, 10}};
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
PlayStationMouse::PlayStationMouse(u32 index) : Controller(index)
|
2020-04-29 03:59:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayStationMouse::~PlayStationMouse() = default;
|
|
|
|
|
|
|
|
ControllerType PlayStationMouse::GetType() const
|
|
|
|
{
|
2020-11-13 09:42:31 +00:00
|
|
|
return ControllerType::PlayStationMouse;
|
2020-04-29 03:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlayStationMouse::Reset()
|
|
|
|
{
|
|
|
|
m_transfer_state = TransferState::Idle;
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
bool PlayStationMouse::DoState(StateWrapper& sw, bool apply_input_state)
|
2020-04-29 03:59:49 +00:00
|
|
|
{
|
2020-12-16 14:09:32 +00:00
|
|
|
if (!Controller::DoState(sw, apply_input_state))
|
2020-04-29 03:59:49 +00:00
|
|
|
return false;
|
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
u16 button_state = m_button_state;
|
2023-09-20 06:56:12 +00:00
|
|
|
float delta_x = m_delta_x;
|
|
|
|
float delta_y = m_delta_y;
|
2020-12-16 14:09:32 +00:00
|
|
|
sw.Do(&button_state);
|
2023-09-20 06:56:12 +00:00
|
|
|
if (sw.GetVersion() >= 60)
|
|
|
|
{
|
|
|
|
sw.Do(&delta_x);
|
|
|
|
sw.Do(&delta_y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u8 dummy = 0;
|
|
|
|
sw.Do(&dummy);
|
|
|
|
sw.Do(&dummy);
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
if (apply_input_state)
|
|
|
|
{
|
|
|
|
m_button_state = button_state;
|
|
|
|
m_delta_x = delta_x;
|
|
|
|
m_delta_y = delta_y;
|
|
|
|
}
|
|
|
|
|
2020-04-29 03:59:49 +00:00
|
|
|
sw.Do(&m_transfer_state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float PlayStationMouse::GetBindState(u32 index) const
|
2021-04-03 17:51:08 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (index >= s_button_indices.size())
|
|
|
|
return 0.0f;
|
2021-04-03 17:51:08 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
const u32 bit = s_button_indices[index];
|
|
|
|
return static_cast<float>(((m_button_state >> bit) & 1u) ^ 1u);
|
2021-04-03 17:51:08 +00:00
|
|
|
}
|
2020-04-29 03:59:49 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void PlayStationMouse::SetBindState(u32 index, float value)
|
2020-04-29 03:59:49 +00:00
|
|
|
{
|
2023-09-20 06:56:12 +00:00
|
|
|
if (index >= s_button_indices.size())
|
|
|
|
{
|
|
|
|
if (index == static_cast<u32>(Binding::PointerX))
|
|
|
|
m_delta_x += value;
|
|
|
|
else if (index == static_cast<u32>(Binding::PointerY))
|
|
|
|
m_delta_y += value;
|
|
|
|
|
2020-04-29 03:59:49 +00:00
|
|
|
return;
|
2023-09-20 06:56:12 +00:00
|
|
|
}
|
2020-04-29 03:59:49 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
if (value >= 0.5f)
|
|
|
|
m_button_state &= ~(u16(1) << s_button_indices[index]);
|
|
|
|
else
|
|
|
|
m_button_state |= u16(1) << s_button_indices[index];
|
2020-04-29 03:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlayStationMouse::ResetTransferState()
|
|
|
|
{
|
|
|
|
m_transfer_state = TransferState::Idle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlayStationMouse::Transfer(const u8 data_in, u8* data_out)
|
|
|
|
{
|
|
|
|
static constexpr u16 ID = 0x5A12;
|
|
|
|
|
|
|
|
switch (m_transfer_state)
|
|
|
|
{
|
|
|
|
case TransferState::Idle:
|
|
|
|
{
|
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:
|
|
|
|
{
|
2020-04-29 03:59:49 +00:00
|
|
|
if (data_in == 0x42)
|
|
|
|
{
|
|
|
|
*data_out = Truncate8(ID);
|
|
|
|
m_transfer_state = TransferState::IDMSB;
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-21 23:20:31 +00:00
|
|
|
|
|
|
|
*data_out = 0xFF;
|
|
|
|
return false;
|
2020-04-29 03:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case TransferState::IDMSB:
|
|
|
|
{
|
|
|
|
*data_out = Truncate8(ID >> 8);
|
|
|
|
m_transfer_state = TransferState::ButtonsLSB;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TransferState::ButtonsLSB:
|
|
|
|
{
|
|
|
|
*data_out = Truncate8(m_button_state);
|
|
|
|
m_transfer_state = TransferState::ButtonsMSB;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TransferState::ButtonsMSB:
|
|
|
|
{
|
|
|
|
*data_out = Truncate8(m_button_state >> 8);
|
|
|
|
m_transfer_state = TransferState::DeltaX;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TransferState::DeltaX:
|
|
|
|
{
|
2023-09-20 06:56:12 +00:00
|
|
|
const float delta_x =
|
|
|
|
std::clamp(std::floor(m_delta_x * m_sensitivity_x), static_cast<float>(std::numeric_limits<s8>::min()),
|
|
|
|
static_cast<float>(std::numeric_limits<s8>::max()));
|
|
|
|
m_delta_x -= delta_x / m_sensitivity_x;
|
|
|
|
*data_out = static_cast<s8>(delta_x);
|
2020-04-29 03:59:49 +00:00
|
|
|
m_transfer_state = TransferState::DeltaY;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TransferState::DeltaY:
|
|
|
|
{
|
2023-09-20 06:56:12 +00:00
|
|
|
const float delta_y =
|
|
|
|
std::clamp(std::floor(m_delta_y * m_sensitivity_y), static_cast<float>(std::numeric_limits<s8>::min()),
|
|
|
|
static_cast<float>(std::numeric_limits<s8>::max()));
|
|
|
|
m_delta_y -= delta_y / m_sensitivity_x;
|
|
|
|
*data_out = static_cast<s8>(delta_y);
|
2020-04-29 03:59:49 +00:00
|
|
|
m_transfer_state = TransferState::Idle;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
UnreachableCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:56:12 +00:00
|
|
|
void PlayStationMouse::LoadSettings(SettingsInterface& si, const char* section)
|
2020-04-29 03:59:49 +00:00
|
|
|
{
|
2023-09-20 06:56:12 +00:00
|
|
|
Controller::LoadSettings(si, section);
|
|
|
|
|
|
|
|
m_sensitivity_x = si.GetFloatValue(section, "SensitivityX", 1.0f);
|
|
|
|
m_sensitivity_y = si.GetFloatValue(section, "SensitivityY", 1.0f);
|
2020-04-29 03:59:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::unique_ptr<PlayStationMouse> PlayStationMouse::Create(u32 index)
|
2020-04-29 03:59:49 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return std::make_unique<PlayStationMouse>(index);
|
2020-04-29 03:59:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static const Controller::ControllerBindingInfo s_binding_info[] = {
|
2023-11-26 10:30:10 +00:00
|
|
|
#define BUTTON(name, display_name, icon_name, button, genb) \
|
2020-04-29 03:59:49 +00:00
|
|
|
{ \
|
2023-11-26 10:30:10 +00:00
|
|
|
name, display_name, icon_name, static_cast<u32>(button), InputBindingInfo::Type::Button, genb \
|
2020-04-29 03:59:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:15:53 +00:00
|
|
|
// clang-format off
|
2023-11-26 10:30:10 +00:00
|
|
|
{ "Pointer", TRANSLATE_NOOP("PlaystationMouse", "Pointer"), ICON_PF_MOUSE_ANY, static_cast<u32>(PlayStationMouse::Binding::PointerX), InputBindingInfo::Type::Pointer, GenericInputBinding::Unknown },
|
|
|
|
BUTTON("Left", TRANSLATE_NOOP("PlayStationMouse", "Left Button"), ICON_PF_MOUSE_BUTTON_1, PlayStationMouse::Binding::Left, GenericInputBinding::Cross),
|
|
|
|
BUTTON("Right", TRANSLATE_NOOP("PlayStationMouse", "Right Button"), ICON_PF_MOUSE_BUTTON_2, PlayStationMouse::Binding::Right, GenericInputBinding::Circle),
|
2023-09-03 04:30:26 +00:00
|
|
|
// clang-format on
|
2020-04-29 03:59:49 +00:00
|
|
|
|
|
|
|
#undef BUTTON
|
2022-07-11 13:03:29 +00:00
|
|
|
};
|
|
|
|
static const SettingInfo s_settings[] = {
|
2023-09-20 06:56:12 +00:00
|
|
|
{SettingInfo::Type::Float, "SensitivityX", TRANSLATE_NOOP("PlayStationMouse", "Horizontal Sensitivity"),
|
|
|
|
TRANSLATE_NOOP("PlayStationMouse", "Adjusts the correspondance between physical and virtual mouse movement."), "1.0",
|
|
|
|
"0.01", "2.0", "0.01", "%.0f", nullptr, 100.0f},
|
|
|
|
{SettingInfo::Type::Float, "SensitivityY", TRANSLATE_NOOP("PlayStationMouse", "Vertical Sensitivity"),
|
|
|
|
TRANSLATE_NOOP("PlayStationMouse", "Adjusts the correspondance between physical and virtual mouse movement."), "1.0",
|
|
|
|
"0.01", "2.0", "0.01", "%.0f", nullptr, 100.0f},
|
2022-07-11 13:03:29 +00:00
|
|
|
};
|
2020-04-29 03:59:49 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
const Controller::ControllerInfo PlayStationMouse::INFO = {ControllerType::PlayStationMouse,
|
|
|
|
"PlayStationMouse",
|
2023-08-19 13:43:37 +00:00
|
|
|
TRANSLATE_NOOP("ControllerType", "PlayStation Mouse"),
|
2023-11-26 10:30:10 +00:00
|
|
|
ICON_PF_MOUSE,
|
2022-07-11 13:03:29 +00:00
|
|
|
s_binding_info,
|
|
|
|
s_settings,
|
2023-09-20 06:56:12 +00:00
|
|
|
Controller::VibrationCapabilities::NoVibration};
|