diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index bde9627e..530ffaa9 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -134,7 +134,6 @@ add_library(skyline SHARED ${source_DIR}/skyline/common.cpp ${source_DIR}/skyline/common/exception.cpp ${source_DIR}/skyline/common/logger.cpp - ${source_DIR}/skyline/common/settings.cpp ${source_DIR}/skyline/common/signal.cpp ${source_DIR}/skyline/common/uuid.cpp ${source_DIR}/skyline/common/trace.cpp diff --git a/app/src/main/cpp/emu_jni.cpp b/app/src/main/cpp/emu_jni.cpp index f0b23f5f..fc49925e 100644 --- a/app/src/main/cpp/emu_jni.cpp +++ b/app/src/main/cpp/emu_jni.cpp @@ -8,7 +8,7 @@ #include "skyline/common.h" #include "skyline/common/language.h" #include "skyline/common/signal.h" -#include "skyline/common/settings.h" +#include "skyline/common/android_settings.h" #include "skyline/common/trace.h" #include "skyline/loader/loader.h" #include "skyline/vfs/android_asset_filesystem.h" @@ -54,14 +54,6 @@ static std::string GetTimeZoneName() { return "GMT"; } -template<> void skyline::Settings::Update(KtSettings newSettings) { - isDocked = newSettings.GetBool("isDocked"); - usernameValue = newSettings.GetString("usernameValue"); - systemLanguage = newSettings.GetInt("systemLanguage"); - forceTripleBuffering = newSettings.GetBool("forceTripleBuffering"); - disableFrameThrottling = newSettings.GetBool("disableFrameThrottling"); -} - extern "C" JNIEXPORT void Java_emu_skyline_SkylineApplication_initializeLog( JNIEnv *env, jobject, @@ -93,8 +85,7 @@ extern "C" JNIEXPORT void Java_emu_skyline_EmulationActivity_executeApplication( auto jvmManager{std::make_shared(env, instance)}; - skyline::KtSettings ktSettings{env, settingsInstance}; - auto settings{std::make_shared(ktSettings)}; + std::shared_ptr settings{std::make_shared(env, settingsInstance)}; skyline::JniString publicAppFilesPath(env, publicAppFilesPathJstring); skyline::Logger::EmulationContext.Initialize(publicAppFilesPath + "logs/emulation.sklog"); @@ -240,10 +231,9 @@ extern "C" JNIEXPORT void JNICALL Java_emu_skyline_EmulationActivity_setTouchSta env->ReleaseIntArrayElements(pointsJni, reinterpret_cast(points.data()), JNI_ABORT); } -extern "C" JNIEXPORT void JNICALL Java_emu_skyline_utils_SettingsValues_updateNative(JNIEnv *env, jobject settingsInstance) { +extern "C" JNIEXPORT void JNICALL Java_emu_skyline_utils_SettingsValues_updateNative(JNIEnv *env, jobject) { 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); + settings->Update(); } diff --git a/app/src/main/cpp/skyline/common/android_settings.h b/app/src/main/cpp/skyline/common/android_settings.h new file mode 100644 index 00000000..ac17b8eb --- /dev/null +++ b/app/src/main/cpp/skyline/common/android_settings.h @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/) + +#pragma once + +#include "settings.h" +#include "jvm.h" + +namespace skyline { + /** + * @brief Handles settings on the android platform + * @note Lifetime of this class must not exceed the one of the JNIEnv contained inside ktSettings + */ + class AndroidSettings final : public Settings { + private: + KtSettings ktSettings; + + public: + /** + * @note Will construct the underlying KtSettings object in-place + */ + AndroidSettings(JNIEnv *env, jobject settingsInstance) : ktSettings(env, settingsInstance) { + Update(); + } + + /** + * @note Will take ownership of the passed KtSettings object + */ + AndroidSettings(KtSettings &&ktSettings) : ktSettings(std::move(ktSettings)) { + Update(); + } + + void Update() override { + isDocked = ktSettings.GetBool("isDocked"); + usernameValue = std::move(ktSettings.GetString("usernameValue")); + systemLanguage = ktSettings.GetInt("systemLanguage"); + forceTripleBuffering = ktSettings.GetBool("forceTripleBuffering"); + disableFrameThrottling = ktSettings.GetBool("disableFrameThrottling"); + }; + }; +} diff --git a/app/src/main/cpp/skyline/common/settings.cpp b/app/src/main/cpp/skyline/common/settings.cpp deleted file mode 100644 index bd33ff07..00000000 --- a/app/src/main/cpp/skyline/common/settings.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: MPL-2.0 -// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/) - -#include "settings.h" - -namespace skyline { - /** - * @note This is a placeholder implementation, it must be overridden via template specialisation for platform-specific behavior - */ - template - void Settings::Update(T newSettings) {} -} diff --git a/app/src/main/cpp/skyline/common/settings.h b/app/src/main/cpp/skyline/common/settings.h index 2e4f6163..ed22b4c4 100644 --- a/app/src/main/cpp/skyline/common/settings.h +++ b/app/src/main/cpp/skyline/common/settings.h @@ -7,7 +7,7 @@ namespace skyline { /** - * @brief The Settings class is used to access preferences set in the Kotlin component of Skyline + * @brief The Settings class provides a simple interface to access user-defined settings, update values and subscribe callbacks to observe changes. */ class Settings { template @@ -60,16 +60,14 @@ namespace skyline { Setting forceTripleBuffering; //!< If the presentation engine should always triple buffer even if the swapchain supports double buffering Setting disableFrameThrottling; //!< Allow the guest to submit frames without any blocking calls - template - Settings(T settings) { - Update(settings); - } + Settings() = default; + + virtual ~Settings() = default; /** * @brief Updates settings with the given values - * @param newSettings A platform-specific object containing the new settings' values + * @note This method is platform-specific and must be overridden */ - template - void Update(T newSettings); + virtual void Update() = 0; }; } diff --git a/app/src/main/cpp/skyline/jvm.h b/app/src/main/cpp/skyline/jvm.h index 0783054e..535a307a 100644 --- a/app/src/main/cpp/skyline/jvm.h +++ b/app/src/main/cpp/skyline/jvm.h @@ -21,6 +21,7 @@ namespace skyline { /** * @brief A wrapper over the `Settings` Kotlin class * @note The lifetime of this class must not exceed that of the JNI environment + * @note Copy construction of this class is disallowed to avoid issues with the JNI environment lifetime */ class KtSettings { private: @@ -31,6 +32,12 @@ namespace skyline { public: KtSettings(JNIEnv *env, jobject settingsInstance) : env(env), settingsInstance(settingsInstance), settingsClass(env->GetObjectClass(settingsInstance)) {} + KtSettings(const KtSettings &) = delete; + + void operator=(const KtSettings &) = delete; + + KtSettings(KtSettings &&) = default; + /** * @param key A null terminated string containing the key of the setting to get */