From e7c4c8b993ce27a50b7a56f90247056048d20f7d Mon Sep 17 00:00:00 2001 From: t895 Date: Mon, 5 Feb 2024 06:07:29 -0500 Subject: android: Move JNI setup and helpers to common --- src/android/app/src/main/jni/CMakeLists.txt | 6 - .../src/main/jni/android_common/android_common.cpp | 60 --- .../src/main/jni/android_common/android_common.h | 22 -- .../app/src/main/jni/applets/software_keyboard.cpp | 277 ------------- .../app/src/main/jni/applets/software_keyboard.h | 78 ---- src/android/app/src/main/jni/game_metadata.cpp | 22 +- src/android/app/src/main/jni/id_cache.cpp | 428 --------------------- src/android/app/src/main/jni/id_cache.h | 68 ---- src/android/app/src/main/jni/native.cpp | 125 +++--- src/android/app/src/main/jni/native.h | 6 +- src/android/app/src/main/jni/native_config.cpp | 121 +++--- src/android/app/src/main/jni/native_log.cpp | 13 +- src/common/CMakeLists.txt | 8 +- src/common/android/android_common.cpp | 65 ++++ src/common/android/android_common.h | 26 ++ src/common/android/applets/software_keyboard.cpp | 277 +++++++++++++ src/common/android/applets/software_keyboard.h | 78 ++++ src/common/android/id_cache.cpp | 428 +++++++++++++++++++++ src/common/android/id_cache.h | 88 +++++ 19 files changed, 1125 insertions(+), 1071 deletions(-) delete mode 100644 src/android/app/src/main/jni/android_common/android_common.cpp delete mode 100644 src/android/app/src/main/jni/android_common/android_common.h delete mode 100644 src/android/app/src/main/jni/applets/software_keyboard.cpp delete mode 100644 src/android/app/src/main/jni/applets/software_keyboard.h delete mode 100644 src/android/app/src/main/jni/id_cache.cpp delete mode 100644 src/android/app/src/main/jni/id_cache.h create mode 100644 src/common/android/android_common.cpp create mode 100644 src/common/android/android_common.h create mode 100644 src/common/android/applets/software_keyboard.cpp create mode 100644 src/common/android/applets/software_keyboard.h create mode 100644 src/common/android/id_cache.cpp create mode 100644 src/common/android/id_cache.h diff --git a/src/android/app/src/main/jni/CMakeLists.txt b/src/android/app/src/main/jni/CMakeLists.txt index abc6055ab..20b319c12 100644 --- a/src/android/app/src/main/jni/CMakeLists.txt +++ b/src/android/app/src/main/jni/CMakeLists.txt @@ -2,14 +2,8 @@ # SPDX-License-Identifier: GPL-3.0-or-later add_library(yuzu-android SHARED - android_common/android_common.cpp - android_common/android_common.h - applets/software_keyboard.cpp - applets/software_keyboard.h emu_window/emu_window.cpp emu_window/emu_window.h - id_cache.cpp - id_cache.h native.cpp native.h native_config.cpp diff --git a/src/android/app/src/main/jni/android_common/android_common.cpp b/src/android/app/src/main/jni/android_common/android_common.cpp deleted file mode 100644 index 7018a52af..000000000 --- a/src/android/app/src/main/jni/android_common/android_common.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "jni/android_common/android_common.h" - -#include -#include - -#include - -#include "common/string_util.h" -#include "jni/id_cache.h" - -std::string GetJString(JNIEnv* env, jstring jstr) { - if (!jstr) { - return {}; - } - - const jchar* jchars = env->GetStringChars(jstr, nullptr); - const jsize length = env->GetStringLength(jstr); - const std::u16string_view string_view(reinterpret_cast(jchars), length); - const std::string converted_string = Common::UTF16ToUTF8(string_view); - env->ReleaseStringChars(jstr, jchars); - - return converted_string; -} - -jstring ToJString(JNIEnv* env, std::string_view str) { - const std::u16string converted_string = Common::UTF8ToUTF16(str); - return env->NewString(reinterpret_cast(converted_string.data()), - static_cast(converted_string.size())); -} - -jstring ToJString(JNIEnv* env, std::u16string_view str) { - return ToJString(env, Common::UTF16ToUTF8(str)); -} - -double GetJDouble(JNIEnv* env, jobject jdouble) { - return env->GetDoubleField(jdouble, IDCache::GetDoubleValueField()); -} - -jobject ToJDouble(JNIEnv* env, double value) { - return env->NewObject(IDCache::GetDoubleClass(), IDCache::GetDoubleConstructor(), value); -} - -s32 GetJInteger(JNIEnv* env, jobject jinteger) { - return env->GetIntField(jinteger, IDCache::GetIntegerValueField()); -} - -jobject ToJInteger(JNIEnv* env, s32 value) { - return env->NewObject(IDCache::GetIntegerClass(), IDCache::GetIntegerConstructor(), value); -} - -bool GetJBoolean(JNIEnv* env, jobject jboolean) { - return env->GetBooleanField(jboolean, IDCache::GetBooleanValueField()); -} - -jobject ToJBoolean(JNIEnv* env, bool value) { - return env->NewObject(IDCache::GetBooleanClass(), IDCache::GetBooleanConstructor(), value); -} diff --git a/src/android/app/src/main/jni/android_common/android_common.h b/src/android/app/src/main/jni/android_common/android_common.h deleted file mode 100644 index 29a338c0a..000000000 --- a/src/android/app/src/main/jni/android_common/android_common.h +++ /dev/null @@ -1,22 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include - -#include -#include "common/common_types.h" - -std::string GetJString(JNIEnv* env, jstring jstr); -jstring ToJString(JNIEnv* env, std::string_view str); -jstring ToJString(JNIEnv* env, std::u16string_view str); - -double GetJDouble(JNIEnv* env, jobject jdouble); -jobject ToJDouble(JNIEnv* env, double value); - -s32 GetJInteger(JNIEnv* env, jobject jinteger); -jobject ToJInteger(JNIEnv* env, s32 value); - -bool GetJBoolean(JNIEnv* env, jobject jboolean); -jobject ToJBoolean(JNIEnv* env, bool value); diff --git a/src/android/app/src/main/jni/applets/software_keyboard.cpp b/src/android/app/src/main/jni/applets/software_keyboard.cpp deleted file mode 100644 index 9943483e8..000000000 --- a/src/android/app/src/main/jni/applets/software_keyboard.cpp +++ /dev/null @@ -1,277 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include -#include - -#include - -#include "common/logging/log.h" -#include "common/string_util.h" -#include "core/core.h" -#include "jni/android_common/android_common.h" -#include "jni/applets/software_keyboard.h" -#include "jni/id_cache.h" - -static jclass s_software_keyboard_class; -static jclass s_keyboard_config_class; -static jclass s_keyboard_data_class; -static jmethodID s_swkbd_execute_normal; -static jmethodID s_swkbd_execute_inline; - -namespace SoftwareKeyboard { - -static jobject ToJKeyboardParams(const Core::Frontend::KeyboardInitializeParameters& config) { - JNIEnv* env = IDCache::GetEnvForThread(); - jobject object = env->AllocObject(s_keyboard_config_class); - - env->SetObjectField(object, - env->GetFieldID(s_keyboard_config_class, "ok_text", "Ljava/lang/String;"), - ToJString(env, config.ok_text)); - env->SetObjectField( - object, env->GetFieldID(s_keyboard_config_class, "header_text", "Ljava/lang/String;"), - ToJString(env, config.header_text)); - env->SetObjectField(object, - env->GetFieldID(s_keyboard_config_class, "sub_text", "Ljava/lang/String;"), - ToJString(env, config.sub_text)); - env->SetObjectField( - object, env->GetFieldID(s_keyboard_config_class, "guide_text", "Ljava/lang/String;"), - ToJString(env, config.guide_text)); - env->SetObjectField( - object, env->GetFieldID(s_keyboard_config_class, "initial_text", "Ljava/lang/String;"), - ToJString(env, config.initial_text)); - env->SetShortField(object, - env->GetFieldID(s_keyboard_config_class, "left_optional_symbol_key", "S"), - static_cast(config.left_optional_symbol_key)); - env->SetShortField(object, - env->GetFieldID(s_keyboard_config_class, "right_optional_symbol_key", "S"), - static_cast(config.right_optional_symbol_key)); - env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "max_text_length", "I"), - static_cast(config.max_text_length)); - env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "min_text_length", "I"), - static_cast(config.min_text_length)); - env->SetIntField(object, - env->GetFieldID(s_keyboard_config_class, "initial_cursor_position", "I"), - static_cast(config.initial_cursor_position)); - env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "type", "I"), - static_cast(config.type)); - env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "password_mode", "I"), - static_cast(config.password_mode)); - env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "text_draw_type", "I"), - static_cast(config.text_draw_type)); - env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "key_disable_flags", "I"), - static_cast(config.key_disable_flags.raw)); - env->SetBooleanField(object, - env->GetFieldID(s_keyboard_config_class, "use_blur_background", "Z"), - static_cast(config.use_blur_background)); - env->SetBooleanField(object, - env->GetFieldID(s_keyboard_config_class, "enable_backspace_button", "Z"), - static_cast(config.enable_backspace_button)); - env->SetBooleanField(object, - env->GetFieldID(s_keyboard_config_class, "enable_return_button", "Z"), - static_cast(config.enable_return_button)); - env->SetBooleanField(object, - env->GetFieldID(s_keyboard_config_class, "disable_cancel_button", "Z"), - static_cast(config.disable_cancel_button)); - - return object; -} - -AndroidKeyboard::ResultData AndroidKeyboard::ResultData::CreateFromFrontend(jobject object) { - JNIEnv* env = IDCache::GetEnvForThread(); - const jstring string = reinterpret_cast(env->GetObjectField( - object, env->GetFieldID(s_keyboard_data_class, "text", "Ljava/lang/String;"))); - return ResultData{GetJString(env, string), - static_cast(env->GetIntField( - object, env->GetFieldID(s_keyboard_data_class, "result", "I")))}; -} - -AndroidKeyboard::~AndroidKeyboard() = default; - -void AndroidKeyboard::InitializeKeyboard( - bool is_inline, Core::Frontend::KeyboardInitializeParameters initialize_parameters, - SubmitNormalCallback submit_normal_callback_, SubmitInlineCallback submit_inline_callback_) { - if (is_inline) { - LOG_WARNING( - Frontend, - "(STUBBED) called, backend requested to initialize the inline software keyboard."); - - submit_inline_callback = std::move(submit_inline_callback_); - } else { - LOG_WARNING( - Frontend, - "(STUBBED) called, backend requested to initialize the normal software keyboard."); - - submit_normal_callback = std::move(submit_normal_callback_); - } - - parameters = std::move(initialize_parameters); - - LOG_INFO(Frontend, - "\nKeyboardInitializeParameters:" - "\nok_text={}" - "\nheader_text={}" - "\nsub_text={}" - "\nguide_text={}" - "\ninitial_text={}" - "\nmax_text_length={}" - "\nmin_text_length={}" - "\ninitial_cursor_position={}" - "\ntype={}" - "\npassword_mode={}" - "\ntext_draw_type={}" - "\nkey_disable_flags={}" - "\nuse_blur_background={}" - "\nenable_backspace_button={}" - "\nenable_return_button={}" - "\ndisable_cancel_button={}", - Common::UTF16ToUTF8(parameters.ok_text), Common::UTF16ToUTF8(parameters.header_text), - Common::UTF16ToUTF8(parameters.sub_text), Common::UTF16ToUTF8(parameters.guide_text), - Common::UTF16ToUTF8(parameters.initial_text), parameters.max_text_length, - parameters.min_text_length, parameters.initial_cursor_position, parameters.type, - parameters.password_mode, parameters.text_draw_type, parameters.key_disable_flags.raw, - parameters.use_blur_background, parameters.enable_backspace_button, - parameters.enable_return_button, parameters.disable_cancel_button); -} - -void AndroidKeyboard::ShowNormalKeyboard() const { - LOG_DEBUG(Frontend, "called, backend requested to show the normal software keyboard."); - - ResultData data{}; - - // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. - std::thread([&] { - data = ResultData::CreateFromFrontend(IDCache::GetEnvForThread()->CallStaticObjectMethod( - s_software_keyboard_class, s_swkbd_execute_normal, ToJKeyboardParams(parameters))); - }).join(); - - SubmitNormalText(data); -} - -void AndroidKeyboard::ShowTextCheckDialog( - Service::AM::Frontend::SwkbdTextCheckResult text_check_result, - std::u16string text_check_message) const { - LOG_WARNING(Frontend, "(STUBBED) called, backend requested to show the text check dialog."); -} - -void AndroidKeyboard::ShowInlineKeyboard( - Core::Frontend::InlineAppearParameters appear_parameters) const { - LOG_WARNING(Frontend, - "(STUBBED) called, backend requested to show the inline software keyboard."); - - LOG_INFO(Frontend, - "\nInlineAppearParameters:" - "\nmax_text_length={}" - "\nmin_text_length={}" - "\nkey_top_scale_x={}" - "\nkey_top_scale_y={}" - "\nkey_top_translate_x={}" - "\nkey_top_translate_y={}" - "\ntype={}" - "\nkey_disable_flags={}" - "\nkey_top_as_floating={}" - "\nenable_backspace_button={}" - "\nenable_return_button={}" - "\ndisable_cancel_button={}", - appear_parameters.max_text_length, appear_parameters.min_text_length, - appear_parameters.key_top_scale_x, appear_parameters.key_top_scale_y, - appear_parameters.key_top_translate_x, appear_parameters.key_top_translate_y, - appear_parameters.type, appear_parameters.key_disable_flags.raw, - appear_parameters.key_top_as_floating, appear_parameters.enable_backspace_button, - appear_parameters.enable_return_button, appear_parameters.disable_cancel_button); - - // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. - m_is_inline_active = true; - std::thread([&] { - IDCache::GetEnvForThread()->CallStaticVoidMethod( - s_software_keyboard_class, s_swkbd_execute_inline, ToJKeyboardParams(parameters)); - }).join(); -} - -void AndroidKeyboard::HideInlineKeyboard() const { - LOG_WARNING(Frontend, - "(STUBBED) called, backend requested to hide the inline software keyboard."); -} - -void AndroidKeyboard::InlineTextChanged( - Core::Frontend::InlineTextParameters text_parameters) const { - LOG_WARNING(Frontend, - "(STUBBED) called, backend requested to change the inline keyboard text."); - - LOG_INFO(Frontend, - "\nInlineTextParameters:" - "\ninput_text={}" - "\ncursor_position={}", - Common::UTF16ToUTF8(text_parameters.input_text), text_parameters.cursor_position); - - submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, - text_parameters.input_text, text_parameters.cursor_position); -} - -void AndroidKeyboard::ExitKeyboard() const { - LOG_WARNING(Frontend, "(STUBBED) called, backend requested to exit the software keyboard."); -} - -void AndroidKeyboard::SubmitInlineKeyboardText(std::u16string submitted_text) { - if (!m_is_inline_active) { - return; - } - - m_current_text += submitted_text; - - submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, - m_current_text.size()); -} - -void AndroidKeyboard::SubmitInlineKeyboardInput(int key_code) { - static constexpr int KEYCODE_BACK = 4; - static constexpr int KEYCODE_ENTER = 66; - static constexpr int KEYCODE_DEL = 67; - - if (!m_is_inline_active) { - return; - } - - switch (key_code) { - case KEYCODE_BACK: - case KEYCODE_ENTER: - m_is_inline_active = false; - submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::DecidedEnter, m_current_text, - static_cast(m_current_text.size())); - break; - case KEYCODE_DEL: - m_current_text.pop_back(); - submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, - m_current_text.size()); - break; - } -} - -void AndroidKeyboard::SubmitNormalText(const ResultData& data) const { - submit_normal_callback(data.result, Common::UTF8ToUTF16(data.text), true); -} - -void InitJNI(JNIEnv* env) { - s_software_keyboard_class = reinterpret_cast( - env->NewGlobalRef(env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard"))); - s_keyboard_config_class = reinterpret_cast(env->NewGlobalRef( - env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig"))); - s_keyboard_data_class = reinterpret_cast(env->NewGlobalRef( - env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardData"))); - - s_swkbd_execute_normal = env->GetStaticMethodID( - s_software_keyboard_class, "executeNormal", - "(Lorg/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig;)Lorg/yuzu/yuzu_emu/" - "applets/keyboard/SoftwareKeyboard$KeyboardData;"); - s_swkbd_execute_inline = env->GetStaticMethodID( - s_software_keyboard_class, "executeInline", - "(Lorg/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig;)V"); -} - -void CleanupJNI(JNIEnv* env) { - env->DeleteGlobalRef(s_software_keyboard_class); - env->DeleteGlobalRef(s_keyboard_config_class); - env->DeleteGlobalRef(s_keyboard_data_class); -} - -} // namespace SoftwareKeyboard diff --git a/src/android/app/src/main/jni/applets/software_keyboard.h b/src/android/app/src/main/jni/applets/software_keyboard.h deleted file mode 100644 index 2affc01f6..000000000 --- a/src/android/app/src/main/jni/applets/software_keyboard.h +++ /dev/null @@ -1,78 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include - -#include "core/frontend/applets/software_keyboard.h" - -namespace SoftwareKeyboard { - -class AndroidKeyboard final : public Core::Frontend::SoftwareKeyboardApplet { -public: - ~AndroidKeyboard() override; - - void Close() const override { - ExitKeyboard(); - } - - void InitializeKeyboard(bool is_inline, - Core::Frontend::KeyboardInitializeParameters initialize_parameters, - SubmitNormalCallback submit_normal_callback_, - SubmitInlineCallback submit_inline_callback_) override; - - void ShowNormalKeyboard() const override; - - void ShowTextCheckDialog(Service::AM::Frontend::SwkbdTextCheckResult text_check_result, - std::u16string text_check_message) const override; - - void ShowInlineKeyboard( - Core::Frontend::InlineAppearParameters appear_parameters) const override; - - void HideInlineKeyboard() const override; - - void InlineTextChanged(Core::Frontend::InlineTextParameters text_parameters) const override; - - void ExitKeyboard() const override; - - void SubmitInlineKeyboardText(std::u16string submitted_text); - - void SubmitInlineKeyboardInput(int key_code); - -private: - struct ResultData { - static ResultData CreateFromFrontend(jobject object); - - std::string text; - Service::AM::Frontend::SwkbdResult result{}; - }; - - void SubmitNormalText(const ResultData& result) const; - - Core::Frontend::KeyboardInitializeParameters parameters{}; - - mutable SubmitNormalCallback submit_normal_callback; - mutable SubmitInlineCallback submit_inline_callback; - -private: - mutable bool m_is_inline_active{}; - std::u16string m_current_text; -}; - -// Should be called in JNI_Load -void InitJNI(JNIEnv* env); - -// Should be called in JNI_Unload -void CleanupJNI(JNIEnv* env); - -} // namespace SoftwareKeyboard - -// Native function calls -extern "C" { -JNIEXPORT jobject JNICALL Java_org_citra_citra_1emu_applets_SoftwareKeyboard_ValidateFilters( - JNIEnv* env, jclass clazz, jstring text); - -JNIEXPORT jobject JNICALL Java_org_citra_citra_1emu_applets_SoftwareKeyboard_ValidateInput( - JNIEnv* env, jclass clazz, jstring text); -} diff --git a/src/android/app/src/main/jni/game_metadata.cpp b/src/android/app/src/main/jni/game_metadata.cpp index 8f0da1413..c33763b47 100644 --- a/src/android/app/src/main/jni/game_metadata.cpp +++ b/src/android/app/src/main/jni/game_metadata.cpp @@ -1,13 +1,12 @@ // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "common/android/android_common.h" #include "core/core.h" #include "core/file_sys/fs_filesystem.h" #include "core/file_sys/patch_manager.h" #include "core/loader/loader.h" #include "core/loader/nro.h" -#include "jni.h" -#include "jni/android_common/android_common.h" #include "native.h" struct RomMetadata { @@ -79,7 +78,7 @@ extern "C" { jboolean Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getIsValid(JNIEnv* env, jobject obj, jstring jpath) { const auto file = EmulationSession::GetInstance().System().GetFilesystem()->OpenFile( - GetJString(env, jpath), FileSys::OpenMode::Read); + Common::Android::GetJString(env, jpath), FileSys::OpenMode::Read); if (!file) { return false; } @@ -104,27 +103,31 @@ jboolean Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getIsValid(JNIEnv* env, jobj jstring Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getTitle(JNIEnv* env, jobject obj, jstring jpath) { - return ToJString(env, GetRomMetadata(GetJString(env, jpath)).title); + return Common::Android::ToJString( + env, GetRomMetadata(Common::Android::GetJString(env, jpath)).title); } jstring Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getProgramId(JNIEnv* env, jobject obj, jstring jpath) { - return ToJString(env, std::to_string(GetRomMetadata(GetJString(env, jpath)).programId)); + return Common::Android::ToJString( + env, std::to_string(GetRomMetadata(Common::Android::GetJString(env, jpath)).programId)); } jstring Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getDeveloper(JNIEnv* env, jobject obj, jstring jpath) { - return ToJString(env, GetRomMetadata(GetJString(env, jpath)).developer); + return Common::Android::ToJString( + env, GetRomMetadata(Common::Android::GetJString(env, jpath)).developer); } jstring Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getVersion(JNIEnv* env, jobject obj, jstring jpath, jboolean jreload) { - return ToJString(env, GetRomMetadata(GetJString(env, jpath), jreload).version); + return Common::Android::ToJString( + env, GetRomMetadata(Common::Android::GetJString(env, jpath), jreload).version); } jbyteArray Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getIcon(JNIEnv* env, jobject obj, jstring jpath) { - auto icon_data = GetRomMetadata(GetJString(env, jpath)).icon; + auto icon_data = GetRomMetadata(Common::Android::GetJString(env, jpath)).icon; jbyteArray icon = env->NewByteArray(static_cast(icon_data.size())); env->SetByteArrayRegion(icon, 0, env->GetArrayLength(icon), reinterpret_cast(icon_data.data())); @@ -133,7 +136,8 @@ jbyteArray Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getIcon(JNIEnv* env, jobje jboolean Java_org_yuzu_yuzu_1emu_utils_GameMetadata_getIsHomebrew(JNIEnv* env, jobject obj, jstring jpath) { - return static_cast(GetRomMetadata(GetJString(env, jpath)).isHomebrew); + return static_cast( + GetRomMetadata(Common::Android::GetJString(env, jpath)).isHomebrew); } void Java_org_yuzu_yuzu_1emu_utils_GameMetadata_resetMetadata(JNIEnv* env, jobject obj) { diff --git a/src/android/app/src/main/jni/id_cache.cpp b/src/android/app/src/main/jni/id_cache.cpp deleted file mode 100644 index f30100bd8..000000000 --- a/src/android/app/src/main/jni/id_cache.cpp +++ /dev/null @@ -1,428 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include - -#include "common/assert.h" -#include "common/fs/fs_android.h" -#include "jni/applets/software_keyboard.h" -#include "jni/id_cache.h" -#include "video_core/rasterizer_interface.h" - -static JavaVM* s_java_vm; -static jclass s_native_library_class; -static jclass s_disk_cache_progress_class; -static jclass s_load_callback_stage_class; -static jclass s_game_dir_class; -static jmethodID s_game_dir_constructor; -static jmethodID s_exit_emulation_activity; -static jmethodID s_disk_cache_load_progress; -static jmethodID s_on_emulation_started; -static jmethodID s_on_emulation_stopped; -static jmethodID s_on_program_changed; - -static jclass s_game_class; -static jmethodID s_game_constructor; -static jfieldID s_game_title_field; -static jfieldID s_game_path_field; -static jfieldID s_game_program_id_field; -static jfieldID s_game_developer_field; -static jfieldID s_game_version_field; -static jfieldID s_game_is_homebrew_field; - -static jclass s_string_class; -static jclass s_pair_class; -static jmethodID s_pair_constructor; -static jfieldID s_pair_first_field; -static jfieldID s_pair_second_field; - -static jclass s_overlay_control_data_class; -static jmethodID s_overlay_control_data_constructor; -static jfieldID s_overlay_control_data_id_field; -static jfieldID s_overlay_control_data_enabled_field; -static jfieldID s_overlay_control_data_landscape_position_field; -static jfieldID s_overlay_control_data_portrait_position_field; -static jfieldID s_overlay_control_data_foldable_position_field; - -static jclass s_patch_class; -static jmethodID s_patch_constructor; -static jfieldID s_patch_enabled_field; -static jfieldID s_patch_name_field; -static jfieldID s_patch_version_field; -static jfieldID s_patch_type_field; -static jfieldID s_patch_program_id_field; -static jfieldID s_patch_title_id_field; - -static jclass s_double_class; -static jmethodID s_double_constructor; -static jfieldID s_double_value_field; - -static jclass s_integer_class; -static jmethodID s_integer_constructor; -static jfieldID s_integer_value_field; - -static jclass s_boolean_class; -static jmethodID s_boolean_constructor; -static jfieldID s_boolean_value_field; - -static constexpr jint JNI_VERSION = JNI_VERSION_1_6; - -namespace IDCache { - -JNIEnv* GetEnvForThread() { - thread_local static struct OwnedEnv { - OwnedEnv() { - status = s_java_vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6); - if (status == JNI_EDETACHED) - s_java_vm->AttachCurrentThread(&env, nullptr); - } - - ~OwnedEnv() { - if (status == JNI_EDETACHED) - s_java_vm->DetachCurrentThread(); - } - - int status; - JNIEnv* env = nullptr; - } owned; - return owned.env; -} - -jclass GetNativeLibraryClass() { - return s_native_library_class; -} - -jclass GetDiskCacheProgressClass() { - return s_disk_cache_progress_class; -} - -jclass GetDiskCacheLoadCallbackStageClass() { - return s_load_callback_stage_class; -} - -jclass GetGameDirClass() { - return s_game_dir_class; -} - -jmethodID GetGameDirConstructor() { - return s_game_dir_constructor; -} - -jmethodID GetExitEmulationActivity() { - return s_exit_emulation_activity; -} - -jmethodID GetDiskCacheLoadProgress() { - return s_disk_cache_load_progress; -} - -jmethodID GetOnEmulationStarted() { - return s_on_emulation_started; -} - -jmethodID GetOnEmulationStopped() { - return s_on_emulation_stopped; -} - -jmethodID GetOnProgramChanged() { - return s_on_program_changed; -} - -jclass GetGameClass() { - return s_game_class; -} - -jmethodID GetGameConstructor() { - return s_game_constructor; -} - -jfieldID GetGameTitleField() { - return s_game_title_field; -} - -jfieldID GetGamePathField() { - return s_game_path_field; -} - -jfieldID GetGameProgramIdField() { - return s_game_program_id_field; -} - -jfieldID GetGameDeveloperField() { - return s_game_developer_field; -} - -jfieldID GetGameVersionField() { - return s_game_version_field; -} - -jfieldID GetGameIsHomebrewField() { - return s_game_is_homebrew_field; -} - -jclass GetStringClass() { - return s_string_class; -} - -jclass GetPairClass() { - return s_pair_class; -} - -jmethodID GetPairConstructor() { - return s_pair_constructor; -} - -jfieldID GetPairFirstField() { - return s_pair_first_field; -} - -jfieldID GetPairSecondField() { - return s_pair_second_field; -} - -jclass GetOverlayControlDataClass() { - return s_overlay_control_data_class; -} - -jmethodID GetOverlayControlDataConstructor() { - return s_overlay_control_data_constructor; -} - -jfieldID GetOverlayControlDataIdField() { - return s_overlay_control_data_id_field; -} - -jfieldID GetOverlayControlDataEnabledField() { - return s_overlay_control_data_enabled_field; -} - -jfieldID GetOverlayControlDataLandscapePositionField() { - return s_overlay_control_data_landscape_position_field; -} - -jfieldID GetOverlayControlDataPortraitPositionField() { - return s_overlay_control_data_portrait_position_field; -} - -jfieldID GetOverlayControlDataFoldablePositionField() { - return s_overlay_control_data_foldable_position_field; -} - -jclass GetPatchClass() { - return s_patch_class; -} - -jmethodID GetPatchConstructor() { - return s_patch_constructor; -} - -jfieldID GetPatchEnabledField() { - return s_patch_enabled_field; -} - -jfieldID GetPatchNameField() { - return s_patch_name_field; -} - -jfieldID GetPatchVersionField() { - return s_patch_version_field; -} - -jfieldID GetPatchTypeField() { - return s_patch_type_field; -} - -jfieldID GetPatchProgramIdField() { - return s_patch_program_id_field; -} - -jfieldID GetPatchTitleIdField() { - return s_patch_title_id_field; -} - -jclass GetDoubleClass() { - return s_double_class; -} - -jmethodID GetDoubleConstructor() { - return s_double_constructor; -} - -jfieldID GetDoubleValueField() { - return s_double_value_field; -} - -jclass GetIntegerClass() { - return s_integer_class; -} - -jmethodID GetIntegerConstructor() { - return s_integer_constructor; -} - -jfieldID GetIntegerValueField() { - return s_integer_value_field; -} - -jclass GetBooleanClass() { - return s_boolean_class; -} - -jmethodID GetBooleanConstructor() { - return s_boolean_constructor; -} - -jfieldID GetBooleanValueField() { - return s_boolean_value_field; -} - -} // namespace IDCache - -#ifdef __cplusplus -extern "C" { -#endif - -jint JNI_OnLoad(JavaVM* vm, void* reserved) { - s_java_vm = vm; - - JNIEnv* env; - if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION) != JNI_OK) - return JNI_ERR; - - // Initialize Java classes - const jclass native_library_class = env->FindClass("org/yuzu/yuzu_emu/NativeLibrary"); - s_native_library_class = reinterpret_cast(env->NewGlobalRef(native_library_class)); - s_disk_cache_progress_class = reinterpret_cast(env->NewGlobalRef( - env->FindClass("org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress"))); - s_load_callback_stage_class = reinterpret_cast(env->NewGlobalRef(env->FindClass( - "org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress$LoadCallbackStage"))); - - const jclass game_dir_class = env->FindClass("org/yuzu/yuzu_emu/model/GameDir"); - s_game_dir_class = reinterpret_cast(env->NewGlobalRef(game_dir_class)); - s_game_dir_constructor = env->GetMethodID(game_dir_class, "", "(Ljava/lang/String;Z)V"); - env->DeleteLocalRef(game_dir_class); - - // Initialize methods - s_exit_emulation_activity = - env->GetStaticMethodID(s_native_library_class, "exitEmulationActivity", "(I)V"); - s_disk_cache_load_progress = - env->GetStaticMethodID(s_disk_cache_progress_class, "loadProgress", "(III)V"); - s_on_emulation_started = - env->GetStaticMethodID(s_native_library_class, "onEmulationStarted", "()V"); - s_on_emulation_stopped = - env->GetStaticMethodID(s_native_library_class, "onEmulationStopped", "(I)V"); - s_on_program_changed = - env->GetStaticMethodID(s_native_library_class, "onProgramChanged", "(I)V"); - - const jclass game_class = env->FindClass("org/yuzu/yuzu_emu/model/Game"); - s_game_class = reinterpret_cast(env->NewGlobalRef(game_class)); - s_game_constructor = env->GetMethodID(game_class, "", - "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/" - "String;Ljava/lang/String;Ljava/lang/String;Z)V"); - s_game_title_field = env->GetFieldID(game_class, "title", "Ljava/lang/String;"); - s_game_path_field = env->GetFieldID(game_class, "path", "Ljava/lang/String;"); - s_game_program_id_field = env->GetFieldID(game_class, "programId", "Ljava/lang/String;"); - s_game_developer_field = env->GetFieldID(game_class, "developer", "Ljava/lang/String;"); - s_game_version_field = env->GetFieldID(game_class, "version", "Ljava/lang/String;"); - s_game_is_homebrew_field = env->GetFieldID(game_class, "isHomebrew", "Z"); - env->DeleteLocalRef(game_class); - - const jclass string_class = env->FindClass("java/lang/String"); - s_string_class = reinterpret_cast(env->NewGlobalRef(string_class)); - env->DeleteLocalRef(string_class); - - const jclass pair_class = env->FindClass("kotlin/Pair"); - s_pair_class = reinterpret_cast(env->NewGlobalRef(pair_class)); - s_pair_constructor = - env->GetMethodID(pair_class, "", "(Ljava/lang/Object;Ljava/lang/Object;)V"); - s_pair_first_field = env->GetFieldID(pair_class, "first", "Ljava/lang/Object;"); - s_pair_second_field = env->GetFieldID(pair_class, "second", "Ljava/lang/Object;"); - env->DeleteLocalRef(pair_class); - - const jclass overlay_control_data_class = - env->FindClass("org/yuzu/yuzu_emu/overlay/model/OverlayControlData"); - s_overlay_control_data_class = - reinterpret_cast(env->NewGlobalRef(overlay_control_data_class)); - s_overlay_control_data_constructor = - env->GetMethodID(overlay_control_data_class, "", - "(Ljava/lang/String;ZLkotlin/Pair;Lkotlin/Pair;Lkotlin/Pair;)V"); - s_overlay_control_data_id_field = - env->GetFieldID(overlay_control_data_class, "id", "Ljava/lang/String;"); - s_overlay_control_data_enabled_field = - env->GetFieldID(overlay_control_data_class, "enabled", "Z"); - s_overlay_control_data_landscape_position_field = - env->GetFieldID(overlay_control_data_class, "landscapePosition", "Lkotlin/Pair;"); - s_overlay_control_data_portrait_position_field = - env->GetFieldID(overlay_control_data_class, "portraitPosition", "Lkotlin/Pair;"); - s_overlay_control_data_foldable_position_field = - env->GetFieldID(overlay_control_data_class, "foldablePosition", "Lkotlin/Pair;"); - env->DeleteLocalRef(overlay_control_data_class); - - const jclass patch_class = env->FindClass("org/yuzu/yuzu_emu/model/Patch"); - s_patch_class = reinterpret_cast(env->NewGlobalRef(patch_class)); - s_patch_constructor = env->GetMethodID( - patch_class, "", - "(ZLjava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V"); - s_patch_enabled_field = env->GetFieldID(patch_class, "enabled", "Z"); - s_patch_name_field = env->GetFieldID(patch_class, "name", "Ljava/lang/String;"); - s_patch_version_field = env->GetFieldID(patch_class, "version", "Ljava/lang/String;"); - s_patch_type_field = env->GetFieldID(patch_class, "type", "I"); - s_patch_program_id_field = env->GetFieldID(patch_class, "programId", "Ljava/lang/String;"); - s_patch_title_id_field = env->GetFieldID(patch_class, "titleId", "Ljava/lang/String;"); - env->DeleteLocalRef(patch_class); - - const jclass double_class = env->FindClass("java/lang/Double"); - s_double_class = reinterpret_cast(env->NewGlobalRef(double_class)); - s_double_constructor = env->GetMethodID(double_class, "", "(D)V"); - s_double_value_field = env->GetFieldID(double_class, "value", "D"); - env->DeleteLocalRef(double_class); - - const jclass int_class = env->FindClass("java/lang/Integer"); - s_integer_class = reinterpret_cast(env->NewGlobalRef(int_class)); - s_integer_constructor = env->GetMethodID(int_class, "", "(I)V"); - s_integer_value_field = env->GetFieldID(int_class, "value", "I"); - env->DeleteLocalRef(int_class); - - const jclass boolean_class = env->FindClass("java/lang/Boolean"); - s_boolean_class = reinterpret_cast(env->NewGlobalRef(boolean_class)); - s_boolean_constructor = env->GetMethodID(boolean_class, "", "(Z)V"); - s_boolean_value_field = env->GetFieldID(boolean_class, "value", "Z"); - env->DeleteLocalRef(boolean_class); - - // Initialize Android Storage - Common::FS::Android::RegisterCallbacks(env, s_native_library_class); - - // Initialize applets - SoftwareKeyboard::InitJNI(env); - - return JNI_VERSION; -} - -void JNI_OnUnload(JavaVM* vm, void* reserved) { - JNIEnv* env; - if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION) != JNI_OK) { - return; - } - - // UnInitialize Android Storage - Common::FS::Android::UnRegisterCallbacks(); - env->DeleteGlobalRef(s_native_library_class); - env->DeleteGlobalRef(s_disk_cache_progress_class); - env->DeleteGlobalRef(s_load_callback_stage_class); - env->DeleteGlobalRef(s_game_dir_class); - env->DeleteGlobalRef(s_game_class); - env->DeleteGlobalRef(s_string_class); - env->DeleteGlobalRef(s_pair_class); - env->DeleteGlobalRef(s_overlay_control_data_class); - env->DeleteGlobalRef(s_patch_class); - env->DeleteGlobalRef(s_double_class); - env->DeleteGlobalRef(s_integer_class); - env->DeleteGlobalRef(s_boolean_class); - - // UnInitialize applets - SoftwareKeyboard::CleanupJNI(env); -} - -#ifdef __cplusplus -} -#endif diff --git a/src/android/app/src/main/jni/id_cache.h b/src/android/app/src/main/jni/id_cache.h deleted file mode 100644 index 00e48afc0..000000000 --- a/src/android/app/src/main/jni/id_cache.h +++ /dev/null @@ -1,68 +0,0 @@ -// SPDX-FileCopyrightText: 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-3.0-or-later - -#pragma once - -#include - -#include "video_core/rasterizer_interface.h" - -namespace IDCache { - -JNIEnv* GetEnvForThread(); -jclass GetNativeLibraryClass(); -jclass GetDiskCacheProgressClass(); -jclass GetDiskCacheLoadCallbackStageClass(); -jclass GetGameDirClass(); -jmethodID GetGameDirConstructor(); -jmethodID GetExitEmulationActivity(); -jmethodID GetDiskCacheLoadProgress(); -jmethodID GetOnEmulationStarted(); -jmethodID GetOnEmulationStopped(); -jmethodID GetOnProgramChanged(); - -jclass GetGameClass(); -jmethodID GetGameConstructor(); -jfieldID GetGameTitleField(); -jfieldID GetGamePathField(); -jfieldID GetGameProgramIdField(); -jfieldID GetGameDeveloperField(); -jfieldID GetGameVersionField(); -jfieldID GetGameIsHomebrewField(); - -jclass GetStringClass(); -jclass GetPairClass(); -jmethodID GetPairConstructor(); -jfieldID GetPairFirstField(); -jfieldID GetPairSecondField(); - -jclass GetOverlayControlDataClass(); -jmethodID GetOverlayControlDataConstructor(); -jfieldID GetOverlayControlDataIdField(); -jfieldID GetOverlayControlDataEnabledField(); -jfieldID GetOverlayControlDataLandscapePositionField(); -jfieldID GetOverlayControlDataPortraitPositionField(); -jfieldID GetOverlayControlDataFoldablePositionField(); - -jclass GetPatchClass(); -jmethodID GetPatchConstructor(); -jfieldID GetPatchEnabledField(); -jfieldID GetPatchNameField(); -jfieldID GetPatchVersionField(); -jfieldID GetPatchTypeField(); -jfieldID GetPatchProgramIdField(); -jfieldID GetPatchTitleIdField(); - -jclass GetDoubleClass(); -jmethodID GetDoubleConstructor(); -jfieldID GetDoubleValueField(); - -jclass GetIntegerClass(); -jmethodID GetIntegerConstructor(); -jfieldID GetIntegerValueField(); - -jclass GetBooleanClass(); -jmethodID GetBooleanConstructor(); -jfieldID GetBooleanValueField(); - -} // namespace IDCache diff --git a/src/android/app/src/main/jni/native.cpp b/src/android/app/src/main/jni/native.cpp index 654510129..4acc60956 100644 --- a/src/android/app/src/main/jni/native.cpp +++ b/src/android/app/src/main/jni/native.cpp @@ -20,6 +20,8 @@ #include #include +#include "common/android/android_common.h" +#include "common/android/id_cache.h" #include "common/detached_tasks.h" #include "common/dynamic_library.h" #include "common/fs/path_util.h" @@ -57,8 +59,6 @@ #include "hid_core/frontend/emulated_controller.h" #include "hid_core/hid_core.h" #include "hid_core/hid_types.h" -#include "jni/android_common/android_common.h" -#include "jni/id_cache.h" #include "jni/native.h" #include "video_core/renderer_base.h" #include "video_core/renderer_vulkan/renderer_vulkan.h" @@ -228,7 +228,7 @@ Core::SystemResultStatus EmulationSession::InitializeEmulation(const std::string std::make_unique(&m_input_subsystem, m_native_window, m_vulkan_library); // Initialize system. - jauto android_keyboard = std::make_unique(); + jauto android_keyboard = std::make_unique(); m_software_keyboard = android_keyboard.get(); m_system.SetShuttingDown(false); m_system.ApplySettings(); @@ -411,37 +411,39 @@ void EmulationSession::OnGamepadDisconnectEvent([[maybe_unused]] int index) { controller->Disconnect(); } -SoftwareKeyboard::AndroidKeyboard* EmulationSession::SoftwareKeyboard() { +Common::Android::SoftwareKeyboard::AndroidKeyboard* EmulationSession::SoftwareKeyboard() { return m_software_keyboard; } void EmulationSession::LoadDiskCacheProgress(VideoCore::LoadCallbackStage stage, int progress, int max) { - JNIEnv* env = IDCache::GetEnvForThread(); - env->CallStaticVoidMethod(IDCache::GetDiskCacheProgressClass(), - IDCache::GetDiskCacheLoadProgress(), static_cast(stage), + JNIEnv* env = Common::Android::GetEnvForThread(); + env->CallStaticVoidMethod(Common::Android::GetDiskCacheProgressClass(), + Common::Android::GetDiskCacheLoadProgress(), static_cast(stage), static_cast(progress), static_cast(max)); } void EmulationSession::OnEmulationStarted() { - JNIEnv* env = IDCache::GetEnvForThread(); - env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), IDCache::GetOnEmulationStarted()); + JNIEnv* env = Common::Android::GetEnvForThread(); + env->CallStaticVoidMethod(Common::Android::GetNativeLibraryClass(), + Common::Android::GetOnEmulationStarted()); } void EmulationSession::OnEmulationStopped(Core::SystemResultStatus result) { - JNIEnv* env = IDCache::GetEnvForThread(); - env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), IDCache::GetOnEmulationStopped(), - static_cast(result)); + JNIEnv* env = Common::Android::GetEnvForThread(); + env->CallStaticVoidMethod(Common::Android::GetNativeLibraryClass(), + Common::Android::GetOnEmulationStopped(), static_cast(result)); } void EmulationSession::ChangeProgram(std::size_t program_index) { - JNIEnv* env = IDCache::GetEnvForThread(); - env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), IDCache::GetOnProgramChanged(), + JNIEnv* env = Common::Android::GetEnvForThread(); + env->CallStaticVoidMethod(Common::Android::GetNativeLibraryClass(), + Common::Android::GetOnProgramChanged(), static_cast(program_index)); } u64 EmulationSession::GetProgramId(JNIEnv* env, jstring jprogramId) { - auto program_id_string = GetJString(env, jprogramId); + auto program_id_string = Common::Android::GetJString(env, jprogramId); try { return std::stoull(program_id_string); } catch (...) { @@ -491,7 +493,7 @@ void Java_org_yuzu_yuzu_1emu_NativeLibrary_surfaceDestroyed(JNIEnv* env, jobject void Java_org_yuzu_yuzu_1emu_NativeLibrary_setAppDirectory(JNIEnv* env, jobject instance, [[maybe_unused]] jstring j_directory) { - Common::FS::SetAppDirectory(GetJString(env, j_directory)); + Common::FS::SetAppDirectory(Common::Android::GetJString(env, j_directory)); } int Java_org_yuzu_yuzu_1emu_NativeLibrary_installFileToNand(JNIEnv* env, jobject instance, @@ -501,21 +503,22 @@ int Java_org_yuzu_yuzu_1emu_NativeLibrary_installFileToNand(JNIEnv* env, jobject jlambdaClass, "invoke", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); const auto callback = [env, jcallback, jlambdaInvokeMethod](size_t max, size_t progress) { auto jwasCancelled = env->CallObjectMethod(jcallback, jlambdaInvokeMethod, - ToJDouble(env, max), ToJDouble(env, progress)); - return GetJBoolean(env, jwasCancelled); + Common::Android::ToJDouble(env, max), + Common::Android::ToJDouble(env, progress)); + return Common::Android::GetJBoolean(env, jwasCancelled); }; return static_cast( ContentManager::InstallNSP(EmulationSession::GetInstance().System(), *EmulationSession::GetInstance().System().GetFilesystem(), - GetJString(env, j_file), callback)); + Common::Android::GetJString(env, j_file), callback)); } jboolean Java_org_yuzu_yuzu_1emu_NativeLibrary_doesUpdateMatchProgram(JNIEnv* env, jobject jobj, jstring jprogramId, jstring jupdatePath) { u64 program_id = EmulationSession::GetProgramId(env, jprogramId); - std::string updatePath = GetJString(env, jupdatePath); + std::string updatePath = Common::Android::GetJString(env, jupdatePath); std::shared_ptr nsp = std::make_shared( EmulationSession::GetInstance().System().GetFilesystem()->OpenFile( updatePath, FileSys::OpenMode::Read)); @@ -538,8 +541,10 @@ void JNICALL Java_org_yuzu_yuzu_1emu_NativeLibrary_initializeGpuDriver(JNIEnv* e jstring custom_driver_name, jstring file_redirect_dir) { EmulationSession::GetInstance().InitializeGpuDriver( - GetJString(env, hook_lib_dir), GetJString(env, custom_driver_dir), - GetJString(env, custom_driver_name), GetJString(env, file_redirect_dir)); + Common::Android::GetJString(env, hook_lib_dir), + Common::Android::GetJString(env, custom_driver_dir), + Common::Android::GetJString(env, custom_driver_name), + Common::Android::GetJString(env, file_redirect_dir)); } [[maybe_unused]] static bool CheckKgslPresent() { @@ -566,7 +571,7 @@ jobjectArray Java_org_yuzu_yuzu_1emu_utils_GpuDriverHelper_getSystemDriverInfo( JNIEnv* env, jobject j_obj, jobject j_surf, jstring j_hook_lib_dir) { const char* file_redirect_dir_{}; int featureFlags{}; - std::string hook_lib_dir = GetJString(env, j_hook_lib_dir); + std::string hook_lib_dir = Common::Android::GetJString(env, j_hook_lib_dir); auto handle = adrenotools_open_libvulkan(RTLD_NOW, featureFlags, nullptr, hook_lib_dir.c_str(), nullptr, nullptr, file_redirect_dir_, nullptr); auto driver_library = std::make_shared(handle); @@ -587,9 +592,10 @@ jobjectArray Java_org_yuzu_yuzu_1emu_utils_GpuDriverHelper_getSystemDriverInfo( fmt::format("{}.{}.{}", VK_API_VERSION_MAJOR(driver_version), VK_API_VERSION_MINOR(driver_version), VK_API_VERSION_PATCH(driver_version)); - jobjectArray j_driver_info = - env->NewObjectArray(2, IDCache::GetStringClass(), ToJString(env, version_string)); - env->SetObjectArrayElement(j_driver_info, 1, ToJString(env, device.GetDriverName())); + jobjectArray j_driver_info = env->NewObjectArray( + 2, Common::Android::GetStringClass(), Common::Android::ToJString(env, version_string)); + env->SetObjectArrayElement(j_driver_info, 1, + Common::Android::ToJString(env, device.GetDriverName())); return j_driver_info; } @@ -742,15 +748,15 @@ jdoubleArray Java_org_yuzu_yuzu_1emu_NativeLibrary_getPerfStats(JNIEnv* env, jcl jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getCpuBackend(JNIEnv* env, jclass clazz) { if (Settings::IsNceEnabled()) { - return ToJString(env, "NCE"); + return Common::Android::ToJString(env, "NCE"); } - return ToJString(env, "JIT"); + return Common::Android::ToJString(env, "JIT"); } jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getGpuDriver(JNIEnv* env, jobject jobj) { - return ToJString(env, - EmulationSession::GetInstance().System().GPU().Renderer().GetDeviceVendor()); + return Common::Android::ToJString( + env, EmulationSession::GetInstance().System().GPU().Renderer().GetDeviceVendor()); } void Java_org_yuzu_yuzu_1emu_NativeLibrary_applySettings(JNIEnv* env, jobject jobj) { @@ -764,13 +770,14 @@ void Java_org_yuzu_yuzu_1emu_NativeLibrary_logSettings(JNIEnv* env, jobject jobj void Java_org_yuzu_yuzu_1emu_NativeLibrary_run(JNIEnv* env, jobject jobj, jstring j_path, jint j_program_index, jboolean j_frontend_initiated) { - const std::string path = GetJString(env, j_path); + const std::string path = Common::Android::GetJString(env, j_path); const Core::SystemResultStatus result{ RunEmulation(path, j_program_index, j_frontend_initiated)}; if (result != Core::SystemResultStatus::Success) { - env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), - IDCache::GetExitEmulationActivity(), static_cast(result)); + env->CallStaticVoidMethod(Common::Android::GetNativeLibraryClass(), + Common::Android::GetExitEmulationActivity(), + static_cast(result)); } } @@ -781,7 +788,7 @@ void Java_org_yuzu_yuzu_1emu_NativeLibrary_logDeviceInfo(JNIEnv* env, jclass cla void Java_org_yuzu_yuzu_1emu_NativeLibrary_submitInlineKeyboardText(JNIEnv* env, jclass clazz, jstring j_text) { - const std::u16string input = Common::UTF8ToUTF16(GetJString(env, j_text)); + const std::u16string input = Common::UTF8ToUTF16(Common::Android::GetJString(env, j_text)); EmulationSession::GetInstance().SoftwareKeyboard()->SubmitInlineKeyboardText(input); } @@ -815,16 +822,16 @@ jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getAppletLaunchPath(JNIEnv* env, j auto bis_system = EmulationSession::GetInstance().System().GetFileSystemController().GetSystemNANDContents(); if (!bis_system) { - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } auto applet_nca = bis_system->GetEntry(static_cast(jid), FileSys::ContentRecordType::Program); if (!applet_nca) { - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } - return ToJString(env, applet_nca->GetFullPath()); + return Common::Android::ToJString(env, applet_nca->GetFullPath()); } void Java_org_yuzu_yuzu_1emu_NativeLibrary_setCurrentAppletId(JNIEnv* env, jclass clazz, @@ -857,7 +864,7 @@ jboolean Java_org_yuzu_yuzu_1emu_NativeLibrary_isFirmwareAvailable(JNIEnv* env, jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_getPatchesForFile(JNIEnv* env, jobject jobj, jstring jpath, jstring jprogramId) { - const auto path = GetJString(env, jpath); + const auto path = Common::Android::GetJString(env, jpath); const auto vFile = Core::GetGameFileFromPath(EmulationSession::GetInstance().System().GetFilesystem(), path); if (vFile == nullptr) { @@ -875,14 +882,15 @@ jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_getPatchesForFile(JNIEnv* env auto patches = pm.GetPatches(update_raw); jobjectArray jpatchArray = - env->NewObjectArray(patches.size(), IDCache::GetPatchClass(), nullptr); + env->NewObjectArray(patches.size(), Common::Android::GetPatchClass(), nullptr); int i = 0; for (const auto& patch : patches) { jobject jpatch = env->NewObject( - IDCache::GetPatchClass(), IDCache::GetPatchConstructor(), patch.enabled, - ToJString(env, patch.name), ToJString(env, patch.version), - static_cast(patch.type), ToJString(env, std::to_string(patch.program_id)), - ToJString(env, std::to_string(patch.title_id))); + Common::Android::GetPatchClass(), Common::Android::GetPatchConstructor(), patch.enabled, + Common::Android::ToJString(env, patch.name), + Common::Android::ToJString(env, patch.version), static_cast(patch.type), + Common::Android::ToJString(env, std::to_string(patch.program_id)), + Common::Android::ToJString(env, std::to_string(patch.title_id))); env->SetObjectArrayElement(jpatchArray, i, jpatch); ++i; } @@ -906,7 +914,7 @@ void Java_org_yuzu_yuzu_1emu_NativeLibrary_removeMod(JNIEnv* env, jobject jobj, jstring jname) { auto program_id = EmulationSession::GetProgramId(env, jprogramId); ContentManager::RemoveMod(EmulationSession::GetInstance().System().GetFileSystemController(), - program_id, GetJString(env, jname)); + program_id, Common::Android::GetJString(env, jname)); } jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_verifyInstalledContents(JNIEnv* env, @@ -917,17 +925,18 @@ jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_verifyInstalledContents(JNIEn jlambdaClass, "invoke", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); const auto callback = [env, jcallback, jlambdaInvokeMethod](size_t max, size_t progress) { auto jwasCancelled = env->CallObjectMethod(jcallback, jlambdaInvokeMethod, - ToJDouble(env, max), ToJDouble(env, progress)); - return GetJBoolean(env, jwasCancelled); + Common::Android::ToJDouble(env, max), + Common::Android::ToJDouble(env, progress)); + return Common::Android::GetJBoolean(env, jwasCancelled); }; auto& session = EmulationSession::GetInstance(); std::vector result = ContentManager::VerifyInstalledContents( session.System(), *session.GetContentProvider(), callback); - jobjectArray jresult = - env->NewObjectArray(result.size(), IDCache::GetStringClass(), ToJString(env, "")); + jobjectArray jresult = env->NewObjectArray(result.size(), Common::Android::GetStringClass(), + Common::Android::ToJString(env, "")); for (size_t i = 0; i < result.size(); ++i) { - env->SetObjectArrayElement(jresult, i, ToJString(env, result[i])); + env->SetObjectArrayElement(jresult, i, Common::Android::ToJString(env, result[i])); } return jresult; } @@ -939,19 +948,20 @@ jint Java_org_yuzu_yuzu_1emu_NativeLibrary_verifyGameContents(JNIEnv* env, jobje jlambdaClass, "invoke", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); const auto callback = [env, jcallback, jlambdaInvokeMethod](size_t max, size_t progress) { auto jwasCancelled = env->CallObjectMethod(jcallback, jlambdaInvokeMethod, - ToJDouble(env, max), ToJDouble(env, progress)); - return GetJBoolean(env, jwasCancelled); + Common::Android::ToJDouble(env, max), + Common::Android::ToJDouble(env, progress)); + return Common::Android::GetJBoolean(env, jwasCancelled); }; auto& session = EmulationSession::GetInstance(); - return static_cast( - ContentManager::VerifyGameContents(session.System(), GetJString(env, jpath), callback)); + return static_cast(ContentManager::VerifyGameContents( + session.System(), Common::Android::GetJString(env, jpath), callback)); } jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getSavePath(JNIEnv* env, jobject jobj, jstring jprogramId) { auto program_id = EmulationSession::GetProgramId(env, jprogramId); if (program_id == 0) { - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } auto& system = EmulationSession::GetInstance().System(); @@ -968,7 +978,7 @@ jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getSavePath(JNIEnv* env, jobject j const auto user_save_data_path = FileSys::SaveDataFactory::GetFullPath( {}, vfsNandDir, FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData, program_id, user_id->AsU128(), 0); - return ToJString(env, user_save_data_path); + return Common::Android::ToJString(env, user_save_data_path); } jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getDefaultProfileSaveDataRoot(JNIEnv* env, @@ -981,12 +991,13 @@ jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getDefaultProfileSaveDataRoot(JNIE const auto user_save_data_root = FileSys::SaveDataFactory::GetUserGameSaveDataRoot(user_id->AsU128(), jfuture); - return ToJString(env, user_save_data_root); + return Common::Android::ToJString(env, user_save_data_root); } void Java_org_yuzu_yuzu_1emu_NativeLibrary_addFileToFilesystemProvider(JNIEnv* env, jobject jobj, jstring jpath) { - EmulationSession::GetInstance().ConfigureFilesystemProvider(GetJString(env, jpath)); + EmulationSession::GetInstance().ConfigureFilesystemProvider( + Common::Android::GetJString(env, jpath)); } void Java_org_yuzu_yuzu_1emu_NativeLibrary_clearFilesystemProvider(JNIEnv* env, jobject jobj) { diff --git a/src/android/app/src/main/jni/native.h b/src/android/app/src/main/jni/native.h index e49d4e015..47936e305 100644 --- a/src/android/app/src/main/jni/native.h +++ b/src/android/app/src/main/jni/native.h @@ -2,13 +2,13 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include +#include "common/android/applets/software_keyboard.h" #include "common/detached_tasks.h" #include "core/core.h" #include "core/file_sys/registered_cache.h" #include "core/hle/service/acc/profile_manager.h" #include "core/perf_stats.h" #include "frontend_common/content_manager.h" -#include "jni/applets/software_keyboard.h" #include "jni/emu_window/emu_window.h" #include "video_core/rasterizer_interface.h" @@ -54,7 +54,7 @@ public: void SetDeviceType([[maybe_unused]] int index, int type); void OnGamepadConnectEvent([[maybe_unused]] int index); void OnGamepadDisconnectEvent([[maybe_unused]] int index); - SoftwareKeyboard::AndroidKeyboard* SoftwareKeyboard(); + Common::Android::SoftwareKeyboard::AndroidKeyboard* SoftwareKeyboard(); static void OnEmulationStarted(); @@ -79,7 +79,7 @@ private: Core::SystemResultStatus m_load_result{Core::SystemResultStatus::ErrorNotInitialized}; std::atomic m_is_running = false; std::atomic m_is_paused = false; - SoftwareKeyboard::AndroidKeyboard* m_software_keyboard{}; + Common::Android::SoftwareKeyboard::AndroidKeyboard* m_software_keyboard{}; std::unique_ptr m_manual_provider; int m_applet_id{1}; diff --git a/src/android/app/src/main/jni/native_config.cpp b/src/android/app/src/main/jni/native_config.cpp index c6c3343dc..8ae10fbc7 100644 --- a/src/android/app/src/main/jni/native_config.cpp +++ b/src/android/app/src/main/jni/native_config.cpp @@ -8,11 +8,11 @@ #include "android_config.h" #include "android_settings.h" +#include "common/android/android_common.h" +#include "common/android/id_cache.h" #include "common/logging/log.h" #include "common/settings.h" #include "frontend_common/config.h" -#include "jni/android_common/android_common.h" -#include "jni/id_cache.h" #include "native.h" std::unique_ptr global_config; @@ -20,7 +20,7 @@ std::unique_ptr per_game_config; template Settings::Setting* getSetting(JNIEnv* env, jstring jkey) { - auto key = GetJString(env, jkey); + auto key = Common::Android::GetJString(env, jkey); auto basic_setting = Settings::values.linkage.by_key[key]; if (basic_setting != 0) { return static_cast*>(basic_setting); @@ -55,7 +55,7 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_initializePerGameConfig(JNIEnv* jstring jprogramId, jstring jfileName) { auto program_id = EmulationSession::GetProgramId(env, jprogramId); - auto file_name = GetJString(env, jfileName); + auto file_name = Common::Android::GetJString(env, jfileName); const auto config_file_name = program_id == 0 ? file_name : fmt::format("{:016X}", program_id); per_game_config = std::make_unique(config_file_name, Config::ConfigType::PerGameConfig); @@ -186,9 +186,9 @@ jstring Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getString(JNIEnv* env, jobjec jboolean needGlobal) { auto setting = getSetting(env, jkey); if (setting == nullptr) { - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } - return ToJString(env, setting->GetValue(static_cast(needGlobal))); + return Common::Android::ToJString(env, setting->GetValue(static_cast(needGlobal))); } void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setString(JNIEnv* env, jobject obj, jstring jkey, @@ -198,7 +198,7 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setString(JNIEnv* env, jobject o return; } - setting->SetValue(GetJString(env, value)); + setting->SetValue(Common::Android::GetJString(env, value)); } jboolean Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getIsRuntimeModifiable(JNIEnv* env, jobject obj, @@ -214,13 +214,13 @@ jstring Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getPairedSettingKey(JNIEnv* e jstring jkey) { auto setting = getSetting(env, jkey); if (setting == nullptr) { - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } if (setting->PairedSetting() == nullptr) { - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } - return ToJString(env, setting->PairedSetting()->GetLabel()); + return Common::Android::ToJString(env, setting->PairedSetting()->GetLabel()); } jboolean Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getIsSwitchable(JNIEnv* env, jobject obj, @@ -262,21 +262,21 @@ jstring Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getDefaultToString(JNIEnv* en jstring jkey) { auto setting = getSetting(env, jkey); if (setting != nullptr) { - return ToJString(env, setting->DefaultToString()); + return Common::Android::ToJString(env, setting->DefaultToString()); } - return ToJString(env, ""); + return Common::Android::ToJString(env, ""); } jobjectArray Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getGameDirs(JNIEnv* env, jobject obj) { - jclass gameDirClass = IDCache::GetGameDirClass(); - jmethodID gameDirConstructor = IDCache::GetGameDirConstructor(); + jclass gameDirClass = Common::Android::GetGameDirClass(); + jmethodID gameDirConstructor = Common::Android::GetGameDirConstructor(); jobjectArray jgameDirArray = env->NewObjectArray(AndroidSettings::values.game_dirs.size(), gameDirClass, nullptr); for (size_t i = 0; i < AndroidSettings::values.game_dirs.size(); ++i) { - jobject jgameDir = - env->NewObject(gameDirClass, gameDirConstructor, - ToJString(env, AndroidSettings::values.game_dirs[i].path), - static_cast(AndroidSettings::values.game_dirs[i].deep_scan)); + jobject jgameDir = env->NewObject( + gameDirClass, gameDirConstructor, + Common::Android::ToJString(env, AndroidSettings::values.game_dirs[i].path), + static_cast(AndroidSettings::values.game_dirs[i].deep_scan)); env->SetObjectArrayElement(jgameDirArray, i, jgameDir); } return jgameDirArray; @@ -292,14 +292,14 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setGameDirs(JNIEnv* env, jobject } jobject dir = env->GetObjectArrayElement(gameDirs, 0); - jclass gameDirClass = IDCache::GetGameDirClass(); + jclass gameDirClass = Common::Android::GetGameDirClass(); jfieldID uriStringField = env->GetFieldID(gameDirClass, "uriString", "Ljava/lang/String;"); jfieldID deepScanBooleanField = env->GetFieldID(gameDirClass, "deepScan", "Z"); for (int i = 0; i < size; ++i) { dir = env->GetObjectArrayElement(gameDirs, i); jstring juriString = static_cast(env->GetObjectField(dir, uriStringField)); jboolean jdeepScanBoolean = env->GetBooleanField(dir, deepScanBooleanField); - std::string uriString = GetJString(env, juriString); + std::string uriString = Common::Android::GetJString(env, juriString); AndroidSettings::values.game_dirs.push_back( AndroidSettings::GameDir{uriString, static_cast(jdeepScanBoolean)}); } @@ -307,13 +307,13 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setGameDirs(JNIEnv* env, jobject void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_addGameDir(JNIEnv* env, jobject obj, jobject gameDir) { - jclass gameDirClass = IDCache::GetGameDirClass(); + jclass gameDirClass = Common::Android::GetGameDirClass(); jfieldID uriStringField = env->GetFieldID(gameDirClass, "uriString", "Ljava/lang/String;"); jfieldID deepScanBooleanField = env->GetFieldID(gameDirClass, "deepScan", "Z"); jstring juriString = static_cast(env->GetObjectField(gameDir, uriStringField)); jboolean jdeepScanBoolean = env->GetBooleanField(gameDir, deepScanBooleanField); - std::string uriString = GetJString(env, juriString); + std::string uriString = Common::Android::GetJString(env, juriString); AndroidSettings::values.game_dirs.push_back( AndroidSettings::GameDir{uriString, static_cast(jdeepScanBoolean)}); } @@ -323,9 +323,11 @@ jobjectArray Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getDisabledAddons(JNIEnv auto program_id = EmulationSession::GetProgramId(env, jprogramId); auto& disabledAddons = Settings::values.disabled_addons[program_id]; jobjectArray jdisabledAddonsArray = - env->NewObjectArray(disabledAddons.size(), IDCache::GetStringClass(), ToJString(env, "")); + env->NewObjectArray(disabledAddons.size(), Common::Android::GetStringClass(), + Common::Android::ToJString(env, "")); for (size_t i = 0; i < disabledAddons.size(); ++i) { - env->SetObjectArrayElement(jdisabledAddonsArray, i, ToJString(env, disabledAddons[i])); + env->SetObjectArrayElement(jdisabledAddonsArray, i, + Common::Android::ToJString(env, disabledAddons[i])); } return jdisabledAddonsArray; } @@ -339,7 +341,7 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setDisabledAddons(JNIEnv* env, j const int size = env->GetArrayLength(jdisabledAddons); for (int i = 0; i < size; ++i) { auto jaddon = static_cast(env->GetObjectArrayElement(jdisabledAddons, i)); - disabled_addons.push_back(GetJString(env, jaddon)); + disabled_addons.push_back(Common::Android::GetJString(env, jaddon)); } Settings::values.disabled_addons[program_id] = disabled_addons; } @@ -348,26 +350,27 @@ jobjectArray Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getOverlayControlData(JN jobject obj) { jobjectArray joverlayControlDataArray = env->NewObjectArray(AndroidSettings::values.overlay_control_data.size(), - IDCache::GetOverlayControlDataClass(), nullptr); + Common::Android::GetOverlayControlDataClass(), nullptr); for (size_t i = 0; i < AndroidSettings::values.overlay_control_data.size(); ++i) { const auto& control_data = AndroidSettings::values.overlay_control_data[i]; jobject jlandscapePosition = - env->NewObject(IDCache::GetPairClass(), IDCache::GetPairConstructor(), - ToJDouble(env, control_data.landscape_position.first), - ToJDouble(env, control_data.landscape_position.second)); + env->NewObject(Common::Android::GetPairClass(), Common::Android::GetPairConstructor(), + Common::Android::ToJDouble(env, control_data.landscape_position.first), + Common::Android::ToJDouble(env, control_data.landscape_position.second)); jobject jportraitPosition = - env->NewObject(IDCache::GetPairClass(), IDCache::GetPairConstructor(), - ToJDouble(env, control_data.portrait_position.first), - ToJDouble(env, control_data.portrait_position.second)); + env->NewObject(Common::Android::GetPairClass(), Common::Android::GetPairConstructor(), + Common::Android::ToJDouble(env, control_data.portrait_position.first), + Common::Android::ToJDouble(env, control_data.portrait_position.second)); jobject jfoldablePosition = - env->NewObject(IDCache::GetPairClass(), IDCache::GetPairConstructor(), - ToJDouble(env, control_data.foldable_position.first), - ToJDouble(env, control_data.foldable_position.second)); - - jobject jcontrolData = env->NewObject( - IDCache::GetOverlayControlDataClass(), IDCache::GetOverlayControlDataConstructor(), - ToJString(env, control_data.id), control_data.enabled, jlandscapePosition, - jportraitPosition, jfoldablePosition); + env->NewObject(Common::Android::GetPairClass(), Common::Android::GetPairConstructor(), + Common::Android::ToJDouble(env, control_data.foldable_position.first), + Common::Android::ToJDouble(env, control_data.foldable_position.second)); + + jobject jcontrolData = + env->NewObject(Common::Android::GetOverlayControlDataClass(), + Common::Android::GetOverlayControlDataConstructor(), + Common::Android::ToJString(env, control_data.id), control_data.enabled, + jlandscapePosition, jportraitPosition, jfoldablePosition); env->SetObjectArrayElement(joverlayControlDataArray, i, jcontrolData); } return joverlayControlDataArray; @@ -384,33 +387,41 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setOverlayControlData( for (int i = 0; i < size; ++i) { jobject joverlayControlData = env->GetObjectArrayElement(joverlayControlDataArray, i); - jstring jidString = static_cast( - env->GetObjectField(joverlayControlData, IDCache::GetOverlayControlDataIdField())); + jstring jidString = static_cast(env->GetObjectField( + joverlayControlData, Common::Android::GetOverlayControlDataIdField())); bool enabled = static_cast(env->GetBooleanField( - joverlayControlData, IDCache::GetOverlayControlDataEnabledField())); + joverlayControlData, Common::Android::GetOverlayControlDataEnabledField())); jobject jlandscapePosition = env->GetObjectField( - joverlayControlData, IDCache::GetOverlayControlDataLandscapePositionField()); + joverlayControlData, Common::Android::GetOverlayControlDataLandscapePositionField()); std::pair landscape_position = std::make_pair( - GetJDouble(env, env->GetObjectField(jlandscapePosition, IDCache::GetPairFirstField())), - GetJDouble(env, - env->GetObjectField(jlandscapePosition, IDCache::GetPairSecondField()))); + Common::Android::GetJDouble( + env, env->GetObjectField(jlandscapePosition, Common::Android::GetPairFirstField())), + Common::Android::GetJDouble( + env, + env->GetObjectField(jlandscapePosition, Common::Android::GetPairSecondField()))); jobject jportraitPosition = env->GetObjectField( - joverlayControlData, IDCache::GetOverlayControlDataPortraitPositionField()); + joverlayControlData, Common::Android::GetOverlayControlDataPortraitPositionField()); std::pair portrait_position = std::make_pair( - GetJDouble(env, env->GetObjectField(jportraitPosition, IDCache::GetPairFirstField())), - GetJDouble(env, env->GetObjectField(jportraitPosition, IDCache::GetPairSecondField()))); + Common::Android::GetJDouble( + env, env->GetObjectField(jportraitPosition, Common::Android::GetPairFirstField())), + Common::Android::GetJDouble( + env, + env->GetObjectField(jportraitPosition, Common::Android::GetPairSecondField()))); jobject jfoldablePosition = env->GetObjectField( - joverlayControlData, IDCache::GetOverlayControlDataFoldablePositionField()); + joverlayControlData, Common::Android::GetOverlayControlDataFoldablePositionField()); std::pair foldable_position = std::make_pair( - GetJDouble(env, env->GetObjectField(jfoldablePosition, IDCache::GetPairFirstField())), - GetJDouble(env, env->GetObjectField(jfoldablePosition, IDCache::GetPairSecondField()))); + Common::Android::GetJDouble( + env, env->GetObjectField(jfoldablePosition, Common::Android::GetPairFirstField())), + Common::Android::GetJDouble( + env, + env->GetObjectField(jfoldablePosition, Common::Android::GetPairSecondField()))); AndroidSettings::values.overlay_control_data.push_back(AndroidSettings::OverlayControlData{ - GetJString(env, jidString), enabled, landscape_position, portrait_position, - foldable_position}); + Common::Android::GetJString(env, jidString), enabled, landscape_position, + portrait_position, foldable_position}); } } diff --git a/src/android/app/src/main/jni/native_log.cpp b/src/android/app/src/main/jni/native_log.cpp index 33d691dc8..95dd1f057 100644 --- a/src/android/app/src/main/jni/native_log.cpp +++ b/src/android/app/src/main/jni/native_log.cpp @@ -1,31 +1,30 @@ // SPDX-FileCopyrightText: 2023 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include #include #include -#include "android_common/android_common.h" - extern "C" { void Java_org_yuzu_yuzu_1emu_utils_Log_debug(JNIEnv* env, jobject obj, jstring jmessage) { - LOG_DEBUG(Frontend, "{}", GetJString(env, jmessage)); + LOG_DEBUG(Frontend, "{}", Common::Android::GetJString(env, jmessage)); } void Java_org_yuzu_yuzu_1emu_utils_Log_warning(JNIEnv* env, jobject obj, jstring jmessage) { - LOG_WARNING(Frontend, "{}", GetJString(env, jmessage)); + LOG_WARNING(Frontend, "{}", Common::Android::GetJString(env, jmessage)); } void Java_org_yuzu_yuzu_1emu_utils_Log_info(JNIEnv* env, jobject obj, jstring jmessage) { - LOG_INFO(Frontend, "{}", GetJString(env, jmessage)); + LOG_INFO(Frontend, "{}", Common::Android::GetJString(env, jmessage)); } void Java_org_yuzu_yuzu_1emu_utils_Log_error(JNIEnv* env, jobject obj, jstring jmessage) { - LOG_ERROR(Frontend, "{}", GetJString(env, jmessage)); + LOG_ERROR(Frontend, "{}", Common::Android::GetJString(env, jmessage)); } void Java_org_yuzu_yuzu_1emu_utils_Log_critical(JNIEnv* env, jobject obj, jstring jmessage) { - LOG_CRITICAL(Frontend, "{}", GetJString(env, jmessage)); + LOG_CRITICAL(Frontend, "{}", Common::Android::GetJString(env, jmessage)); } } // extern "C" diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 85926fc8f..8ff1326f2 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -179,9 +179,15 @@ endif() if(ANDROID) target_sources(common - PRIVATE + PUBLIC fs/fs_android.cpp fs/fs_android.h + android/android_common.cpp + android/android_common.h + android/id_cache.cpp + android/id_cache.h + android/applets/software_keyboard.cpp + android/applets/software_keyboard.h ) endif() diff --git a/src/common/android/android_common.cpp b/src/common/android/android_common.cpp new file mode 100644 index 000000000..e79005658 --- /dev/null +++ b/src/common/android/android_common.cpp @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "android_common.h" + +#include +#include + +#include + +#include "common/android/id_cache.h" +#include "common/string_util.h" + +namespace Common::Android { + +std::string GetJString(JNIEnv* env, jstring jstr) { + if (!jstr) { + return {}; + } + + const jchar* jchars = env->GetStringChars(jstr, nullptr); + const jsize length = env->GetStringLength(jstr); + const std::u16string_view string_view(reinterpret_cast(jchars), + static_cast(length)); + const std::string converted_string = Common::UTF16ToUTF8(string_view); + env->ReleaseStringChars(jstr, jchars); + + return converted_string; +} + +jstring ToJString(JNIEnv* env, std::string_view str) { + const std::u16string converted_string = Common::UTF8ToUTF16(str); + return env->NewString(reinterpret_cast(converted_string.data()), + static_cast(converted_string.size())); +} + +jstring ToJString(JNIEnv* env, std::u16string_view str) { + return ToJString(env, Common::UTF16ToUTF8(str)); +} + +double GetJDouble(JNIEnv* env, jobject jdouble) { + return env->GetDoubleField(jdouble, GetDoubleValueField()); +} + +jobject ToJDouble(JNIEnv* env, double value) { + return env->NewObject(GetDoubleClass(), GetDoubleConstructor(), value); +} + +s32 GetJInteger(JNIEnv* env, jobject jinteger) { + return env->GetIntField(jinteger, GetIntegerValueField()); +} + +jobject ToJInteger(JNIEnv* env, s32 value) { + return env->NewObject(GetIntegerClass(), GetIntegerConstructor(), value); +} + +bool GetJBoolean(JNIEnv* env, jobject jboolean) { + return env->GetBooleanField(jboolean, GetBooleanValueField()); +} + +jobject ToJBoolean(JNIEnv* env, bool value) { + return env->NewObject(GetBooleanClass(), GetBooleanConstructor(), value); +} + +} // namespace Common::Android diff --git a/src/common/android/android_common.h b/src/common/android/android_common.h new file mode 100644 index 000000000..d0ccb4ec2 --- /dev/null +++ b/src/common/android/android_common.h @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +#include +#include "common/common_types.h" + +namespace Common::Android { + +std::string GetJString(JNIEnv* env, jstring jstr); +jstring ToJString(JNIEnv* env, std::string_view str); +jstring ToJString(JNIEnv* env, std::u16string_view str); + +double GetJDouble(JNIEnv* env, jobject jdouble); +jobject ToJDouble(JNIEnv* env, double value); + +s32 GetJInteger(JNIEnv* env, jobject jinteger); +jobject ToJInteger(JNIEnv* env, s32 value); + +bool GetJBoolean(JNIEnv* env, jobject jboolean); +jobject ToJBoolean(JNIEnv* env, bool value); + +} // namespace Common::Android diff --git a/src/common/android/applets/software_keyboard.cpp b/src/common/android/applets/software_keyboard.cpp new file mode 100644 index 000000000..477e62b16 --- /dev/null +++ b/src/common/android/applets/software_keyboard.cpp @@ -0,0 +1,277 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include + +#include "common/android/android_common.h" +#include "common/android/applets/software_keyboard.h" +#include "common/android/id_cache.h" +#include "common/logging/log.h" +#include "common/string_util.h" +#include "core/core.h" + +static jclass s_software_keyboard_class; +static jclass s_keyboard_config_class; +static jclass s_keyboard_data_class; +static jmethodID s_swkbd_execute_normal; +static jmethodID s_swkbd_execute_inline; + +namespace Common::Android::SoftwareKeyboard { + +static jobject ToJKeyboardParams(const Core::Frontend::KeyboardInitializeParameters& config) { + JNIEnv* env = GetEnvForThread(); + jobject object = env->AllocObject(s_keyboard_config_class); + + env->SetObjectField(object, + env->GetFieldID(s_keyboard_config_class, "ok_text", "Ljava/lang/String;"), + ToJString(env, config.ok_text)); + env->SetObjectField( + object, env->GetFieldID(s_keyboard_config_class, "header_text", "Ljava/lang/String;"), + ToJString(env, config.header_text)); + env->SetObjectField(object, + env->GetFieldID(s_keyboard_config_class, "sub_text", "Ljava/lang/String;"), + ToJString(env, config.sub_text)); + env->SetObjectField( + object, env->GetFieldID(s_keyboard_config_class, "guide_text", "Ljava/lang/String;"), + ToJString(env, config.guide_text)); + env->SetObjectField( + object, env->GetFieldID(s_keyboard_config_class, "initial_text", "Ljava/lang/String;"), + ToJString(env, config.initial_text)); + env->SetShortField(object, + env->GetFieldID(s_keyboard_config_class, "left_optional_symbol_key", "S"), + static_cast(config.left_optional_symbol_key)); + env->SetShortField(object, + env->GetFieldID(s_keyboard_config_class, "right_optional_symbol_key", "S"), + static_cast(config.right_optional_symbol_key)); + env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "max_text_length", "I"), + static_cast(config.max_text_length)); + env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "min_text_length", "I"), + static_cast(config.min_text_length)); + env->SetIntField(object, + env->GetFieldID(s_keyboard_config_class, "initial_cursor_position", "I"), + static_cast(config.initial_cursor_position)); + env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "type", "I"), + static_cast(config.type)); + env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "password_mode", "I"), + static_cast(config.password_mode)); + env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "text_draw_type", "I"), + static_cast(config.text_draw_type)); + env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "key_disable_flags", "I"), + static_cast(config.key_disable_flags.raw)); + env->SetBooleanField(object, + env->GetFieldID(s_keyboard_config_class, "use_blur_background", "Z"), + static_cast(config.use_blur_background)); + env->SetBooleanField(object, + env->GetFieldID(s_keyboard_config_class, "enable_backspace_button", "Z"), + static_cast(config.enable_backspace_button)); + env->SetBooleanField(object, + env->GetFieldID(s_keyboard_config_class, "enable_return_button", "Z"), + static_cast(config.enable_return_button)); + env->SetBooleanField(object, + env->GetFieldID(s_keyboard_config_class, "disable_cancel_button", "Z"), + static_cast(config.disable_cancel_button)); + + return object; +} + +AndroidKeyboard::ResultData AndroidKeyboard::ResultData::CreateFromFrontend(jobject object) { + JNIEnv* env = GetEnvForThread(); + const jstring string = reinterpret_cast(env->GetObjectField( + object, env->GetFieldID(s_keyboard_data_class, "text", "Ljava/lang/String;"))); + return ResultData{GetJString(env, string), + static_cast(env->GetIntField( + object, env->GetFieldID(s_keyboard_data_class, "result", "I")))}; +} + +AndroidKeyboard::~AndroidKeyboard() = default; + +void AndroidKeyboard::InitializeKeyboard( + bool is_inline, Core::Frontend::KeyboardInitializeParameters initialize_parameters, + SubmitNormalCallback submit_normal_callback_, SubmitInlineCallback submit_inline_callback_) { + if (is_inline) { + LOG_WARNING( + Frontend, + "(STUBBED) called, backend requested to initialize the inline software keyboard."); + + submit_inline_callback = std::move(submit_inline_callback_); + } else { + LOG_WARNING( + Frontend, + "(STUBBED) called, backend requested to initialize the normal software keyboard."); + + submit_normal_callback = std::move(submit_normal_callback_); + } + + parameters = std::move(initialize_parameters); + + LOG_INFO(Frontend, + "\nKeyboardInitializeParameters:" + "\nok_text={}" + "\nheader_text={}" + "\nsub_text={}" + "\nguide_text={}" + "\ninitial_text={}" + "\nmax_text_length={}" + "\nmin_text_length={}" + "\ninitial_cursor_position={}" + "\ntype={}" + "\npassword_mode={}" + "\ntext_draw_type={}" + "\nkey_disable_flags={}" + "\nuse_blur_background={}" + "\nenable_backspace_button={}" + "\nenable_return_button={}" + "\ndisable_cancel_button={}", + Common::UTF16ToUTF8(parameters.ok_text), Common::UTF16ToUTF8(parameters.header_text), + Common::UTF16ToUTF8(parameters.sub_text), Common::UTF16ToUTF8(parameters.guide_text), + Common::UTF16ToUTF8(parameters.initial_text), parameters.max_text_length, + parameters.min_text_length, parameters.initial_cursor_position, parameters.type, + parameters.password_mode, parameters.text_draw_type, parameters.key_disable_flags.raw, + parameters.use_blur_background, parameters.enable_backspace_button, + parameters.enable_return_button, parameters.disable_cancel_button); +} + +void AndroidKeyboard::ShowNormalKeyboard() const { + LOG_DEBUG(Frontend, "called, backend requested to show the normal software keyboard."); + + ResultData data{}; + + // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. + std::thread([&] { + data = ResultData::CreateFromFrontend(GetEnvForThread()->CallStaticObjectMethod( + s_software_keyboard_class, s_swkbd_execute_normal, ToJKeyboardParams(parameters))); + }).join(); + + SubmitNormalText(data); +} + +void AndroidKeyboard::ShowTextCheckDialog( + Service::AM::Frontend::SwkbdTextCheckResult text_check_result, + std::u16string text_check_message) const { + LOG_WARNING(Frontend, "(STUBBED) called, backend requested to show the text check dialog."); +} + +void AndroidKeyboard::ShowInlineKeyboard( + Core::Frontend::InlineAppearParameters appear_parameters) const { + LOG_WARNING(Frontend, + "(STUBBED) called, backend requested to show the inline software keyboard."); + + LOG_INFO(Frontend, + "\nInlineAppearParameters:" + "\nmax_text_length={}" + "\nmin_text_length={}" + "\nkey_top_scale_x={}" + "\nkey_top_scale_y={}" + "\nkey_top_translate_x={}" + "\nkey_top_translate_y={}" + "\ntype={}" + "\nkey_disable_flags={}" + "\nkey_top_as_floating={}" + "\nenable_backspace_button={}" + "\nenable_return_button={}" + "\ndisable_cancel_button={}", + appear_parameters.max_text_length, appear_parameters.min_text_length, + appear_parameters.key_top_scale_x, appear_parameters.key_top_scale_y, + appear_parameters.key_top_translate_x, appear_parameters.key_top_translate_y, + appear_parameters.type, appear_parameters.key_disable_flags.raw, + appear_parameters.key_top_as_floating, appear_parameters.enable_backspace_button, + appear_parameters.enable_return_button, appear_parameters.disable_cancel_button); + + // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. + m_is_inline_active = true; + std::thread([&] { + GetEnvForThread()->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_execute_inline, + ToJKeyboardParams(parameters)); + }).join(); +} + +void AndroidKeyboard::HideInlineKeyboard() const { + LOG_WARNING(Frontend, + "(STUBBED) called, backend requested to hide the inline software keyboard."); +} + +void AndroidKeyboard::InlineTextChanged( + Core::Frontend::InlineTextParameters text_parameters) const { + LOG_WARNING(Frontend, + "(STUBBED) called, backend requested to change the inline keyboard text."); + + LOG_INFO(Frontend, + "\nInlineTextParameters:" + "\ninput_text={}" + "\ncursor_position={}", + Common::UTF16ToUTF8(text_parameters.input_text), text_parameters.cursor_position); + + submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, + text_parameters.input_text, text_parameters.cursor_position); +} + +void AndroidKeyboard::ExitKeyboard() const { + LOG_WARNING(Frontend, "(STUBBED) called, backend requested to exit the software keyboard."); +} + +void AndroidKeyboard::SubmitInlineKeyboardText(std::u16string submitted_text) { + if (!m_is_inline_active) { + return; + } + + m_current_text += submitted_text; + + submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, + static_cast(m_current_text.size())); +} + +void AndroidKeyboard::SubmitInlineKeyboardInput(int key_code) { + static constexpr int KEYCODE_BACK = 4; + static constexpr int KEYCODE_ENTER = 66; + static constexpr int KEYCODE_DEL = 67; + + if (!m_is_inline_active) { + return; + } + + switch (key_code) { + case KEYCODE_BACK: + case KEYCODE_ENTER: + m_is_inline_active = false; + submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::DecidedEnter, m_current_text, + static_cast(m_current_text.size())); + break; + case KEYCODE_DEL: + m_current_text.pop_back(); + submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, + static_cast(m_current_text.size())); + break; + } +} + +void AndroidKeyboard::SubmitNormalText(const ResultData& data) const { + submit_normal_callback(data.result, Common::UTF8ToUTF16(data.text), true); +} + +void InitJNI(JNIEnv* env) { + s_software_keyboard_class = reinterpret_cast( + env->NewGlobalRef(env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard"))); + s_keyboard_config_class = reinterpret_cast(env->NewGlobalRef( + env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig"))); + s_keyboard_data_class = reinterpret_cast(env->NewGlobalRef( + env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardData"))); + + s_swkbd_execute_normal = env->GetStaticMethodID( + s_software_keyboard_class, "executeNormal", + "(Lorg/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig;)Lorg/yuzu/yuzu_emu/" + "applets/keyboard/SoftwareKeyboard$KeyboardData;"); + s_swkbd_execute_inline = env->GetStaticMethodID( + s_software_keyboard_class, "executeInline", + "(Lorg/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig;)V"); +} + +void CleanupJNI(JNIEnv* env) { + env->DeleteGlobalRef(s_software_keyboard_class); + env->DeleteGlobalRef(s_keyboard_config_class); + env->DeleteGlobalRef(s_keyboard_data_class); +} + +} // namespace Common::Android::SoftwareKeyboard diff --git a/src/common/android/applets/software_keyboard.h b/src/common/android/applets/software_keyboard.h new file mode 100644 index 000000000..9fd09d27c --- /dev/null +++ b/src/common/android/applets/software_keyboard.h @@ -0,0 +1,78 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +#include "core/frontend/applets/software_keyboard.h" + +namespace Common::Android::SoftwareKeyboard { + +class AndroidKeyboard final : public Core::Frontend::SoftwareKeyboardApplet { +public: + ~AndroidKeyboard() override; + + void Close() const override { + ExitKeyboard(); + } + + void InitializeKeyboard(bool is_inline, + Core::Frontend::KeyboardInitializeParameters initialize_parameters, + SubmitNormalCallback submit_normal_callback_, + SubmitInlineCallback submit_inline_callback_) override; + + void ShowNormalKeyboard() const override; + + void ShowTextCheckDialog(Service::AM::Frontend::SwkbdTextCheckResult text_check_result, + std::u16string text_check_message) const override; + + void ShowInlineKeyboard( + Core::Frontend::InlineAppearParameters appear_parameters) const override; + + void HideInlineKeyboard() const override; + + void InlineTextChanged(Core::Frontend::InlineTextParameters text_parameters) const override; + + void ExitKeyboard() const override; + + void SubmitInlineKeyboardText(std::u16string submitted_text); + + void SubmitInlineKeyboardInput(int key_code); + +private: + struct ResultData { + static ResultData CreateFromFrontend(jobject object); + + std::string text; + Service::AM::Frontend::SwkbdResult result{}; + }; + + void SubmitNormalText(const ResultData& result) const; + + Core::Frontend::KeyboardInitializeParameters parameters{}; + + mutable SubmitNormalCallback submit_normal_callback; + mutable SubmitInlineCallback submit_inline_callback; + +private: + mutable bool m_is_inline_active{}; + std::u16string m_current_text; +}; + +// Should be called in JNI_Load +void InitJNI(JNIEnv* env); + +// Should be called in JNI_Unload +void CleanupJNI(JNIEnv* env); + +} // namespace Common::Android::SoftwareKeyboard + +// Native function calls +extern "C" { +JNIEXPORT jobject JNICALL Java_org_citra_citra_1emu_applets_SoftwareKeyboard_ValidateFilters( + JNIEnv* env, jclass clazz, jstring text); + +JNIEXPORT jobject JNICALL Java_org_citra_citra_1emu_applets_SoftwareKeyboard_ValidateInput( + JNIEnv* env, jclass clazz, jstring text); +} diff --git a/src/common/android/id_cache.cpp b/src/common/android/id_cache.cpp new file mode 100644 index 000000000..f39262db9 --- /dev/null +++ b/src/common/android/id_cache.cpp @@ -0,0 +1,428 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +#include "applets/software_keyboard.h" +#include "common/android/id_cache.h" +#include "common/assert.h" +#include "common/fs/fs_android.h" +#include "video_core/rasterizer_interface.h" + +static JavaVM* s_java_vm; +static jclass s_native_library_class; +static jclass s_disk_cache_progress_class; +static jclass s_load_callback_stage_class; +static jclass s_game_dir_class; +static jmethodID s_game_dir_constructor; +static jmethodID s_exit_emulation_activity; +static jmethodID s_disk_cache_load_progress; +static jmethodID s_on_emulation_started; +static jmethodID s_on_emulation_stopped; +static jmethodID s_on_program_changed; + +static jclass s_game_class; +static jmethodID s_game_constructor; +static jfieldID s_game_title_field; +static jfieldID s_game_path_field; +static jfieldID s_game_program_id_field; +static jfieldID s_game_developer_field; +static jfieldID s_game_version_field; +static jfieldID s_game_is_homebrew_field; + +static jclass s_string_class; +static jclass s_pair_class; +static jmethodID s_pair_constructor; +static jfieldID s_pair_first_field; +static jfieldID s_pair_second_field; + +static jclass s_overlay_control_data_class; +static jmethodID s_overlay_control_data_constructor; +static jfieldID s_overlay_control_data_id_field; +static jfieldID s_overlay_control_data_enabled_field; +static jfieldID s_overlay_control_data_landscape_position_field; +static jfieldID s_overlay_control_data_portrait_position_field; +static jfieldID s_overlay_control_data_foldable_position_field; + +static jclass s_patch_class; +static jmethodID s_patch_constructor; +static jfieldID s_patch_enabled_field; +static jfieldID s_patch_name_field; +static jfieldID s_patch_version_field; +static jfieldID s_patch_type_field; +static jfieldID s_patch_program_id_field; +static jfieldID s_patch_title_id_field; + +static jclass s_double_class; +static jmethodID s_double_constructor; +static jfieldID s_double_value_field; + +static jclass s_integer_class; +static jmethodID s_integer_constructor; +static jfieldID s_integer_value_field; + +static jclass s_boolean_class; +static jmethodID s_boolean_constructor; +static jfieldID s_boolean_value_field; + +static constexpr jint JNI_VERSION = JNI_VERSION_1_6; + +namespace Common::Android { + +JNIEnv* GetEnvForThread() { + thread_local static struct OwnedEnv { + OwnedEnv() { + status = s_java_vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6); + if (status == JNI_EDETACHED) + s_java_vm->AttachCurrentThread(&env, nullptr); + } + + ~OwnedEnv() { + if (status == JNI_EDETACHED) + s_java_vm->DetachCurrentThread(); + } + + int status; + JNIEnv* env = nullptr; + } owned; + return owned.env; +} + +jclass GetNativeLibraryClass() { + return s_native_library_class; +} + +jclass GetDiskCacheProgressClass() { + return s_disk_cache_progress_class; +} + +jclass GetDiskCacheLoadCallbackStageClass() { + return s_load_callback_stage_class; +} + +jclass GetGameDirClass() { + return s_game_dir_class; +} + +jmethodID GetGameDirConstructor() { + return s_game_dir_constructor; +} + +jmethodID GetExitEmulationActivity() { + return s_exit_emulation_activity; +} + +jmethodID GetDiskCacheLoadProgress() { + return s_disk_cache_load_progress; +} + +jmethodID GetOnEmulationStarted() { + return s_on_emulation_started; +} + +jmethodID GetOnEmulationStopped() { + return s_on_emulation_stopped; +} + +jmethodID GetOnProgramChanged() { + return s_on_program_changed; +} + +jclass GetGameClass() { + return s_game_class; +} + +jmethodID GetGameConstructor() { + return s_game_constructor; +} + +jfieldID GetGameTitleField() { + return s_game_title_field; +} + +jfieldID GetGamePathField() { + return s_game_path_field; +} + +jfieldID GetGameProgramIdField() { + return s_game_program_id_field; +} + +jfieldID GetGameDeveloperField() { + return s_game_developer_field; +} + +jfieldID GetGameVersionField() { + return s_game_version_field; +} + +jfieldID GetGameIsHomebrewField() { + return s_game_is_homebrew_field; +} + +jclass GetStringClass() { + return s_string_class; +} + +jclass GetPairClass() { + return s_pair_class; +} + +jmethodID GetPairConstructor() { + return s_pair_constructor; +} + +jfieldID GetPairFirstField() { + return s_pair_first_field; +} + +jfieldID GetPairSecondField() { + return s_pair_second_field; +} + +jclass GetOverlayControlDataClass() { + return s_overlay_control_data_class; +} + +jmethodID GetOverlayControlDataConstructor() { + return s_overlay_control_data_constructor; +} + +jfieldID GetOverlayControlDataIdField() { + return s_overlay_control_data_id_field; +} + +jfieldID GetOverlayControlDataEnabledField() { + return s_overlay_control_data_enabled_field; +} + +jfieldID GetOverlayControlDataLandscapePositionField() { + return s_overlay_control_data_landscape_position_field; +} + +jfieldID GetOverlayControlDataPortraitPositionField() { + return s_overlay_control_data_portrait_position_field; +} + +jfieldID GetOverlayControlDataFoldablePositionField() { + return s_overlay_control_data_foldable_position_field; +} + +jclass GetPatchClass() { + return s_patch_class; +} + +jmethodID GetPatchConstructor() { + return s_patch_constructor; +} + +jfieldID GetPatchEnabledField() { + return s_patch_enabled_field; +} + +jfieldID GetPatchNameField() { + return s_patch_name_field; +} + +jfieldID GetPatchVersionField() { + return s_patch_version_field; +} + +jfieldID GetPatchTypeField() { + return s_patch_type_field; +} + +jfieldID GetPatchProgramIdField() { + return s_patch_program_id_field; +} + +jfieldID GetPatchTitleIdField() { + return s_patch_title_id_field; +} + +jclass GetDoubleClass() { + return s_double_class; +} + +jmethodID GetDoubleConstructor() { + return s_double_constructor; +} + +jfieldID GetDoubleValueField() { + return s_double_value_field; +} + +jclass GetIntegerClass() { + return s_integer_class; +} + +jmethodID GetIntegerConstructor() { + return s_integer_constructor; +} + +jfieldID GetIntegerValueField() { + return s_integer_value_field; +} + +jclass GetBooleanClass() { + return s_boolean_class; +} + +jmethodID GetBooleanConstructor() { + return s_boolean_constructor; +} + +jfieldID GetBooleanValueField() { + return s_boolean_value_field; +} + +#ifdef __cplusplus +extern "C" { +#endif + +jint JNI_OnLoad(JavaVM* vm, void* reserved) { + s_java_vm = vm; + + JNIEnv* env; + if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION) != JNI_OK) + return JNI_ERR; + + // Initialize Java classes + const jclass native_library_class = env->FindClass("org/yuzu/yuzu_emu/NativeLibrary"); + s_native_library_class = reinterpret_cast(env->NewGlobalRef(native_library_class)); + s_disk_cache_progress_class = reinterpret_cast(env->NewGlobalRef( + env->FindClass("org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress"))); + s_load_callback_stage_class = reinterpret_cast(env->NewGlobalRef(env->FindClass( + "org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress$LoadCallbackStage"))); + + const jclass game_dir_class = env->FindClass("org/yuzu/yuzu_emu/model/GameDir"); + s_game_dir_class = reinterpret_cast(env->NewGlobalRef(game_dir_class)); + s_game_dir_constructor = env->GetMethodID(game_dir_class, "", "(Ljava/lang/String;Z)V"); + env->DeleteLocalRef(game_dir_class); + + // Initialize methods + s_exit_emulation_activity = + env->GetStaticMethodID(s_native_library_class, "exitEmulationActivity", "(I)V"); + s_disk_cache_load_progress = + env->GetStaticMethodID(s_disk_cache_progress_class, "loadProgress", "(III)V"); + s_on_emulation_started = + env->GetStaticMethodID(s_native_library_class, "onEmulationStarted", "()V"); + s_on_emulation_stopped = + env->GetStaticMethodID(s_native_library_class, "onEmulationStopped", "(I)V"); + s_on_program_changed = + env->GetStaticMethodID(s_native_library_class, "onProgramChanged", "(I)V"); + + const jclass game_class = env->FindClass("org/yuzu/yuzu_emu/model/Game"); + s_game_class = reinterpret_cast(env->NewGlobalRef(game_class)); + s_game_constructor = env->GetMethodID(game_class, "", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/" + "String;Ljava/lang/String;Ljava/lang/String;Z)V"); + s_game_title_field = env->GetFieldID(game_class, "title", "Ljava/lang/String;"); + s_game_path_field = env->GetFieldID(game_class, "path", "Ljava/lang/String;"); + s_game_program_id_field = env->GetFieldID(game_class, "programId", "Ljava/lang/String;"); + s_game_developer_field = env->GetFieldID(game_class, "developer", "Ljava/lang/String;"); + s_game_version_field = env->GetFieldID(game_class, "version", "Ljava/lang/String;"); + s_game_is_homebrew_field = env->GetFieldID(game_class, "isHomebrew", "Z"); + env->DeleteLocalRef(game_class); + + const jclass string_class = env->FindClass("java/lang/String"); + s_string_class = reinterpret_cast(env->NewGlobalRef(string_class)); + env->DeleteLocalRef(string_class); + + const jclass pair_class = env->FindClass("kotlin/Pair"); + s_pair_class = reinterpret_cast(env->NewGlobalRef(pair_class)); + s_pair_constructor = + env->GetMethodID(pair_class, "", "(Ljava/lang/Object;Ljava/lang/Object;)V"); + s_pair_first_field = env->GetFieldID(pair_class, "first", "Ljava/lang/Object;"); + s_pair_second_field = env->GetFieldID(pair_class, "second", "Ljava/lang/Object;"); + env->DeleteLocalRef(pair_class); + + const jclass overlay_control_data_class = + env->FindClass("org/yuzu/yuzu_emu/overlay/model/OverlayControlData"); + s_overlay_control_data_class = + reinterpret_cast(env->NewGlobalRef(overlay_control_data_class)); + s_overlay_control_data_constructor = + env->GetMethodID(overlay_control_data_class, "", + "(Ljava/lang/String;ZLkotlin/Pair;Lkotlin/Pair;Lkotlin/Pair;)V"); + s_overlay_control_data_id_field = + env->GetFieldID(overlay_control_data_class, "id", "Ljava/lang/String;"); + s_overlay_control_data_enabled_field = + env->GetFieldID(overlay_control_data_class, "enabled", "Z"); + s_overlay_control_data_landscape_position_field = + env->GetFieldID(overlay_control_data_class, "landscapePosition", "Lkotlin/Pair;"); + s_overlay_control_data_portrait_position_field = + env->GetFieldID(overlay_control_data_class, "portraitPosition", "Lkotlin/Pair;"); + s_overlay_control_data_foldable_position_field = + env->GetFieldID(overlay_control_data_class, "foldablePosition", "Lkotlin/Pair;"); + env->DeleteLocalRef(overlay_control_data_class); + + const jclass patch_class = env->FindClass("org/yuzu/yuzu_emu/model/Patch"); + s_patch_class = reinterpret_cast(env->NewGlobalRef(patch_class)); + s_patch_constructor = env->GetMethodID( + patch_class, "", + "(ZLjava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V"); + s_patch_enabled_field = env->GetFieldID(patch_class, "enabled", "Z"); + s_patch_name_field = env->GetFieldID(patch_class, "name", "Ljava/lang/String;"); + s_patch_version_field = env->GetFieldID(patch_class, "version", "Ljava/lang/String;"); + s_patch_type_field = env->GetFieldID(patch_class, "type", "I"); + s_patch_program_id_field = env->GetFieldID(patch_class, "programId", "Ljava/lang/String;"); + s_patch_title_id_field = env->GetFieldID(patch_class, "titleId", "Ljava/lang/String;"); + env->DeleteLocalRef(patch_class); + + const jclass double_class = env->FindClass("java/lang/Double"); + s_double_class = reinterpret_cast(env->NewGlobalRef(double_class)); + s_double_constructor = env->GetMethodID(double_class, "", "(D)V"); + s_double_value_field = env->GetFieldID(double_class, "value", "D"); + env->DeleteLocalRef(double_class); + + const jclass int_class = env->FindClass("java/lang/Integer"); + s_integer_class = reinterpret_cast(env->NewGlobalRef(int_class)); + s_integer_constructor = env->GetMethodID(int_class, "", "(I)V"); + s_integer_value_field = env->GetFieldID(int_class, "value", "I"); + env->DeleteLocalRef(int_class); + + const jclass boolean_class = env->FindClass("java/lang/Boolean"); + s_boolean_class = reinterpret_cast(env->NewGlobalRef(boolean_class)); + s_boolean_constructor = env->GetMethodID(boolean_class, "", "(Z)V"); + s_boolean_value_field = env->GetFieldID(boolean_class, "value", "Z"); + env->DeleteLocalRef(boolean_class); + + // Initialize Android Storage + Common::FS::Android::RegisterCallbacks(env, s_native_library_class); + + // Initialize applets + Common::Android::SoftwareKeyboard::InitJNI(env); + + return JNI_VERSION; +} + +void JNI_OnUnload(JavaVM* vm, void* reserved) { + JNIEnv* env; + if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION) != JNI_OK) { + return; + } + + // UnInitialize Android Storage + Common::FS::Android::UnRegisterCallbacks(); + env->DeleteGlobalRef(s_native_library_class); + env->DeleteGlobalRef(s_disk_cache_progress_class); + env->DeleteGlobalRef(s_load_callback_stage_class); + env->DeleteGlobalRef(s_game_dir_class); + env->DeleteGlobalRef(s_game_class); + env->DeleteGlobalRef(s_string_class); + env->DeleteGlobalRef(s_pair_class); + env->DeleteGlobalRef(s_overlay_control_data_class); + env->DeleteGlobalRef(s_patch_class); + env->DeleteGlobalRef(s_double_class); + env->DeleteGlobalRef(s_integer_class); + env->DeleteGlobalRef(s_boolean_class); + + // UnInitialize applets + SoftwareKeyboard::CleanupJNI(env); +} + +#ifdef __cplusplus +} +#endif + +} // namespace Common::Android diff --git a/src/common/android/id_cache.h b/src/common/android/id_cache.h new file mode 100644 index 000000000..47802f96c --- /dev/null +++ b/src/common/android/id_cache.h @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include + +#include "video_core/rasterizer_interface.h" + +namespace Common::Android { + +JNIEnv* GetEnvForThread(); + +/** + * Starts a new thread to run JNI. Intended to be used when you must run JNI from a fiber. + * @tparam T Typename of the return value for the work param + * @param work Lambda that runs JNI code. This function will take care of attaching this thread to + * the JVM + * @return The result from the work lambda param + */ +template +T RunJNIOnFiber(const std::function& work) { + std::future j_result = std::async(std::launch::async, [&] { + auto env = GetEnvForThread(); + return work(env); + }); + return j_result.get(); +} + +jclass GetNativeLibraryClass(); + +jclass GetDiskCacheProgressClass(); +jclass GetDiskCacheLoadCallbackStageClass(); +jclass GetGameDirClass(); +jmethodID GetGameDirConstructor(); +jmethodID GetDiskCacheLoadProgress(); + +jmethodID GetExitEmulationActivity(); +jmethodID GetOnEmulationStarted(); +jmethodID GetOnEmulationStopped(); +jmethodID GetOnProgramChanged(); + +jclass GetGameClass(); +jmethodID GetGameConstructor(); +jfieldID GetGameTitleField(); +jfieldID GetGamePathField(); +jfieldID GetGameProgramIdField(); +jfieldID GetGameDeveloperField(); +jfieldID GetGameVersionField(); +jfieldID GetGameIsHomebrewField(); + +jclass GetStringClass(); +jclass GetPairClass(); +jmethodID GetPairConstructor(); +jfieldID GetPairFirstField(); +jfieldID GetPairSecondField(); + +jclass GetOverlayControlDataClass(); +jmethodID GetOverlayControlDataConstructor(); +jfieldID GetOverlayControlDataIdField(); +jfieldID GetOverlayControlDataEnabledField(); +jfieldID GetOverlayControlDataLandscapePositionField(); +jfieldID GetOverlayControlDataPortraitPositionField(); +jfieldID GetOverlayControlDataFoldablePositionField(); + +jclass GetPatchClass(); +jmethodID GetPatchConstructor(); +jfieldID GetPatchEnabledField(); +jfieldID GetPatchNameField(); +jfieldID GetPatchVersionField(); +jfieldID GetPatchTypeField(); +jfieldID GetPatchProgramIdField(); +jfieldID GetPatchTitleIdField(); + +jclass GetDoubleClass(); +jmethodID GetDoubleConstructor(); +jfieldID GetDoubleValueField(); + +jclass GetIntegerClass(); +jmethodID GetIntegerConstructor(); +jfieldID GetIntegerValueField(); + +jclass GetBooleanClass(); +jmethodID GetBooleanConstructor(); +jfieldID GetBooleanValueField(); + +} // namespace Common::Android -- cgit v1.2.3