Controller: Add basic PlayStation Mouse support

Still needs capture/relative movement for a better experience.
This commit is contained in:
Connor McLaughlin 2020-04-29 13:59:49 +10:00
parent b56546d8ad
commit 8f82987341
8 changed files with 301 additions and 5 deletions

View File

@ -56,6 +56,8 @@ add_library(core
namco_guncon.h
pad.cpp
pad.h
playstation_mouse.cpp
playstation_mouse.h
psf_loader.cpp
psf_loader.h
save_state_version.h

View File

@ -1,8 +1,9 @@
#include "controller.h"
#include "analog_controller.h"
#include "common/state_wrapper.h"
#include "namco_guncon.h"
#include "digital_controller.h"
#include "namco_guncon.h"
#include "playstation_mouse.h"
Controller::Controller() = default;
@ -50,6 +51,9 @@ std::unique_ptr<Controller> Controller::Create(System* system, ControllerType ty
case ControllerType::NamcoGunCon:
return NamcoGunCon::Create(system);
case ControllerType::PlayStationMouse:
return PlayStationMouse::Create(system);
case ControllerType::None:
default:
return {};
@ -79,6 +83,9 @@ Controller::AxisList Controller::GetAxisNames(ControllerType type)
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetAxisNames();
case ControllerType::PlayStationMouse:
return PlayStationMouse::StaticGetAxisNames();
case ControllerType::None:
default:
return {};
@ -98,6 +105,9 @@ Controller::ButtonList Controller::GetButtonNames(ControllerType type)
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetButtonNames();
case ControllerType::PlayStationMouse:
return PlayStationMouse::StaticGetButtonNames();
case ControllerType::None:
default:
return {};
@ -117,6 +127,9 @@ u32 Controller::GetVibrationMotorCount(ControllerType type)
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetVibrationMotorCount();
case ControllerType::PlayStationMouse:
return PlayStationMouse::StaticGetVibrationMotorCount();
case ControllerType::None:
default:
return 0;
@ -136,6 +149,9 @@ std::optional<s32> Controller::GetAxisCodeByName(ControllerType type, std::strin
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetAxisCodeByName(axis_name);
case ControllerType::PlayStationMouse:
return PlayStationMouse::StaticGetAxisCodeByName(axis_name);
case ControllerType::None:
default:
return std::nullopt;
@ -155,6 +171,9 @@ std::optional<s32> Controller::GetButtonCodeByName(ControllerType type, std::str
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetButtonCodeByName(button_name);
case ControllerType::PlayStationMouse:
return PlayStationMouse::StaticGetButtonCodeByName(button_name);
case ControllerType::None:
default:
return std::nullopt;

View File

@ -78,6 +78,7 @@
<ClCompile Include="namco_guncon.cpp" />
<ClCompile Include="pad.cpp" />
<ClCompile Include="controller.cpp" />
<ClCompile Include="playstation_mouse.cpp" />
<ClCompile Include="psf_loader.cpp" />
<ClCompile Include="settings.cpp" />
<ClCompile Include="sio.cpp" />
@ -119,6 +120,7 @@
<ClInclude Include="namco_guncon.h" />
<ClInclude Include="pad.h" />
<ClInclude Include="controller.h" />
<ClInclude Include="playstation_mouse.h" />
<ClInclude Include="psf_loader.h" />
<ClInclude Include="save_state_version.h" />
<ClInclude Include="settings.h" />

View File

@ -42,6 +42,7 @@
<ClCompile Include="cdrom_async_reader.cpp" />
<ClCompile Include="psf_loader.cpp" />
<ClCompile Include="namco_guncon.cpp" />
<ClCompile Include="playstation_mouse.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="types.h" />
@ -85,6 +86,7 @@
<ClInclude Include="cdrom_async_reader.h" />
<ClInclude Include="psf_loader.h" />
<ClInclude Include="namco_guncon.h" />
<ClInclude Include="playstation_mouse.h" />
</ItemGroup>
<ItemGroup>
<None Include="cpu_core.inl" />

View File

@ -0,0 +1,204 @@
#include "playstation_mouse.h"
#include "common/assert.h"
#include "common/log.h"
#include "common/state_wrapper.h"
#include "gpu.h"
#include "host_display.h"
#include "host_interface.h"
#include "system.h"
#include <array>
Log_SetChannel(PlayStationMouse);
PlayStationMouse::PlayStationMouse(System* system) : m_system(system)
{
m_last_host_position_y = system->GetHostInterface()->GetDisplay()->GetMousePositionX();
m_last_host_position_y = system->GetHostInterface()->GetDisplay()->GetMousePositionY();
}
PlayStationMouse::~PlayStationMouse() = default;
ControllerType PlayStationMouse::GetType() const
{
return ControllerType::NamcoGunCon;
}
std::optional<s32> PlayStationMouse::GetAxisCodeByName(std::string_view axis_name) const
{
return StaticGetAxisCodeByName(axis_name);
}
std::optional<s32> PlayStationMouse::GetButtonCodeByName(std::string_view button_name) const
{
return StaticGetButtonCodeByName(button_name);
}
void PlayStationMouse::Reset()
{
m_transfer_state = TransferState::Idle;
}
bool PlayStationMouse::DoState(StateWrapper& sw)
{
if (!Controller::DoState(sw))
return false;
sw.Do(&m_button_state);
sw.Do(&m_delta_x);
sw.Do(&m_delta_y);
sw.Do(&m_transfer_state);
return true;
}
void PlayStationMouse::SetAxisState(s32 axis_code, float value) {}
void PlayStationMouse::SetButtonState(Button button, bool pressed)
{
static constexpr std::array<u8, static_cast<size_t>(Button::Count)> indices = {{11, 10}};
if (pressed)
m_button_state &= ~(u16(1) << indices[static_cast<u8>(button)]);
else
m_button_state |= u16(1) << indices[static_cast<u8>(button)];
}
void PlayStationMouse::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);
}
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:
{
// ack when sent 0x01, send ID for 0x42
if (data_in == 0x42)
{
*data_out = Truncate8(ID);
m_transfer_state = TransferState::IDMSB;
return true;
}
else
{
*data_out = 0xFF;
return (data_in == 0x01);
}
}
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:
{
UpdatePosition();
*data_out = static_cast<u8>(m_delta_x);
m_transfer_state = TransferState::DeltaY;
return true;
}
case TransferState::DeltaY:
{
*data_out = static_cast<u8>(m_delta_y);
m_transfer_state = TransferState::Idle;
return false;
}
default:
{
UnreachableCode();
return false;
}
}
}
void PlayStationMouse::UpdatePosition()
{
// get screen coordinates
const HostDisplay* display = m_system->GetHostInterface()->GetDisplay();
const s32 mouse_x = display->GetMousePositionX();
const s32 mouse_y = display->GetMousePositionY();
const s32 delta_x = mouse_x - m_last_host_position_x;
const s32 delta_y = mouse_y - m_last_host_position_y;
m_last_host_position_x = mouse_x;
m_last_host_position_y = mouse_y;
if (delta_x != 0 || delta_y != 0)
Log_InfoPrintf("dx=%d, dy=%d", delta_x, delta_y);
m_delta_x = static_cast<s8>(std::clamp<s32>(delta_x, std::numeric_limits<s8>::min(), std::numeric_limits<s8>::max()));
m_delta_y = static_cast<s8>(std::clamp<s32>(delta_y, std::numeric_limits<s8>::min(), std::numeric_limits<s8>::max()));
}
std::unique_ptr<PlayStationMouse> PlayStationMouse::Create(System* system)
{
return std::make_unique<PlayStationMouse>(system);
}
std::optional<s32> PlayStationMouse::StaticGetAxisCodeByName(std::string_view button_name)
{
return std::nullopt;
}
std::optional<s32> PlayStationMouse::StaticGetButtonCodeByName(std::string_view button_name)
{
#define BUTTON(name) \
if (button_name == #name) \
{ \
return static_cast<s32>(ZeroExtend32(static_cast<u8>(Button::name))); \
}
BUTTON(Left);
BUTTON(Right);
return std::nullopt;
#undef BUTTON
}
Controller::AxisList PlayStationMouse::StaticGetAxisNames()
{
return {};
}
Controller::ButtonList PlayStationMouse::StaticGetButtonNames()
{
#define B(n) \
{ \
#n, static_cast < s32>(Button::n) \
}
return {B(Left), B(Right)};
#undef B
}
u32 PlayStationMouse::StaticGetVibrationMotorCount()
{
return 0;
}

View File

@ -0,0 +1,66 @@
#pragma once
#include "controller.h"
#include <memory>
#include <optional>
#include <string_view>
class PlayStationMouse final : public Controller
{
public:
enum class Button : u8
{
Left = 0,
Right = 1,
Count
};
PlayStationMouse(System* system);
~PlayStationMouse() override;
static std::unique_ptr<PlayStationMouse> Create(System* system);
static std::optional<s32> StaticGetAxisCodeByName(std::string_view button_name);
static std::optional<s32> StaticGetButtonCodeByName(std::string_view button_name);
static AxisList StaticGetAxisNames();
static ButtonList StaticGetButtonNames();
static u32 StaticGetVibrationMotorCount();
ControllerType GetType() const override;
std::optional<s32> GetAxisCodeByName(std::string_view axis_name) const override;
std::optional<s32> GetButtonCodeByName(std::string_view button_name) const override;
void Reset() override;
bool DoState(StateWrapper& sw) override;
void SetAxisState(s32 axis_code, float value) override;
void SetButtonState(s32 button_code, bool pressed) override;
void ResetTransferState() override;
bool Transfer(const u8 data_in, u8* data_out) override;
void SetButtonState(Button button, bool pressed);
private:
void UpdatePosition();
enum class TransferState : u8
{
Idle,
IDMSB,
ButtonsLSB,
ButtonsMSB,
DeltaX,
DeltaY
};
System* m_system;
s32 m_last_host_position_x = 0;
s32 m_last_host_position_y = 0;
// buttons are active low
u16 m_button_state = UINT16_C(0xFFFF);
s8 m_delta_x = 0;
s8 m_delta_y = 0;
TransferState m_transfer_state = TransferState::Idle;
};

View File

@ -354,10 +354,10 @@ const char* Settings::GetAudioBackendDisplayName(AudioBackend backend)
return s_audio_backend_display_names[static_cast<int>(backend)];
}
static std::array<const char*, 4> s_controller_type_names = {
{"None", "DigitalController", "AnalogController", "NamcoGunCon"}};
static std::array<const char*, 4> s_controller_display_names = {
{"None", "Digital Controller", "Analog Controller (DualShock)", "Namco GunCon"}};
static std::array<const char*, 5> s_controller_type_names = {
{"None", "DigitalController", "AnalogController", "NamcoGunCon", "PlayStationMouse"}};
static std::array<const char*, 5> s_controller_display_names = {
{"None", "Digital Controller", "Analog Controller (DualShock)", "Namco GunCon", "PlayStation Mouse"}};
std::optional<ControllerType> Settings::ParseControllerTypeName(const char* str)
{

View File

@ -88,6 +88,7 @@ enum class ControllerType
DigitalController,
AnalogController,
NamcoGunCon,
PlayStationMouse,
Count
};