diff --git a/core/rend/gui.cpp b/core/rend/gui.cpp index 191ad95ac..f25565dc9 100644 --- a/core/rend/gui.cpp +++ b/core/rend/gui.cpp @@ -78,7 +78,8 @@ static GameScanner scanner; static BackgroundGameLoader gameLoader; static Boxart boxart; static Chat chat; -static std::mutex guiMutex; +static std::recursive_mutex guiMutex; +using LockGuard = std::lock_guard; static void emuEventCallback(Event event, void *) { @@ -447,7 +448,7 @@ void gui_plot_render_time(int width, int height) void gui_open_settings() { - std::lock_guard lock(guiMutex); + const LockGuard lock(guiMutex); if (gui_state == GuiState::Closed) { if (!ggpo::active()) @@ -477,6 +478,7 @@ void gui_open_settings() void gui_start_game(const std::string& path) { + const LockGuard lock(guiMutex); emu.unloadGame(); reset_vmus(); chat.reset(); @@ -488,6 +490,7 @@ void gui_start_game(const std::string& path) void gui_stop_game(const std::string& message) { + const LockGuard lock(guiMutex); if (!commandLineStart) { // Exit to main menu @@ -2725,7 +2728,7 @@ static void gui_display_loadscreen() void gui_display_ui() { FC_PROFILE_SCOPE; - std::lock_guard lock(guiMutex); + const LockGuard lock(guiMutex); if (gui_state == GuiState::Closed || gui_state == GuiState::VJoyEdit) return; diff --git a/shell/android-studio/flycast/src/main/jni/src/Android.cpp b/shell/android-studio/flycast/src/main/jni/src/Android.cpp index 1ad839e18..170994738 100644 --- a/shell/android-studio/flycast/src/main/jni/src/Android.cpp +++ b/shell/android-studio/flycast/src/main/jni/src/Android.cpp @@ -286,21 +286,36 @@ extern "C" JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_setExterna gui_refresh_files(); } +static void stopEmu() +{ + if (!emu.running()) + game_started = false; + else + emu.stop(); + // in single-threaded mode, stopping is delayed + while (game_started) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); +} + extern "C" JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_setGameUri(JNIEnv *env,jobject obj,jstring fileName) { - if (fileName != NULL) - { - // Get filename string from Java - const char* file_path = env->GetStringUTFChars(fileName, 0); - NOTICE_LOG(BOOT, "Game Disk URI: '%s'", file_path); - settings.content.path = strlen(file_path) >= 7 && !memcmp(file_path, "file://", 7) ? file_path + 7 : file_path; - env->ReleaseStringUTFChars(fileName, file_path); - // TODO game paused/settings/... - if (game_started) { - emu.unloadGame(); - gui_state = GuiState::Main; - } - } + if (fileName == nullptr) + return; + + // Get filename string from Java + const char* file_path = env->GetStringUTFChars(fileName, 0); + if (file_path[0] != '\0') + { + NOTICE_LOG(BOOT, "Game Disk URI: '%s'", file_path); + if (game_started) + { + stopEmu(); + gui_stop_game(); + } + std::string path = strlen(file_path) >= 7 && !memcmp(file_path, "file://", 7) ? file_path + 7 : file_path; + gui_start_game(path); + } + env->ReleaseStringUTFChars(fileName, file_path); } //stuff for microphone @@ -324,17 +339,6 @@ extern "C" JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_setupMic(J stopRecordingMid = env->GetMethodID(env->GetObjectClass(sipemu),"stopRecording","()V"); } -static void stopEmu() -{ - if (!emu.running()) - game_started = false; - else - emu.stop(); - // in single-threaded mode, stopping is delayed - while (game_started) - std::this_thread::sleep_for(std::chrono::milliseconds(1)); -} - extern "C" JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_pause(JNIEnv *env,jobject obj) { if (emu.running())