From 69cf25b1a780d0e2ba405e72a1ca66cb12fd554f Mon Sep 17 00:00:00 2001 From: lynxnb Date: Mon, 10 Jan 2022 18:45:37 +0100 Subject: [PATCH] Initial support for updating settings during emulation + observing settings changes --- app/src/main/cpp/emu_jni.cpp | 10 ++++++++++ app/src/main/cpp/skyline/common/settings.cpp | 10 ++++++++++ app/src/main/cpp/skyline/common/settings.h | 14 ++++++++++++++ .../main/java/emu/skyline/utils/SettingsValues.kt | 5 +++++ 4 files changed, 39 insertions(+) diff --git a/app/src/main/cpp/emu_jni.cpp b/app/src/main/cpp/emu_jni.cpp index 6b2d6da7..0426038d 100644 --- a/app/src/main/cpp/emu_jni.cpp +++ b/app/src/main/cpp/emu_jni.cpp @@ -60,6 +60,8 @@ template<> void skyline::Settings::Update(KtSettings newSet systemLanguage = newSettings.GetInt("systemLanguage"); forceTripleBuffering = newSettings.GetBool("forceTripleBuffering"); disableFrameThrottling = newSettings.GetBool("disableFrameThrottling"); + + OnSettingsChanged(); } extern "C" JNIEXPORT void Java_emu_skyline_SkylineApplication_initializeLog( @@ -239,3 +241,11 @@ extern "C" JNIEXPORT void JNICALL Java_emu_skyline_EmulationActivity_setTouchSta input->touch.SetState(points); env->ReleaseIntArrayElements(pointsJni, reinterpret_cast(points.data()), JNI_ABORT); } + +extern "C" JNIEXPORT void JNICALL Java_emu_skyline_utils_SettingsValues_updateNative(JNIEnv *env, jobject settingsInstance) { + auto settings{SettingsWeak.lock()}; + if (!settings) + return; // We don't mind if we miss settings updates while settings haven't been initialized + skyline::KtSettings ktSettings{env, settingsInstance}; + settings->Update(ktSettings); +} diff --git a/app/src/main/cpp/skyline/common/settings.cpp b/app/src/main/cpp/skyline/common/settings.cpp index bd33ff07..e25c5409 100644 --- a/app/src/main/cpp/skyline/common/settings.cpp +++ b/app/src/main/cpp/skyline/common/settings.cpp @@ -4,6 +4,16 @@ #include "settings.h" namespace skyline { + void Settings::Subscribe(Callback callback) { + callbacks.push_back(std::move(callback)); + } + + void Settings::OnSettingsChanged() { + std::for_each(callbacks.begin(), callbacks.end(), [&](const Callback& listener) { + listener(*this); + }); + } + /** * @note This is a placeholder implementation, it must be overridden via template specialisation for platform-specific behavior */ diff --git a/app/src/main/cpp/skyline/common/settings.h b/app/src/main/cpp/skyline/common/settings.h index b47e043d..a07cbeea 100644 --- a/app/src/main/cpp/skyline/common/settings.h +++ b/app/src/main/cpp/skyline/common/settings.h @@ -27,9 +27,23 @@ namespace skyline { /** * @brief Updates settings with the given values + * @note The implementations of this method must call OnSettingsChanged * @param newSettings A platform-specific object containing the new settings' values */ template void Update(T newSettings); + + using Callback = std::function; + + /** + * @brief Subscribe to settings changes + * @param callback The function to be called when settings change + */ + void Subscribe(Callback callback); + + private: + std::vector callbacks; //!< Callbacks to be called when settings change + + void OnSettingsChanged(); }; } diff --git a/app/src/main/java/emu/skyline/utils/SettingsValues.kt b/app/src/main/java/emu/skyline/utils/SettingsValues.kt index 65eb1d74..c83a2b9f 100644 --- a/app/src/main/java/emu/skyline/utils/SettingsValues.kt +++ b/app/src/main/java/emu/skyline/utils/SettingsValues.kt @@ -13,4 +13,9 @@ class SettingsValues(pref: Settings) : Serializable { var systemLanguage : Int = pref.systemLanguage var forceTripleBuffering : Boolean = pref.forceTripleBuffering var disableFrameThrottling : Boolean = pref.disableFrameThrottling + + /** + * Updates settings in libskyline during emulation + */ + external fun updateNative() }