From de388d342f3bd182d9b044310d8de86f086b3abd Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 19 Aug 2020 00:04:37 +1000 Subject: [PATCH] libretro: Add rumble interface --- .../libretro_host_interface.cpp | 24 +++++++++++++++++++ .../libretro_host_interface.h | 14 +++++++---- src/duckstation-libretro/main.cpp | 7 +----- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/duckstation-libretro/libretro_host_interface.cpp b/src/duckstation-libretro/libretro_host_interface.cpp index 3c47950f0..b788874f9 100644 --- a/src/duckstation-libretro/libretro_host_interface.cpp +++ b/src/duckstation-libretro/libretro_host_interface.cpp @@ -63,6 +63,14 @@ LibretroHostInterface::~LibretroHostInterface() } } +void LibretroHostInterface::InitInterfaces() +{ + SetCoreOptions(); + InitLogging(); + InitDiskControlInterface(); + InitRumbleInterface(); +} + void LibretroHostInterface::InitLogging() { if (s_libretro_log_callback_registered) @@ -650,6 +658,11 @@ void LibretroHostInterface::CheckForSettingsChanges(const Settings& old_settings UpdateLogging(); } +void LibretroHostInterface::InitRumbleInterface() +{ + m_rumble_interface_valid = g_retro_environment_callback(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &m_rumble_interface); +} + void LibretroHostInterface::UpdateControllers() { g_retro_input_poll_callback(); @@ -745,6 +758,17 @@ void LibretroHostInterface::UpdateControllersAnalogController(u32 index) const int16_t state = g_retro_input_state_callback(index, RETRO_DEVICE_ANALOG, it.second.first, it.second.second); controller->SetAxisState(static_cast(it.first), std::clamp(static_cast(state) / 32767.0f, -1.0f, 1.0f)); } + + if (m_rumble_interface_valid) + { + const u32 motor_count = controller->GetVibrationMotorCount(); + for (u32 i = 0; i < motor_count; i++) + { + const float strength = controller->GetVibrationMotorStrength(i); + m_rumble_interface.set_rumble_state(index, RETRO_RUMBLE_STRONG, + static_cast(static_cast(strength * 65565.0f))); + } + } } static std::optional RetroHwContextToRenderer(retro_hw_context_type type) diff --git a/src/duckstation-libretro/libretro_host_interface.h b/src/duckstation-libretro/libretro_host_interface.h index a1d97a519..8255d55d4 100644 --- a/src/duckstation-libretro/libretro_host_interface.h +++ b/src/duckstation-libretro/libretro_host_interface.h @@ -11,10 +11,7 @@ public: LibretroHostInterface(); ~LibretroHostInterface() override; - static void InitLogging(); - static bool SetCoreOptions(); - static bool HasCoreVariablesChanged(); - static void InitDiskControlInterface(); + void InitInterfaces(); ALWAYS_INLINE u32 GetResolutionScale() const { return g_settings.gpu_resolution_scale; } @@ -51,6 +48,12 @@ protected: void CheckForSettingsChanges(const Settings& old_settings) override; private: + bool SetCoreOptions(); + bool HasCoreVariablesChanged(); + void InitLogging(); + void InitDiskControlInterface(); + void InitRumbleInterface(); + void LoadSettings(); void UpdateSettings(); void UpdateControllers(); @@ -86,6 +89,9 @@ private: bool m_hw_render_callback_valid = false; bool m_using_hardware_renderer = false; std::optional m_next_disc_index; + + retro_rumble_interface m_rumble_interface = {}; + bool m_rumble_interface_valid = false; }; extern LibretroHostInterface g_libretro_host_interface; diff --git a/src/duckstation-libretro/main.cpp b/src/duckstation-libretro/main.cpp index 773a54a60..848fd53b3 100644 --- a/src/duckstation-libretro/main.cpp +++ b/src/duckstation-libretro/main.cpp @@ -122,12 +122,7 @@ RETRO_API size_t retro_get_memory_size(unsigned id) RETRO_API void retro_set_environment(retro_environment_t f) { g_retro_environment_callback = f; - - if (!g_libretro_host_interface.SetCoreOptions()) - Log_WarningPrintf("Failed to set core options, settings will not be changeable."); - - g_libretro_host_interface.InitLogging(); - g_libretro_host_interface.InitDiskControlInterface(); + g_libretro_host_interface.InitInterfaces(); } RETRO_API void retro_set_video_refresh(retro_video_refresh_t f)