mirror of
https://github.com/azahar-emu/azahar
synced 2025-11-06 23:19:57 +01:00
Fix android termination bug (#1354)
* move hook additions to onCreateView * Updated license header * Formatting nitpick * Added prefix to log messages --------- Co-authored-by: OpenSauce04 <opensauce04@gmail.com>
This commit is contained in:
parent
b63d7841dd
commit
70f9379eef
@ -85,7 +85,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
private val preferences: SharedPreferences
|
private val preferences: SharedPreferences
|
||||||
get() = PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext)
|
get() = PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext)
|
||||||
|
|
||||||
private lateinit var emulationState: EmulationState
|
private var emulationState: EmulationState? = null
|
||||||
private var perfStatsUpdater: Runnable? = null
|
private var perfStatsUpdater: Runnable? = null
|
||||||
|
|
||||||
private lateinit var emulationActivity: EmulationActivity
|
private lateinit var emulationActivity: EmulationActivity
|
||||||
@ -106,6 +106,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
if (context is EmulationActivity) {
|
if (context is EmulationActivity) {
|
||||||
emulationActivity = context
|
emulationActivity = context
|
||||||
NativeLibrary.setEmulationActivity(context)
|
NativeLibrary.setEmulationActivity(context)
|
||||||
|
EmulationLifecycleUtil.addPauseResumeHook(hook = { togglePause() })
|
||||||
|
if (emulationState != null) {
|
||||||
|
EmulationLifecycleUtil.addShutdownHook(hook = { emulationState!!.stop() })
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw IllegalStateException("EmulationFragment must have EmulationActivity parent")
|
throw IllegalStateException("EmulationFragment must have EmulationActivity parent")
|
||||||
}
|
}
|
||||||
@ -156,8 +160,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
emulationState = EmulationState(game.path)
|
emulationState = EmulationState(game.path)
|
||||||
emulationActivity = requireActivity() as EmulationActivity
|
emulationActivity = requireActivity() as EmulationActivity
|
||||||
screenAdjustmentUtil = ScreenAdjustmentUtil(requireContext(), requireActivity().windowManager, settingsViewModel.settings)
|
screenAdjustmentUtil = ScreenAdjustmentUtil(requireContext(), requireActivity().windowManager, settingsViewModel.settings)
|
||||||
EmulationLifecycleUtil.addShutdownHook(hook = { emulationState.stop() })
|
EmulationLifecycleUtil.addShutdownHook(hook = { emulationState!!.stop() })
|
||||||
EmulationLifecycleUtil.addPauseResumeHook(hook = { togglePause() })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@ -256,8 +259,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
binding.inGameMenu.setNavigationItemSelectedListener {
|
binding.inGameMenu.setNavigationItemSelectedListener {
|
||||||
when (it.itemId) {
|
when (it.itemId) {
|
||||||
R.id.menu_emulation_pause -> {
|
R.id.menu_emulation_pause -> {
|
||||||
if (emulationState.isPaused) {
|
if (emulationState!!.isPaused) {
|
||||||
emulationState.unpause()
|
emulationState!!.unpause()
|
||||||
it.title = resources.getString(R.string.pause_emulation)
|
it.title = resources.getString(R.string.pause_emulation)
|
||||||
it.icon = ResourcesCompat.getDrawable(
|
it.icon = ResourcesCompat.getDrawable(
|
||||||
resources,
|
resources,
|
||||||
@ -265,7 +268,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
requireContext().theme
|
requireContext().theme
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
emulationState.pause()
|
emulationState!!.pause()
|
||||||
it.title = resources.getString(R.string.resume_emulation)
|
it.title = resources.getString(R.string.resume_emulation)
|
||||||
it.icon = ResourcesCompat.getDrawable(
|
it.icon = ResourcesCompat.getDrawable(
|
||||||
resources,
|
resources,
|
||||||
@ -355,7 +358,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
}
|
}
|
||||||
|
|
||||||
R.id.menu_exit -> {
|
R.id.menu_exit -> {
|
||||||
emulationState.pause()
|
emulationState!!.pause()
|
||||||
MaterialAlertDialogBuilder(requireContext())
|
MaterialAlertDialogBuilder(requireContext())
|
||||||
.setTitle(R.string.emulation_close_game)
|
.setTitle(R.string.emulation_close_game)
|
||||||
.setMessage(R.string.emulation_close_game_message)
|
.setMessage(R.string.emulation_close_game_message)
|
||||||
@ -363,9 +366,9 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
EmulationLifecycleUtil.closeGame()
|
EmulationLifecycleUtil.closeGame()
|
||||||
}
|
}
|
||||||
.setNegativeButton(android.R.string.cancel) { _: DialogInterface?, _: Int ->
|
.setNegativeButton(android.R.string.cancel) { _: DialogInterface?, _: Int ->
|
||||||
emulationState.unpause()
|
emulationState!!.unpause()
|
||||||
}
|
}
|
||||||
.setOnCancelListener { emulationState.unpause() }
|
.setOnCancelListener { emulationState!!.unpause() }
|
||||||
.show()
|
.show()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@ -459,10 +462,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun togglePause() {
|
private fun togglePause() {
|
||||||
if (emulationState.isPaused) {
|
if (emulationState!!.isPaused) {
|
||||||
emulationState.unpause()
|
emulationState!!.unpause()
|
||||||
} else {
|
} else {
|
||||||
emulationState.pause()
|
emulationState!!.pause()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,7 +473,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
super.onResume()
|
super.onResume()
|
||||||
Choreographer.getInstance().postFrameCallback(this)
|
Choreographer.getInstance().postFrameCallback(this)
|
||||||
if (NativeLibrary.isRunning()) {
|
if (NativeLibrary.isRunning()) {
|
||||||
emulationState.pause()
|
emulationState!!.pause()
|
||||||
|
|
||||||
// If the overlay is enabled, we need to update the position if changed
|
// If the overlay is enabled, we need to update the position if changed
|
||||||
val position = IntSetting.PERFORMANCE_OVERLAY_POSITION.int
|
val position = IntSetting.PERFORMANCE_OVERLAY_POSITION.int
|
||||||
@ -488,7 +491,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (DirectoryInitialization.areCitraDirectoriesReady()) {
|
if (DirectoryInitialization.areCitraDirectoriesReady()) {
|
||||||
emulationState.run(emulationActivity.isActivityRecreated)
|
emulationState!!.run(emulationActivity.isActivityRecreated)
|
||||||
} else {
|
} else {
|
||||||
setupCitraDirectoriesThenStartEmulation()
|
setupCitraDirectoriesThenStartEmulation()
|
||||||
}
|
}
|
||||||
@ -496,7 +499,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
if (NativeLibrary.isRunning()) {
|
if (NativeLibrary.isRunning()) {
|
||||||
emulationState.pause()
|
emulationState!!.pause()
|
||||||
}
|
}
|
||||||
Choreographer.getInstance().removeFrameCallback(this)
|
Choreographer.getInstance().removeFrameCallback(this)
|
||||||
super.onPause()
|
super.onPause()
|
||||||
@ -512,7 +515,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
if (directoryInitializationState ===
|
if (directoryInitializationState ===
|
||||||
DirectoryInitializationState.CITRA_DIRECTORIES_INITIALIZED
|
DirectoryInitializationState.CITRA_DIRECTORIES_INITIALIZED
|
||||||
) {
|
) {
|
||||||
emulationState.run(emulationActivity.isActivityRecreated)
|
emulationState!!.run(emulationActivity.isActivityRecreated)
|
||||||
} else if (directoryInitializationState ===
|
} else if (directoryInitializationState ===
|
||||||
DirectoryInitializationState.EXTERNAL_STORAGE_PERMISSION_NEEDED
|
DirectoryInitializationState.EXTERNAL_STORAGE_PERMISSION_NEEDED
|
||||||
) {
|
) {
|
||||||
@ -1348,11 +1351,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
|
|||||||
|
|
||||||
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
|
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
|
||||||
Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height)
|
Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height)
|
||||||
emulationState.newSurface(holder.surface)
|
emulationState!!.newSurface(holder.surface)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun surfaceDestroyed(holder: SurfaceHolder) {
|
override fun surfaceDestroyed(holder: SurfaceHolder) {
|
||||||
emulationState.clearSurface()
|
emulationState!!.clearSurface()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun doFrame(frameTimeNanos: Long) {
|
override fun doFrame(frameTimeNanos: Long) {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2023 Citra Emulator Project
|
// Copyright Citra Emulator Project / Azahar Emulator Project
|
||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
@ -18,11 +18,19 @@ object EmulationLifecycleUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun addShutdownHook(hook: Runnable) {
|
fun addShutdownHook(hook: Runnable) {
|
||||||
shutdownHooks.add(hook)
|
if (shutdownHooks.contains(hook)) {
|
||||||
|
Log.warning("[EmulationLifecycleUtil] Tried to add shutdown hook that already existed. Skipping.")
|
||||||
|
} else {
|
||||||
|
shutdownHooks.add(hook)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addPauseResumeHook(hook: Runnable) {
|
fun addPauseResumeHook(hook: Runnable) {
|
||||||
pauseResumeHooks.add(hook)
|
if (pauseResumeHooks.contains(hook)) {
|
||||||
|
Log.warning("[EmulationLifecycleUtil] Tried to add pause resume hook that already existed. Skipping.")
|
||||||
|
} else {
|
||||||
|
pauseResumeHooks.add(hook)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clear() {
|
fun clear() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user