From 5fa9bc192c012cbf31005f1d84a9ec4f8d391c96 Mon Sep 17 00:00:00 2001 From: t895 Date: Thu, 8 Feb 2024 20:36:07 -0500 Subject: [PATCH] android: Add thermal throttling overlay --- .../features/settings/model/BooleanSetting.kt | 3 +- .../yuzu_emu/fragments/EmulationFragment.kt | 55 ++++++++++++++++++- .../app/src/main/jni/android_settings.h | 2 + .../main/res/layout/fragment_emulation.xml | 15 ++++- .../main/res/menu/menu_overlay_options.xml | 5 ++ .../app/src/main/res/values/strings.xml | 1 + 6 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt index 86bd33672..664478472 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt @@ -25,7 +25,8 @@ enum class BooleanSetting(override val key: String) : AbstractBooleanSetting { HAPTIC_FEEDBACK("haptic_feedback"), SHOW_PERFORMANCE_OVERLAY("show_performance_overlay"), SHOW_INPUT_OVERLAY("show_input_overlay"), - TOUCHSCREEN("touchscreen"); + TOUCHSCREEN("touchscreen"), + SHOW_THERMAL_OVERLAY("show_thermal_overlay"); override fun getBoolean(needsGlobal: Boolean): Boolean = NativeConfig.getBoolean(key, needsGlobal) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt index 937b8faf1..44af896da 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt @@ -13,6 +13,7 @@ import android.net.Uri import android.os.Bundle import android.os.Handler import android.os.Looper +import android.os.PowerManager import android.os.SystemClock import android.view.* import android.widget.TextView @@ -23,6 +24,7 @@ import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout.DrawerListener import androidx.fragment.app.Fragment @@ -38,7 +40,6 @@ import androidx.window.layout.WindowLayoutInfo import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.slider.Slider import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import org.yuzu.yuzu_emu.HomeNavigationDirections @@ -64,6 +65,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { private lateinit var emulationState: EmulationState private var emulationActivity: EmulationActivity? = null private var perfStatsUpdater: (() -> Unit)? = null + private var thermalStatsUpdater: (() -> Unit)? = null private var _binding: FragmentEmulationBinding? = null private val binding get() = _binding!! @@ -77,6 +79,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { private var isInFoldableLayout = false + private lateinit var powerManager: PowerManager + override fun onAttach(context: Context) { super.onAttach(context) if (context is EmulationActivity) { @@ -102,6 +106,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { super.onCreate(savedInstanceState) updateOrientation() + powerManager = requireContext().getSystemService(Context.POWER_SERVICE) as PowerManager + val intentUri: Uri? = requireActivity().intent.data var intentGame: Game? = null if (intentUri != null) { @@ -394,8 +400,9 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { emulationState.updateSurface() - // Setup overlay + // Setup overlays updateShowFpsOverlay() + updateThermalOverlay() } } } @@ -553,6 +560,38 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } } + private fun updateThermalOverlay() { + if (BooleanSetting.SHOW_THERMAL_OVERLAY.getBoolean()) { + thermalStatsUpdater = { + if (emulationViewModel.emulationStarted.value && + !emulationViewModel.isEmulationStopping.value + ) { + val thermalStatus = when (powerManager.currentThermalStatus) { + PowerManager.THERMAL_STATUS_LIGHT -> "😥" + PowerManager.THERMAL_STATUS_MODERATE -> "🥵" + PowerManager.THERMAL_STATUS_SEVERE -> "🔥" + PowerManager.THERMAL_STATUS_CRITICAL, + PowerManager.THERMAL_STATUS_EMERGENCY, + PowerManager.THERMAL_STATUS_SHUTDOWN -> "☢️" + + else -> "🙂" + } + if (_binding != null) { + binding.showThermalsText.text = thermalStatus + } + thermalStatsUpdateHandler.postDelayed(thermalStatsUpdater!!, 1000) + } + } + thermalStatsUpdateHandler.post(thermalStatsUpdater!!) + binding.showThermalsText.visibility = View.VISIBLE + } else { + if (thermalStatsUpdater != null) { + thermalStatsUpdateHandler.removeCallbacks(thermalStatsUpdater!!) + } + binding.showThermalsText.visibility = View.GONE + } + } + @SuppressLint("SourceLockedOrientationActivity") private fun updateOrientation() { emulationActivity?.let { @@ -641,6 +680,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { popup.menu.apply { findItem(R.id.menu_toggle_fps).isChecked = BooleanSetting.SHOW_PERFORMANCE_OVERLAY.getBoolean() + findItem(R.id.thermal_indicator).isChecked = + BooleanSetting.SHOW_THERMAL_OVERLAY.getBoolean() findItem(R.id.menu_rel_stick_center).isChecked = BooleanSetting.JOYSTICK_REL_CENTER.getBoolean() findItem(R.id.menu_dpad_slide).isChecked = BooleanSetting.DPAD_SLIDE.getBoolean() @@ -660,6 +701,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { true } + R.id.thermal_indicator -> { + it.isChecked = !it.isChecked + BooleanSetting.SHOW_THERMAL_OVERLAY.setBoolean(it.isChecked) + updateThermalOverlay() + true + } + R.id.menu_edit_overlay -> { binding.drawerLayout.close() binding.surfaceInputOverlay.requestFocus() @@ -850,7 +898,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { right = cutInsets.right } - v.setPadding(left, cutInsets.top, right, 0) + v.updatePadding(left = left, top = cutInsets.top, right = right) windowInsets } } @@ -1003,5 +1051,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { companion object { private val perfStatsUpdateHandler = Handler(Looper.myLooper()!!) + private val thermalStatsUpdateHandler = Handler(Looper.myLooper()!!) } } diff --git a/src/android/app/src/main/jni/android_settings.h b/src/android/app/src/main/jni/android_settings.h index cf93304da..4a3bc8e53 100644 --- a/src/android/app/src/main/jni/android_settings.h +++ b/src/android/app/src/main/jni/android_settings.h @@ -60,6 +60,8 @@ struct Values { Settings::Category::Overlay}; Settings::Setting show_performance_overlay{linkage, true, "show_performance_overlay", Settings::Category::Overlay}; + Settings::Setting show_thermal_overlay{linkage, false, "show_thermal_overlay", + Settings::Category::Overlay}; Settings::Setting show_input_overlay{linkage, true, "show_input_overlay", Settings::Category::Overlay}; Settings::Setting touchscreen{linkage, true, "touchscreen", Settings::Category::Overlay}; diff --git a/src/android/app/src/main/res/layout/fragment_emulation.xml b/src/android/app/src/main/res/layout/fragment_emulation.xml index 0d2bfe8d6..e99a15783 100644 --- a/src/android/app/src/main/res/layout/fragment_emulation.xml +++ b/src/android/app/src/main/res/layout/fragment_emulation.xml @@ -140,6 +140,7 @@ android:id="@+id/overlay_container" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginHorizontal="20dp" android:fitsSystemWindows="true"> + + + + diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index 3cd1586fd..f9e2122ee 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -380,6 +380,7 @@ Exit emulation Done FPS counter + Thermal indicator Toggle controls Relative stick center D-pad slide