diff options
Diffstat (limited to 'src/common/android/id_cache.cpp')
| -rw-r--r-- | src/common/android/id_cache.cpp | 428 |
1 files changed, 428 insertions, 0 deletions
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 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <jni.h> | ||
| 5 | |||
| 6 | #include "applets/software_keyboard.h" | ||
| 7 | #include "common/android/id_cache.h" | ||
| 8 | #include "common/assert.h" | ||
| 9 | #include "common/fs/fs_android.h" | ||
| 10 | #include "video_core/rasterizer_interface.h" | ||
| 11 | |||
| 12 | static JavaVM* s_java_vm; | ||
| 13 | static jclass s_native_library_class; | ||
| 14 | static jclass s_disk_cache_progress_class; | ||
| 15 | static jclass s_load_callback_stage_class; | ||
| 16 | static jclass s_game_dir_class; | ||
| 17 | static jmethodID s_game_dir_constructor; | ||
| 18 | static jmethodID s_exit_emulation_activity; | ||
| 19 | static jmethodID s_disk_cache_load_progress; | ||
| 20 | static jmethodID s_on_emulation_started; | ||
| 21 | static jmethodID s_on_emulation_stopped; | ||
| 22 | static jmethodID s_on_program_changed; | ||
| 23 | |||
| 24 | static jclass s_game_class; | ||
| 25 | static jmethodID s_game_constructor; | ||
| 26 | static jfieldID s_game_title_field; | ||
| 27 | static jfieldID s_game_path_field; | ||
| 28 | static jfieldID s_game_program_id_field; | ||
| 29 | static jfieldID s_game_developer_field; | ||
| 30 | static jfieldID s_game_version_field; | ||
| 31 | static jfieldID s_game_is_homebrew_field; | ||
| 32 | |||
| 33 | static jclass s_string_class; | ||
| 34 | static jclass s_pair_class; | ||
| 35 | static jmethodID s_pair_constructor; | ||
| 36 | static jfieldID s_pair_first_field; | ||
| 37 | static jfieldID s_pair_second_field; | ||
| 38 | |||
| 39 | static jclass s_overlay_control_data_class; | ||
| 40 | static jmethodID s_overlay_control_data_constructor; | ||
| 41 | static jfieldID s_overlay_control_data_id_field; | ||
| 42 | static jfieldID s_overlay_control_data_enabled_field; | ||
| 43 | static jfieldID s_overlay_control_data_landscape_position_field; | ||
| 44 | static jfieldID s_overlay_control_data_portrait_position_field; | ||
| 45 | static jfieldID s_overlay_control_data_foldable_position_field; | ||
| 46 | |||
| 47 | static jclass s_patch_class; | ||
| 48 | static jmethodID s_patch_constructor; | ||
| 49 | static jfieldID s_patch_enabled_field; | ||
| 50 | static jfieldID s_patch_name_field; | ||
| 51 | static jfieldID s_patch_version_field; | ||
| 52 | static jfieldID s_patch_type_field; | ||
| 53 | static jfieldID s_patch_program_id_field; | ||
| 54 | static jfieldID s_patch_title_id_field; | ||
| 55 | |||
| 56 | static jclass s_double_class; | ||
| 57 | static jmethodID s_double_constructor; | ||
| 58 | static jfieldID s_double_value_field; | ||
| 59 | |||
| 60 | static jclass s_integer_class; | ||
| 61 | static jmethodID s_integer_constructor; | ||
| 62 | static jfieldID s_integer_value_field; | ||
| 63 | |||
| 64 | static jclass s_boolean_class; | ||
| 65 | static jmethodID s_boolean_constructor; | ||
| 66 | static jfieldID s_boolean_value_field; | ||
| 67 | |||
| 68 | static constexpr jint JNI_VERSION = JNI_VERSION_1_6; | ||
| 69 | |||
| 70 | namespace Common::Android { | ||
| 71 | |||
| 72 | JNIEnv* GetEnvForThread() { | ||
| 73 | thread_local static struct OwnedEnv { | ||
| 74 | OwnedEnv() { | ||
| 75 | status = s_java_vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6); | ||
| 76 | if (status == JNI_EDETACHED) | ||
| 77 | s_java_vm->AttachCurrentThread(&env, nullptr); | ||
| 78 | } | ||
| 79 | |||
| 80 | ~OwnedEnv() { | ||
| 81 | if (status == JNI_EDETACHED) | ||
| 82 | s_java_vm->DetachCurrentThread(); | ||
| 83 | } | ||
| 84 | |||
| 85 | int status; | ||
| 86 | JNIEnv* env = nullptr; | ||
| 87 | } owned; | ||
| 88 | return owned.env; | ||
| 89 | } | ||
| 90 | |||
| 91 | jclass GetNativeLibraryClass() { | ||
| 92 | return s_native_library_class; | ||
| 93 | } | ||
| 94 | |||
| 95 | jclass GetDiskCacheProgressClass() { | ||
| 96 | return s_disk_cache_progress_class; | ||
| 97 | } | ||
| 98 | |||
| 99 | jclass GetDiskCacheLoadCallbackStageClass() { | ||
| 100 | return s_load_callback_stage_class; | ||
| 101 | } | ||
| 102 | |||
| 103 | jclass GetGameDirClass() { | ||
| 104 | return s_game_dir_class; | ||
| 105 | } | ||
| 106 | |||
| 107 | jmethodID GetGameDirConstructor() { | ||
| 108 | return s_game_dir_constructor; | ||
| 109 | } | ||
| 110 | |||
| 111 | jmethodID GetExitEmulationActivity() { | ||
| 112 | return s_exit_emulation_activity; | ||
| 113 | } | ||
| 114 | |||
| 115 | jmethodID GetDiskCacheLoadProgress() { | ||
| 116 | return s_disk_cache_load_progress; | ||
| 117 | } | ||
| 118 | |||
| 119 | jmethodID GetOnEmulationStarted() { | ||
| 120 | return s_on_emulation_started; | ||
| 121 | } | ||
| 122 | |||
| 123 | jmethodID GetOnEmulationStopped() { | ||
| 124 | return s_on_emulation_stopped; | ||
| 125 | } | ||
| 126 | |||
| 127 | jmethodID GetOnProgramChanged() { | ||
| 128 | return s_on_program_changed; | ||
| 129 | } | ||
| 130 | |||
| 131 | jclass GetGameClass() { | ||
| 132 | return s_game_class; | ||
| 133 | } | ||
| 134 | |||
| 135 | jmethodID GetGameConstructor() { | ||
| 136 | return s_game_constructor; | ||
| 137 | } | ||
| 138 | |||
| 139 | jfieldID GetGameTitleField() { | ||
| 140 | return s_game_title_field; | ||
| 141 | } | ||
| 142 | |||
| 143 | jfieldID GetGamePathField() { | ||
| 144 | return s_game_path_field; | ||
| 145 | } | ||
| 146 | |||
| 147 | jfieldID GetGameProgramIdField() { | ||
| 148 | return s_game_program_id_field; | ||
| 149 | } | ||
| 150 | |||
| 151 | jfieldID GetGameDeveloperField() { | ||
| 152 | return s_game_developer_field; | ||
| 153 | } | ||
| 154 | |||
| 155 | jfieldID GetGameVersionField() { | ||
| 156 | return s_game_version_field; | ||
| 157 | } | ||
| 158 | |||
| 159 | jfieldID GetGameIsHomebrewField() { | ||
| 160 | return s_game_is_homebrew_field; | ||
| 161 | } | ||
| 162 | |||
| 163 | jclass GetStringClass() { | ||
| 164 | return s_string_class; | ||
| 165 | } | ||
| 166 | |||
| 167 | jclass GetPairClass() { | ||
| 168 | return s_pair_class; | ||
| 169 | } | ||
| 170 | |||
| 171 | jmethodID GetPairConstructor() { | ||
| 172 | return s_pair_constructor; | ||
| 173 | } | ||
| 174 | |||
| 175 | jfieldID GetPairFirstField() { | ||
| 176 | return s_pair_first_field; | ||
| 177 | } | ||
| 178 | |||
| 179 | jfieldID GetPairSecondField() { | ||
| 180 | return s_pair_second_field; | ||
| 181 | } | ||
| 182 | |||
| 183 | jclass GetOverlayControlDataClass() { | ||
| 184 | return s_overlay_control_data_class; | ||
| 185 | } | ||
| 186 | |||
| 187 | jmethodID GetOverlayControlDataConstructor() { | ||
| 188 | return s_overlay_control_data_constructor; | ||
| 189 | } | ||
| 190 | |||
| 191 | jfieldID GetOverlayControlDataIdField() { | ||
| 192 | return s_overlay_control_data_id_field; | ||
| 193 | } | ||
| 194 | |||
| 195 | jfieldID GetOverlayControlDataEnabledField() { | ||
| 196 | return s_overlay_control_data_enabled_field; | ||
| 197 | } | ||
| 198 | |||
| 199 | jfieldID GetOverlayControlDataLandscapePositionField() { | ||
| 200 | return s_overlay_control_data_landscape_position_field; | ||
| 201 | } | ||
| 202 | |||
| 203 | jfieldID GetOverlayControlDataPortraitPositionField() { | ||
| 204 | return s_overlay_control_data_portrait_position_field; | ||
| 205 | } | ||
| 206 | |||
| 207 | jfieldID GetOverlayControlDataFoldablePositionField() { | ||
| 208 | return s_overlay_control_data_foldable_position_field; | ||
| 209 | } | ||
| 210 | |||
| 211 | jclass GetPatchClass() { | ||
| 212 | return s_patch_class; | ||
| 213 | } | ||
| 214 | |||
| 215 | jmethodID GetPatchConstructor() { | ||
| 216 | return s_patch_constructor; | ||
| 217 | } | ||
| 218 | |||
| 219 | jfieldID GetPatchEnabledField() { | ||
| 220 | return s_patch_enabled_field; | ||
| 221 | } | ||
| 222 | |||
| 223 | jfieldID GetPatchNameField() { | ||
| 224 | return s_patch_name_field; | ||
| 225 | } | ||
| 226 | |||
| 227 | jfieldID GetPatchVersionField() { | ||
| 228 | return s_patch_version_field; | ||
| 229 | } | ||
| 230 | |||
| 231 | jfieldID GetPatchTypeField() { | ||
| 232 | return s_patch_type_field; | ||
| 233 | } | ||
| 234 | |||
| 235 | jfieldID GetPatchProgramIdField() { | ||
| 236 | return s_patch_program_id_field; | ||
| 237 | } | ||
| 238 | |||
| 239 | jfieldID GetPatchTitleIdField() { | ||
| 240 | return s_patch_title_id_field; | ||
| 241 | } | ||
| 242 | |||
| 243 | jclass GetDoubleClass() { | ||
| 244 | return s_double_class; | ||
| 245 | } | ||
| 246 | |||
| 247 | jmethodID GetDoubleConstructor() { | ||
| 248 | return s_double_constructor; | ||
| 249 | } | ||
| 250 | |||
| 251 | jfieldID GetDoubleValueField() { | ||
| 252 | return s_double_value_field; | ||
| 253 | } | ||
| 254 | |||
| 255 | jclass GetIntegerClass() { | ||
| 256 | return s_integer_class; | ||
| 257 | } | ||
| 258 | |||
| 259 | jmethodID GetIntegerConstructor() { | ||
| 260 | return s_integer_constructor; | ||
| 261 | } | ||
| 262 | |||
| 263 | jfieldID GetIntegerValueField() { | ||
| 264 | return s_integer_value_field; | ||
| 265 | } | ||
| 266 | |||
| 267 | jclass GetBooleanClass() { | ||
| 268 | return s_boolean_class; | ||
| 269 | } | ||
| 270 | |||
| 271 | jmethodID GetBooleanConstructor() { | ||
| 272 | return s_boolean_constructor; | ||
| 273 | } | ||
| 274 | |||
| 275 | jfieldID GetBooleanValueField() { | ||
| 276 | return s_boolean_value_field; | ||
| 277 | } | ||
| 278 | |||
| 279 | #ifdef __cplusplus | ||
| 280 | extern "C" { | ||
| 281 | #endif | ||
| 282 | |||
| 283 | jint JNI_OnLoad(JavaVM* vm, void* reserved) { | ||
| 284 | s_java_vm = vm; | ||
| 285 | |||
| 286 | JNIEnv* env; | ||
| 287 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) | ||
| 288 | return JNI_ERR; | ||
| 289 | |||
| 290 | // Initialize Java classes | ||
| 291 | const jclass native_library_class = env->FindClass("org/yuzu/yuzu_emu/NativeLibrary"); | ||
| 292 | s_native_library_class = reinterpret_cast<jclass>(env->NewGlobalRef(native_library_class)); | ||
| 293 | s_disk_cache_progress_class = reinterpret_cast<jclass>(env->NewGlobalRef( | ||
| 294 | env->FindClass("org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress"))); | ||
| 295 | s_load_callback_stage_class = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass( | ||
| 296 | "org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress$LoadCallbackStage"))); | ||
| 297 | |||
| 298 | const jclass game_dir_class = env->FindClass("org/yuzu/yuzu_emu/model/GameDir"); | ||
| 299 | s_game_dir_class = reinterpret_cast<jclass>(env->NewGlobalRef(game_dir_class)); | ||
| 300 | s_game_dir_constructor = env->GetMethodID(game_dir_class, "<init>", "(Ljava/lang/String;Z)V"); | ||
| 301 | env->DeleteLocalRef(game_dir_class); | ||
| 302 | |||
| 303 | // Initialize methods | ||
| 304 | s_exit_emulation_activity = | ||
| 305 | env->GetStaticMethodID(s_native_library_class, "exitEmulationActivity", "(I)V"); | ||
| 306 | s_disk_cache_load_progress = | ||
| 307 | env->GetStaticMethodID(s_disk_cache_progress_class, "loadProgress", "(III)V"); | ||
| 308 | s_on_emulation_started = | ||
| 309 | env->GetStaticMethodID(s_native_library_class, "onEmulationStarted", "()V"); | ||
| 310 | s_on_emulation_stopped = | ||
| 311 | env->GetStaticMethodID(s_native_library_class, "onEmulationStopped", "(I)V"); | ||
| 312 | s_on_program_changed = | ||
| 313 | env->GetStaticMethodID(s_native_library_class, "onProgramChanged", "(I)V"); | ||
| 314 | |||
| 315 | const jclass game_class = env->FindClass("org/yuzu/yuzu_emu/model/Game"); | ||
| 316 | s_game_class = reinterpret_cast<jclass>(env->NewGlobalRef(game_class)); | ||
| 317 | s_game_constructor = env->GetMethodID(game_class, "<init>", | ||
| 318 | "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/" | ||
| 319 | "String;Ljava/lang/String;Ljava/lang/String;Z)V"); | ||
| 320 | s_game_title_field = env->GetFieldID(game_class, "title", "Ljava/lang/String;"); | ||
| 321 | s_game_path_field = env->GetFieldID(game_class, "path", "Ljava/lang/String;"); | ||
| 322 | s_game_program_id_field = env->GetFieldID(game_class, "programId", "Ljava/lang/String;"); | ||
| 323 | s_game_developer_field = env->GetFieldID(game_class, "developer", "Ljava/lang/String;"); | ||
| 324 | s_game_version_field = env->GetFieldID(game_class, "version", "Ljava/lang/String;"); | ||
| 325 | s_game_is_homebrew_field = env->GetFieldID(game_class, "isHomebrew", "Z"); | ||
| 326 | env->DeleteLocalRef(game_class); | ||
| 327 | |||
| 328 | const jclass string_class = env->FindClass("java/lang/String"); | ||
| 329 | s_string_class = reinterpret_cast<jclass>(env->NewGlobalRef(string_class)); | ||
| 330 | env->DeleteLocalRef(string_class); | ||
| 331 | |||
| 332 | const jclass pair_class = env->FindClass("kotlin/Pair"); | ||
| 333 | s_pair_class = reinterpret_cast<jclass>(env->NewGlobalRef(pair_class)); | ||
| 334 | s_pair_constructor = | ||
| 335 | env->GetMethodID(pair_class, "<init>", "(Ljava/lang/Object;Ljava/lang/Object;)V"); | ||
| 336 | s_pair_first_field = env->GetFieldID(pair_class, "first", "Ljava/lang/Object;"); | ||
| 337 | s_pair_second_field = env->GetFieldID(pair_class, "second", "Ljava/lang/Object;"); | ||
| 338 | env->DeleteLocalRef(pair_class); | ||
| 339 | |||
| 340 | const jclass overlay_control_data_class = | ||
| 341 | env->FindClass("org/yuzu/yuzu_emu/overlay/model/OverlayControlData"); | ||
| 342 | s_overlay_control_data_class = | ||
| 343 | reinterpret_cast<jclass>(env->NewGlobalRef(overlay_control_data_class)); | ||
| 344 | s_overlay_control_data_constructor = | ||
| 345 | env->GetMethodID(overlay_control_data_class, "<init>", | ||
| 346 | "(Ljava/lang/String;ZLkotlin/Pair;Lkotlin/Pair;Lkotlin/Pair;)V"); | ||
| 347 | s_overlay_control_data_id_field = | ||
| 348 | env->GetFieldID(overlay_control_data_class, "id", "Ljava/lang/String;"); | ||
| 349 | s_overlay_control_data_enabled_field = | ||
| 350 | env->GetFieldID(overlay_control_data_class, "enabled", "Z"); | ||
| 351 | s_overlay_control_data_landscape_position_field = | ||
| 352 | env->GetFieldID(overlay_control_data_class, "landscapePosition", "Lkotlin/Pair;"); | ||
| 353 | s_overlay_control_data_portrait_position_field = | ||
| 354 | env->GetFieldID(overlay_control_data_class, "portraitPosition", "Lkotlin/Pair;"); | ||
| 355 | s_overlay_control_data_foldable_position_field = | ||
| 356 | env->GetFieldID(overlay_control_data_class, "foldablePosition", "Lkotlin/Pair;"); | ||
| 357 | env->DeleteLocalRef(overlay_control_data_class); | ||
| 358 | |||
| 359 | const jclass patch_class = env->FindClass("org/yuzu/yuzu_emu/model/Patch"); | ||
| 360 | s_patch_class = reinterpret_cast<jclass>(env->NewGlobalRef(patch_class)); | ||
| 361 | s_patch_constructor = env->GetMethodID( | ||
| 362 | patch_class, "<init>", | ||
| 363 | "(ZLjava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V"); | ||
| 364 | s_patch_enabled_field = env->GetFieldID(patch_class, "enabled", "Z"); | ||
| 365 | s_patch_name_field = env->GetFieldID(patch_class, "name", "Ljava/lang/String;"); | ||
| 366 | s_patch_version_field = env->GetFieldID(patch_class, "version", "Ljava/lang/String;"); | ||
| 367 | s_patch_type_field = env->GetFieldID(patch_class, "type", "I"); | ||
| 368 | s_patch_program_id_field = env->GetFieldID(patch_class, "programId", "Ljava/lang/String;"); | ||
| 369 | s_patch_title_id_field = env->GetFieldID(patch_class, "titleId", "Ljava/lang/String;"); | ||
| 370 | env->DeleteLocalRef(patch_class); | ||
| 371 | |||
| 372 | const jclass double_class = env->FindClass("java/lang/Double"); | ||
| 373 | s_double_class = reinterpret_cast<jclass>(env->NewGlobalRef(double_class)); | ||
| 374 | s_double_constructor = env->GetMethodID(double_class, "<init>", "(D)V"); | ||
| 375 | s_double_value_field = env->GetFieldID(double_class, "value", "D"); | ||
| 376 | env->DeleteLocalRef(double_class); | ||
| 377 | |||
| 378 | const jclass int_class = env->FindClass("java/lang/Integer"); | ||
| 379 | s_integer_class = reinterpret_cast<jclass>(env->NewGlobalRef(int_class)); | ||
| 380 | s_integer_constructor = env->GetMethodID(int_class, "<init>", "(I)V"); | ||
| 381 | s_integer_value_field = env->GetFieldID(int_class, "value", "I"); | ||
| 382 | env->DeleteLocalRef(int_class); | ||
| 383 | |||
| 384 | const jclass boolean_class = env->FindClass("java/lang/Boolean"); | ||
| 385 | s_boolean_class = reinterpret_cast<jclass>(env->NewGlobalRef(boolean_class)); | ||
| 386 | s_boolean_constructor = env->GetMethodID(boolean_class, "<init>", "(Z)V"); | ||
| 387 | s_boolean_value_field = env->GetFieldID(boolean_class, "value", "Z"); | ||
| 388 | env->DeleteLocalRef(boolean_class); | ||
| 389 | |||
| 390 | // Initialize Android Storage | ||
| 391 | Common::FS::Android::RegisterCallbacks(env, s_native_library_class); | ||
| 392 | |||
| 393 | // Initialize applets | ||
| 394 | Common::Android::SoftwareKeyboard::InitJNI(env); | ||
| 395 | |||
| 396 | return JNI_VERSION; | ||
| 397 | } | ||
| 398 | |||
| 399 | void JNI_OnUnload(JavaVM* vm, void* reserved) { | ||
| 400 | JNIEnv* env; | ||
| 401 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) { | ||
| 402 | return; | ||
| 403 | } | ||
| 404 | |||
| 405 | // UnInitialize Android Storage | ||
| 406 | Common::FS::Android::UnRegisterCallbacks(); | ||
| 407 | env->DeleteGlobalRef(s_native_library_class); | ||
| 408 | env->DeleteGlobalRef(s_disk_cache_progress_class); | ||
| 409 | env->DeleteGlobalRef(s_load_callback_stage_class); | ||
| 410 | env->DeleteGlobalRef(s_game_dir_class); | ||
| 411 | env->DeleteGlobalRef(s_game_class); | ||
| 412 | env->DeleteGlobalRef(s_string_class); | ||
| 413 | env->DeleteGlobalRef(s_pair_class); | ||
| 414 | env->DeleteGlobalRef(s_overlay_control_data_class); | ||
| 415 | env->DeleteGlobalRef(s_patch_class); | ||
| 416 | env->DeleteGlobalRef(s_double_class); | ||
| 417 | env->DeleteGlobalRef(s_integer_class); | ||
| 418 | env->DeleteGlobalRef(s_boolean_class); | ||
| 419 | |||
| 420 | // UnInitialize applets | ||
| 421 | SoftwareKeyboard::CleanupJNI(env); | ||
| 422 | } | ||
| 423 | |||
| 424 | #ifdef __cplusplus | ||
| 425 | } | ||
| 426 | #endif | ||
| 427 | |||
| 428 | } // namespace Common::Android | ||