diff options
109 files changed, 3131 insertions, 1499 deletions
diff --git a/src/android/app/build.gradle.kts b/src/android/app/build.gradle.kts index f763c657e..53aafa08c 100644 --- a/src/android/app/build.gradle.kts +++ b/src/android/app/build.gradle.kts @@ -10,7 +10,7 @@ plugins { id("com.android.application") id("org.jetbrains.kotlin.android") id("kotlin-parcelize") - kotlin("plugin.serialization") version "1.8.21" + kotlin("plugin.serialization") version "1.9.20" id("androidx.navigation.safeargs.kotlin") id("org.jlleitschuh.gradle.ktlint") version "11.4.0" } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt index 9b08f008d..93c8ce922 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt @@ -49,6 +49,7 @@ import org.yuzu.yuzu_emu.utils.ForegroundService import org.yuzu.yuzu_emu.utils.InputHandler import org.yuzu.yuzu_emu.utils.Log import org.yuzu.yuzu_emu.utils.MemoryUtil +import org.yuzu.yuzu_emu.utils.NativeConfig import org.yuzu.yuzu_emu.utils.NfcReader import org.yuzu.yuzu_emu.utils.ThemeHelper import java.text.NumberFormat @@ -170,6 +171,11 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { stopMotionSensorListener() } + override fun onStop() { + super.onStop() + NativeConfig.saveGlobalConfig() + } + override fun onUserLeaveHint() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { if (BooleanSetting.PICTURE_IN_PICTURE.getBoolean() && !isInPictureInPictureMode) { 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 16f06cd0a..86bd33672 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 @@ -18,7 +18,14 @@ enum class BooleanSetting(override val key: String) : AbstractBooleanSetting { RENDERER_REACTIVE_FLUSHING("use_reactive_flushing"), RENDERER_DEBUG("debug"), PICTURE_IN_PICTURE("picture_in_picture"), - USE_CUSTOM_RTC("custom_rtc_enabled"); + USE_CUSTOM_RTC("custom_rtc_enabled"), + BLACK_BACKGROUNDS("black_backgrounds"), + JOYSTICK_REL_CENTER("joystick_rel_center"), + DPAD_SLIDE("dpad_slide"), + HAPTIC_FEEDBACK("haptic_feedback"), + SHOW_PERFORMANCE_OVERLAY("show_performance_overlay"), + SHOW_INPUT_OVERLAY("show_input_overlay"), + TOUCHSCREEN("touchscreen"); override fun getBoolean(needsGlobal: Boolean): Boolean = NativeConfig.getBoolean(key, needsGlobal) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt index df760440f..16fb87614 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt @@ -19,7 +19,11 @@ enum class IntSetting(override val key: String) : AbstractIntSetting { RENDERER_SCREEN_LAYOUT("screen_layout"), RENDERER_ASPECT_RATIO("aspect_ratio"), AUDIO_OUTPUT_ENGINE("output_engine"), - MAX_ANISOTROPY("max_anisotropy"); + MAX_ANISOTROPY("max_anisotropy"), + THEME("theme"), + THEME_MODE("theme_mode"), + OVERLAY_SCALE("control_scale"), + OVERLAY_OPACITY("control_opacity"); override fun getInt(needsGlobal: Boolean): Int = NativeConfig.getInt(key, needsGlobal) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt index 9551fc05e..43caac989 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt @@ -15,18 +15,10 @@ object Settings { SECTION_DEBUG(R.string.preferences_debug); } + const val PREF_FIRST_APP_LAUNCH = "FirstApplicationLaunch" const val PREF_MEMORY_WARNING_SHOWN = "MemoryWarningShown" - const val PREF_OVERLAY_VERSION = "OverlayVersion" - const val PREF_LANDSCAPE_OVERLAY_VERSION = "LandscapeOverlayVersion" - const val PREF_PORTRAIT_OVERLAY_VERSION = "PortraitOverlayVersion" - const val PREF_FOLDABLE_OVERLAY_VERSION = "FoldableOverlayVersion" - val overlayLayoutPrefs = listOf( - PREF_LANDSCAPE_OVERLAY_VERSION, - PREF_PORTRAIT_OVERLAY_VERSION, - PREF_FOLDABLE_OVERLAY_VERSION - ) - + // Deprecated input overlay preference keys const val PREF_CONTROL_SCALE = "controlScale" const val PREF_CONTROL_OPACITY = "controlOpacity" const val PREF_TOUCH_ENABLED = "isTouchEnabled" @@ -47,23 +39,12 @@ object Settings { const val PREF_BUTTON_STICK_R = "buttonToggle14" const val PREF_BUTTON_HOME = "buttonToggle15" const val PREF_BUTTON_SCREENSHOT = "buttonToggle16" - const val PREF_MENU_SETTINGS_JOYSTICK_REL_CENTER = "EmulationMenuSettings_JoystickRelCenter" const val PREF_MENU_SETTINGS_DPAD_SLIDE = "EmulationMenuSettings_DpadSlideEnable" const val PREF_MENU_SETTINGS_HAPTICS = "EmulationMenuSettings_Haptics" const val PREF_MENU_SETTINGS_SHOW_FPS = "EmulationMenuSettings_ShowFps" const val PREF_MENU_SETTINGS_SHOW_OVERLAY = "EmulationMenuSettings_ShowOverlay" - - const val PREF_FIRST_APP_LAUNCH = "FirstApplicationLaunch" - const val PREF_THEME = "Theme" - const val PREF_THEME_MODE = "ThemeMode" - const val PREF_BLACK_BACKGROUNDS = "BlackBackgrounds" - val overlayPreferences = listOf( - PREF_OVERLAY_VERSION, - PREF_CONTROL_SCALE, - PREF_CONTROL_OPACITY, - PREF_TOUCH_ENABLED, PREF_BUTTON_A, PREF_BUTTON_B, PREF_BUTTON_X, @@ -83,6 +64,21 @@ object Settings { PREF_BUTTON_STICK_R ) + // Deprecated layout preference keys + const val PREF_LANDSCAPE_SUFFIX = "_Landscape" + const val PREF_PORTRAIT_SUFFIX = "_Portrait" + const val PREF_FOLDABLE_SUFFIX = "_Foldable" + val overlayLayoutSuffixes = listOf( + PREF_LANDSCAPE_SUFFIX, + PREF_PORTRAIT_SUFFIX, + PREF_FOLDABLE_SUFFIX + ) + + // Deprecated theme preference keys + const val PREF_THEME = "Theme" + const val PREF_THEME_MODE = "ThemeMode" + const val PREF_BLACK_BACKGROUNDS = "BlackBackgrounds" + const val LayoutOption_Unspecified = 0 const val LayoutOption_MobilePortrait = 4 const val LayoutOption_MobileLandscape = 5 diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt index db1a1076c..2ad2f4966 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt @@ -3,10 +3,8 @@ package org.yuzu.yuzu_emu.features.settings.ui -import android.content.SharedPreferences import android.os.Build import android.widget.Toast -import androidx.preference.PreferenceManager import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.YuzuApplication @@ -29,9 +27,6 @@ class SettingsFragmentPresenter( ) { private var settingsList = ArrayList<SettingsItem>() - private val preferences: SharedPreferences - get() = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - // Extension for altering settings list based on each setting's properties fun ArrayList<SettingsItem>.add(key: String) { val item = SettingsItem.settingsItems[key]!! @@ -170,25 +165,19 @@ class SettingsFragmentPresenter( private fun addThemeSettings(sl: ArrayList<SettingsItem>) { sl.apply { val theme: AbstractIntSetting = object : AbstractIntSetting { - override fun getInt(needsGlobal: Boolean): Int = - preferences.getInt(Settings.PREF_THEME, 0) - + override fun getInt(needsGlobal: Boolean): Int = IntSetting.THEME.getInt() override fun setInt(value: Int) { - preferences.edit() - .putInt(Settings.PREF_THEME, value) - .apply() + IntSetting.THEME.setInt(value) settingsViewModel.setShouldRecreate(true) } - override val key: String = Settings.PREF_THEME - override val isRuntimeModifiable: Boolean = false - override fun getValueAsString(needsGlobal: Boolean): String = getInt().toString() - override val defaultValue: Int = 0 - override fun reset() { - preferences.edit() - .putInt(Settings.PREF_THEME, defaultValue) - .apply() - } + override val key: String = IntSetting.THEME.key + override val isRuntimeModifiable: Boolean = IntSetting.THEME.isRuntimeModifiable + override fun getValueAsString(needsGlobal: Boolean): String = + IntSetting.THEME.getValueAsString() + + override val defaultValue: Int = IntSetting.THEME.defaultValue + override fun reset() = IntSetting.THEME.setInt(defaultValue) } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { @@ -214,24 +203,22 @@ class SettingsFragmentPresenter( } val themeMode: AbstractIntSetting = object : AbstractIntSetting { - override fun getInt(needsGlobal: Boolean): Int = - preferences.getInt(Settings.PREF_THEME_MODE, -1) - + override fun getInt(needsGlobal: Boolean): Int = IntSetting.THEME_MODE.getInt() override fun setInt(value: Int) { - preferences.edit() - .putInt(Settings.PREF_THEME_MODE, value) - .apply() + IntSetting.THEME_MODE.setInt(value) settingsViewModel.setShouldRecreate(true) } - override val key: String = Settings.PREF_THEME_MODE - override val isRuntimeModifiable: Boolean = false - override fun getValueAsString(needsGlobal: Boolean): String = getInt().toString() - override val defaultValue: Int = -1 + override val key: String = IntSetting.THEME_MODE.key + override val isRuntimeModifiable: Boolean = + IntSetting.THEME_MODE.isRuntimeModifiable + + override fun getValueAsString(needsGlobal: Boolean): String = + IntSetting.THEME_MODE.getValueAsString() + + override val defaultValue: Int = IntSetting.THEME_MODE.defaultValue override fun reset() { - preferences.edit() - .putInt(Settings.PREF_BLACK_BACKGROUNDS, defaultValue) - .apply() + IntSetting.THEME_MODE.setInt(defaultValue) settingsViewModel.setShouldRecreate(true) } } @@ -248,25 +235,24 @@ class SettingsFragmentPresenter( val blackBackgrounds: AbstractBooleanSetting = object : AbstractBooleanSetting { override fun getBoolean(needsGlobal: Boolean): Boolean = - preferences.getBoolean(Settings.PREF_BLACK_BACKGROUNDS, false) + BooleanSetting.BLACK_BACKGROUNDS.getBoolean() override fun setBoolean(value: Boolean) { - preferences.edit() - .putBoolean(Settings.PREF_BLACK_BACKGROUNDS, value) - .apply() + BooleanSetting.BLACK_BACKGROUNDS.setBoolean(value) settingsViewModel.setShouldRecreate(true) } - override val key: String = Settings.PREF_BLACK_BACKGROUNDS - override val isRuntimeModifiable: Boolean = false + override val key: String = BooleanSetting.BLACK_BACKGROUNDS.key + override val isRuntimeModifiable: Boolean = + BooleanSetting.BLACK_BACKGROUNDS.isRuntimeModifiable + override fun getValueAsString(needsGlobal: Boolean): String = - getBoolean().toString() + BooleanSetting.BLACK_BACKGROUNDS.getValueAsString() - override val defaultValue: Boolean = false + override val defaultValue: Boolean = BooleanSetting.BLACK_BACKGROUNDS.defaultValue override fun reset() { - preferences.edit() - .putBoolean(Settings.PREF_BLACK_BACKGROUNDS, defaultValue) - .apply() + BooleanSetting.BLACK_BACKGROUNDS + .setBoolean(BooleanSetting.BLACK_BACKGROUNDS.defaultValue) settingsViewModel.setShouldRecreate(true) } } 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 d7b38f62d..510b2b5eb 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 @@ -7,7 +7,6 @@ import android.annotation.SuppressLint import android.app.AlertDialog import android.content.Context import android.content.DialogInterface -import android.content.SharedPreferences import android.content.pm.ActivityInfo import android.content.res.Configuration import android.net.Uri @@ -33,7 +32,6 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import androidx.navigation.findNavController import androidx.navigation.fragment.navArgs -import androidx.preference.PreferenceManager import androidx.window.layout.FoldingFeature import androidx.window.layout.WindowInfoTracker import androidx.window.layout.WindowLayoutInfo @@ -46,22 +44,22 @@ import kotlinx.coroutines.launch import org.yuzu.yuzu_emu.HomeNavigationDirections import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.activities.EmulationActivity import org.yuzu.yuzu_emu.databinding.DialogOverlayAdjustBinding import org.yuzu.yuzu_emu.databinding.FragmentEmulationBinding +import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting import org.yuzu.yuzu_emu.features.settings.model.IntSetting import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile import org.yuzu.yuzu_emu.model.DriverViewModel import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.EmulationViewModel -import org.yuzu.yuzu_emu.overlay.InputOverlay +import org.yuzu.yuzu_emu.overlay.model.OverlayControl +import org.yuzu.yuzu_emu.overlay.model.OverlayLayout import org.yuzu.yuzu_emu.utils.* import java.lang.NullPointerException class EmulationFragment : Fragment(), SurfaceHolder.Callback { - private lateinit var preferences: SharedPreferences private lateinit var emulationState: EmulationState private var emulationActivity: EmulationActivity? = null private var perfStatsUpdater: (() -> Unit)? = null @@ -141,7 +139,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { // So this fragment doesn't restart on configuration changes; i.e. rotation. retainInstance = true - preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) emulationState = EmulationState(game.path) } @@ -382,24 +379,25 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } updateScreenLayout() + val showInputOverlay = BooleanSetting.SHOW_INPUT_OVERLAY.getBoolean() if (emulationActivity?.isInPictureInPictureMode == true) { if (binding.drawerLayout.isOpen) { binding.drawerLayout.close() } - if (EmulationMenuSettings.showOverlay) { + if (showInputOverlay) { binding.surfaceInputOverlay.visibility = View.INVISIBLE } } else { - if (EmulationMenuSettings.showOverlay && emulationViewModel.emulationStarted.value) { + if (showInputOverlay && emulationViewModel.emulationStarted.value) { binding.surfaceInputOverlay.visibility = View.VISIBLE } else { binding.surfaceInputOverlay.visibility = View.INVISIBLE } if (!isInFoldableLayout) { if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { - binding.surfaceInputOverlay.layout = InputOverlay.PORTRAIT + binding.surfaceInputOverlay.layout = OverlayLayout.Portrait } else { - binding.surfaceInputOverlay.layout = InputOverlay.LANDSCAPE + binding.surfaceInputOverlay.layout = OverlayLayout.Landscape } } } @@ -423,17 +421,15 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } private fun resetInputOverlay() { - preferences.edit() - .remove(Settings.PREF_CONTROL_SCALE) - .remove(Settings.PREF_CONTROL_OPACITY) - .apply() + IntSetting.OVERLAY_SCALE.reset() + IntSetting.OVERLAY_OPACITY.reset() binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.resetLayoutVisibilityAndPlacement() } } private fun updateShowFpsOverlay() { - if (EmulationMenuSettings.showFps) { + if (BooleanSetting.SHOW_PERFORMANCE_OVERLAY.getBoolean()) { val SYSTEM_FPS = 0 val FPS = 1 val FRAMETIME = 2 @@ -496,7 +492,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { binding.inGameMenu.layoutParams.height = it.bounds.bottom isInFoldableLayout = true - binding.surfaceInputOverlay.layout = InputOverlay.FOLDABLE + binding.surfaceInputOverlay.layout = OverlayLayout.Foldable } } it.isSeparating @@ -535,18 +531,22 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { popup.menuInflater.inflate(R.menu.menu_overlay_options, popup.menu) popup.menu.apply { - findItem(R.id.menu_toggle_fps).isChecked = EmulationMenuSettings.showFps - findItem(R.id.menu_rel_stick_center).isChecked = EmulationMenuSettings.joystickRelCenter - findItem(R.id.menu_dpad_slide).isChecked = EmulationMenuSettings.dpadSlide - findItem(R.id.menu_show_overlay).isChecked = EmulationMenuSettings.showOverlay - findItem(R.id.menu_haptics).isChecked = EmulationMenuSettings.hapticFeedback + findItem(R.id.menu_toggle_fps).isChecked = + BooleanSetting.SHOW_PERFORMANCE_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() + findItem(R.id.menu_show_overlay).isChecked = + BooleanSetting.SHOW_INPUT_OVERLAY.getBoolean() + findItem(R.id.menu_haptics).isChecked = BooleanSetting.HAPTIC_FEEDBACK.getBoolean() + findItem(R.id.menu_touchscreen).isChecked = BooleanSetting.TOUCHSCREEN.getBoolean() } popup.setOnMenuItemClickListener { when (it.itemId) { R.id.menu_toggle_fps -> { it.isChecked = !it.isChecked - EmulationMenuSettings.showFps = it.isChecked + BooleanSetting.SHOW_PERFORMANCE_OVERLAY.setBoolean(it.isChecked) updateShowFpsOverlay() true } @@ -564,11 +564,12 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } R.id.menu_toggle_controls -> { - val preferences = - PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - val optionsArray = BooleanArray(Settings.overlayPreferences.size) - Settings.overlayPreferences.forEachIndexed { i, _ -> - optionsArray[i] = preferences.getBoolean("buttonToggle$i", i < 15) + val overlayControlData = NativeConfig.getOverlayControlData() + val optionsArray = BooleanArray(overlayControlData.size) + overlayControlData.forEachIndexed { i, _ -> + optionsArray[i] = overlayControlData.firstOrNull { data -> + OverlayControl.entries[i].id == data.id + }?.enabled == true } val dialog = MaterialAlertDialogBuilder(requireContext()) @@ -577,11 +578,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { R.array.gamepadButtons, optionsArray ) { _, indexSelected, isChecked -> - preferences.edit() - .putBoolean("buttonToggle$indexSelected", isChecked) - .apply() + overlayControlData.firstOrNull { data -> + OverlayControl.entries[indexSelected].id == data.id + }?.enabled = isChecked } .setPositiveButton(android.R.string.ok) { _, _ -> + NativeConfig.setOverlayControlData(overlayControlData) + NativeConfig.saveGlobalConfig() binding.surfaceInputOverlay.refreshControls() } .setNegativeButton(android.R.string.cancel, null) @@ -592,12 +595,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { dialog.getButton(AlertDialog.BUTTON_NEUTRAL) .setOnClickListener { val isChecked = !optionsArray[0] - Settings.overlayPreferences.forEachIndexed { i, _ -> + overlayControlData.forEachIndexed { i, _ -> optionsArray[i] = isChecked dialog.listView.setItemChecked(i, isChecked) - preferences.edit() - .putBoolean("buttonToggle$i", isChecked) - .apply() + overlayControlData[i].enabled = isChecked } } true @@ -605,26 +606,32 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { R.id.menu_show_overlay -> { it.isChecked = !it.isChecked - EmulationMenuSettings.showOverlay = it.isChecked + BooleanSetting.SHOW_INPUT_OVERLAY.setBoolean(it.isChecked) binding.surfaceInputOverlay.refreshControls() true } R.id.menu_rel_stick_center -> { it.isChecked = !it.isChecked - EmulationMenuSettings.joystickRelCenter = it.isChecked + BooleanSetting.JOYSTICK_REL_CENTER.setBoolean(it.isChecked) true } R.id.menu_dpad_slide -> { it.isChecked = !it.isChecked - EmulationMenuSettings.dpadSlide = it.isChecked + BooleanSetting.DPAD_SLIDE.setBoolean(it.isChecked) true } R.id.menu_haptics -> { it.isChecked = !it.isChecked - EmulationMenuSettings.hapticFeedback = it.isChecked + BooleanSetting.HAPTIC_FEEDBACK.setBoolean(it.isChecked) + true + } + + R.id.menu_touchscreen -> { + it.isChecked = !it.isChecked + BooleanSetting.TOUCHSCREEN.setBoolean(it.isChecked) true } @@ -667,6 +674,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED } } + NativeConfig.saveGlobalConfig() } @SuppressLint("SetTextI18n") @@ -675,7 +683,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { adjustBinding.apply { inputScaleSlider.apply { valueTo = 150F - value = preferences.getInt(Settings.PREF_CONTROL_SCALE, 50).toFloat() + value = IntSetting.OVERLAY_SCALE.getInt().toFloat() addOnChangeListener( Slider.OnChangeListener { _, value, _ -> inputScaleValue.text = "${value.toInt()}%" @@ -685,7 +693,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } inputOpacitySlider.apply { valueTo = 100F - value = preferences.getInt(Settings.PREF_CONTROL_OPACITY, 100).toFloat() + value = IntSetting.OVERLAY_OPACITY.getInt().toFloat() addOnChangeListener( Slider.OnChangeListener { _, value, _ -> inputOpacityValue.text = "${value.toInt()}%" @@ -709,16 +717,12 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } private fun setControlScale(scale: Int) { - preferences.edit() - .putInt(Settings.PREF_CONTROL_SCALE, scale) - .apply() + IntSetting.OVERLAY_SCALE.setInt(scale) binding.surfaceInputOverlay.refreshControls() } private fun setControlOpacity(opacity: Int) { - preferences.edit() - .putInt(Settings.PREF_CONTROL_OPACITY, opacity) - .apply() + IntSetting.OVERLAY_OPACITY.setInt(opacity) binding.surfaceInputOverlay.refreshControls() } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt index a13faf3c7..bb69b8bd5 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt @@ -21,7 +21,6 @@ import android.view.View import android.view.View.OnTouchListener import android.view.WindowInsets import androidx.core.content.ContextCompat -import androidx.preference.PreferenceManager import androidx.window.layout.WindowMetricsCalculator import kotlin.math.max import kotlin.math.min @@ -29,9 +28,13 @@ import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.NativeLibrary.ButtonType import org.yuzu.yuzu_emu.NativeLibrary.StickType import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.YuzuApplication +import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting +import org.yuzu.yuzu_emu.features.settings.model.IntSetting import org.yuzu.yuzu_emu.features.settings.model.Settings -import org.yuzu.yuzu_emu.utils.EmulationMenuSettings +import org.yuzu.yuzu_emu.overlay.model.OverlayControl +import org.yuzu.yuzu_emu.overlay.model.OverlayControlData +import org.yuzu.yuzu_emu.overlay.model.OverlayLayout +import org.yuzu.yuzu_emu.utils.NativeConfig /** * Draws the interactive input overlay on top of the @@ -51,23 +54,18 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : private lateinit var windowInsets: WindowInsets - var layout = LANDSCAPE + var layout = OverlayLayout.Landscape override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { super.onLayout(changed, left, top, right, bottom) windowInsets = rootWindowInsets - val overlayVersion = preferences.getInt(Settings.PREF_OVERLAY_VERSION, 0) - if (overlayVersion != OVERLAY_VERSION) { - resetAllLayouts() + val overlayControlData = NativeConfig.getOverlayControlData() + if (overlayControlData.isEmpty()) { + populateDefaultConfig() } else { - val layoutIndex = overlayLayouts.indexOf(layout) - val currentLayoutVersion = - preferences.getInt(Settings.overlayLayoutPrefs[layoutIndex], 0) - if (currentLayoutVersion != overlayLayoutVersions[layoutIndex]) { - resetCurrentLayout() - } + checkForNewControls(overlayControlData) } // Load the controls. @@ -123,7 +121,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : } for (dpad in overlayDpads) { - if (!dpad.updateStatus(event, EmulationMenuSettings.dpadSlide)) { + if (!dpad.updateStatus(event, BooleanSetting.DPAD_SLIDE.getBoolean())) { continue } NativeLibrary.onGamePadButtonEvent( @@ -174,7 +172,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : invalidate() } - if (!preferences.getBoolean(Settings.PREF_TOUCH_ENABLED, true)) { + if (!BooleanSetting.TOUCHSCREEN.getBoolean()) { return true } @@ -211,7 +209,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : } private fun playHaptics(event: MotionEvent) { - if (EmulationMenuSettings.hapticFeedback) { + if (BooleanSetting.HAPTIC_FEEDBACK.getBoolean()) { when (event.actionMasked) { MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> @@ -255,10 +253,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : MotionEvent.ACTION_POINTER_DOWN -> // If no button is being moved now, remember the currently touched button to move. if (buttonBeingConfigured == null && - button.bounds.contains( - fingerPositionX, - fingerPositionY - ) + button.bounds.contains(fingerPositionX, fingerPositionY) ) { buttonBeingConfigured = button buttonBeingConfigured!!.onConfigureTouch(event) @@ -274,7 +269,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : MotionEvent.ACTION_POINTER_UP -> if (buttonBeingConfigured === button) { // Persist button position by saving new place. saveControlPosition( - buttonBeingConfigured!!.prefId, + buttonBeingConfigured!!.overlayControlData.id, buttonBeingConfigured!!.bounds.centerX(), buttonBeingConfigured!!.bounds.centerY(), layout @@ -321,10 +316,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : when (event.action) { MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> if (joystickBeingConfigured == null && - joystick.bounds.contains( - fingerPositionX, - fingerPositionY - ) + joystick.bounds.contains(fingerPositionX, fingerPositionY) ) { joystickBeingConfigured = joystick joystickBeingConfigured!!.onConfigureTouch(event) @@ -351,231 +343,257 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : return true } - private fun addOverlayControls(layout: String) { + private fun addOverlayControls(layout: OverlayLayout) { val windowSize = getSafeScreenSize(context, Pair(measuredWidth, measuredHeight)) - if (preferences.getBoolean(Settings.PREF_BUTTON_A, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_a, - R.drawable.facebutton_a_depressed, - ButtonType.BUTTON_A, - Settings.PREF_BUTTON_A, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_B, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_b, - R.drawable.facebutton_b_depressed, - ButtonType.BUTTON_B, - Settings.PREF_BUTTON_B, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_X, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_x, - R.drawable.facebutton_x_depressed, - ButtonType.BUTTON_X, - Settings.PREF_BUTTON_X, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_Y, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_y, - R.drawable.facebutton_y_depressed, - ButtonType.BUTTON_Y, - Settings.PREF_BUTTON_Y, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_L, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.l_shoulder, - R.drawable.l_shoulder_depressed, - ButtonType.TRIGGER_L, - Settings.PREF_BUTTON_L, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_R, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.r_shoulder, - R.drawable.r_shoulder_depressed, - ButtonType.TRIGGER_R, - Settings.PREF_BUTTON_R, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_ZL, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.zl_trigger, - R.drawable.zl_trigger_depressed, - ButtonType.TRIGGER_ZL, - Settings.PREF_BUTTON_ZL, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_ZR, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.zr_trigger, - R.drawable.zr_trigger_depressed, - ButtonType.TRIGGER_ZR, - Settings.PREF_BUTTON_ZR, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_PLUS, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_plus, - R.drawable.facebutton_plus_depressed, - ButtonType.BUTTON_PLUS, - Settings.PREF_BUTTON_PLUS, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_MINUS, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_minus, - R.drawable.facebutton_minus_depressed, - ButtonType.BUTTON_MINUS, - Settings.PREF_BUTTON_MINUS, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_DPAD, true)) { - overlayDpads.add( - initializeOverlayDpad( - context, - windowSize, - R.drawable.dpad_standard, - R.drawable.dpad_standard_cardinal_depressed, - R.drawable.dpad_standard_diagonal_depressed, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_STICK_L, true)) { - overlayJoysticks.add( - initializeOverlayJoystick( - context, - windowSize, - R.drawable.joystick_range, - R.drawable.joystick, - R.drawable.joystick_depressed, - StickType.STICK_L, - ButtonType.STICK_L, - Settings.PREF_STICK_L, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_STICK_R, true)) { - overlayJoysticks.add( - initializeOverlayJoystick( - context, - windowSize, - R.drawable.joystick_range, - R.drawable.joystick, - R.drawable.joystick_depressed, - StickType.STICK_R, - ButtonType.STICK_R, - Settings.PREF_STICK_R, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_HOME, false)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_home, - R.drawable.facebutton_home_depressed, - ButtonType.BUTTON_HOME, - Settings.PREF_BUTTON_HOME, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_SCREENSHOT, false)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.facebutton_screenshot, - R.drawable.facebutton_screenshot_depressed, - ButtonType.BUTTON_CAPTURE, - Settings.PREF_BUTTON_SCREENSHOT, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_STICK_L, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.button_l3, - R.drawable.button_l3_depressed, - ButtonType.STICK_L, - Settings.PREF_BUTTON_STICK_L, - layout - ) - ) - } - if (preferences.getBoolean(Settings.PREF_BUTTON_STICK_R, true)) { - overlayButtons.add( - initializeOverlayButton( - context, - windowSize, - R.drawable.button_r3, - R.drawable.button_r3_depressed, - ButtonType.STICK_R, - Settings.PREF_BUTTON_STICK_R, - layout - ) - ) + val overlayControlData = NativeConfig.getOverlayControlData() + for (data in overlayControlData) { + if (!data.enabled) { + continue + } + + val position = data.positionFromLayout(layout) + when (data.id) { + OverlayControl.BUTTON_A.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_a, + R.drawable.facebutton_a_depressed, + ButtonType.BUTTON_A, + data, + position + ) + ) + } + + OverlayControl.BUTTON_B.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_b, + R.drawable.facebutton_b_depressed, + ButtonType.BUTTON_B, + data, + position + ) + ) + } + + OverlayControl.BUTTON_X.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_x, + R.drawable.facebutton_x_depressed, + ButtonType.BUTTON_X, + data, + position + ) + ) + } + + OverlayControl.BUTTON_Y.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_y, + R.drawable.facebutton_y_depressed, + ButtonType.BUTTON_Y, + data, + position + ) + ) + } + + OverlayControl.BUTTON_PLUS.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_plus, + R.drawable.facebutton_plus_depressed, + ButtonType.BUTTON_PLUS, + data, + position + ) + ) + } + + OverlayControl.BUTTON_MINUS.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_minus, + R.drawable.facebutton_minus_depressed, + ButtonType.BUTTON_MINUS, + data, + position + ) + ) + } + + OverlayControl.BUTTON_HOME.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_home, + R.drawable.facebutton_home_depressed, + ButtonType.BUTTON_HOME, + data, + position + ) + ) + } + + OverlayControl.BUTTON_CAPTURE.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.facebutton_screenshot, + R.drawable.facebutton_screenshot_depressed, + ButtonType.BUTTON_CAPTURE, + data, + position + ) + ) + } + + OverlayControl.BUTTON_L.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.l_shoulder, + R.drawable.l_shoulder_depressed, + ButtonType.TRIGGER_L, + data, + position + ) + ) + } + + OverlayControl.BUTTON_R.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.r_shoulder, + R.drawable.r_shoulder_depressed, + ButtonType.TRIGGER_R, + data, + position + ) + ) + } + + OverlayControl.BUTTON_ZL.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.zl_trigger, + R.drawable.zl_trigger_depressed, + ButtonType.TRIGGER_ZL, + data, + position + ) + ) + } + + OverlayControl.BUTTON_ZR.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.zr_trigger, + R.drawable.zr_trigger_depressed, + ButtonType.TRIGGER_ZR, + data, + position + ) + ) + } + + OverlayControl.BUTTON_STICK_L.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.button_l3, + R.drawable.button_l3_depressed, + ButtonType.STICK_L, + data, + position + ) + ) + } + + OverlayControl.BUTTON_STICK_R.id -> { + overlayButtons.add( + initializeOverlayButton( + context, + windowSize, + R.drawable.button_r3, + R.drawable.button_r3_depressed, + ButtonType.STICK_R, + data, + position + ) + ) + } + + OverlayControl.STICK_L.id -> { + overlayJoysticks.add( + initializeOverlayJoystick( + context, + windowSize, + R.drawable.joystick_range, + R.drawable.joystick, + R.drawable.joystick_depressed, + StickType.STICK_L, + ButtonType.STICK_L, + data, + position + ) + ) + } + + OverlayControl.STICK_R.id -> { + overlayJoysticks.add( + initializeOverlayJoystick( + context, + windowSize, + R.drawable.joystick_range, + R.drawable.joystick, + R.drawable.joystick_depressed, + StickType.STICK_R, + ButtonType.STICK_R, + data, + position + ) + ) + } + + OverlayControl.COMBINED_DPAD.id -> { + overlayDpads.add( + initializeOverlayDpad( + context, + windowSize, + R.drawable.dpad_standard, + R.drawable.dpad_standard_cardinal_depressed, + R.drawable.dpad_standard_diagonal_depressed, + position + ) + ) + } + } } } @@ -586,313 +604,87 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : overlayJoysticks.clear() // Add all the enabled overlay items back to the HashSet. - if (EmulationMenuSettings.showOverlay) { + if (BooleanSetting.SHOW_INPUT_OVERLAY.getBoolean()) { addOverlayControls(layout) } invalidate() } - private fun saveControlPosition(prefId: String, x: Int, y: Int, layout: String) { + private fun saveControlPosition(id: String, x: Int, y: Int, layout: OverlayLayout) { val windowSize = getSafeScreenSize(context, Pair(measuredWidth, measuredHeight)) val min = windowSize.first val max = windowSize.second - PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext).edit() - .putFloat("$prefId-X$layout", (x - min.x).toFloat() / max.x) - .putFloat("$prefId-Y$layout", (y - min.y).toFloat() / max.y) - .apply() + val overlayControlData = NativeConfig.getOverlayControlData() + val data = overlayControlData.firstOrNull { it.id == id } + val newPosition = Pair((x - min.x).toDouble() / max.x, (y - min.y).toDouble() / max.y) + when (layout) { + OverlayLayout.Landscape -> data?.landscapePosition = newPosition + OverlayLayout.Portrait -> data?.portraitPosition = newPosition + OverlayLayout.Foldable -> data?.foldablePosition = newPosition + } + NativeConfig.setOverlayControlData(overlayControlData) } fun setIsInEditMode(editMode: Boolean) { inEditMode = editMode } - private fun resetCurrentLayout() { - defaultOverlayByLayout(layout) - val layoutIndex = overlayLayouts.indexOf(layout) - preferences.edit() - .putInt(Settings.overlayLayoutPrefs[layoutIndex], overlayLayoutVersions[layoutIndex]) - .apply() + /** + * Applies and saves all default values for the overlay + */ + private fun populateDefaultConfig() { + val newConfig = OverlayControl.entries.map { it.toOverlayControlData() } + NativeConfig.setOverlayControlData(newConfig.toTypedArray()) + NativeConfig.saveGlobalConfig() } - private fun resetAllLayouts() { - val editor = preferences.edit() - overlayLayouts.forEachIndexed { i, layout -> - defaultOverlayByLayout(layout) - editor.putInt(Settings.overlayLayoutPrefs[i], overlayLayoutVersions[i]) + /** + * Checks if any new controls were added to OverlayControl that do not exist within deserialized + * config and adds / saves them if necessary + * + * @param overlayControlData Overlay control data from [NativeConfig.getOverlayControlData] + */ + private fun checkForNewControls(overlayControlData: Array<OverlayControlData>) { + val missingControls = mutableListOf<OverlayControlData>() + OverlayControl.entries.forEach { defaultControl -> + val controlData = overlayControlData.firstOrNull { it.id == defaultControl.id } + if (controlData == null) { + missingControls.add(defaultControl.toOverlayControlData()) + } + } + + if (missingControls.isNotEmpty()) { + NativeConfig.setOverlayControlData( + arrayOf(*overlayControlData, *(missingControls.toTypedArray())) + ) + NativeConfig.saveGlobalConfig() } - editor.putInt(Settings.PREF_OVERLAY_VERSION, OVERLAY_VERSION) - editor.apply() } fun resetLayoutVisibilityAndPlacement() { - defaultOverlayByLayout(layout) - val editor = preferences.edit() - Settings.overlayPreferences.forEachIndexed { _, pref -> - editor.remove(pref) + defaultOverlayPositionByLayout(layout) + + val overlayControlData = NativeConfig.getOverlayControlData() + overlayControlData.forEach { + it.enabled = OverlayControl.from(it.id)?.defaultVisibility == false } - editor.apply() + NativeConfig.setOverlayControlData(overlayControlData) + refreshControls() } - private val landscapeResources = arrayOf( - R.integer.SWITCH_BUTTON_A_X, - R.integer.SWITCH_BUTTON_A_Y, - R.integer.SWITCH_BUTTON_B_X, - R.integer.SWITCH_BUTTON_B_Y, - R.integer.SWITCH_BUTTON_X_X, - R.integer.SWITCH_BUTTON_X_Y, - R.integer.SWITCH_BUTTON_Y_X, - R.integer.SWITCH_BUTTON_Y_Y, - R.integer.SWITCH_TRIGGER_ZL_X, - R.integer.SWITCH_TRIGGER_ZL_Y, - R.integer.SWITCH_TRIGGER_ZR_X, - R.integer.SWITCH_TRIGGER_ZR_Y, - R.integer.SWITCH_BUTTON_DPAD_X, - R.integer.SWITCH_BUTTON_DPAD_Y, - R.integer.SWITCH_TRIGGER_L_X, - R.integer.SWITCH_TRIGGER_L_Y, - R.integer.SWITCH_TRIGGER_R_X, - R.integer.SWITCH_TRIGGER_R_Y, - R.integer.SWITCH_BUTTON_PLUS_X, - R.integer.SWITCH_BUTTON_PLUS_Y, - R.integer.SWITCH_BUTTON_MINUS_X, - R.integer.SWITCH_BUTTON_MINUS_Y, - R.integer.SWITCH_BUTTON_HOME_X, - R.integer.SWITCH_BUTTON_HOME_Y, - R.integer.SWITCH_BUTTON_CAPTURE_X, - R.integer.SWITCH_BUTTON_CAPTURE_Y, - R.integer.SWITCH_STICK_R_X, - R.integer.SWITCH_STICK_R_Y, - R.integer.SWITCH_STICK_L_X, - R.integer.SWITCH_STICK_L_Y, - R.integer.SWITCH_BUTTON_STICK_L_X, - R.integer.SWITCH_BUTTON_STICK_L_Y, - R.integer.SWITCH_BUTTON_STICK_R_X, - R.integer.SWITCH_BUTTON_STICK_R_Y - ) - - private val portraitResources = arrayOf( - R.integer.SWITCH_BUTTON_A_X_PORTRAIT, - R.integer.SWITCH_BUTTON_A_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_B_X_PORTRAIT, - R.integer.SWITCH_BUTTON_B_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_X_X_PORTRAIT, - R.integer.SWITCH_BUTTON_X_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_Y_X_PORTRAIT, - R.integer.SWITCH_BUTTON_Y_Y_PORTRAIT, - R.integer.SWITCH_TRIGGER_ZL_X_PORTRAIT, - R.integer.SWITCH_TRIGGER_ZL_Y_PORTRAIT, - R.integer.SWITCH_TRIGGER_ZR_X_PORTRAIT, - R.integer.SWITCH_TRIGGER_ZR_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_DPAD_X_PORTRAIT, - R.integer.SWITCH_BUTTON_DPAD_Y_PORTRAIT, - R.integer.SWITCH_TRIGGER_L_X_PORTRAIT, - R.integer.SWITCH_TRIGGER_L_Y_PORTRAIT, - R.integer.SWITCH_TRIGGER_R_X_PORTRAIT, - R.integer.SWITCH_TRIGGER_R_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_PLUS_X_PORTRAIT, - R.integer.SWITCH_BUTTON_PLUS_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_MINUS_X_PORTRAIT, - R.integer.SWITCH_BUTTON_MINUS_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_HOME_X_PORTRAIT, - R.integer.SWITCH_BUTTON_HOME_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_CAPTURE_X_PORTRAIT, - R.integer.SWITCH_BUTTON_CAPTURE_Y_PORTRAIT, - R.integer.SWITCH_STICK_R_X_PORTRAIT, - R.integer.SWITCH_STICK_R_Y_PORTRAIT, - R.integer.SWITCH_STICK_L_X_PORTRAIT, - R.integer.SWITCH_STICK_L_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_STICK_L_X_PORTRAIT, - R.integer.SWITCH_BUTTON_STICK_L_Y_PORTRAIT, - R.integer.SWITCH_BUTTON_STICK_R_X_PORTRAIT, - R.integer.SWITCH_BUTTON_STICK_R_Y_PORTRAIT - ) - - private val foldableResources = arrayOf( - R.integer.SWITCH_BUTTON_A_X_FOLDABLE, - R.integer.SWITCH_BUTTON_A_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_B_X_FOLDABLE, - R.integer.SWITCH_BUTTON_B_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_X_X_FOLDABLE, - R.integer.SWITCH_BUTTON_X_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_Y_X_FOLDABLE, - R.integer.SWITCH_BUTTON_Y_Y_FOLDABLE, - R.integer.SWITCH_TRIGGER_ZL_X_FOLDABLE, - R.integer.SWITCH_TRIGGER_ZL_Y_FOLDABLE, - R.integer.SWITCH_TRIGGER_ZR_X_FOLDABLE, - R.integer.SWITCH_TRIGGER_ZR_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_DPAD_X_FOLDABLE, - R.integer.SWITCH_BUTTON_DPAD_Y_FOLDABLE, - R.integer.SWITCH_TRIGGER_L_X_FOLDABLE, - R.integer.SWITCH_TRIGGER_L_Y_FOLDABLE, - R.integer.SWITCH_TRIGGER_R_X_FOLDABLE, - R.integer.SWITCH_TRIGGER_R_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_PLUS_X_FOLDABLE, - R.integer.SWITCH_BUTTON_PLUS_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_MINUS_X_FOLDABLE, - R.integer.SWITCH_BUTTON_MINUS_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_HOME_X_FOLDABLE, - R.integer.SWITCH_BUTTON_HOME_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_CAPTURE_X_FOLDABLE, - R.integer.SWITCH_BUTTON_CAPTURE_Y_FOLDABLE, - R.integer.SWITCH_STICK_R_X_FOLDABLE, - R.integer.SWITCH_STICK_R_Y_FOLDABLE, - R.integer.SWITCH_STICK_L_X_FOLDABLE, - R.integer.SWITCH_STICK_L_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_STICK_L_X_FOLDABLE, - R.integer.SWITCH_BUTTON_STICK_L_Y_FOLDABLE, - R.integer.SWITCH_BUTTON_STICK_R_X_FOLDABLE, - R.integer.SWITCH_BUTTON_STICK_R_Y_FOLDABLE - ) - - private fun getResourceValue(layout: String, position: Int): Float { - return when (layout) { - PORTRAIT -> resources.getInteger(portraitResources[position]).toFloat() / 1000 - FOLDABLE -> resources.getInteger(foldableResources[position]).toFloat() / 1000 - else -> resources.getInteger(landscapeResources[position]).toFloat() / 1000 + private fun defaultOverlayPositionByLayout(layout: OverlayLayout) { + val overlayControlData = NativeConfig.getOverlayControlData() + for (data in overlayControlData) { + val defaultControlData = OverlayControl.from(data.id) ?: continue + val position = defaultControlData.getDefaultPositionForLayout(layout) + when (layout) { + OverlayLayout.Landscape -> data.landscapePosition = position + OverlayLayout.Portrait -> data.portraitPosition = position + OverlayLayout.Foldable -> data.foldablePosition = position + } } - } - - private fun defaultOverlayByLayout(layout: String) { - // Each value represents the position of the button in relation to the screen size without insets. - preferences.edit() - .putFloat( - "${Settings.PREF_BUTTON_A}-X$layout", - getResourceValue(layout, 0) - ) - .putFloat( - "${Settings.PREF_BUTTON_A}-Y$layout", - getResourceValue(layout, 1) - ) - .putFloat( - "${Settings.PREF_BUTTON_B}-X$layout", - getResourceValue(layout, 2) - ) - .putFloat( - "${Settings.PREF_BUTTON_B}-Y$layout", - getResourceValue(layout, 3) - ) - .putFloat( - "${Settings.PREF_BUTTON_X}-X$layout", - getResourceValue(layout, 4) - ) - .putFloat( - "${Settings.PREF_BUTTON_X}-Y$layout", - getResourceValue(layout, 5) - ) - .putFloat( - "${Settings.PREF_BUTTON_Y}-X$layout", - getResourceValue(layout, 6) - ) - .putFloat( - "${Settings.PREF_BUTTON_Y}-Y$layout", - getResourceValue(layout, 7) - ) - .putFloat( - "${Settings.PREF_BUTTON_ZL}-X$layout", - getResourceValue(layout, 8) - ) - .putFloat( - "${Settings.PREF_BUTTON_ZL}-Y$layout", - getResourceValue(layout, 9) - ) - .putFloat( - "${Settings.PREF_BUTTON_ZR}-X$layout", - getResourceValue(layout, 10) - ) - .putFloat( - "${Settings.PREF_BUTTON_ZR}-Y$layout", - getResourceValue(layout, 11) - ) - .putFloat( - "${Settings.PREF_BUTTON_DPAD}-X$layout", - getResourceValue(layout, 12) - ) - .putFloat( - "${Settings.PREF_BUTTON_DPAD}-Y$layout", - getResourceValue(layout, 13) - ) - .putFloat( - "${Settings.PREF_BUTTON_L}-X$layout", - getResourceValue(layout, 14) - ) - .putFloat( - "${Settings.PREF_BUTTON_L}-Y$layout", - getResourceValue(layout, 15) - ) - .putFloat( - "${Settings.PREF_BUTTON_R}-X$layout", - getResourceValue(layout, 16) - ) - .putFloat( - "${Settings.PREF_BUTTON_R}-Y$layout", - getResourceValue(layout, 17) - ) - .putFloat( - "${Settings.PREF_BUTTON_PLUS}-X$layout", - getResourceValue(layout, 18) - ) - .putFloat( - "${Settings.PREF_BUTTON_PLUS}-Y$layout", - getResourceValue(layout, 19) - ) - .putFloat( - "${Settings.PREF_BUTTON_MINUS}-X$layout", - getResourceValue(layout, 20) - ) - .putFloat( - "${Settings.PREF_BUTTON_MINUS}-Y$layout", - getResourceValue(layout, 21) - ) - .putFloat( - "${Settings.PREF_BUTTON_HOME}-X$layout", - getResourceValue(layout, 22) - ) - .putFloat( - "${Settings.PREF_BUTTON_HOME}-Y$layout", - getResourceValue(layout, 23) - ) - .putFloat( - "${Settings.PREF_BUTTON_SCREENSHOT}-X$layout", - getResourceValue(layout, 24) - ) - .putFloat( - "${Settings.PREF_BUTTON_SCREENSHOT}-Y$layout", - getResourceValue(layout, 25) - ) - .putFloat( - "${Settings.PREF_STICK_R}-X$layout", - getResourceValue(layout, 26) - ) - .putFloat( - "${Settings.PREF_STICK_R}-Y$layout", - getResourceValue(layout, 27) - ) - .putFloat( - "${Settings.PREF_STICK_L}-X$layout", - getResourceValue(layout, 28) - ) - .putFloat( - "${Settings.PREF_STICK_L}-Y$layout", - getResourceValue(layout, 29) - ) - .putFloat( - "${Settings.PREF_BUTTON_STICK_L}-X$layout", - getResourceValue(layout, 30) - ) - .putFloat( - "${Settings.PREF_BUTTON_STICK_L}-Y$layout", - getResourceValue(layout, 31) - ) - .putFloat( - "${Settings.PREF_BUTTON_STICK_R}-X$layout", - getResourceValue(layout, 32) - ) - .putFloat( - "${Settings.PREF_BUTTON_STICK_R}-Y$layout", - getResourceValue(layout, 33) - ) - .apply() + NativeConfig.setOverlayControlData(overlayControlData) } override fun isInEditMode(): Boolean { @@ -913,18 +705,6 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : FOLDABLE_OVERLAY_VERSION ) - private val preferences: SharedPreferences = - PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - - const val LANDSCAPE = "_Landscape" - const val PORTRAIT = "_Portrait" - const val FOLDABLE = "_Foldable" - val overlayLayouts = listOf( - LANDSCAPE, - PORTRAIT, - FOLDABLE - ) - /** * Resizes a [Bitmap] by a given scale factor * @@ -1036,29 +816,19 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : * In the input overlay configuration menu, * once a touch event begins and then ends (ie. Organizing the buttons to one's own liking for the overlay). * the X and Y coordinates of the button at the END of its touch event - * (when you remove your finger/stylus from the touchscreen) are then stored - * within a SharedPreferences instance so that those values can be retrieved here. - * - * - * This has a few benefits over the conventional way of storing the values - * (ie. within the yuzu ini file). - * - * * No native calls - * * Keeps Android-only values inside the Android environment - * - * + * (when you remove your finger/stylus from the touchscreen) are then stored in a native . * * Technically no modifications should need to be performed on the returned * InputOverlayDrawableButton. Simply add it to the HashSet of overlay items and wait * for Android to call the onDraw method. * - * @param context The current [Context]. - * @param windowSize The size of the window to draw the overlay on. - * @param defaultResId The resource ID of the [Drawable] to get the [Bitmap] of (Default State). - * @param pressedResId The resource ID of the [Drawable] to get the [Bitmap] of (Pressed State). - * @param buttonId Identifier for determining what type of button the initialized InputOverlayDrawableButton represents. - * @param prefId Identifier for determining where a button appears on screen. - * @param layout The current screen layout as determined by [LANDSCAPE], [PORTRAIT], or [FOLDABLE]. + * @param context The current [Context]. + * @param windowSize The size of the window to draw the overlay on. + * @param defaultResId The resource ID of the [Drawable] to get the [Bitmap] of (Default State). + * @param pressedResId The resource ID of the [Drawable] to get the [Bitmap] of (Pressed State). + * @param buttonId Identifier for determining what type of button the initialized InputOverlayDrawableButton represents. + * @param overlayControlData Identifier for determining where a button appears on screen. + * @param position The position on screen as represented by an x and y value between 0 and 1. * @return An [InputOverlayDrawableButton] with the correct drawing bounds set. */ private fun initializeOverlayButton( @@ -1067,33 +837,30 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : defaultResId: Int, pressedResId: Int, buttonId: Int, - prefId: String, - layout: String + overlayControlData: OverlayControlData, + position: Pair<Double, Double> ): InputOverlayDrawableButton { // Resources handle for fetching the initial Drawable resource. val res = context.resources - // SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableButton. - val sPrefs = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - // Decide scale based on button preference ID and user preference - var scale: Float = when (prefId) { - Settings.PREF_BUTTON_HOME, - Settings.PREF_BUTTON_SCREENSHOT, - Settings.PREF_BUTTON_PLUS, - Settings.PREF_BUTTON_MINUS -> 0.07f + var scale: Float = when (overlayControlData.id) { + OverlayControl.BUTTON_HOME.id, + OverlayControl.BUTTON_CAPTURE.id, + OverlayControl.BUTTON_PLUS.id, + OverlayControl.BUTTON_MINUS.id -> 0.07f - Settings.PREF_BUTTON_L, - Settings.PREF_BUTTON_R, - Settings.PREF_BUTTON_ZL, - Settings.PREF_BUTTON_ZR -> 0.26f + OverlayControl.BUTTON_L.id, + OverlayControl.BUTTON_R.id, + OverlayControl.BUTTON_ZL.id, + OverlayControl.BUTTON_ZR.id -> 0.26f - Settings.PREF_BUTTON_STICK_L, - Settings.PREF_BUTTON_STICK_R -> 0.155f + OverlayControl.BUTTON_STICK_L.id, + OverlayControl.BUTTON_STICK_R.id -> 0.155f else -> 0.11f } - scale *= (sPrefs.getInt(Settings.PREF_CONTROL_SCALE, 50) + 50).toFloat() + scale *= (IntSetting.OVERLAY_SCALE.getInt() + 50).toFloat() scale /= 100f // Initialize the InputOverlayDrawableButton. @@ -1104,7 +871,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : defaultStateBitmap, pressedStateBitmap, buttonId, - prefId + overlayControlData ) // Get the minimum and maximum coordinates of the screen where the button can be placed. @@ -1113,12 +880,8 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : // The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay. // These were set in the input overlay configuration menu. - val xKey = "$prefId-X$layout" - val yKey = "$prefId-Y$layout" - val drawableXPercent = sPrefs.getFloat(xKey, 0f) - val drawableYPercent = sPrefs.getFloat(yKey, 0f) - val drawableX = (drawableXPercent * max.x + min.x).toInt() - val drawableY = (drawableYPercent * max.y + min.y).toInt() + val drawableX = (position.first * max.x + min.x).toInt() + val drawableY = (position.second * max.y + min.y).toInt() val width = overlayDrawable.width val height = overlayDrawable.height @@ -1136,8 +899,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : drawableX - (width / 2), drawableY - (height / 2) ) - val savedOpacity = preferences.getInt(Settings.PREF_CONTROL_OPACITY, 100) - overlayDrawable.setOpacity(savedOpacity * 255 / 100) + overlayDrawable.setOpacity(IntSetting.OVERLAY_OPACITY.getInt() * 255 / 100) return overlayDrawable } @@ -1149,7 +911,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : * @param defaultResId The [Bitmap] resource ID of the default state. * @param pressedOneDirectionResId The [Bitmap] resource ID of the pressed state in one direction. * @param pressedTwoDirectionsResId The [Bitmap] resource ID of the pressed state in two directions. - * @param layout The current screen layout as determined by [LANDSCAPE], [PORTRAIT], or [FOLDABLE]. + * @param position The position on screen as represented by an x and y value between 0 and 1. * @return The initialized [InputOverlayDrawableDpad] */ private fun initializeOverlayDpad( @@ -1158,17 +920,14 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : defaultResId: Int, pressedOneDirectionResId: Int, pressedTwoDirectionsResId: Int, - layout: String + position: Pair<Double, Double> ): InputOverlayDrawableDpad { // Resources handle for fetching the initial Drawable resource. val res = context.resources - // SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableDpad. - val sPrefs = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - // Decide scale based on button ID and user preference var scale = 0.25f - scale *= (sPrefs.getInt(Settings.PREF_CONTROL_SCALE, 50) + 50).toFloat() + scale *= (IntSetting.OVERLAY_SCALE.getInt() + 50).toFloat() scale /= 100f // Initialize the InputOverlayDrawableDpad. @@ -1195,10 +954,8 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : // The X and Y coordinates of the InputOverlayDrawableDpad on the InputOverlay. // These were set in the input overlay configuration menu. - val drawableXPercent = sPrefs.getFloat("${Settings.PREF_BUTTON_DPAD}-X$layout", 0f) - val drawableYPercent = sPrefs.getFloat("${Settings.PREF_BUTTON_DPAD}-Y$layout", 0f) - val drawableX = (drawableXPercent * max.x + min.x).toInt() - val drawableY = (drawableYPercent * max.y + min.y).toInt() + val drawableX = (position.first * max.x + min.x).toInt() + val drawableY = (position.second * max.y + min.y).toInt() val width = overlayDrawable.width val height = overlayDrawable.height @@ -1213,8 +970,7 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : // Need to set the image's position overlayDrawable.setPosition(drawableX - (width / 2), drawableY - (height / 2)) - val savedOpacity = preferences.getInt(Settings.PREF_CONTROL_OPACITY, 100) - overlayDrawable.setOpacity(savedOpacity * 255 / 100) + overlayDrawable.setOpacity(IntSetting.OVERLAY_OPACITY.getInt() * 255 / 100) return overlayDrawable } @@ -1227,9 +983,9 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : * @param defaultResInner Resource ID for the default inner image of the joystick (the one you actually move around). * @param pressedResInner Resource ID for the pressed inner image of the joystick. * @param joystick Identifier for which joystick this is. - * @param button Identifier for which joystick button this is. - * @param prefId Identifier for determining where a button appears on screen. - * @param layout The current screen layout as determined by [LANDSCAPE], [PORTRAIT], or [FOLDABLE]. + * @param buttonId Identifier for which joystick button this is. + * @param overlayControlData Identifier for determining where a button appears on screen. + * @param position The position on screen as represented by an x and y value between 0 and 1. * @return The initialized [InputOverlayDrawableJoystick]. */ private fun initializeOverlayJoystick( @@ -1239,19 +995,16 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : defaultResInner: Int, pressedResInner: Int, joystick: Int, - button: Int, - prefId: String, - layout: String + buttonId: Int, + overlayControlData: OverlayControlData, + position: Pair<Double, Double> ): InputOverlayDrawableJoystick { // Resources handle for fetching the initial Drawable resource. val res = context.resources - // SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableJoystick. - val sPrefs = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - // Decide scale based on user preference var scale = 0.3f - scale *= (sPrefs.getInt(Settings.PREF_CONTROL_SCALE, 50) + 50).toFloat() + scale *= (IntSetting.OVERLAY_SCALE.getInt() + 50).toFloat() scale /= 100f // Initialize the InputOverlayDrawableJoystick. @@ -1265,10 +1018,8 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : // The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay. // These were set in the input overlay configuration menu. - val drawableXPercent = sPrefs.getFloat("$prefId-X$layout", 0f) - val drawableYPercent = sPrefs.getFloat("$prefId-Y$layout", 0f) - val drawableX = (drawableXPercent * max.x + min.x).toInt() - val drawableY = (drawableYPercent * max.y + min.y).toInt() + val drawableX = (position.first * max.x + min.x).toInt() + val drawableY = (position.second * max.y + min.y).toInt() val outerScale = 1.66f // Now set the bounds for the InputOverlayDrawableJoystick. @@ -1292,14 +1043,13 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : outerRect, innerRect, joystick, - button, - prefId + buttonId, + overlayControlData.id ) // Need to set the image's position overlayDrawable.setPosition(drawableX, drawableY) - val savedOpacity = preferences.getInt(Settings.PREF_CONTROL_OPACITY, 100) - overlayDrawable.setOpacity(savedOpacity * 255 / 100) + overlayDrawable.setOpacity(IntSetting.OVERLAY_OPACITY.getInt() * 255 / 100) return overlayDrawable } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt index 2c28dda88..b14a4f96e 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt @@ -10,6 +10,7 @@ import android.graphics.Rect import android.graphics.drawable.BitmapDrawable import android.view.MotionEvent import org.yuzu.yuzu_emu.NativeLibrary.ButtonState +import org.yuzu.yuzu_emu.overlay.model.OverlayControlData /** * Custom [BitmapDrawable] that is capable @@ -25,7 +26,7 @@ class InputOverlayDrawableButton( defaultStateBitmap: Bitmap, pressedStateBitmap: Bitmap, val buttonId: Int, - val prefId: String + val overlayControlData: OverlayControlData ) { // The ID value what motion event is tracking var trackId: Int diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.kt index 518b1e783..113bf7c24 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.kt @@ -14,7 +14,7 @@ import kotlin.math.cos import kotlin.math.sin import kotlin.math.sqrt import org.yuzu.yuzu_emu.NativeLibrary -import org.yuzu.yuzu_emu.utils.EmulationMenuSettings +import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting /** * Custom [BitmapDrawable] that is capable @@ -125,7 +125,7 @@ class InputOverlayDrawableJoystick( pressedState = true outerBitmap.alpha = 0 boundsBoxBitmap.alpha = opacity - if (EmulationMenuSettings.joystickRelCenter) { + if (BooleanSetting.JOYSTICK_REL_CENTER.getBoolean()) { virtBounds.offset( xPosition - virtBounds.centerX(), yPosition - virtBounds.centerY() diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControl.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControl.kt new file mode 100644 index 000000000..a0eeadf4b --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControl.kt @@ -0,0 +1,188 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.overlay.model + +import androidx.annotation.IntegerRes +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.YuzuApplication + +enum class OverlayControl( + val id: String, + val defaultVisibility: Boolean, + @IntegerRes val defaultLandscapePositionResources: Pair<Int, Int>, + @IntegerRes val defaultPortraitPositionResources: Pair<Int, Int>, + @IntegerRes val defaultFoldablePositionResources: Pair<Int, Int> +) { + BUTTON_A( + "button_a", + true, + Pair(R.integer.BUTTON_A_X, R.integer.BUTTON_A_Y), + Pair(R.integer.BUTTON_A_X_PORTRAIT, R.integer.BUTTON_A_Y_PORTRAIT), + Pair(R.integer.BUTTON_A_X_FOLDABLE, R.integer.BUTTON_A_Y_FOLDABLE) + ), + BUTTON_B( + "button_b", + true, + Pair(R.integer.BUTTON_B_X, R.integer.BUTTON_B_Y), + Pair(R.integer.BUTTON_B_X_PORTRAIT, R.integer.BUTTON_B_Y_PORTRAIT), + Pair(R.integer.BUTTON_B_X_FOLDABLE, R.integer.BUTTON_B_Y_FOLDABLE) + ), + BUTTON_X( + "button_x", + true, + Pair(R.integer.BUTTON_X_X, R.integer.BUTTON_X_Y), + Pair(R.integer.BUTTON_X_X_PORTRAIT, R.integer.BUTTON_X_Y_PORTRAIT), + Pair(R.integer.BUTTON_X_X_FOLDABLE, R.integer.BUTTON_X_Y_FOLDABLE) + ), + BUTTON_Y( + "button_y", + true, + Pair(R.integer.BUTTON_Y_X, R.integer.BUTTON_Y_Y), + Pair(R.integer.BUTTON_Y_X_PORTRAIT, R.integer.BUTTON_Y_Y_PORTRAIT), + Pair(R.integer.BUTTON_Y_X_FOLDABLE, R.integer.BUTTON_Y_Y_FOLDABLE) + ), + BUTTON_PLUS( + "button_plus", + true, + Pair(R.integer.BUTTON_PLUS_X, R.integer.BUTTON_PLUS_Y), + Pair(R.integer.BUTTON_PLUS_X_PORTRAIT, R.integer.BUTTON_PLUS_Y_PORTRAIT), + Pair(R.integer.BUTTON_PLUS_X_FOLDABLE, R.integer.BUTTON_PLUS_Y_FOLDABLE) + ), + BUTTON_MINUS( + "button_minus", + true, + Pair(R.integer.BUTTON_MINUS_X, R.integer.BUTTON_MINUS_Y), + Pair(R.integer.BUTTON_MINUS_X_PORTRAIT, R.integer.BUTTON_MINUS_Y_PORTRAIT), + Pair(R.integer.BUTTON_MINUS_X_FOLDABLE, R.integer.BUTTON_MINUS_Y_FOLDABLE) + ), + BUTTON_HOME( + "button_home", + false, + Pair(R.integer.BUTTON_HOME_X, R.integer.BUTTON_HOME_Y), + Pair(R.integer.BUTTON_HOME_X_PORTRAIT, R.integer.BUTTON_HOME_Y_PORTRAIT), + Pair(R.integer.BUTTON_HOME_X_FOLDABLE, R.integer.BUTTON_HOME_Y_FOLDABLE) + ), + BUTTON_CAPTURE( + "button_capture", + false, + Pair(R.integer.BUTTON_CAPTURE_X, R.integer.BUTTON_CAPTURE_Y), + Pair(R.integer.BUTTON_CAPTURE_X_PORTRAIT, R.integer.BUTTON_CAPTURE_Y_PORTRAIT), + Pair(R.integer.BUTTON_CAPTURE_X_FOLDABLE, R.integer.BUTTON_CAPTURE_Y_FOLDABLE) + ), + BUTTON_L( + "button_l", + true, + Pair(R.integer.BUTTON_L_X, R.integer.BUTTON_L_Y), + Pair(R.integer.BUTTON_L_X_PORTRAIT, R.integer.BUTTON_L_Y_PORTRAIT), + Pair(R.integer.BUTTON_L_X_FOLDABLE, R.integer.BUTTON_L_Y_FOLDABLE) + ), + BUTTON_R( + "button_r", + true, + Pair(R.integer.BUTTON_R_X, R.integer.BUTTON_R_Y), + Pair(R.integer.BUTTON_R_X_PORTRAIT, R.integer.BUTTON_R_Y_PORTRAIT), + Pair(R.integer.BUTTON_R_X_FOLDABLE, R.integer.BUTTON_R_Y_FOLDABLE) + ), + BUTTON_ZL( + "button_zl", + true, + Pair(R.integer.BUTTON_ZL_X, R.integer.BUTTON_ZL_Y), + Pair(R.integer.BUTTON_ZL_X_PORTRAIT, R.integer.BUTTON_ZL_Y_PORTRAIT), + Pair(R.integer.BUTTON_ZL_X_FOLDABLE, R.integer.BUTTON_ZL_Y_FOLDABLE) + ), + BUTTON_ZR( + "button_zr", + true, + Pair(R.integer.BUTTON_ZR_X, R.integer.BUTTON_ZR_Y), + Pair(R.integer.BUTTON_ZR_X_PORTRAIT, R.integer.BUTTON_ZR_Y_PORTRAIT), + Pair(R.integer.BUTTON_ZR_X_FOLDABLE, R.integer.BUTTON_ZR_Y_FOLDABLE) + ), + BUTTON_STICK_L( + "button_stick_l", + true, + Pair(R.integer.BUTTON_STICK_L_X, R.integer.BUTTON_STICK_L_Y), + Pair(R.integer.BUTTON_STICK_L_X_PORTRAIT, R.integer.BUTTON_STICK_L_Y_PORTRAIT), + Pair(R.integer.BUTTON_STICK_L_X_FOLDABLE, R.integer.BUTTON_STICK_L_Y_FOLDABLE) + ), + BUTTON_STICK_R( + "button_stick_r", + true, + Pair(R.integer.BUTTON_STICK_R_X, R.integer.BUTTON_STICK_R_Y), + Pair(R.integer.BUTTON_STICK_R_X_PORTRAIT, R.integer.BUTTON_STICK_R_Y_PORTRAIT), + Pair(R.integer.BUTTON_STICK_R_X_FOLDABLE, R.integer.BUTTON_STICK_R_Y_FOLDABLE) + ), + STICK_L( + "stick_l", + true, + Pair(R.integer.STICK_L_X, R.integer.STICK_L_Y), + Pair(R.integer.STICK_L_X_PORTRAIT, R.integer.STICK_L_Y_PORTRAIT), + Pair(R.integer.STICK_L_X_FOLDABLE, R.integer.STICK_L_Y_FOLDABLE) + ), + STICK_R( + "stick_r", + true, + Pair(R.integer.STICK_R_X, R.integer.STICK_R_Y), + Pair(R.integer.STICK_R_X_PORTRAIT, R.integer.STICK_R_Y_PORTRAIT), + Pair(R.integer.STICK_R_X_FOLDABLE, R.integer.STICK_R_Y_FOLDABLE) + ), + COMBINED_DPAD( + "combined_dpad", + true, + Pair(R.integer.COMBINED_DPAD_X, R.integer.COMBINED_DPAD_Y), + Pair(R.integer.COMBINED_DPAD_X_PORTRAIT, R.integer.COMBINED_DPAD_Y_PORTRAIT), + Pair(R.integer.COMBINED_DPAD_X_FOLDABLE, R.integer.COMBINED_DPAD_Y_FOLDABLE) + ); + + fun getDefaultPositionForLayout(layout: OverlayLayout): Pair<Double, Double> { + val rawResourcePair: Pair<Int, Int> + YuzuApplication.appContext.resources.apply { + rawResourcePair = when (layout) { + OverlayLayout.Landscape -> { + Pair( + getInteger(this@OverlayControl.defaultLandscapePositionResources.first), + getInteger(this@OverlayControl.defaultLandscapePositionResources.second) + ) + } + + OverlayLayout.Portrait -> { + Pair( + getInteger(this@OverlayControl.defaultPortraitPositionResources.first), + getInteger(this@OverlayControl.defaultPortraitPositionResources.second) + ) + } + + OverlayLayout.Foldable -> { + Pair( + getInteger(this@OverlayControl.defaultFoldablePositionResources.first), + getInteger(this@OverlayControl.defaultFoldablePositionResources.second) + ) + } + } + } + + return Pair( + rawResourcePair.first.toDouble() / 1000, + rawResourcePair.second.toDouble() / 1000 + ) + } + + fun toOverlayControlData(): OverlayControlData = + OverlayControlData( + id, + defaultVisibility, + getDefaultPositionForLayout(OverlayLayout.Landscape), + getDefaultPositionForLayout(OverlayLayout.Portrait), + getDefaultPositionForLayout(OverlayLayout.Foldable) + ) + + companion object { + val map: HashMap<String, OverlayControl> by lazy { + val hashMap = hashMapOf<String, OverlayControl>() + entries.forEach { hashMap[it.id] = it } + hashMap + } + + fun from(id: String): OverlayControl? = map[id] + } +} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControlData.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControlData.kt new file mode 100644 index 000000000..26cfeb1db --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControlData.kt @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.overlay.model + +data class OverlayControlData( + val id: String, + var enabled: Boolean, + var landscapePosition: Pair<Double, Double>, + var portraitPosition: Pair<Double, Double>, + var foldablePosition: Pair<Double, Double> +) { + fun positionFromLayout(layout: OverlayLayout): Pair<Double, Double> = + when (layout) { + OverlayLayout.Landscape -> landscapePosition + OverlayLayout.Portrait -> portraitPosition + OverlayLayout.Foldable -> foldablePosition + } +} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControlDefault.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControlDefault.kt new file mode 100644 index 000000000..6bd74c82f --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayControlDefault.kt @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.overlay.model + +import androidx.annotation.IntegerRes + +data class OverlayControlDefault( + val buttonId: String, + @IntegerRes val landscapePositionResource: Pair<Int, Int>, + @IntegerRes val portraitPositionResource: Pair<Int, Int>, + @IntegerRes val foldablePositionResource: Pair<Int, Int> +) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayLayout.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayLayout.kt new file mode 100644 index 000000000..d728164e5 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/model/OverlayLayout.kt @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.overlay.model + +enum class OverlayLayout(val id: String) { + Landscape("Landscape"), + Portrait("Portrait"), + Foldable("Foldable") +} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt index 0197fd712..de0794a17 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt @@ -3,9 +3,17 @@ package org.yuzu.yuzu_emu.utils +import androidx.preference.PreferenceManager import java.io.IOException import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.YuzuApplication +import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting +import org.yuzu.yuzu_emu.features.settings.model.IntSetting +import org.yuzu.yuzu_emu.features.settings.model.Settings +import org.yuzu.yuzu_emu.overlay.model.OverlayControlData +import org.yuzu.yuzu_emu.overlay.model.OverlayControl +import org.yuzu.yuzu_emu.overlay.model.OverlayLayout +import org.yuzu.yuzu_emu.utils.PreferenceUtil.migratePreference object DirectoryInitialization { private var userPath: String? = null @@ -17,6 +25,7 @@ object DirectoryInitialization { initializeInternalStorage() NativeLibrary.initializeSystem(false) NativeConfig.initializeGlobalConfig() + migrateSettings() areDirectoriesReady = true } } @@ -35,4 +44,170 @@ object DirectoryInitialization { e.printStackTrace() } } + + private fun migrateSettings() { + val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) + var saveConfig = false + val theme = preferences.migratePreference<Int>(Settings.PREF_THEME) + if (theme != null) { + IntSetting.THEME.setInt(theme) + saveConfig = true + } + + val themeMode = preferences.migratePreference<Int>(Settings.PREF_THEME_MODE) + if (themeMode != null) { + IntSetting.THEME_MODE.setInt(themeMode) + saveConfig = true + } + + val blackBackgrounds = + preferences.migratePreference<Boolean>(Settings.PREF_BLACK_BACKGROUNDS) + if (blackBackgrounds != null) { + BooleanSetting.BLACK_BACKGROUNDS.setBoolean(blackBackgrounds) + saveConfig = true + } + + val joystickRelCenter = + preferences.migratePreference<Boolean>(Settings.PREF_MENU_SETTINGS_JOYSTICK_REL_CENTER) + if (joystickRelCenter != null) { + BooleanSetting.JOYSTICK_REL_CENTER.setBoolean(joystickRelCenter) + saveConfig = true + } + + val dpadSlide = + preferences.migratePreference<Boolean>(Settings.PREF_MENU_SETTINGS_DPAD_SLIDE) + if (dpadSlide != null) { + BooleanSetting.DPAD_SLIDE.setBoolean(dpadSlide) + saveConfig = true + } + + val hapticFeedback = + preferences.migratePreference<Boolean>(Settings.PREF_MENU_SETTINGS_HAPTICS) + if (hapticFeedback != null) { + BooleanSetting.HAPTIC_FEEDBACK.setBoolean(hapticFeedback) + saveConfig = true + } + + val showPerformanceOverlay = + preferences.migratePreference<Boolean>(Settings.PREF_MENU_SETTINGS_SHOW_FPS) + if (showPerformanceOverlay != null) { + BooleanSetting.SHOW_PERFORMANCE_OVERLAY.setBoolean(showPerformanceOverlay) + saveConfig = true + } + + val showInputOverlay = + preferences.migratePreference<Boolean>(Settings.PREF_MENU_SETTINGS_SHOW_OVERLAY) + if (showInputOverlay != null) { + BooleanSetting.SHOW_INPUT_OVERLAY.setBoolean(showInputOverlay) + saveConfig = true + } + + val overlayOpacity = preferences.migratePreference<Int>(Settings.PREF_CONTROL_OPACITY) + if (overlayOpacity != null) { + IntSetting.OVERLAY_OPACITY.setInt(overlayOpacity) + saveConfig = true + } + + val overlayScale = preferences.migratePreference<Int>(Settings.PREF_CONTROL_SCALE) + if (overlayScale != null) { + IntSetting.OVERLAY_SCALE.setInt(overlayScale) + saveConfig = true + } + + var setOverlayData = false + val overlayControlData = NativeConfig.getOverlayControlData() + if (overlayControlData.isEmpty()) { + val overlayControlDataMap = + NativeConfig.getOverlayControlData().associateBy { it.id }.toMutableMap() + for (button in Settings.overlayPreferences) { + val buttonId = convertButtonId(button) + var buttonEnabled = preferences.migratePreference<Boolean>(button) + if (buttonEnabled == null) { + buttonEnabled = OverlayControl.map[buttonId]?.defaultVisibility == true + } + + var landscapeXPosition = preferences.migratePreference<Float>( + "$button-X${Settings.PREF_LANDSCAPE_SUFFIX}" + )?.toDouble() + var landscapeYPosition = preferences.migratePreference<Float>( + "$button-Y${Settings.PREF_LANDSCAPE_SUFFIX}" + )?.toDouble() + if (landscapeXPosition == null || landscapeYPosition == null) { + val landscapePosition = OverlayControl.map[buttonId] + ?.getDefaultPositionForLayout(OverlayLayout.Landscape) ?: Pair(0.0, 0.0) + landscapeXPosition = landscapePosition.first + landscapeYPosition = landscapePosition.second + } + + var portraitXPosition = preferences.migratePreference<Float>( + "$button-X${Settings.PREF_PORTRAIT_SUFFIX}" + )?.toDouble() + var portraitYPosition = preferences.migratePreference<Float>( + "$button-Y${Settings.PREF_PORTRAIT_SUFFIX}" + )?.toDouble() + if (portraitXPosition == null || portraitYPosition == null) { + val portraitPosition = OverlayControl.map[buttonId] + ?.getDefaultPositionForLayout(OverlayLayout.Portrait) ?: Pair(0.0, 0.0) + portraitXPosition = portraitPosition.first + portraitYPosition = portraitPosition.second + } + + var foldableXPosition = preferences.migratePreference<Float>( + "$button-X${Settings.PREF_FOLDABLE_SUFFIX}" + )?.toDouble() + var foldableYPosition = preferences.migratePreference<Float>( + "$button-Y${Settings.PREF_FOLDABLE_SUFFIX}" + )?.toDouble() + if (foldableXPosition == null || foldableYPosition == null) { + val foldablePosition = OverlayControl.map[buttonId] + ?.getDefaultPositionForLayout(OverlayLayout.Foldable) ?: Pair(0.0, 0.0) + foldableXPosition = foldablePosition.first + foldableYPosition = foldablePosition.second + } + + val controlData = OverlayControlData( + buttonId, + buttonEnabled, + Pair(landscapeXPosition, landscapeYPosition), + Pair(portraitXPosition, portraitYPosition), + Pair(foldableXPosition, foldableYPosition) + ) + overlayControlDataMap[buttonId] = controlData + setOverlayData = true + } + + if (setOverlayData) { + NativeConfig.setOverlayControlData( + overlayControlDataMap.map { it.value }.toTypedArray() + ) + saveConfig = true + } + } + + if (saveConfig) { + NativeConfig.saveGlobalConfig() + } + } + + private fun convertButtonId(buttonId: String): String = + when (buttonId) { + Settings.PREF_BUTTON_A -> OverlayControl.BUTTON_A.id + Settings.PREF_BUTTON_B -> OverlayControl.BUTTON_B.id + Settings.PREF_BUTTON_X -> OverlayControl.BUTTON_X.id + Settings.PREF_BUTTON_Y -> OverlayControl.BUTTON_Y.id + Settings.PREF_BUTTON_L -> OverlayControl.BUTTON_L.id + Settings.PREF_BUTTON_R -> OverlayControl.BUTTON_R.id + Settings.PREF_BUTTON_ZL -> OverlayControl.BUTTON_ZL.id + Settings.PREF_BUTTON_ZR -> OverlayControl.BUTTON_ZR.id + Settings.PREF_BUTTON_PLUS -> OverlayControl.BUTTON_PLUS.id + Settings.PREF_BUTTON_MINUS -> OverlayControl.BUTTON_MINUS.id + Settings.PREF_BUTTON_DPAD -> OverlayControl.COMBINED_DPAD.id + Settings.PREF_STICK_L -> OverlayControl.STICK_L.id + Settings.PREF_STICK_R -> OverlayControl.STICK_R.id + Settings.PREF_BUTTON_HOME -> OverlayControl.BUTTON_HOME.id + Settings.PREF_BUTTON_SCREENSHOT -> OverlayControl.BUTTON_CAPTURE.id + Settings.PREF_BUTTON_STICK_L -> OverlayControl.BUTTON_STICK_L.id + Settings.PREF_BUTTON_STICK_R -> OverlayControl.BUTTON_STICK_R.id + else -> "" + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/EmulationMenuSettings.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/EmulationMenuSettings.kt deleted file mode 100644 index 7e8f058c1..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/EmulationMenuSettings.kt +++ /dev/null @@ -1,50 +0,0 @@ -// SPDX-FileCopyrightText: 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.yuzu.yuzu_emu.utils - -import androidx.preference.PreferenceManager -import org.yuzu.yuzu_emu.YuzuApplication -import org.yuzu.yuzu_emu.features.settings.model.Settings - -object EmulationMenuSettings { - private val preferences = - PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) - - var joystickRelCenter: Boolean - get() = preferences.getBoolean(Settings.PREF_MENU_SETTINGS_JOYSTICK_REL_CENTER, true) - set(value) { - preferences.edit() - .putBoolean(Settings.PREF_MENU_SETTINGS_JOYSTICK_REL_CENTER, value) - .apply() - } - var dpadSlide: Boolean - get() = preferences.getBoolean(Settings.PREF_MENU_SETTINGS_DPAD_SLIDE, true) - set(value) { - preferences.edit() - .putBoolean(Settings.PREF_MENU_SETTINGS_DPAD_SLIDE, value) - .apply() - } - var hapticFeedback: Boolean - get() = preferences.getBoolean(Settings.PREF_MENU_SETTINGS_HAPTICS, false) - set(value) { - preferences.edit() - .putBoolean(Settings.PREF_MENU_SETTINGS_HAPTICS, value) - .apply() - } - - var showFps: Boolean - get() = preferences.getBoolean(Settings.PREF_MENU_SETTINGS_SHOW_FPS, false) - set(value) { - preferences.edit() - .putBoolean(Settings.PREF_MENU_SETTINGS_SHOW_FPS, value) - .apply() - } - var showOverlay: Boolean - get() = preferences.getBoolean(Settings.PREF_MENU_SETTINGS_SHOW_OVERLAY, true) - set(value) { - preferences.edit() - .putBoolean(Settings.PREF_MENU_SETTINGS_SHOW_OVERLAY, value) - .apply() - } -} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/NativeConfig.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/NativeConfig.kt index 7512d5eed..a4c14b3a7 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/NativeConfig.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/NativeConfig.kt @@ -4,6 +4,7 @@ package org.yuzu.yuzu_emu.utils import org.yuzu.yuzu_emu.model.GameDir +import org.yuzu.yuzu_emu.overlay.model.OverlayControlData object NativeConfig { /** @@ -150,4 +151,21 @@ object NativeConfig { */ @Synchronized external fun setDisabledAddons(programId: String, disabledAddons: Array<String>) + + /** + * Gets an array of [OverlayControlData] from settings + * + * @return An array of [OverlayControlData] + */ + @Synchronized + external fun getOverlayControlData(): Array<OverlayControlData> + + /** + * Clears the AndroidSettings::values.overlay_control_data array and replaces its values + * with [overlayControlData] + * + * @param overlayControlData Replacement array of [OverlayControlData] + */ + @Synchronized + external fun setOverlayControlData(overlayControlData: Array<OverlayControlData>) } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/PreferenceUtil.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/PreferenceUtil.kt new file mode 100644 index 000000000..a233ba25c --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/PreferenceUtil.kt @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.utils + +import android.content.SharedPreferences + +object PreferenceUtil { + /** + * Retrieves a shared preference value and then deletes the value in storage. + * @param key Associated key for the value in this preferences instance + * @return Typed value associated with [key]. Null if no such key exists. + */ + inline fun <reified T> SharedPreferences.migratePreference(key: String): T? { + if (!this.contains(key)) { + return null + } + + val value: Any = when (T::class) { + String::class -> this.getString(key, "")!! + + Boolean::class -> this.getBoolean(key, false) + + Int::class -> this.getInt(key, 0) + + Float::class -> this.getFloat(key, 0f) + + Long::class -> this.getLong(key, 0) + + else -> throw IllegalStateException("Tried to migrate preference with invalid type!") + } + deletePreference(key) + return value as T + } + + fun SharedPreferences.deletePreference(key: String) = this.edit().remove(key).apply() +} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ThemeHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ThemeHelper.kt index f312e24cf..6f7f40e43 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ThemeHelper.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ThemeHelper.kt @@ -5,38 +5,38 @@ package org.yuzu.yuzu_emu.utils import android.content.res.Configuration import android.graphics.Color +import android.os.Build import androidx.annotation.ColorInt import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsControllerCompat -import androidx.preference.PreferenceManager import kotlin.math.roundToInt import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.YuzuApplication -import org.yuzu.yuzu_emu.features.settings.model.Settings +import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting +import org.yuzu.yuzu_emu.features.settings.model.IntSetting import org.yuzu.yuzu_emu.ui.main.ThemeProvider object ThemeHelper { const val SYSTEM_BAR_ALPHA = 0.9f - private const val DEFAULT = 0 - private const val MATERIAL_YOU = 1 - fun setTheme(activity: AppCompatActivity) { - val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) setThemeMode(activity) - when (preferences.getInt(Settings.PREF_THEME, 0)) { - DEFAULT -> activity.setTheme(R.style.Theme_Yuzu_Main) - MATERIAL_YOU -> activity.setTheme(R.style.Theme_Yuzu_Main_MaterialYou) + when (Theme.from(IntSetting.THEME.getInt())) { + Theme.Default -> activity.setTheme(R.style.Theme_Yuzu_Main) + Theme.MaterialYou -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + activity.setTheme(R.style.Theme_Yuzu_Main_MaterialYou) + } else { + activity.setTheme(R.style.Theme_Yuzu_Main) + } + } } // Using a specific night mode check because this could apply incorrectly when using the // light app mode, dark system mode, and black backgrounds. Launching the settings activity // will then show light mode colors/navigation bars but with black backgrounds. - if (preferences.getBoolean(Settings.PREF_BLACK_BACKGROUNDS, false) && - isNightMode(activity) - ) { + if (BooleanSetting.BLACK_BACKGROUNDS.getBoolean() && isNightMode(activity)) { activity.setTheme(R.style.ThemeOverlay_Yuzu_Dark) } } @@ -60,8 +60,7 @@ object ThemeHelper { } fun setThemeMode(activity: AppCompatActivity) { - val themeMode = PreferenceManager.getDefaultSharedPreferences(activity.applicationContext) - .getInt(Settings.PREF_THEME_MODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) + val themeMode = IntSetting.THEME_MODE.getInt() activity.delegate.localNightMode = themeMode val windowController = WindowCompat.getInsetsController( activity.window, @@ -95,3 +94,12 @@ object ThemeHelper { windowController.isAppearanceLightNavigationBars = false } } + +enum class Theme(val int: Int) { + Default(0), + MaterialYou(1); + + companion object { + fun from(int: Int): Theme = entries.firstOrNull { it.int == int } ?: Default + } +} 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 index 52d8ecfeb..1e884ffdd 100644 --- a/src/android/app/src/main/jni/android_common/android_common.cpp +++ b/src/android/app/src/main/jni/android_common/android_common.cpp @@ -9,6 +9,7 @@ #include <jni.h> #include "common/string_util.h" +#include "jni/id_cache.h" std::string GetJString(JNIEnv* env, jstring jstr) { if (!jstr) { @@ -33,3 +34,11 @@ jstring ToJString(JNIEnv* env, std::string_view str) { 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); +} 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 index ccb0c06f7..8eb803e1b 100644 --- a/src/android/app/src/main/jni/android_common/android_common.h +++ b/src/android/app/src/main/jni/android_common/android_common.h @@ -10,3 +10,6 @@ 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); diff --git a/src/android/app/src/main/jni/android_config.cpp b/src/android/app/src/main/jni/android_config.cpp index 9c3a5a9b2..c86aa1c39 100644 --- a/src/android/app/src/main/jni/android_config.cpp +++ b/src/android/app/src/main/jni/android_config.cpp @@ -35,6 +35,7 @@ void AndroidConfig::ReadAndroidValues() { if (global) { ReadAndroidUIValues(); ReadUIValues(); + ReadOverlayValues(); } ReadDriverValues(); } @@ -81,10 +82,42 @@ void AndroidConfig::ReadDriverValues() { EndGroup(); } +void AndroidConfig::ReadOverlayValues() { + BeginGroup(Settings::TranslateCategory(Settings::Category::Overlay)); + + ReadCategory(Settings::Category::Overlay); + + AndroidSettings::values.overlay_control_data.clear(); + const int control_data_size = BeginArray("control_data"); + for (int i = 0; i < control_data_size; ++i) { + SetArrayIndex(i); + AndroidSettings::OverlayControlData control_data; + control_data.id = ReadStringSetting(std::string("id")); + control_data.enabled = ReadBooleanSetting(std::string("enabled")); + control_data.landscape_position.first = + ReadDoubleSetting(std::string("landscape\\x_position")); + control_data.landscape_position.second = + ReadDoubleSetting(std::string("landscape\\y_position")); + control_data.portrait_position.first = + ReadDoubleSetting(std::string("portrait\\x_position")); + control_data.portrait_position.second = + ReadDoubleSetting(std::string("portrait\\y_position")); + control_data.foldable_position.first = + ReadDoubleSetting(std::string("foldable\\x_position")); + control_data.foldable_position.second = + ReadDoubleSetting(std::string("foldable\\y_position")); + AndroidSettings::values.overlay_control_data.push_back(control_data); + } + EndArray(); + + EndGroup(); +} + void AndroidConfig::SaveAndroidValues() { if (global) { SaveAndroidUIValues(); SaveUIValues(); + SaveOverlayValues(); } SaveDriverValues(); @@ -114,8 +147,9 @@ void AndroidConfig::SavePathValues() { for (size_t i = 0; i < AndroidSettings::values.game_dirs.size(); ++i) { SetArrayIndex(i); const auto& game_dir = AndroidSettings::values.game_dirs[i]; - WriteSetting(std::string("path"), game_dir.path); - WriteSetting(std::string("deep_scan"), game_dir.deep_scan, std::make_optional(false)); + WriteStringSetting(std::string("path"), game_dir.path); + WriteBooleanSetting(std::string("deep_scan"), game_dir.deep_scan, + std::make_optional(false)); } EndArray(); @@ -130,6 +164,35 @@ void AndroidConfig::SaveDriverValues() { EndGroup(); } +void AndroidConfig::SaveOverlayValues() { + BeginGroup(Settings::TranslateCategory(Settings::Category::Overlay)); + + WriteCategory(Settings::Category::Overlay); + + BeginArray("control_data"); + for (size_t i = 0; i < AndroidSettings::values.overlay_control_data.size(); ++i) { + SetArrayIndex(i); + const auto& control_data = AndroidSettings::values.overlay_control_data[i]; + WriteStringSetting(std::string("id"), control_data.id); + WriteBooleanSetting(std::string("enabled"), control_data.enabled); + WriteDoubleSetting(std::string("landscape\\x_position"), + control_data.landscape_position.first); + WriteDoubleSetting(std::string("landscape\\y_position"), + control_data.landscape_position.second); + WriteDoubleSetting(std::string("portrait\\x_position"), + control_data.portrait_position.first); + WriteDoubleSetting(std::string("portrait\\y_position"), + control_data.portrait_position.second); + WriteDoubleSetting(std::string("foldable\\x_position"), + control_data.foldable_position.first); + WriteDoubleSetting(std::string("foldable\\y_position"), + control_data.foldable_position.second); + } + EndArray(); + + EndGroup(); +} + std::vector<Settings::BasicSetting*>& AndroidConfig::FindRelevantList(Settings::Category category) { auto& map = Settings::values.linkage.by_category; if (map.contains(category)) { diff --git a/src/android/app/src/main/jni/android_config.h b/src/android/app/src/main/jni/android_config.h index 2c12874e1..d83852de9 100644 --- a/src/android/app/src/main/jni/android_config.h +++ b/src/android/app/src/main/jni/android_config.h @@ -18,6 +18,7 @@ protected: void ReadAndroidValues(); void ReadAndroidUIValues(); void ReadDriverValues(); + void ReadOverlayValues(); void ReadHidbusValues() override {} void ReadDebugControlValues() override {} void ReadPathValues() override; @@ -30,6 +31,7 @@ protected: void SaveAndroidValues(); void SaveAndroidUIValues(); void SaveDriverValues(); + void SaveOverlayValues(); void SaveHidbusValues() override {} void SaveDebugControlValues() override {} void SavePathValues() override; diff --git a/src/android/app/src/main/jni/android_settings.h b/src/android/app/src/main/jni/android_settings.h index 3733f5a3c..559ae83eb 100644 --- a/src/android/app/src/main/jni/android_settings.h +++ b/src/android/app/src/main/jni/android_settings.h @@ -14,6 +14,14 @@ struct GameDir { bool deep_scan = false; }; +struct OverlayControlData { + std::string id; + bool enabled; + std::pair<double, double> landscape_position; + std::pair<double, double> portrait_position; + std::pair<double, double> foldable_position; +}; + struct Values { Settings::Linkage linkage; @@ -33,6 +41,28 @@ struct Values { Settings::SwitchableSetting<std::string, false> driver_path{linkage, "", "driver_path", Settings::Category::GpuDriver}; + + Settings::Setting<s32> theme{linkage, 0, "theme", Settings::Category::Android}; + Settings::Setting<s32> theme_mode{linkage, -1, "theme_mode", Settings::Category::Android}; + Settings::Setting<bool> black_backgrounds{linkage, false, "black_backgrounds", + Settings::Category::Android}; + + // Input/performance overlay settings + std::vector<OverlayControlData> overlay_control_data; + Settings::Setting<s32> overlay_scale{linkage, 50, "control_scale", Settings::Category::Overlay}; + Settings::Setting<s32> overlay_opacity{linkage, 100, "control_opacity", + Settings::Category::Overlay}; + + Settings::Setting<bool> joystick_rel_center{linkage, true, "joystick_rel_center", + Settings::Category::Overlay}; + Settings::Setting<bool> dpad_slide{linkage, true, "dpad_slide", Settings::Category::Overlay}; + Settings::Setting<bool> haptic_feedback{linkage, true, "haptic_feedback", + Settings::Category::Overlay}; + Settings::Setting<bool> show_performance_overlay{linkage, true, "show_performance_overlay", + Settings::Category::Overlay}; + Settings::Setting<bool> show_input_overlay{linkage, true, "show_input_overlay", + Settings::Category::Overlay}; + Settings::Setting<bool> touchscreen{linkage, true, "touchscreen", Settings::Category::Overlay}; }; extern Values values; diff --git a/src/android/app/src/main/jni/id_cache.cpp b/src/android/app/src/main/jni/id_cache.cpp index e7a86d3fd..c79ad7d76 100644 --- a/src/android/app/src/main/jni/id_cache.cpp +++ b/src/android/app/src/main/jni/id_cache.cpp @@ -35,6 +35,18 @@ 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_double_class; +static jmethodID s_double_constructor; +static jfieldID s_double_value_field; + static constexpr jint JNI_VERSION = JNI_VERSION_1_6; namespace IDCache { @@ -146,6 +158,46 @@ 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 GetDoubleClass() { + return s_double_class; +} + +jmethodID GetDoubleConstructor() { + return s_double_constructor; +} + +jfieldID GetDoubleValueField() { + return s_double_value_field; +} + } // namespace IDCache #ifdef __cplusplus @@ -207,6 +259,31 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { 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<jclass>(env->NewGlobalRef(overlay_control_data_class)); + s_overlay_control_data_constructor = + env->GetMethodID(overlay_control_data_class, "<init>", + "(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 double_class = env->FindClass("java/lang/Double"); + s_double_class = reinterpret_cast<jclass>(env->NewGlobalRef(double_class)); + s_double_constructor = env->GetMethodID(double_class, "<init>", "(D)V"); + s_double_value_field = env->GetFieldID(double_class, "value", "D"); + env->DeleteLocalRef(double_class); + // Initialize Android Storage Common::FS::Android::RegisterCallbacks(env, s_native_library_class); @@ -231,6 +308,8 @@ void JNI_OnUnload(JavaVM* vm, void* reserved) { 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_double_class); // UnInitialize applets SoftwareKeyboard::CleanupJNI(env); diff --git a/src/android/app/src/main/jni/id_cache.h b/src/android/app/src/main/jni/id_cache.h index 24030be42..784d1412f 100644 --- a/src/android/app/src/main/jni/id_cache.h +++ b/src/android/app/src/main/jni/id_cache.h @@ -35,4 +35,16 @@ jmethodID GetPairConstructor(); jfieldID GetPairFirstField(); jfieldID GetPairSecondField(); +jclass GetOverlayControlDataClass(); +jmethodID GetOverlayControlDataConstructor(); +jfieldID GetOverlayControlDataIdField(); +jfieldID GetOverlayControlDataEnabledField(); +jfieldID GetOverlayControlDataLandscapePositionField(); +jfieldID GetOverlayControlDataPortraitPositionField(); +jfieldID GetOverlayControlDataFoldablePositionField(); + +jclass GetDoubleClass(); +jmethodID GetDoubleConstructor(); +jfieldID GetDoubleValueField(); + } // namespace IDCache diff --git a/src/android/app/src/main/jni/native_config.cpp b/src/android/app/src/main/jni/native_config.cpp index 324d9e9cd..535902483 100644 --- a/src/android/app/src/main/jni/native_config.cpp +++ b/src/android/app/src/main/jni/native_config.cpp @@ -344,4 +344,74 @@ void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setDisabledAddons(JNIEnv* env, j Settings::values.disabled_addons[program_id] = disabled_addons; } +jobjectArray Java_org_yuzu_yuzu_1emu_utils_NativeConfig_getOverlayControlData(JNIEnv* env, + jobject obj) { + jobjectArray joverlayControlDataArray = + env->NewObjectArray(AndroidSettings::values.overlay_control_data.size(), + IDCache::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)); + jobject jportraitPosition = + env->NewObject(IDCache::GetPairClass(), IDCache::GetPairConstructor(), + ToJDouble(env, control_data.portrait_position.first), + 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->SetObjectArrayElement(joverlayControlDataArray, i, jcontrolData); + } + return joverlayControlDataArray; +} + +void Java_org_yuzu_yuzu_1emu_utils_NativeConfig_setOverlayControlData( + JNIEnv* env, jobject obj, jobjectArray joverlayControlDataArray) { + AndroidSettings::values.overlay_control_data.clear(); + int size = env->GetArrayLength(joverlayControlDataArray); + + if (size == 0) { + return; + } + + for (int i = 0; i < size; ++i) { + jobject joverlayControlData = env->GetObjectArrayElement(joverlayControlDataArray, i); + jstring jidString = static_cast<jstring>( + env->GetObjectField(joverlayControlData, IDCache::GetOverlayControlDataIdField())); + bool enabled = static_cast<bool>(env->GetBooleanField( + joverlayControlData, IDCache::GetOverlayControlDataEnabledField())); + + jobject jlandscapePosition = env->GetObjectField( + joverlayControlData, IDCache::GetOverlayControlDataLandscapePositionField()); + std::pair<double, double> landscape_position = std::make_pair( + GetJDouble(env, env->GetObjectField(jlandscapePosition, IDCache::GetPairFirstField())), + GetJDouble(env, + env->GetObjectField(jlandscapePosition, IDCache::GetPairSecondField()))); + + jobject jportraitPosition = env->GetObjectField( + joverlayControlData, IDCache::GetOverlayControlDataPortraitPositionField()); + std::pair<double, double> portrait_position = std::make_pair( + GetJDouble(env, env->GetObjectField(jportraitPosition, IDCache::GetPairFirstField())), + GetJDouble(env, env->GetObjectField(jportraitPosition, IDCache::GetPairSecondField()))); + + jobject jfoldablePosition = env->GetObjectField( + joverlayControlData, IDCache::GetOverlayControlDataFoldablePositionField()); + std::pair<double, double> foldable_position = std::make_pair( + GetJDouble(env, env->GetObjectField(jfoldablePosition, IDCache::GetPairFirstField())), + GetJDouble(env, env->GetObjectField(jfoldablePosition, IDCache::GetPairSecondField()))); + + AndroidSettings::values.overlay_control_data.push_back(AndroidSettings::OverlayControlData{ + GetJString(env, jidString), enabled, landscape_position, portrait_position, + foldable_position}); + } +} + } // extern "C" diff --git a/src/android/app/src/main/res/menu/menu_overlay_options.xml b/src/android/app/src/main/res/menu/menu_overlay_options.xml index 4885b4f6f..363781652 100644 --- a/src/android/app/src/main/res/menu/menu_overlay_options.xml +++ b/src/android/app/src/main/res/menu/menu_overlay_options.xml @@ -39,6 +39,11 @@ android:checkable="true" /> <item + android:id="@+id/menu_touchscreen" + android:title="@string/touchscreen" + android:checkable="true" /> + + <item android:id="@+id/menu_reset_overlay" android:title="@string/emulation_touch_overlay_reset" /> diff --git a/src/android/app/src/main/res/values/arrays.xml b/src/android/app/src/main/res/values/arrays.xml index c882a8e62..45d57c3ea 100644 --- a/src/android/app/src/main/res/values/arrays.xml +++ b/src/android/app/src/main/res/values/arrays.xml @@ -212,19 +212,19 @@ <item>B</item> <item>X</item> <item>Y</item> + <item>+</item> + <item>-</item> + <item>@string/gamepad_home</item> + <item>@string/gamepad_screenshot</item> <item>L</item> <item>R</item> <item>ZL</item> <item>ZR</item> - <item>+</item> - <item>-</item> - <item>@string/gamepad_d_pad</item> <item>@string/gamepad_left_stick</item> <item>@string/gamepad_right_stick</item> <item>L3</item> <item>R3</item> - <item>@string/gamepad_home</item> - <item>@string/gamepad_screenshot</item> + <item>@string/gamepad_d_pad</item> </string-array> <string-array name="themeEntries"> diff --git a/src/android/app/src/main/res/values/integers.xml b/src/android/app/src/main/res/values/integers.xml index dc527965c..1c6f5db93 100644 --- a/src/android/app/src/main/res/values/integers.xml +++ b/src/android/app/src/main/res/values/integers.xml @@ -3,111 +3,111 @@ <integer name="grid_columns">1</integer> <!-- Default SWITCH landscape layout --> - <integer name="SWITCH_BUTTON_A_X">760</integer> - <integer name="SWITCH_BUTTON_A_Y">790</integer> - <integer name="SWITCH_BUTTON_B_X">710</integer> - <integer name="SWITCH_BUTTON_B_Y">900</integer> - <integer name="SWITCH_BUTTON_X_X">710</integer> - <integer name="SWITCH_BUTTON_X_Y">680</integer> - <integer name="SWITCH_BUTTON_Y_X">660</integer> - <integer name="SWITCH_BUTTON_Y_Y">790</integer> - <integer name="SWITCH_STICK_L_X">100</integer> - <integer name="SWITCH_STICK_L_Y">670</integer> - <integer name="SWITCH_STICK_R_X">900</integer> - <integer name="SWITCH_STICK_R_Y">670</integer> - <integer name="SWITCH_TRIGGER_L_X">70</integer> - <integer name="SWITCH_TRIGGER_L_Y">220</integer> - <integer name="SWITCH_TRIGGER_R_X">930</integer> - <integer name="SWITCH_TRIGGER_R_Y">220</integer> - <integer name="SWITCH_TRIGGER_ZL_X">70</integer> - <integer name="SWITCH_TRIGGER_ZL_Y">90</integer> - <integer name="SWITCH_TRIGGER_ZR_X">930</integer> - <integer name="SWITCH_TRIGGER_ZR_Y">90</integer> - <integer name="SWITCH_BUTTON_MINUS_X">460</integer> - <integer name="SWITCH_BUTTON_MINUS_Y">950</integer> - <integer name="SWITCH_BUTTON_PLUS_X">540</integer> - <integer name="SWITCH_BUTTON_PLUS_Y">950</integer> - <integer name="SWITCH_BUTTON_HOME_X">600</integer> - <integer name="SWITCH_BUTTON_HOME_Y">950</integer> - <integer name="SWITCH_BUTTON_CAPTURE_X">400</integer> - <integer name="SWITCH_BUTTON_CAPTURE_Y">950</integer> - <integer name="SWITCH_BUTTON_DPAD_X">260</integer> - <integer name="SWITCH_BUTTON_DPAD_Y">790</integer> - <integer name="SWITCH_BUTTON_STICK_L_X">870</integer> - <integer name="SWITCH_BUTTON_STICK_L_Y">400</integer> - <integer name="SWITCH_BUTTON_STICK_R_X">960</integer> - <integer name="SWITCH_BUTTON_STICK_R_Y">430</integer> + <integer name="BUTTON_A_X">760</integer> + <integer name="BUTTON_A_Y">790</integer> + <integer name="BUTTON_B_X">710</integer> + <integer name="BUTTON_B_Y">900</integer> + <integer name="BUTTON_X_X">710</integer> + <integer name="BUTTON_X_Y">680</integer> + <integer name="BUTTON_Y_X">660</integer> + <integer name="BUTTON_Y_Y">790</integer> + <integer name="BUTTON_PLUS_X">540</integer> + <integer name="BUTTON_PLUS_Y">950</integer> + <integer name="BUTTON_MINUS_X">460</integer> + <integer name="BUTTON_MINUS_Y">950</integer> + <integer name="BUTTON_HOME_X">600</integer> + <integer name="BUTTON_HOME_Y">950</integer> + <integer name="BUTTON_CAPTURE_X">400</integer> + <integer name="BUTTON_CAPTURE_Y">950</integer> + <integer name="BUTTON_L_X">70</integer> + <integer name="BUTTON_L_Y">220</integer> + <integer name="BUTTON_R_X">930</integer> + <integer name="BUTTON_R_Y">220</integer> + <integer name="BUTTON_ZL_X">70</integer> + <integer name="BUTTON_ZL_Y">90</integer> + <integer name="BUTTON_ZR_X">930</integer> + <integer name="BUTTON_ZR_Y">90</integer> + <integer name="BUTTON_STICK_L_X">870</integer> + <integer name="BUTTON_STICK_L_Y">400</integer> + <integer name="BUTTON_STICK_R_X">960</integer> + <integer name="BUTTON_STICK_R_Y">430</integer> + <integer name="STICK_L_X">100</integer> + <integer name="STICK_L_Y">670</integer> + <integer name="STICK_R_X">900</integer> + <integer name="STICK_R_Y">670</integer> + <integer name="COMBINED_DPAD_X">260</integer> + <integer name="COMBINED_DPAD_Y">790</integer> <!-- Default SWITCH portrait layout --> - <integer name="SWITCH_BUTTON_A_X_PORTRAIT">840</integer> - <integer name="SWITCH_BUTTON_A_Y_PORTRAIT">840</integer> - <integer name="SWITCH_BUTTON_B_X_PORTRAIT">740</integer> - <integer name="SWITCH_BUTTON_B_Y_PORTRAIT">880</integer> - <integer name="SWITCH_BUTTON_X_X_PORTRAIT">740</integer> - <integer name="SWITCH_BUTTON_X_Y_PORTRAIT">800</integer> - <integer name="SWITCH_BUTTON_Y_X_PORTRAIT">640</integer> - <integer name="SWITCH_BUTTON_Y_Y_PORTRAIT">840</integer> - <integer name="SWITCH_STICK_L_X_PORTRAIT">180</integer> - <integer name="SWITCH_STICK_L_Y_PORTRAIT">660</integer> - <integer name="SWITCH_STICK_R_X_PORTRAIT">820</integer> - <integer name="SWITCH_STICK_R_Y_PORTRAIT">660</integer> - <integer name="SWITCH_TRIGGER_L_X_PORTRAIT">140</integer> - <integer name="SWITCH_TRIGGER_L_Y_PORTRAIT">260</integer> - <integer name="SWITCH_TRIGGER_R_X_PORTRAIT">860</integer> - <integer name="SWITCH_TRIGGER_R_Y_PORTRAIT">260</integer> - <integer name="SWITCH_TRIGGER_ZL_X_PORTRAIT">140</integer> - <integer name="SWITCH_TRIGGER_ZL_Y_PORTRAIT">200</integer> - <integer name="SWITCH_TRIGGER_ZR_X_PORTRAIT">860</integer> - <integer name="SWITCH_TRIGGER_ZR_Y_PORTRAIT">200</integer> - <integer name="SWITCH_BUTTON_MINUS_X_PORTRAIT">440</integer> - <integer name="SWITCH_BUTTON_MINUS_Y_PORTRAIT">950</integer> - <integer name="SWITCH_BUTTON_PLUS_X_PORTRAIT">560</integer> - <integer name="SWITCH_BUTTON_PLUS_Y_PORTRAIT">950</integer> - <integer name="SWITCH_BUTTON_HOME_X_PORTRAIT">680</integer> - <integer name="SWITCH_BUTTON_HOME_Y_PORTRAIT">950</integer> - <integer name="SWITCH_BUTTON_CAPTURE_X_PORTRAIT">320</integer> - <integer name="SWITCH_BUTTON_CAPTURE_Y_PORTRAIT">950</integer> - <integer name="SWITCH_BUTTON_DPAD_X_PORTRAIT">240</integer> - <integer name="SWITCH_BUTTON_DPAD_Y_PORTRAIT">840</integer> - <integer name="SWITCH_BUTTON_STICK_L_X_PORTRAIT">730</integer> - <integer name="SWITCH_BUTTON_STICK_L_Y_PORTRAIT">510</integer> - <integer name="SWITCH_BUTTON_STICK_R_X_PORTRAIT">900</integer> - <integer name="SWITCH_BUTTON_STICK_R_Y_PORTRAIT">540</integer> + <integer name="BUTTON_A_X_PORTRAIT">840</integer> + <integer name="BUTTON_A_Y_PORTRAIT">840</integer> + <integer name="BUTTON_B_X_PORTRAIT">740</integer> + <integer name="BUTTON_B_Y_PORTRAIT">880</integer> + <integer name="BUTTON_X_X_PORTRAIT">740</integer> + <integer name="BUTTON_X_Y_PORTRAIT">800</integer> + <integer name="BUTTON_Y_X_PORTRAIT">640</integer> + <integer name="BUTTON_Y_Y_PORTRAIT">840</integer> + <integer name="BUTTON_PLUS_Y_PORTRAIT">950</integer> + <integer name="BUTTON_MINUS_X_PORTRAIT">440</integer> + <integer name="BUTTON_MINUS_Y_PORTRAIT">950</integer> + <integer name="BUTTON_HOME_X_PORTRAIT">680</integer> + <integer name="BUTTON_HOME_Y_PORTRAIT">950</integer> + <integer name="BUTTON_CAPTURE_X_PORTRAIT">320</integer> + <integer name="BUTTON_CAPTURE_Y_PORTRAIT">950</integer> + <integer name="BUTTON_L_X_PORTRAIT">140</integer> + <integer name="BUTTON_L_Y_PORTRAIT">260</integer> + <integer name="BUTTON_R_X_PORTRAIT">860</integer> + <integer name="BUTTON_R_Y_PORTRAIT">260</integer> + <integer name="BUTTON_ZL_X_PORTRAIT">140</integer> + <integer name="BUTTON_ZL_Y_PORTRAIT">200</integer> + <integer name="BUTTON_ZR_X_PORTRAIT">860</integer> + <integer name="BUTTON_ZR_Y_PORTRAIT">200</integer> + <integer name="BUTTON_PLUS_X_PORTRAIT">560</integer> + <integer name="BUTTON_STICK_L_X_PORTRAIT">730</integer> + <integer name="BUTTON_STICK_L_Y_PORTRAIT">510</integer> + <integer name="BUTTON_STICK_R_X_PORTRAIT">900</integer> + <integer name="BUTTON_STICK_R_Y_PORTRAIT">540</integer> + <integer name="STICK_L_X_PORTRAIT">180</integer> + <integer name="STICK_L_Y_PORTRAIT">660</integer> + <integer name="STICK_R_X_PORTRAIT">820</integer> + <integer name="STICK_R_Y_PORTRAIT">660</integer> + <integer name="COMBINED_DPAD_X_PORTRAIT">240</integer> + <integer name="COMBINED_DPAD_Y_PORTRAIT">840</integer> <!-- Default SWITCH foldable layout --> - <integer name="SWITCH_BUTTON_A_X_FOLDABLE">840</integer> - <integer name="SWITCH_BUTTON_A_Y_FOLDABLE">390</integer> - <integer name="SWITCH_BUTTON_B_X_FOLDABLE">740</integer> - <integer name="SWITCH_BUTTON_B_Y_FOLDABLE">430</integer> - <integer name="SWITCH_BUTTON_X_X_FOLDABLE">740</integer> - <integer name="SWITCH_BUTTON_X_Y_FOLDABLE">350</integer> - <integer name="SWITCH_BUTTON_Y_X_FOLDABLE">640</integer> - <integer name="SWITCH_BUTTON_Y_Y_FOLDABLE">390</integer> - <integer name="SWITCH_STICK_L_X_FOLDABLE">180</integer> - <integer name="SWITCH_STICK_L_Y_FOLDABLE">250</integer> - <integer name="SWITCH_STICK_R_X_FOLDABLE">820</integer> - <integer name="SWITCH_STICK_R_Y_FOLDABLE">250</integer> - <integer name="SWITCH_TRIGGER_L_X_FOLDABLE">140</integer> - <integer name="SWITCH_TRIGGER_L_Y_FOLDABLE">130</integer> - <integer name="SWITCH_TRIGGER_R_X_FOLDABLE">860</integer> - <integer name="SWITCH_TRIGGER_R_Y_FOLDABLE">130</integer> - <integer name="SWITCH_TRIGGER_ZL_X_FOLDABLE">140</integer> - <integer name="SWITCH_TRIGGER_ZL_Y_FOLDABLE">70</integer> - <integer name="SWITCH_TRIGGER_ZR_X_FOLDABLE">860</integer> - <integer name="SWITCH_TRIGGER_ZR_Y_FOLDABLE">70</integer> - <integer name="SWITCH_BUTTON_MINUS_X_FOLDABLE">440</integer> - <integer name="SWITCH_BUTTON_MINUS_Y_FOLDABLE">470</integer> - <integer name="SWITCH_BUTTON_PLUS_X_FOLDABLE">560</integer> - <integer name="SWITCH_BUTTON_PLUS_Y_FOLDABLE">470</integer> - <integer name="SWITCH_BUTTON_HOME_X_FOLDABLE">680</integer> - <integer name="SWITCH_BUTTON_HOME_Y_FOLDABLE">470</integer> - <integer name="SWITCH_BUTTON_CAPTURE_X_FOLDABLE">320</integer> - <integer name="SWITCH_BUTTON_CAPTURE_Y_FOLDABLE">470</integer> - <integer name="SWITCH_BUTTON_DPAD_X_FOLDABLE">240</integer> - <integer name="SWITCH_BUTTON_DPAD_Y_FOLDABLE">390</integer> - <integer name="SWITCH_BUTTON_STICK_L_X_FOLDABLE">550</integer> - <integer name="SWITCH_BUTTON_STICK_L_Y_FOLDABLE">210</integer> - <integer name="SWITCH_BUTTON_STICK_R_X_FOLDABLE">550</integer> - <integer name="SWITCH_BUTTON_STICK_R_Y_FOLDABLE">280</integer> + <integer name="BUTTON_A_X_FOLDABLE">840</integer> + <integer name="BUTTON_A_Y_FOLDABLE">390</integer> + <integer name="BUTTON_B_X_FOLDABLE">740</integer> + <integer name="BUTTON_B_Y_FOLDABLE">430</integer> + <integer name="BUTTON_X_X_FOLDABLE">740</integer> + <integer name="BUTTON_X_Y_FOLDABLE">350</integer> + <integer name="BUTTON_Y_X_FOLDABLE">640</integer> + <integer name="BUTTON_Y_Y_FOLDABLE">390</integer> + <integer name="BUTTON_PLUS_X_FOLDABLE">560</integer> + <integer name="BUTTON_PLUS_Y_FOLDABLE">470</integer> + <integer name="BUTTON_MINUS_X_FOLDABLE">440</integer> + <integer name="BUTTON_MINUS_Y_FOLDABLE">470</integer> + <integer name="BUTTON_HOME_X_FOLDABLE">680</integer> + <integer name="BUTTON_HOME_Y_FOLDABLE">470</integer> + <integer name="BUTTON_CAPTURE_X_FOLDABLE">320</integer> + <integer name="BUTTON_CAPTURE_Y_FOLDABLE">470</integer> + <integer name="BUTTON_L_X_FOLDABLE">140</integer> + <integer name="BUTTON_L_Y_FOLDABLE">130</integer> + <integer name="BUTTON_R_X_FOLDABLE">860</integer> + <integer name="BUTTON_R_Y_FOLDABLE">130</integer> + <integer name="BUTTON_ZL_X_FOLDABLE">140</integer> + <integer name="BUTTON_ZL_Y_FOLDABLE">70</integer> + <integer name="BUTTON_ZR_X_FOLDABLE">860</integer> + <integer name="BUTTON_ZR_Y_FOLDABLE">70</integer> + <integer name="BUTTON_STICK_L_X_FOLDABLE">550</integer> + <integer name="BUTTON_STICK_L_Y_FOLDABLE">210</integer> + <integer name="BUTTON_STICK_R_X_FOLDABLE">550</integer> + <integer name="BUTTON_STICK_R_Y_FOLDABLE">280</integer> + <integer name="STICK_L_X_FOLDABLE">180</integer> + <integer name="STICK_L_Y_FOLDABLE">250</integer> + <integer name="STICK_R_X_FOLDABLE">820</integer> + <integer name="STICK_R_Y_FOLDABLE">250</integer> + <integer name="COMBINED_DPAD_X_FOLDABLE">240</integer> + <integer name="COMBINED_DPAD_Y_FOLDABLE">390</integer> </resources> diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index 4d5c268fe..1bedcb1ef 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -366,6 +366,7 @@ <string name="emulation_pause">Pause emulation</string> <string name="emulation_unpause">Unpause emulation</string> <string name="emulation_input_overlay">Overlay options</string> + <string name="touchscreen">Touchscreen</string> <string name="load_settings">Loading settings…</string> diff --git a/src/android/build.gradle.kts b/src/android/build.gradle.kts index 51e559321..b77906ed6 100644 --- a/src/android/build.gradle.kts +++ b/src/android/build.gradle.kts @@ -5,7 +5,7 @@ plugins { id("com.android.application") version "8.1.2" apply false id("com.android.library") version "8.1.2" apply false - id("org.jetbrains.kotlin.android") version "1.8.21" apply false + id("org.jetbrains.kotlin.android") version "1.9.20" apply false } tasks.register("clean").configure { diff --git a/src/audio_core/device/device_session.cpp b/src/audio_core/device/device_session.cpp index c41d9d1ea..ee42ae529 100644 --- a/src/audio_core/device/device_session.cpp +++ b/src/audio_core/device/device_session.cpp @@ -18,9 +18,7 @@ constexpr auto INCREMENT_TIME{5ms}; DeviceSession::DeviceSession(Core::System& system_) : system{system_}, thread_event{Core::Timing::CreateEvent( "AudioOutSampleTick", - [this](std::uintptr_t, s64 time, std::chrono::nanoseconds) { - return ThreadFunc(); - })} {} + [this](s64 time, std::chrono::nanoseconds) { return ThreadFunc(); })} {} DeviceSession::~DeviceSession() { Finalize(); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index b58a7073f..8c57d47c6 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -64,6 +64,8 @@ add_library(common STATIC fs/path_util.cpp fs/path_util.h hash.h + heap_tracker.cpp + heap_tracker.h hex_util.cpp hex_util.h host_memory.cpp diff --git a/src/common/heap_tracker.cpp b/src/common/heap_tracker.cpp new file mode 100644 index 000000000..683208795 --- /dev/null +++ b/src/common/heap_tracker.cpp @@ -0,0 +1,281 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <fstream> +#include <vector> + +#include "common/heap_tracker.h" +#include "common/logging/log.h" + +namespace Common { + +namespace { + +s64 GetMaxPermissibleResidentMapCount() { + // Default value. + s64 value = 65530; + + // Try to read how many mappings we can make. + std::ifstream s("/proc/sys/vm/max_map_count"); + s >> value; + + // Print, for debug. + LOG_INFO(HW_Memory, "Current maximum map count: {}", value); + + // Allow 20000 maps for other code and to account for split inaccuracy. + return std::max<s64>(value - 20000, 0); +} + +} // namespace + +HeapTracker::HeapTracker(Common::HostMemory& buffer) + : m_buffer(buffer), m_max_resident_map_count(GetMaxPermissibleResidentMapCount()) {} +HeapTracker::~HeapTracker() = default; + +void HeapTracker::Map(size_t virtual_offset, size_t host_offset, size_t length, + MemoryPermission perm, bool is_separate_heap) { + // When mapping other memory, map pages immediately. + if (!is_separate_heap) { + m_buffer.Map(virtual_offset, host_offset, length, perm, false); + return; + } + + { + // We are mapping part of a separate heap. + std::scoped_lock lk{m_lock}; + + auto* const map = new SeparateHeapMap{ + .vaddr = virtual_offset, + .paddr = host_offset, + .size = length, + .tick = m_tick++, + .perm = perm, + .is_resident = false, + }; + + // Insert into mappings. + m_map_count++; + m_mappings.insert(*map); + } + + // Finally, map. + this->DeferredMapSeparateHeap(virtual_offset); +} + +void HeapTracker::Unmap(size_t virtual_offset, size_t size, bool is_separate_heap) { + // If this is a separate heap... + if (is_separate_heap) { + std::scoped_lock lk{m_lock}; + + const SeparateHeapMap key{ + .vaddr = virtual_offset, + }; + + // Split at the boundaries of the region we are removing. + this->SplitHeapMapLocked(virtual_offset); + this->SplitHeapMapLocked(virtual_offset + size); + + // Erase all mappings in range. + auto it = m_mappings.find(key); + while (it != m_mappings.end() && it->vaddr < virtual_offset + size) { + // Get underlying item. + auto* const item = std::addressof(*it); + + // If resident, erase from resident map. + if (item->is_resident) { + ASSERT(--m_resident_map_count >= 0); + m_resident_mappings.erase(m_resident_mappings.iterator_to(*item)); + } + + // Erase from map. + ASSERT(--m_map_count >= 0); + it = m_mappings.erase(it); + + // Free the item. + delete item; + } + } + + // Unmap pages. + m_buffer.Unmap(virtual_offset, size, false); +} + +void HeapTracker::Protect(size_t virtual_offset, size_t size, MemoryPermission perm) { + // Ensure no rebuild occurs while reprotecting. + std::shared_lock lk{m_rebuild_lock}; + + // Split at the boundaries of the region we are reprotecting. + this->SplitHeapMap(virtual_offset, size); + + // Declare tracking variables. + const VAddr end = virtual_offset + size; + VAddr cur = virtual_offset; + + while (cur < end) { + VAddr next = cur; + bool should_protect = false; + + { + std::scoped_lock lk2{m_lock}; + + const SeparateHeapMap key{ + .vaddr = next, + }; + + // Try to get the next mapping corresponding to this address. + const auto it = m_mappings.nfind(key); + + if (it == m_mappings.end()) { + // There are no separate heap mappings remaining. + next = end; + should_protect = true; + } else if (it->vaddr == cur) { + // We are in range. + // Update permission bits. + it->perm = perm; + + // Determine next address and whether we should protect. + next = cur + it->size; + should_protect = it->is_resident; + } else /* if (it->vaddr > cur) */ { + // We weren't in range, but there is a block coming up that will be. + next = it->vaddr; + should_protect = true; + } + } + + // Clamp to end. + next = std::min(next, end); + + // Reprotect, if we need to. + if (should_protect) { + m_buffer.Protect(cur, next - cur, perm); + } + + // Advance. + cur = next; + } +} + +bool HeapTracker::DeferredMapSeparateHeap(u8* fault_address) { + if (m_buffer.IsInVirtualRange(fault_address)) { + return this->DeferredMapSeparateHeap(fault_address - m_buffer.VirtualBasePointer()); + } + + return false; +} + +bool HeapTracker::DeferredMapSeparateHeap(size_t virtual_offset) { + bool rebuild_required = false; + + { + std::scoped_lock lk{m_lock}; + + // Check to ensure this was a non-resident separate heap mapping. + const auto it = this->GetNearestHeapMapLocked(virtual_offset); + if (it == m_mappings.end() || it->is_resident) { + return false; + } + + // Update tick before possible rebuild. + it->tick = m_tick++; + + // Check if we need to rebuild. + if (m_resident_map_count > m_max_resident_map_count) { + rebuild_required = true; + } + + // Map the area. + m_buffer.Map(it->vaddr, it->paddr, it->size, it->perm, false); + + // This map is now resident. + it->is_resident = true; + m_resident_map_count++; + m_resident_mappings.insert(*it); + } + + if (rebuild_required) { + // A rebuild was required, so perform it now. + this->RebuildSeparateHeapAddressSpace(); + } + + return true; +} + +void HeapTracker::RebuildSeparateHeapAddressSpace() { + std::scoped_lock lk{m_rebuild_lock, m_lock}; + + ASSERT(!m_resident_mappings.empty()); + + // Dump half of the mappings. + // + // Despite being worse in theory, this has proven to be better in practice than more + // regularly dumping a smaller amount, because it significantly reduces average case + // lock contention. + const size_t desired_count = std::min(m_resident_map_count, m_max_resident_map_count) / 2; + const size_t evict_count = m_resident_map_count - desired_count; + auto it = m_resident_mappings.begin(); + + for (size_t i = 0; i < evict_count && it != m_resident_mappings.end(); i++) { + // Unmark and unmap. + it->is_resident = false; + m_buffer.Unmap(it->vaddr, it->size, false); + + // Advance. + ASSERT(--m_resident_map_count >= 0); + it = m_resident_mappings.erase(it); + } +} + +void HeapTracker::SplitHeapMap(VAddr offset, size_t size) { + std::scoped_lock lk{m_lock}; + + this->SplitHeapMapLocked(offset); + this->SplitHeapMapLocked(offset + size); +} + +void HeapTracker::SplitHeapMapLocked(VAddr offset) { + const auto it = this->GetNearestHeapMapLocked(offset); + if (it == m_mappings.end() || it->vaddr == offset) { + // Not contained or no split required. + return; + } + + // Cache the original values. + auto* const left = std::addressof(*it); + const size_t orig_size = left->size; + + // Adjust the left map. + const size_t left_size = offset - left->vaddr; + left->size = left_size; + + // Create the new right map. + auto* const right = new SeparateHeapMap{ + .vaddr = left->vaddr + left_size, + .paddr = left->paddr + left_size, + .size = orig_size - left_size, + .tick = left->tick, + .perm = left->perm, + .is_resident = left->is_resident, + }; + + // Insert the new right map. + m_map_count++; + m_mappings.insert(*right); + + // If resident, also insert into resident map. + if (right->is_resident) { + m_resident_map_count++; + m_resident_mappings.insert(*right); + } +} + +HeapTracker::AddrTree::iterator HeapTracker::GetNearestHeapMapLocked(VAddr offset) { + const SeparateHeapMap key{ + .vaddr = offset, + }; + + return m_mappings.find(key); +} + +} // namespace Common diff --git a/src/common/heap_tracker.h b/src/common/heap_tracker.h new file mode 100644 index 000000000..ee5b0bf43 --- /dev/null +++ b/src/common/heap_tracker.h @@ -0,0 +1,98 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <atomic> +#include <mutex> +#include <set> +#include <shared_mutex> + +#include "common/host_memory.h" +#include "common/intrusive_red_black_tree.h" + +namespace Common { + +struct SeparateHeapMap { + Common::IntrusiveRedBlackTreeNode addr_node{}; + Common::IntrusiveRedBlackTreeNode tick_node{}; + VAddr vaddr{}; + PAddr paddr{}; + size_t size{}; + size_t tick{}; + MemoryPermission perm{}; + bool is_resident{}; +}; + +struct SeparateHeapMapAddrComparator { + static constexpr int Compare(const SeparateHeapMap& lhs, const SeparateHeapMap& rhs) { + if (lhs.vaddr < rhs.vaddr) { + return -1; + } else if (lhs.vaddr <= (rhs.vaddr + rhs.size - 1)) { + return 0; + } else { + return 1; + } + } +}; + +struct SeparateHeapMapTickComparator { + static constexpr int Compare(const SeparateHeapMap& lhs, const SeparateHeapMap& rhs) { + if (lhs.tick < rhs.tick) { + return -1; + } else if (lhs.tick > rhs.tick) { + return 1; + } else { + return SeparateHeapMapAddrComparator::Compare(lhs, rhs); + } + } +}; + +class HeapTracker { +public: + explicit HeapTracker(Common::HostMemory& buffer); + ~HeapTracker(); + + void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perm, + bool is_separate_heap); + void Unmap(size_t virtual_offset, size_t size, bool is_separate_heap); + void Protect(size_t virtual_offset, size_t length, MemoryPermission perm); + u8* VirtualBasePointer() { + return m_buffer.VirtualBasePointer(); + } + + bool DeferredMapSeparateHeap(u8* fault_address); + bool DeferredMapSeparateHeap(size_t virtual_offset); + +private: + using AddrTreeTraits = + Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<&SeparateHeapMap::addr_node>; + using AddrTree = AddrTreeTraits::TreeType<SeparateHeapMapAddrComparator>; + + using TickTreeTraits = + Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<&SeparateHeapMap::tick_node>; + using TickTree = TickTreeTraits::TreeType<SeparateHeapMapTickComparator>; + + AddrTree m_mappings{}; + TickTree m_resident_mappings{}; + +private: + void SplitHeapMap(VAddr offset, size_t size); + void SplitHeapMapLocked(VAddr offset); + + AddrTree::iterator GetNearestHeapMapLocked(VAddr offset); + + void RebuildSeparateHeapAddressSpace(); + +private: + Common::HostMemory& m_buffer; + const s64 m_max_resident_map_count; + + std::shared_mutex m_rebuild_lock{}; + std::mutex m_lock{}; + s64 m_map_count{}; + s64 m_resident_map_count{}; + size_t m_tick{}; +}; + +} // namespace Common diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index e540375b8..860c39e6a 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -679,7 +679,7 @@ HostMemory::HostMemory(HostMemory&&) noexcept = default; HostMemory& HostMemory::operator=(HostMemory&&) noexcept = default; void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, - MemoryPermission perms) { + MemoryPermission perms, bool separate_heap) { ASSERT(virtual_offset % PageAlignment == 0); ASSERT(host_offset % PageAlignment == 0); ASSERT(length % PageAlignment == 0); @@ -691,7 +691,7 @@ void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, impl->Map(virtual_offset + virtual_base_offset, host_offset, length, perms); } -void HostMemory::Unmap(size_t virtual_offset, size_t length) { +void HostMemory::Unmap(size_t virtual_offset, size_t length, bool separate_heap) { ASSERT(virtual_offset % PageAlignment == 0); ASSERT(length % PageAlignment == 0); ASSERT(virtual_offset + length <= virtual_size); @@ -701,14 +701,16 @@ void HostMemory::Unmap(size_t virtual_offset, size_t length) { impl->Unmap(virtual_offset + virtual_base_offset, length); } -void HostMemory::Protect(size_t virtual_offset, size_t length, bool read, bool write, - bool execute) { +void HostMemory::Protect(size_t virtual_offset, size_t length, MemoryPermission perm) { ASSERT(virtual_offset % PageAlignment == 0); ASSERT(length % PageAlignment == 0); ASSERT(virtual_offset + length <= virtual_size); if (length == 0 || !virtual_base || !impl) { return; } + const bool read = True(perm & MemoryPermission::Read); + const bool write = True(perm & MemoryPermission::Write); + const bool execute = True(perm & MemoryPermission::Execute); impl->Protect(virtual_offset + virtual_base_offset, length, read, write, execute); } diff --git a/src/common/host_memory.h b/src/common/host_memory.h index 747c5850c..72fbb05af 100644 --- a/src/common/host_memory.h +++ b/src/common/host_memory.h @@ -40,11 +40,12 @@ public: HostMemory(HostMemory&& other) noexcept; HostMemory& operator=(HostMemory&& other) noexcept; - void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms); + void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms, + bool separate_heap); - void Unmap(size_t virtual_offset, size_t length); + void Unmap(size_t virtual_offset, size_t length, bool separate_heap); - void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute = false); + void Protect(size_t virtual_offset, size_t length, MemoryPermission perms); void EnableDirectMappedAddress(); @@ -64,6 +65,10 @@ public: return virtual_base; } + bool IsInVirtualRange(void* address) const noexcept { + return address >= virtual_base && address < virtual_base + virtual_size; + } + private: size_t backing_size{}; size_t virtual_size{}; diff --git a/src/common/settings.cpp b/src/common/settings.cpp index ea52bbfa6..07709d4e5 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -199,6 +199,8 @@ const char* TranslateCategory(Category category) { case Category::CpuDebug: case Category::CpuUnsafe: return "Cpu"; + case Category::Overlay: + return "Overlay"; case Category::Renderer: case Category::RendererAdvanced: case Category::RendererDebug: diff --git a/src/common/settings_common.h b/src/common/settings_common.h index c82e17495..1a290ad77 100644 --- a/src/common/settings_common.h +++ b/src/common/settings_common.h @@ -18,6 +18,7 @@ enum class Category : u32 { Cpu, CpuDebug, CpuUnsafe, + Overlay, Renderer, RendererAdvanced, RendererDebug, diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 96ab39cb8..dfba79267 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -553,17 +553,26 @@ add_library(core STATIC hle/service/hid/controllers/types/keyboard_types.h hle/service/hid/controllers/types/mouse_types.h hle/service/hid/controllers/types/npad_types.h + hle/service/hid/controllers/types/shared_memory_format.h hle/service/hid/controllers/types/touch_types.h hle/service/hid/controllers/applet_resource.cpp hle/service/hid/controllers/applet_resource.h + hle/service/hid/controllers/capture_button.cpp + hle/service/hid/controllers/capture_button.h hle/service/hid/controllers/console_six_axis.cpp hle/service/hid/controllers/console_six_axis.h hle/service/hid/controllers/controller_base.cpp hle/service/hid/controllers/controller_base.h + hle/service/hid/controllers/debug_mouse.cpp + hle/service/hid/controllers/debug_mouse.h hle/service/hid/controllers/debug_pad.cpp hle/service/hid/controllers/debug_pad.h + hle/service/hid/controllers/digitizer.cpp + hle/service/hid/controllers/digitizer.h hle/service/hid/controllers/gesture.cpp hle/service/hid/controllers/gesture.h + hle/service/hid/controllers/home_button.cpp + hle/service/hid/controllers/home_button.h hle/service/hid/controllers/keyboard.cpp hle/service/hid/controllers/keyboard.h hle/service/hid/controllers/mouse.cpp @@ -574,15 +583,16 @@ add_library(core STATIC hle/service/hid/controllers/palma.h hle/service/hid/controllers/seven_six_axis.cpp hle/service/hid/controllers/seven_six_axis.h - hle/service/hid/controllers/shared_memory_format.h hle/service/hid/controllers/shared_memory_holder.cpp hle/service/hid/controllers/shared_memory_holder.h hle/service/hid/controllers/six_axis.cpp hle/service/hid/controllers/six_axis.h - hle/service/hid/controllers/stubbed.cpp - hle/service/hid/controllers/stubbed.h + hle/service/hid/controllers/sleep_button.cpp + hle/service/hid/controllers/sleep_button.h hle/service/hid/controllers/touchscreen.cpp hle/service/hid/controllers/touchscreen.h + hle/service/hid/controllers/unique_pad.cpp + hle/service/hid/controllers/unique_pad.h hle/service/hid/hidbus/hidbus_base.cpp hle/service/hid/hidbus/hidbus_base.h hle/service/hid/hidbus/ringcon.cpp @@ -978,6 +988,7 @@ endif() if (ARCHITECTURE_x86_64 OR ARCHITECTURE_arm64) target_sources(core PRIVATE + arm/dynarmic/arm_dynarmic.cpp arm/dynarmic/arm_dynarmic.h arm/dynarmic/arm_dynarmic_64.cpp arm/dynarmic/arm_dynarmic_64.h @@ -987,6 +998,8 @@ if (ARCHITECTURE_x86_64 OR ARCHITECTURE_arm64) arm/dynarmic/dynarmic_cp15.h arm/dynarmic/dynarmic_exclusive_monitor.cpp arm/dynarmic/dynarmic_exclusive_monitor.h + hle/service/jit/jit_code_memory.cpp + hle/service/jit/jit_code_memory.h hle/service/jit/jit_context.cpp hle/service/jit/jit_context.h hle/service/jit/jit.cpp diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp new file mode 100644 index 000000000..e6e9fc45b --- /dev/null +++ b/src/core/arm/dynarmic/arm_dynarmic.cpp @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifdef __linux__ + +#include "common/signal_chain.h" + +#include "core/arm/dynarmic/arm_dynarmic.h" +#include "core/hle/kernel/k_process.h" +#include "core/memory.h" + +namespace Core { + +namespace { + +thread_local Core::Memory::Memory* g_current_memory{}; +std::once_flag g_registered{}; +struct sigaction g_old_segv {}; + +void HandleSigSegv(int sig, siginfo_t* info, void* ctx) { + if (g_current_memory && g_current_memory->InvalidateSeparateHeap(info->si_addr)) { + return; + } + + return g_old_segv.sa_sigaction(sig, info, ctx); +} + +} // namespace + +ScopedJitExecution::ScopedJitExecution(Kernel::KProcess* process) { + g_current_memory = std::addressof(process->GetMemory()); +} + +ScopedJitExecution::~ScopedJitExecution() { + g_current_memory = nullptr; +} + +void ScopedJitExecution::RegisterHandler() { + std::call_once(g_registered, [] { + struct sigaction sa {}; + sa.sa_sigaction = &HandleSigSegv; + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; + Common::SigAction(SIGSEGV, std::addressof(sa), std::addressof(g_old_segv)); + }); +} + +} // namespace Core + +#endif diff --git a/src/core/arm/dynarmic/arm_dynarmic.h b/src/core/arm/dynarmic/arm_dynarmic.h index eef7c3116..53dd18815 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.h +++ b/src/core/arm/dynarmic/arm_dynarmic.h @@ -26,4 +26,24 @@ constexpr HaltReason TranslateHaltReason(Dynarmic::HaltReason hr) { return static_cast<HaltReason>(hr); } +#ifdef __linux__ + +class ScopedJitExecution { +public: + explicit ScopedJitExecution(Kernel::KProcess* process); + ~ScopedJitExecution(); + static void RegisterHandler(); +}; + +#else + +class ScopedJitExecution { +public: + explicit ScopedJitExecution(Kernel::KProcess* process) {} + ~ScopedJitExecution() {} + static void RegisterHandler() {} +}; + +#endif + } // namespace Core diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.cpp b/src/core/arm/dynarmic/arm_dynarmic_32.cpp index c78cfd528..36478f722 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_32.cpp @@ -331,11 +331,15 @@ bool ArmDynarmic32::IsInThumbMode() const { } HaltReason ArmDynarmic32::RunThread(Kernel::KThread* thread) { + ScopedJitExecution sj(thread->GetOwnerProcess()); + m_jit->ClearExclusiveState(); return TranslateHaltReason(m_jit->Run()); } HaltReason ArmDynarmic32::StepThread(Kernel::KThread* thread) { + ScopedJitExecution sj(thread->GetOwnerProcess()); + m_jit->ClearExclusiveState(); return TranslateHaltReason(m_jit->Step()); } @@ -377,6 +381,7 @@ ArmDynarmic32::ArmDynarmic32(System& system, bool uses_wall_clock, Kernel::KProc m_cp15(std::make_shared<DynarmicCP15>(*this)), m_core_index{core_index} { auto& page_table_impl = process->GetPageTable().GetBasePageTable().GetImpl(); m_jit = MakeJit(&page_table_impl); + ScopedJitExecution::RegisterHandler(); } ArmDynarmic32::~ArmDynarmic32() = default; diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.cpp b/src/core/arm/dynarmic/arm_dynarmic_64.cpp index f351b13d9..c811c8ad5 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_64.cpp @@ -362,11 +362,15 @@ std::shared_ptr<Dynarmic::A64::Jit> ArmDynarmic64::MakeJit(Common::PageTable* pa } HaltReason ArmDynarmic64::RunThread(Kernel::KThread* thread) { + ScopedJitExecution sj(thread->GetOwnerProcess()); + m_jit->ClearExclusiveState(); return TranslateHaltReason(m_jit->Run()); } HaltReason ArmDynarmic64::StepThread(Kernel::KThread* thread) { + ScopedJitExecution sj(thread->GetOwnerProcess()); + m_jit->ClearExclusiveState(); return TranslateHaltReason(m_jit->Step()); } @@ -406,6 +410,7 @@ ArmDynarmic64::ArmDynarmic64(System& system, bool uses_wall_clock, Kernel::KProc auto& page_table = process->GetPageTable().GetBasePageTable(); auto& page_table_impl = page_table.GetImpl(); m_jit = MakeJit(&page_table_impl, page_table.GetAddressSpaceWidth()); + ScopedJitExecution::RegisterHandler(); } ArmDynarmic64::~ArmDynarmic64() = default; diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index d6b5abc68..fc536413b 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -29,7 +29,6 @@ std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callbac struct CoreTiming::Event { s64 time; u64 fifo_order; - std::uintptr_t user_data; std::weak_ptr<EventType> type; s64 reschedule_time; heap_t::handle_type handle{}; @@ -67,17 +66,15 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) { event_fifo_id = 0; shutting_down = false; cpu_ticks = 0; - const auto empty_timed_callback = [](std::uintptr_t, u64, std::chrono::nanoseconds) - -> std::optional<std::chrono::nanoseconds> { return std::nullopt; }; - ev_lost = CreateEvent("_lost_event", empty_timed_callback); if (is_multicore) { timer_thread = std::make_unique<std::jthread>(ThreadEntry, std::ref(*this)); } } void CoreTiming::ClearPendingEvents() { - std::scoped_lock lock{basic_lock}; + std::scoped_lock lock{advance_lock, basic_lock}; event_queue.clear(); + event.Set(); } void CoreTiming::Pause(bool is_paused) { @@ -119,14 +116,12 @@ bool CoreTiming::HasPendingEvents() const { } void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, - const std::shared_ptr<EventType>& event_type, - std::uintptr_t user_data, bool absolute_time) { + const std::shared_ptr<EventType>& event_type, bool absolute_time) { { std::scoped_lock scope{basic_lock}; const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future}; - auto h{event_queue.emplace( - Event{next_time.count(), event_fifo_id++, user_data, event_type, 0})}; + auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, event_type, 0})}; (*h).handle = h; } @@ -136,13 +131,13 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, std::chrono::nanoseconds resched_time, const std::shared_ptr<EventType>& event_type, - std::uintptr_t user_data, bool absolute_time) { + bool absolute_time) { { std::scoped_lock scope{basic_lock}; const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; - auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, user_data, event_type, - resched_time.count()})}; + auto h{event_queue.emplace( + Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()})}; (*h).handle = h; } @@ -150,14 +145,14 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, } void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, - std::uintptr_t user_data, bool wait) { + UnscheduleEventType type) { { std::scoped_lock lk{basic_lock}; std::vector<heap_t::handle_type> to_remove; for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) { const Event& e = *itr; - if (e.type.lock().get() == event_type.get() && e.user_data == user_data) { + if (e.type.lock().get() == event_type.get()) { to_remove.push_back(itr->handle); } } @@ -165,10 +160,12 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, for (auto h : to_remove) { event_queue.erase(h); } + + event_type->sequence_number++; } // Force any in-progress events to finish - if (wait) { + if (type == UnscheduleEventType::Wait) { std::scoped_lock lk{advance_lock}; } } @@ -208,28 +205,31 @@ std::optional<s64> CoreTiming::Advance() { const Event& evt = event_queue.top(); if (const auto event_type{evt.type.lock()}) { - if (evt.reschedule_time == 0) { - const auto evt_user_data = evt.user_data; - const auto evt_time = evt.time; + const auto evt_time = evt.time; + const auto evt_sequence_num = event_type->sequence_number; + if (evt.reschedule_time == 0) { event_queue.pop(); basic_lock.unlock(); event_type->callback( - evt_user_data, evt_time, - std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time}); + evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time}); basic_lock.lock(); } else { basic_lock.unlock(); const auto new_schedule_time{event_type->callback( - evt.user_data, evt.time, - std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})}; + evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time})}; basic_lock.lock(); + if (evt_sequence_num != event_type->sequence_number) { + // Heap handle is invalidated after external modification. + continue; + } + const auto next_schedule_time{new_schedule_time.has_value() ? new_schedule_time.value().count() : evt.reschedule_time}; @@ -241,8 +241,8 @@ std::optional<s64> CoreTiming::Advance() { next_time = pause_end_time + next_schedule_time; } - event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.user_data, - evt.type, next_schedule_time, evt.handle}); + event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.type, + next_schedule_time, evt.handle}); } } diff --git a/src/core/core_timing.h b/src/core/core_timing.h index 21548f0a9..7e4dff7f3 100644 --- a/src/core/core_timing.h +++ b/src/core/core_timing.h @@ -22,17 +22,25 @@ namespace Core::Timing { /// A callback that may be scheduled for a particular core timing event. using TimedCallback = std::function<std::optional<std::chrono::nanoseconds>( - std::uintptr_t user_data, s64 time, std::chrono::nanoseconds ns_late)>; + s64 time, std::chrono::nanoseconds ns_late)>; /// Contains the characteristics of a particular event. struct EventType { explicit EventType(TimedCallback&& callback_, std::string&& name_) - : callback{std::move(callback_)}, name{std::move(name_)} {} + : callback{std::move(callback_)}, name{std::move(name_)}, sequence_number{0} {} /// The event's callback function. TimedCallback callback; /// A pointer to the name of the event. const std::string name; + /// A monotonic sequence number, incremented when this event is + /// changed externally. + size_t sequence_number; +}; + +enum class UnscheduleEventType { + Wait, + NoWait, }; /** @@ -89,23 +97,17 @@ public: /// Schedules an event in core timing void ScheduleEvent(std::chrono::nanoseconds ns_into_future, - const std::shared_ptr<EventType>& event_type, std::uintptr_t user_data = 0, - bool absolute_time = false); + const std::shared_ptr<EventType>& event_type, bool absolute_time = false); /// Schedules an event which will automatically re-schedule itself with the given time, until /// unscheduled void ScheduleLoopingEvent(std::chrono::nanoseconds start_time, std::chrono::nanoseconds resched_time, const std::shared_ptr<EventType>& event_type, - std::uintptr_t user_data = 0, bool absolute_time = false); + bool absolute_time = false); - void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, std::uintptr_t user_data, - bool wait = true); - - void UnscheduleEventWithoutWait(const std::shared_ptr<EventType>& event_type, - std::uintptr_t user_data) { - UnscheduleEvent(event_type, user_data, false); - } + void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, + UnscheduleEventType type = UnscheduleEventType::Wait); void AddTicks(u64 ticks_to_add); @@ -158,7 +160,6 @@ private: heap_t event_queue; u64 event_fifo_id = 0; - std::shared_ptr<EventType> ev_lost; Common::Event event{}; Common::Event pause_event{}; mutable std::mutex basic_lock; diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp index 7be1322cc..31033634c 100644 --- a/src/core/file_sys/ips_layer.cpp +++ b/src/core/file_sys/ips_layer.cpp @@ -73,6 +73,9 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { return nullptr; auto in_data = in->ReadAllBytes(); + if (in_data.size() == 0) { + return nullptr; + } std::vector<u8> temp(type == IPSFileType::IPS ? 3 : 4); u64 offset = 5; // After header @@ -88,6 +91,10 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { else real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2]; + if (real_offset > in_data.size()) { + return nullptr; + } + u16 data_size{}; if (ips->ReadObject(&data_size, offset) != sizeof(u16)) return nullptr; diff --git a/src/core/hle/kernel/k_hardware_timer.cpp b/src/core/hle/kernel/k_hardware_timer.cpp index 8e2e40307..4e947dd6b 100644 --- a/src/core/hle/kernel/k_hardware_timer.cpp +++ b/src/core/hle/kernel/k_hardware_timer.cpp @@ -10,15 +10,15 @@ namespace Kernel { void KHardwareTimer::Initialize() { // Create the timing callback to register with CoreTiming. - m_event_type = Core::Timing::CreateEvent( - "KHardwareTimer::Callback", [](std::uintptr_t timer_handle, s64, std::chrono::nanoseconds) { - reinterpret_cast<KHardwareTimer*>(timer_handle)->DoTask(); - return std::nullopt; - }); + m_event_type = Core::Timing::CreateEvent("KHardwareTimer::Callback", + [this](s64, std::chrono::nanoseconds) { + this->DoTask(); + return std::nullopt; + }); } void KHardwareTimer::Finalize() { - m_kernel.System().CoreTiming().UnscheduleEvent(m_event_type, reinterpret_cast<uintptr_t>(this)); + m_kernel.System().CoreTiming().UnscheduleEvent(m_event_type); m_wakeup_time = std::numeric_limits<s64>::max(); m_event_type.reset(); } @@ -57,13 +57,12 @@ void KHardwareTimer::EnableInterrupt(s64 wakeup_time) { m_wakeup_time = wakeup_time; m_kernel.System().CoreTiming().ScheduleEvent(std::chrono::nanoseconds{m_wakeup_time}, - m_event_type, reinterpret_cast<uintptr_t>(this), - true); + m_event_type, true); } void KHardwareTimer::DisableInterrupt() { - m_kernel.System().CoreTiming().UnscheduleEventWithoutWait(m_event_type, - reinterpret_cast<uintptr_t>(this)); + m_kernel.System().CoreTiming().UnscheduleEvent(m_event_type, + Core::Timing::UnscheduleEventType::NoWait); m_wakeup_time = std::numeric_limits<s64>::max(); } diff --git a/src/core/hle/kernel/k_page_table_base.cpp b/src/core/hle/kernel/k_page_table_base.cpp index 423289145..8c1549559 100644 --- a/src/core/hle/kernel/k_page_table_base.cpp +++ b/src/core/hle/kernel/k_page_table_base.cpp @@ -434,7 +434,7 @@ Result KPageTableBase::InitializeForProcess(Svc::CreateProcessFlag as_type, bool void KPageTableBase::Finalize() { auto HostUnmapCallback = [&](KProcessAddress addr, u64 size) { if (Settings::IsFastmemEnabled()) { - m_system.DeviceMemory().buffer.Unmap(GetInteger(addr), size); + m_system.DeviceMemory().buffer.Unmap(GetInteger(addr), size, false); } }; @@ -5243,7 +5243,7 @@ Result KPageTableBase::MapPhysicalMemory(KProcessAddress address, size_t size) { // Unmap. R_ASSERT(this->Operate(updater.GetPageList(), cur_address, cur_pages, 0, false, unmap_properties, - OperationType::Unmap, true)); + OperationType::UnmapPhysical, true)); } // Check if we're done. @@ -5326,7 +5326,7 @@ Result KPageTableBase::MapPhysicalMemory(KProcessAddress address, size_t size) { // Map the papges. R_TRY(this->Operate(updater.GetPageList(), cur_address, map_pages, cur_pg, map_properties, - OperationType::MapFirstGroup, false)); + OperationType::MapFirstGroupPhysical, false)); } } @@ -5480,7 +5480,7 @@ Result KPageTableBase::UnmapPhysicalMemory(KProcessAddress address, size_t size) // Unmap. R_ASSERT(this->Operate(updater.GetPageList(), cur_address, cur_pages, 0, false, - unmap_properties, OperationType::Unmap, false)); + unmap_properties, OperationType::UnmapPhysical, false)); } // Check if we're done. @@ -5655,7 +5655,10 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a // or free them to the page list, and so it goes unused (along with page properties). switch (operation) { - case OperationType::Unmap: { + case OperationType::Unmap: + case OperationType::UnmapPhysical: { + const bool separate_heap = operation == OperationType::UnmapPhysical; + // Ensure that any pages we track are closed on exit. KPageGroup pages_to_close(m_kernel, this->GetBlockInfoManager()); SCOPE_EXIT({ pages_to_close.CloseAndReset(); }); @@ -5664,7 +5667,7 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a this->MakePageGroup(pages_to_close, virt_addr, num_pages); // Unmap. - m_memory->UnmapRegion(*m_impl, virt_addr, num_pages * PageSize); + m_memory->UnmapRegion(*m_impl, virt_addr, num_pages * PageSize, separate_heap); R_SUCCEED(); } @@ -5672,7 +5675,7 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a ASSERT(virt_addr != 0); ASSERT(Common::IsAligned(GetInteger(virt_addr), PageSize)); m_memory->MapMemoryRegion(*m_impl, virt_addr, num_pages * PageSize, phys_addr, - ConvertToMemoryPermission(properties.perm)); + ConvertToMemoryPermission(properties.perm), false); // Open references to pages, if we should. if (this->IsHeapPhysicalAddress(phys_addr)) { @@ -5711,16 +5714,19 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a switch (operation) { case OperationType::MapGroup: - case OperationType::MapFirstGroup: { + case OperationType::MapFirstGroup: + case OperationType::MapFirstGroupPhysical: { + const bool separate_heap = operation == OperationType::MapFirstGroupPhysical; + // We want to maintain a new reference to every page in the group. - KScopedPageGroup spg(page_group, operation != OperationType::MapFirstGroup); + KScopedPageGroup spg(page_group, operation == OperationType::MapGroup); for (const auto& node : page_group) { const size_t size{node.GetNumPages() * PageSize}; // Map the pages. m_memory->MapMemoryRegion(*m_impl, virt_addr, size, node.GetAddress(), - ConvertToMemoryPermission(properties.perm)); + ConvertToMemoryPermission(properties.perm), separate_heap); virt_addr += size; } diff --git a/src/core/hle/kernel/k_page_table_base.h b/src/core/hle/kernel/k_page_table_base.h index 556d230b3..077cafc96 100644 --- a/src/core/hle/kernel/k_page_table_base.h +++ b/src/core/hle/kernel/k_page_table_base.h @@ -104,6 +104,9 @@ protected: ChangePermissionsAndRefresh = 5, ChangePermissionsAndRefreshAndFlush = 6, Separate = 7, + + MapFirstGroupPhysical = 65000, + UnmapPhysical = 65001, }; static constexpr size_t MaxPhysicalMapAlignment = 1_GiB; diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index d6869c228..068e71dff 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -1237,8 +1237,10 @@ void KProcess::LoadModule(CodeSet code_set, KProcessAddress base_addr) { auto& buffer = m_kernel.System().DeviceMemory().buffer; const auto& code = code_set.CodeSegment(); const auto& patch = code_set.PatchSegment(); - buffer.Protect(GetInteger(base_addr + code.addr), code.size, true, true, true); - buffer.Protect(GetInteger(base_addr + patch.addr), patch.size, true, true, true); + buffer.Protect(GetInteger(base_addr + code.addr), code.size, + Common::MemoryPermission::Read | Common::MemoryPermission::Execute); + buffer.Protect(GetInteger(base_addr + patch.addr), patch.size, + Common::MemoryPermission::Read | Common::MemoryPermission::Execute); ReprotectSegment(code_set.PatchSegment(), Svc::MemoryPermission::None); } #endif diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index c14d2d2f3..1030f0c12 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -238,7 +238,7 @@ struct KernelCore::Impl { void InitializePreemption(KernelCore& kernel) { preemption_event = Core::Timing::CreateEvent( "PreemptionCallback", - [this, &kernel](std::uintptr_t, s64 time, + [this, &kernel](s64 time, std::chrono::nanoseconds) -> std::optional<std::chrono::nanoseconds> { { KScopedSchedulerLock lock(kernel); diff --git a/src/core/hle/service/hid/controllers/applet_resource.cpp b/src/core/hle/service/hid/controllers/applet_resource.cpp index c8e74c764..b4ff663c2 100644 --- a/src/core/hle/service/hid/controllers/applet_resource.cpp +++ b/src/core/hle/service/hid/controllers/applet_resource.cpp @@ -4,7 +4,7 @@ #include "core/core.h" #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/service/hid/controllers/applet_resource.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" #include "core/hle/service/hid/errors.h" namespace Service::HID { @@ -164,6 +164,22 @@ Result AppletResource::GetSharedMemoryFormat(SharedMemoryFormat** out_shared_mem return ResultSuccess; } +AruidData* AppletResource::GetAruidData(u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index == AruidIndexMax) { + return nullptr; + } + return &data[aruid_index]; +} + +AruidData* AppletResource::GetAruidDataByIndex(std::size_t aruid_index) { + return &data[aruid_index]; +} + +bool AppletResource::IsVibrationAruidActive(u64 aruid) const { + return aruid == 0 || aruid == active_vibration_aruid; +} + u64 AppletResource::GetIndexFromAruid(u64 aruid) { for (std::size_t i = 0; i < AruidIndexMax; i++) { if (registration_list.flag[i] == RegistrationStatus::Initialized && diff --git a/src/core/hle/service/hid/controllers/applet_resource.h b/src/core/hle/service/hid/controllers/applet_resource.h index e7991f93a..52cc4cf42 100644 --- a/src/core/hle/service/hid/controllers/applet_resource.h +++ b/src/core/hle/service/hid/controllers/applet_resource.h @@ -4,6 +4,7 @@ #pragma once #include <array> +#include <mutex> #include "common/bit_field.h" #include "common/common_types.h" @@ -20,6 +21,59 @@ class KSharedMemory; namespace Service::HID { struct SharedMemoryFormat; +class AppletResource; +class NPadResource; + +static constexpr std::size_t AruidIndexMax = 0x20; + +enum class RegistrationStatus : u32 { + None, + Initialized, + PendingDelete, +}; + +struct DataStatusFlag { + union { + u32 raw{}; + + BitField<0, 1, u32> is_initialized; + BitField<1, 1, u32> is_assigned; + BitField<16, 1, u32> enable_pad_input; + BitField<17, 1, u32> enable_six_axis_sensor; + BitField<18, 1, u32> bit_18; + BitField<19, 1, u32> is_palma_connectable; + BitField<20, 1, u32> enable_palma_boost_mode; + BitField<21, 1, u32> enable_touchscreen; + }; +}; + +struct AruidRegisterList { + std::array<RegistrationStatus, AruidIndexMax> flag{}; + std::array<u64, AruidIndexMax> aruid{}; +}; +static_assert(sizeof(AruidRegisterList) == 0x180, "AruidRegisterList is an invalid size"); + +struct AruidData { + DataStatusFlag flag{}; + u64 aruid{}; + SharedMemoryFormat* shared_memory_format{nullptr}; +}; + +struct HandheldConfig { + bool is_handheld_hid_enabled; + bool is_force_handheld; + bool is_joycon_rail_enabled; + bool is_force_handheld_style_vibration; +}; +static_assert(sizeof(HandheldConfig) == 0x4, "HandheldConfig is an invalid size"); + +struct AppletResourceHolder { + std::shared_ptr<AppletResource> applet_resource{nullptr}; + std::recursive_mutex* shared_mutex{nullptr}; + NPadResource* shared_npad_resource{nullptr}; + std::shared_ptr<HandheldConfig> handheld_config{nullptr}; + long* handle_1; +}; class AppletResource { public: @@ -36,6 +90,10 @@ public: u64 GetActiveAruid(); Result GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid); Result GetSharedMemoryFormat(SharedMemoryFormat** out_shared_memory_format, u64 aruid); + AruidData* GetAruidData(u64 aruid); + AruidData* GetAruidDataByIndex(std::size_t aruid_index); + + bool IsVibrationAruidActive(u64 aruid) const; u64 GetIndexFromAruid(u64 aruid); @@ -52,46 +110,12 @@ public: Result UnregisterCoreAppletResource(); private: - static constexpr std::size_t AruidIndexMax = 0x20; - - enum RegistrationStatus : u32 { - None, - Initialized, - PendingDelete, - }; - - struct DataStatusFlag { - union { - u32 raw{}; - - BitField<0, 1, u32> is_initialized; - BitField<1, 1, u32> is_assigned; - BitField<16, 1, u32> enable_pad_input; - BitField<17, 1, u32> enable_six_axis_sensor; - BitField<18, 1, u32> bit_18; - BitField<19, 1, u32> is_palma_connectable; - BitField<20, 1, u32> enable_palma_boost_mode; - BitField<21, 1, u32> enable_touchscreen; - }; - }; - - struct AruidRegisterList { - std::array<RegistrationStatus, AruidIndexMax> flag{}; - std::array<u64, AruidIndexMax> aruid{}; - }; - static_assert(sizeof(AruidRegisterList) == 0x180, "AruidRegisterList is an invalid size"); - - struct AruidData { - DataStatusFlag flag{}; - u64 aruid{}; - SharedMemoryFormat* shared_memory_format{nullptr}; - }; - u64 active_aruid{}; AruidRegisterList registration_list{}; std::array<AruidData, AruidIndexMax> data{}; std::array<SharedMemoryHolder, AruidIndexMax> shared_memory_holder{}; s32 ref_counter{}; + u64 active_vibration_aruid; Core::System& system; }; diff --git a/src/core/hle/service/hid/controllers/capture_button.cpp b/src/core/hle/service/hid/controllers/capture_button.cpp new file mode 100644 index 000000000..8b486fcb5 --- /dev/null +++ b/src/core/hle/service/hid/controllers/capture_button.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/core_timing.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/capture_button.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" + +namespace Service::HID { + +CaptureButton::CaptureButton(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {} + +CaptureButton::~CaptureButton() = default; + +void CaptureButton::OnInit() {} + +void CaptureButton::OnRelease() {} + +void CaptureButton::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + if (!smart_update) { + return; + } + + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + auto& header = data->shared_memory_format->capture_button.header; + header.timestamp = core_timing.GetGlobalTimeNs().count(); + header.total_entry_count = 17; + header.entry_count = 0; + header.last_entry_index = 0; +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/stubbed.h b/src/core/hle/service/hid/controllers/capture_button.h index d2052fb17..dcc4715c5 100644 --- a/src/core/hle/service/hid/controllers/stubbed.h +++ b/src/core/hle/service/hid/controllers/capture_button.h @@ -6,12 +6,11 @@ #include "core/hle/service/hid/controllers/controller_base.h" namespace Service::HID { -struct CommonHeader; -class Controller_Stubbed final : public ControllerBase { +class CaptureButton final : public ControllerBase { public: - explicit Controller_Stubbed(Core::HID::HIDCore& hid_core_, CommonHeader& ring_lifo_header); - ~Controller_Stubbed() override; + explicit CaptureButton(Core::HID::HIDCore& hid_core_); + ~CaptureButton() override; // Called when the controller is initialized void OnInit() override; @@ -23,7 +22,6 @@ public: void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; private: - CommonHeader& header; bool smart_update{}; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/console_six_axis.cpp b/src/core/hle/service/hid/controllers/console_six_axis.cpp index 3961d2b5f..8eba2c292 100644 --- a/src/core/hle/service/hid/controllers/console_six_axis.cpp +++ b/src/core/hle/service/hid/controllers/console_six_axis.cpp @@ -5,13 +5,11 @@ #include "core/hid/emulated_console.h" #include "core/hid/hid_core.h" #include "core/hle/service/hid/controllers/console_six_axis.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" namespace Service::HID { -ConsoleSixAxis::ConsoleSixAxis(Core::HID::HIDCore& hid_core_, - ConsoleSixAxisSensorSharedMemoryFormat& console_shared_memory) - : ControllerBase{hid_core_}, shared_memory{console_shared_memory} { +ConsoleSixAxis::ConsoleSixAxis(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} { console = hid_core.GetEmulatedConsole(); } @@ -22,6 +20,15 @@ void ConsoleSixAxis::OnInit() {} void ConsoleSixAxis::OnRelease() {} void ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + ConsoleSixAxisSensorSharedMemoryFormat& shared_memory = data->shared_memory_format->console; + if (!IsControllerActivated()) { return; } diff --git a/src/core/hle/service/hid/controllers/console_six_axis.h b/src/core/hle/service/hid/controllers/console_six_axis.h index 3d1c9ce23..e3351f83c 100644 --- a/src/core/hle/service/hid/controllers/console_six_axis.h +++ b/src/core/hle/service/hid/controllers/console_six_axis.h @@ -10,12 +10,9 @@ class EmulatedConsole; } // namespace Core::HID namespace Service::HID { -struct ConsoleSixAxisSensorSharedMemoryFormat; - class ConsoleSixAxis final : public ControllerBase { public: - explicit ConsoleSixAxis(Core::HID::HIDCore& hid_core_, - ConsoleSixAxisSensorSharedMemoryFormat& console_shared_memory); + explicit ConsoleSixAxis(Core::HID::HIDCore& hid_core_); ~ConsoleSixAxis() override; // Called when the controller is initialized @@ -28,7 +25,6 @@ public: void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; private: - ConsoleSixAxisSensorSharedMemoryFormat& shared_memory; Core::HID::EmulatedConsole* console = nullptr; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/controller_base.cpp b/src/core/hle/service/hid/controllers/controller_base.cpp index 0bcd87062..2083ccfad 100644 --- a/src/core/hle/service/hid/controllers/controller_base.cpp +++ b/src/core/hle/service/hid/controllers/controller_base.cpp @@ -31,4 +31,9 @@ void ControllerBase::DeactivateController() { bool ControllerBase::IsControllerActivated() const { return is_activated; } + +void ControllerBase::SetAppletResource(std::shared_ptr<AppletResource> resource) { + applet_resource = resource; +} + } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/controller_base.h b/src/core/hle/service/hid/controllers/controller_base.h index 4326c7821..759ae0053 100644 --- a/src/core/hle/service/hid/controllers/controller_base.h +++ b/src/core/hle/service/hid/controllers/controller_base.h @@ -3,8 +3,11 @@ #pragma once +#include <memory> + #include "common/common_types.h" #include "core/hle/result.h" +#include "core/hle/service/hid/controllers/applet_resource.h" namespace Core::Timing { class CoreTiming; @@ -12,7 +15,7 @@ class CoreTiming; namespace Core::HID { class HIDCore; -} +} // namespace Core::HID namespace Service::HID { class ControllerBase { @@ -39,8 +42,11 @@ public: bool IsControllerActivated() const; + void SetAppletResource(std::shared_ptr<AppletResource> resource); + protected: bool is_activated{false}; + std::shared_ptr<AppletResource> applet_resource{nullptr}; Core::HID::HIDCore& hid_core; }; diff --git a/src/core/hle/service/hid/controllers/debug_mouse.cpp b/src/core/hle/service/hid/controllers/debug_mouse.cpp new file mode 100644 index 000000000..f2f1a27f8 --- /dev/null +++ b/src/core/hle/service/hid/controllers/debug_mouse.cpp @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/core_timing.h" +#include "core/frontend/emu_window.h" +#include "core/hid/emulated_devices.h" +#include "core/hid/hid_core.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/debug_mouse.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" + +namespace Service::HID { + +DebugMouse::DebugMouse(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} { + emulated_devices = hid_core.GetEmulatedDevices(); +} + +DebugMouse::~DebugMouse() = default; + +void DebugMouse::OnInit() {} +void DebugMouse::OnRelease() {} + +void DebugMouse::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + MouseSharedMemoryFormat& shared_memory = data->shared_memory_format->debug_mouse; + + if (!IsControllerActivated()) { + shared_memory.mouse_lifo.buffer_count = 0; + shared_memory.mouse_lifo.buffer_tail = 0; + return; + } + + next_state = {}; + + const auto& last_entry = shared_memory.mouse_lifo.ReadCurrentEntry().state; + next_state.sampling_number = last_entry.sampling_number + 1; + + if (Settings::values.mouse_enabled) { + const auto& mouse_button_state = emulated_devices->GetMouseButtons(); + const auto& mouse_position_state = emulated_devices->GetMousePosition(); + const auto& mouse_wheel_state = emulated_devices->GetMouseWheel(); + next_state.attribute.is_connected.Assign(1); + next_state.x = static_cast<s32>(mouse_position_state.x * Layout::ScreenUndocked::Width); + next_state.y = static_cast<s32>(mouse_position_state.y * Layout::ScreenUndocked::Height); + next_state.delta_x = next_state.x - last_entry.x; + next_state.delta_y = next_state.y - last_entry.y; + next_state.delta_wheel_x = mouse_wheel_state.x - last_mouse_wheel_state.x; + next_state.delta_wheel_y = mouse_wheel_state.y - last_mouse_wheel_state.y; + + last_mouse_wheel_state = mouse_wheel_state; + next_state.button = mouse_button_state; + } + + shared_memory.mouse_lifo.WriteNextEntry(next_state); +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/debug_mouse.h b/src/core/hle/service/hid/controllers/debug_mouse.h new file mode 100644 index 000000000..ec939fa9f --- /dev/null +++ b/src/core/hle/service/hid/controllers/debug_mouse.h @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/service/hid/controllers/controller_base.h" + +namespace Core::HID { +class EmulatedDevices; +struct MouseState; +struct AnalogStickState; +} // namespace Core::HID + +namespace Service::HID { +class DebugMouse final : public ControllerBase { +public: + explicit DebugMouse(Core::HID::HIDCore& hid_core_); + ~DebugMouse() override; + + // Called when the controller is initialized + void OnInit() override; + + // When the controller is released + void OnRelease() override; + + // When the controller is requesting an update for the shared memory + void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; + +private: + Core::HID::MouseState next_state{}; + Core::HID::AnalogStickState last_mouse_wheel_state{}; + Core::HID::EmulatedDevices* emulated_devices = nullptr; +}; +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/debug_pad.cpp b/src/core/hle/service/hid/controllers/debug_pad.cpp index 7d2370b4f..1811cf620 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.cpp +++ b/src/core/hle/service/hid/controllers/debug_pad.cpp @@ -6,14 +6,13 @@ #include "core/hid/emulated_controller.h" #include "core/hid/hid_core.h" #include "core/hid/hid_types.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/debug_pad.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" namespace Service::HID { -DebugPad::DebugPad(Core::HID::HIDCore& hid_core_, - DebugPadSharedMemoryFormat& debug_pad_shared_memory) - : ControllerBase{hid_core_}, shared_memory{debug_pad_shared_memory} { +DebugPad::DebugPad(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} { controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Other); } @@ -24,6 +23,15 @@ void DebugPad::OnInit() {} void DebugPad::OnRelease() {} void DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + DebugPadSharedMemoryFormat& shared_memory = data->shared_memory_format->debug_pad; + if (!IsControllerActivated()) { shared_memory.debug_pad_lifo.buffer_count = 0; shared_memory.debug_pad_lifo.buffer_tail = 0; diff --git a/src/core/hle/service/hid/controllers/debug_pad.h b/src/core/hle/service/hid/controllers/debug_pad.h index 8ab29eca8..dd00b2402 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.h +++ b/src/core/hle/service/hid/controllers/debug_pad.h @@ -15,12 +15,9 @@ class CoreTiming; } namespace Service::HID { -struct DebugPadSharedMemoryFormat; - class DebugPad final : public ControllerBase { public: - explicit DebugPad(Core::HID::HIDCore& hid_core_, - DebugPadSharedMemoryFormat& debug_pad_shared_memory); + explicit DebugPad(Core::HID::HIDCore& hid_core_); ~DebugPad() override; // Called when the controller is initialized @@ -34,7 +31,6 @@ public: private: DebugPadState next_state{}; - DebugPadSharedMemoryFormat& shared_memory; Core::HID::EmulatedController* controller = nullptr; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/digitizer.cpp b/src/core/hle/service/hid/controllers/digitizer.cpp new file mode 100644 index 000000000..c01580fd6 --- /dev/null +++ b/src/core/hle/service/hid/controllers/digitizer.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/core_timing.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/digitizer.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" + +namespace Service::HID { + +Digitizer::Digitizer(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {} + +Digitizer::~Digitizer() = default; + +void Digitizer::OnInit() {} + +void Digitizer::OnRelease() {} + +void Digitizer::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + if (!smart_update) { + return; + } + + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + auto& header = data->shared_memory_format->digitizer.header; + header.timestamp = core_timing.GetGlobalTimeNs().count(); + header.total_entry_count = 17; + header.entry_count = 0; + header.last_entry_index = 0; +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/digitizer.h b/src/core/hle/service/hid/controllers/digitizer.h new file mode 100644 index 000000000..d81f814c3 --- /dev/null +++ b/src/core/hle/service/hid/controllers/digitizer.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/service/hid/controllers/controller_base.h" + +namespace Service::HID { + +class Digitizer final : public ControllerBase { +public: + explicit Digitizer(Core::HID::HIDCore& hid_core_); + ~Digitizer() override; + + // Called when the controller is initialized + void OnInit() override; + + // When the controller is released + void OnRelease() override; + + // When the controller is requesting an update for the shared memory + void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; + +private: + bool smart_update{}; +}; +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index f658005f6..6e686fe65 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -6,8 +6,9 @@ #include "core/frontend/emu_window.h" #include "core/hid/emulated_console.h" #include "core/hid/hid_core.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/gesture.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" namespace Service::HID { // HW is around 700, value is set to 400 to make it easier to trigger with mouse @@ -21,24 +22,40 @@ constexpr f32 Square(s32 num) { return static_cast<f32>(num * num); } -Gesture::Gesture(Core::HID::HIDCore& hid_core_, GestureSharedMemoryFormat& gesture_shared_memory) - : ControllerBase(hid_core_), shared_memory{gesture_shared_memory} { +Gesture::Gesture(Core::HID::HIDCore& hid_core_) : ControllerBase(hid_core_) { console = hid_core.GetEmulatedConsole(); } Gesture::~Gesture() = default; void Gesture::OnInit() { - shared_memory.gesture_lifo.buffer_count = 0; - shared_memory.gesture_lifo.buffer_tail = 0; + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + shared_memory = &data->shared_memory_format->gesture; + shared_memory->gesture_lifo.buffer_count = 0; + shared_memory->gesture_lifo.buffer_tail = 0; force_update = true; } void Gesture::OnRelease() {} void Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + shared_memory = &data->shared_memory_format->gesture; + if (!IsControllerActivated()) { - shared_memory.gesture_lifo.buffer_count = 0; - shared_memory.gesture_lifo.buffer_tail = 0; + shared_memory->gesture_lifo.buffer_count = 0; + shared_memory->gesture_lifo.buffer_tail = 0; return; } @@ -46,7 +63,7 @@ void Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing) { GestureProperties gesture = GetGestureProperties(); f32 time_difference = - static_cast<f32>(shared_memory.gesture_lifo.timestamp - last_update_timestamp) / + static_cast<f32>(shared_memory->gesture_lifo.timestamp - last_update_timestamp) / (1000 * 1000 * 1000); // Only update if necessary @@ -54,7 +71,7 @@ void Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing) { return; } - last_update_timestamp = shared_memory.gesture_lifo.timestamp; + last_update_timestamp = shared_memory->gesture_lifo.timestamp; UpdateGestureSharedMemory(gesture, time_difference); } @@ -97,7 +114,7 @@ void Gesture::UpdateGestureSharedMemory(GestureProperties& gesture, f32 time_dif GestureType type = GestureType::Idle; GestureAttribute attributes{}; - const auto& last_entry = shared_memory.gesture_lifo.ReadCurrentEntry().state; + const auto& last_entry = shared_memory->gesture_lifo.ReadCurrentEntry().state; // Reset next state to default next_state.sampling_number = last_entry.sampling_number + 1; @@ -127,7 +144,7 @@ void Gesture::UpdateGestureSharedMemory(GestureProperties& gesture, f32 time_dif next_state.points = gesture.points; last_gesture = gesture; - shared_memory.gesture_lifo.WriteNextEntry(next_state); + shared_memory->gesture_lifo.WriteNextEntry(next_state); } void Gesture::NewGesture(GestureProperties& gesture, GestureType& type, @@ -300,7 +317,7 @@ void Gesture::SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_ } const GestureState& Gesture::GetLastGestureEntry() const { - return shared_memory.gesture_lifo.ReadCurrentEntry().state; + return shared_memory->gesture_lifo.ReadCurrentEntry().state; } GestureProperties Gesture::GetGestureProperties() { diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index 41fdfcd03..78da1552a 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -18,8 +18,7 @@ struct GestureSharedMemoryFormat; class Gesture final : public ControllerBase { public: - explicit Gesture(Core::HID::HIDCore& hid_core_, - GestureSharedMemoryFormat& gesture_shared_memory); + explicit Gesture(Core::HID::HIDCore& hid_core_); ~Gesture() override; // Called when the controller is initialized @@ -74,7 +73,7 @@ private: GestureProperties GetGestureProperties(); GestureState next_state{}; - GestureSharedMemoryFormat& shared_memory; + GestureSharedMemoryFormat* shared_memory; Core::HID::EmulatedConsole* console = nullptr; std::array<Core::HID::TouchFinger, MAX_POINTS> fingers{}; diff --git a/src/core/hle/service/hid/controllers/home_button.cpp b/src/core/hle/service/hid/controllers/home_button.cpp new file mode 100644 index 000000000..71dd9bc08 --- /dev/null +++ b/src/core/hle/service/hid/controllers/home_button.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/core_timing.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/home_button.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" + +namespace Service::HID { + +HomeButton::HomeButton(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {} + +HomeButton::~HomeButton() = default; + +void HomeButton::OnInit() {} + +void HomeButton::OnRelease() {} + +void HomeButton::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + if (!smart_update) { + return; + } + + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + auto& header = data->shared_memory_format->home_button.header; + header.timestamp = core_timing.GetGlobalTimeNs().count(); + header.total_entry_count = 17; + header.entry_count = 0; + header.last_entry_index = 0; +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/home_button.h b/src/core/hle/service/hid/controllers/home_button.h new file mode 100644 index 000000000..e91c2aa5d --- /dev/null +++ b/src/core/hle/service/hid/controllers/home_button.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/service/hid/controllers/controller_base.h" + +namespace Service::HID { + +class HomeButton final : public ControllerBase { +public: + explicit HomeButton(Core::HID::HIDCore& hid_core_); + ~HomeButton() override; + + // Called when the controller is initialized + void OnInit() override; + + // When the controller is released + void OnRelease() override; + + // When the controller is requesting an update for the shared memory + void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; + +private: + bool smart_update{}; +}; +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp index 871e5036a..c72b3e5ce 100644 --- a/src/core/hle/service/hid/controllers/keyboard.cpp +++ b/src/core/hle/service/hid/controllers/keyboard.cpp @@ -5,14 +5,13 @@ #include "core/core_timing.h" #include "core/hid/emulated_devices.h" #include "core/hid/hid_core.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/keyboard.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" namespace Service::HID { -Keyboard::Keyboard(Core::HID::HIDCore& hid_core_, - KeyboardSharedMemoryFormat& keyboard_shared_memory) - : ControllerBase{hid_core_}, shared_memory{keyboard_shared_memory} { +Keyboard::Keyboard(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} { emulated_devices = hid_core.GetEmulatedDevices(); } @@ -23,6 +22,15 @@ void Keyboard::OnInit() {} void Keyboard::OnRelease() {} void Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + KeyboardSharedMemoryFormat& shared_memory = data->shared_memory_format->keyboard; + if (!IsControllerActivated()) { shared_memory.keyboard_lifo.buffer_count = 0; shared_memory.keyboard_lifo.buffer_tail = 0; diff --git a/src/core/hle/service/hid/controllers/keyboard.h b/src/core/hle/service/hid/controllers/keyboard.h index 4d72171b9..e8ca326c6 100644 --- a/src/core/hle/service/hid/controllers/keyboard.h +++ b/src/core/hle/service/hid/controllers/keyboard.h @@ -7,12 +7,9 @@ #include "core/hle/service/hid/controllers/types/keyboard_types.h" namespace Service::HID { -struct KeyboardSharedMemoryFormat; - class Keyboard final : public ControllerBase { public: - explicit Keyboard(Core::HID::HIDCore& hid_core_, - KeyboardSharedMemoryFormat& keyboard_shared_memory); + explicit Keyboard(Core::HID::HIDCore& hid_core_); ~Keyboard() override; // Called when the controller is initialized @@ -26,7 +23,6 @@ public: private: KeyboardState next_state{}; - KeyboardSharedMemoryFormat& shared_memory; Core::HID::EmulatedDevices* emulated_devices = nullptr; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/mouse.cpp b/src/core/hle/service/hid/controllers/mouse.cpp index de5b2c804..58deafbc5 100644 --- a/src/core/hle/service/hid/controllers/mouse.cpp +++ b/src/core/hle/service/hid/controllers/mouse.cpp @@ -5,13 +5,13 @@ #include "core/frontend/emu_window.h" #include "core/hid/emulated_devices.h" #include "core/hid/hid_core.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/mouse.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" namespace Service::HID { -Mouse::Mouse(Core::HID::HIDCore& hid_core_, MouseSharedMemoryFormat& mouse_shared_memory) - : ControllerBase{hid_core_}, shared_memory{mouse_shared_memory} { +Mouse::Mouse(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} { emulated_devices = hid_core.GetEmulatedDevices(); } @@ -21,6 +21,15 @@ void Mouse::OnInit() {} void Mouse::OnRelease() {} void Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + MouseSharedMemoryFormat& shared_memory = data->shared_memory_format->mouse; + if (!IsControllerActivated()) { shared_memory.mouse_lifo.buffer_count = 0; shared_memory.mouse_lifo.buffer_tail = 0; diff --git a/src/core/hle/service/hid/controllers/mouse.h b/src/core/hle/service/hid/controllers/mouse.h index 363f316a5..cefad956c 100644 --- a/src/core/hle/service/hid/controllers/mouse.h +++ b/src/core/hle/service/hid/controllers/mouse.h @@ -12,11 +12,9 @@ struct AnalogStickState; } // namespace Core::HID namespace Service::HID { -struct MouseSharedMemoryFormat; - class Mouse final : public ControllerBase { public: - explicit Mouse(Core::HID::HIDCore& hid_core_, MouseSharedMemoryFormat& mouse_shared_memory); + explicit Mouse(Core::HID::HIDCore& hid_core_); ~Mouse() override; // Called when the controller is initialized @@ -31,7 +29,6 @@ public: private: Core::HID::MouseState next_state{}; Core::HID::AnalogStickState last_mouse_wheel_state{}; - MouseSharedMemoryFormat& shared_memory; Core::HID::EmulatedDevices* emulated_devices = nullptr; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 53a737cf5..c7aa606bc 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -16,8 +16,9 @@ #include "core/hid/hid_core.h" #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/npad.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" #include "core/hle/service/hid/errors.h" #include "core/hle/service/hid/hid_util.h" #include "core/hle/service/kernel_helpers.h" @@ -30,12 +31,10 @@ constexpr std::array<Core::HID::NpadIdType, 10> npad_id_list{ Core::HID::NpadIdType::Handheld, }; -NPad::NPad(Core::HID::HIDCore& hid_core_, NpadSharedMemoryFormat& npad_shared_memory_format, - KernelHelpers::ServiceContext& service_context_) +NPad::NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_) : ControllerBase{hid_core_}, service_context{service_context_} { for (std::size_t i = 0; i < controller_data.size(); ++i) { auto& controller = controller_data[i]; - controller.shared_memory = &npad_shared_memory_format.npad_entry[i].internal_state; controller.device = hid_core.GetEmulatedControllerByIndex(i); controller.vibration[Core::HID::EmulatedDeviceIndex::LeftIndex].latest_vibration_value = Core::HID::DEFAULT_VIBRATION_VALUE; @@ -297,12 +296,20 @@ void NPad::InitNewlyAddedController(Core::HID::NpadIdType npad_id) { } void NPad::OnInit() { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + if (!IsControllerActivated()) { return; } for (std::size_t i = 0; i < controller_data.size(); ++i) { auto& controller = controller_data[i]; + controller.shared_memory = &data->shared_memory_format->npad.npad_entry[i].internal_state; controller.styleset_changed_event = service_context.CreateEvent(fmt::format("npad:NpadStyleSetChanged_{}", i)); } @@ -355,7 +362,9 @@ void NPad::OnRelease() { is_controller_initialized = false; for (std::size_t i = 0; i < controller_data.size(); ++i) { auto& controller = controller_data[i]; - service_context.CloseEvent(controller.styleset_changed_event); + if (controller.styleset_changed_event) { + service_context.CloseEvent(controller.styleset_changed_event); + } for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) { VibrateControllerAtIndex(controller.device->GetNpadIdType(), device_idx, {}); } @@ -432,12 +441,20 @@ void NPad::RequestPadStateUpdate(Core::HID::NpadIdType npad_id) { } void NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + if (!IsControllerActivated()) { return; } for (std::size_t i = 0; i < controller_data.size(); ++i) { auto& controller = controller_data[i]; + controller.shared_memory = &data->shared_memory_format->npad.npad_entry[i].internal_state; auto* npad = controller.shared_memory; const auto& controller_type = controller.device->GetNpadStyleIndex(); @@ -976,30 +993,6 @@ Result NPad::ResetIsSixAxisSensorDeviceNewlyAssigned( return ResultSuccess; } -NpadSixAxisSensorLifo& NPad::GetSixAxisFullkeyLifo(Core::HID::NpadIdType npad_id) { - return GetControllerFromNpadIdType(npad_id).shared_memory->sixaxis_fullkey_lifo; -} - -NpadSixAxisSensorLifo& NPad::GetSixAxisHandheldLifo(Core::HID::NpadIdType npad_id) { - return GetControllerFromNpadIdType(npad_id).shared_memory->sixaxis_handheld_lifo; -} - -NpadSixAxisSensorLifo& NPad::GetSixAxisDualLeftLifo(Core::HID::NpadIdType npad_id) { - return GetControllerFromNpadIdType(npad_id).shared_memory->sixaxis_dual_left_lifo; -} - -NpadSixAxisSensorLifo& NPad::GetSixAxisDualRightLifo(Core::HID::NpadIdType npad_id) { - return GetControllerFromNpadIdType(npad_id).shared_memory->sixaxis_dual_right_lifo; -} - -NpadSixAxisSensorLifo& NPad::GetSixAxisLeftLifo(Core::HID::NpadIdType npad_id) { - return GetControllerFromNpadIdType(npad_id).shared_memory->sixaxis_left_lifo; -} - -NpadSixAxisSensorLifo& NPad::GetSixAxisRightLifo(Core::HID::NpadIdType npad_id) { - return GetControllerFromNpadIdType(npad_id).shared_memory->sixaxis_right_lifo; -} - Result NPad::MergeSingleJoyAsDualJoy(Core::HID::NpadIdType npad_id_1, Core::HID::NpadIdType npad_id_2) { if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) { diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index 4e2412356..80cfcb2bb 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -30,14 +30,14 @@ class ServiceContext; union Result; namespace Service::HID { +class AppletResource; struct NpadInternalState; struct NpadSixAxisSensorLifo; struct NpadSharedMemoryFormat; class NPad final : public ControllerBase { public: - explicit NPad(Core::HID::HIDCore& hid_core_, NpadSharedMemoryFormat& npad_shared_memory_format, - KernelHelpers::ServiceContext& service_context_); + explicit NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_); ~NPad() override; // Called when the controller is initialized @@ -106,13 +106,6 @@ public: Result ResetIsSixAxisSensorDeviceNewlyAssigned( const Core::HID::SixAxisSensorHandle& sixaxis_handle); - NpadSixAxisSensorLifo& GetSixAxisFullkeyLifo(Core::HID::NpadIdType npad_id); - NpadSixAxisSensorLifo& GetSixAxisHandheldLifo(Core::HID::NpadIdType npad_id); - NpadSixAxisSensorLifo& GetSixAxisDualLeftLifo(Core::HID::NpadIdType npad_id); - NpadSixAxisSensorLifo& GetSixAxisDualRightLifo(Core::HID::NpadIdType npad_id); - NpadSixAxisSensorLifo& GetSixAxisLeftLifo(Core::HID::NpadIdType npad_id); - NpadSixAxisSensorLifo& GetSixAxisRightLifo(Core::HID::NpadIdType npad_id); - Result GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const; Result IsUnintendedHomeButtonInputProtectionEnabled(Core::HID::NpadIdType npad_id, bool& is_enabled) const; diff --git a/src/core/hle/service/hid/controllers/shared_memory_holder.cpp b/src/core/hle/service/hid/controllers/shared_memory_holder.cpp index 51581188e..0bc5169c6 100644 --- a/src/core/hle/service/hid/controllers/shared_memory_holder.cpp +++ b/src/core/hle/service/hid/controllers/shared_memory_holder.cpp @@ -3,8 +3,9 @@ #include "core/core.h" #include "core/hle/kernel/k_shared_memory.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/shared_memory_holder.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" #include "core/hle/service/hid/errors.h" namespace Service::HID { diff --git a/src/core/hle/service/hid/controllers/six_axis.cpp b/src/core/hle/service/hid/controllers/six_axis.cpp index 36b72f9ea..a5a67dea6 100644 --- a/src/core/hle/service/hid/controllers/six_axis.cpp +++ b/src/core/hle/service/hid/controllers/six_axis.cpp @@ -6,8 +6,8 @@ #include "core/hid/emulated_controller.h" #include "core/hid/hid_core.h" #include "core/hle/service/hid/controllers/npad.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" #include "core/hle/service/hid/controllers/six_axis.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" #include "core/hle/service/hid/errors.h" #include "core/hle/service/hid/hid_util.h" @@ -27,14 +27,20 @@ void SixAxis::OnInit() {} void SixAxis::OnRelease() {} void SixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + if (!IsControllerActivated()) { return; } for (std::size_t i = 0; i < controller_data.size(); ++i) { + NpadSharedMemoryEntry& shared_memory = data->shared_memory_format->npad.npad_entry[i]; auto& controller = controller_data[i]; - - const auto npad_id = IndexToNpadIdType(i); const auto& controller_type = controller.device->GetNpadStyleIndex(); if (controller_type == Core::HID::NpadStyleIndex::None || @@ -50,12 +56,12 @@ void SixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { auto& sixaxis_left_lifo_state = controller.sixaxis_left_lifo_state; auto& sixaxis_right_lifo_state = controller.sixaxis_right_lifo_state; - auto& sixaxis_fullkey_lifo = npad->GetSixAxisFullkeyLifo(npad_id); - auto& sixaxis_handheld_lifo = npad->GetSixAxisHandheldLifo(npad_id); - auto& sixaxis_dual_left_lifo = npad->GetSixAxisDualLeftLifo(npad_id); - auto& sixaxis_dual_right_lifo = npad->GetSixAxisDualRightLifo(npad_id); - auto& sixaxis_left_lifo = npad->GetSixAxisLeftLifo(npad_id); - auto& sixaxis_right_lifo = npad->GetSixAxisRightLifo(npad_id); + auto& sixaxis_fullkey_lifo = shared_memory.internal_state.sixaxis_fullkey_lifo; + auto& sixaxis_handheld_lifo = shared_memory.internal_state.sixaxis_handheld_lifo; + auto& sixaxis_dual_left_lifo = shared_memory.internal_state.sixaxis_dual_left_lifo; + auto& sixaxis_dual_right_lifo = shared_memory.internal_state.sixaxis_dual_right_lifo; + auto& sixaxis_left_lifo = shared_memory.internal_state.sixaxis_left_lifo; + auto& sixaxis_right_lifo = shared_memory.internal_state.sixaxis_right_lifo; // Clear previous state sixaxis_fullkey_state = {}; diff --git a/src/core/hle/service/hid/controllers/sleep_button.cpp b/src/core/hle/service/hid/controllers/sleep_button.cpp new file mode 100644 index 000000000..978dc4c1f --- /dev/null +++ b/src/core/hle/service/hid/controllers/sleep_button.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/core_timing.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/sleep_button.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" + +namespace Service::HID { + +SleepButton::SleepButton(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {} + +SleepButton::~SleepButton() = default; + +void SleepButton::OnInit() {} + +void SleepButton::OnRelease() {} + +void SleepButton::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + if (!smart_update) { + return; + } + + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + auto& header = data->shared_memory_format->capture_button.header; + header.timestamp = core_timing.GetGlobalTimeNs().count(); + header.total_entry_count = 17; + header.entry_count = 0; + header.last_entry_index = 0; +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/sleep_button.h b/src/core/hle/service/hid/controllers/sleep_button.h new file mode 100644 index 000000000..59964bf63 --- /dev/null +++ b/src/core/hle/service/hid/controllers/sleep_button.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/service/hid/controllers/controller_base.h" + +namespace Service::HID { + +class SleepButton final : public ControllerBase { +public: + explicit SleepButton(Core::HID::HIDCore& hid_core_); + ~SleepButton() override; + + // Called when the controller is initialized + void OnInit() override; + + // When the controller is released + void OnRelease() override; + + // When the controller is requesting an update for the shared memory + void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; + +private: + bool smart_update{}; +}; +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/stubbed.cpp b/src/core/hle/service/hid/controllers/stubbed.cpp deleted file mode 100644 index e2a5f5d79..000000000 --- a/src/core/hle/service/hid/controllers/stubbed.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "core/core_timing.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" -#include "core/hle/service/hid/controllers/stubbed.h" - -namespace Service::HID { - -Controller_Stubbed::Controller_Stubbed(Core::HID::HIDCore& hid_core_, - CommonHeader& ring_lifo_header) - : ControllerBase{hid_core_}, header{ring_lifo_header} {} - -Controller_Stubbed::~Controller_Stubbed() = default; - -void Controller_Stubbed::OnInit() {} - -void Controller_Stubbed::OnRelease() {} - -void Controller_Stubbed::OnUpdate(const Core::Timing::CoreTiming& core_timing) { - if (!smart_update) { - return; - } - - header.timestamp = core_timing.GetGlobalTimeNs().count(); - header.total_entry_count = 17; - header.entry_count = 0; - header.last_entry_index = 0; -} - -} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index 469750006..291dc707e 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -8,15 +8,14 @@ #include "core/frontend/emu_window.h" #include "core/hid/emulated_console.h" #include "core/hid/hid_core.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/touchscreen.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" namespace Service::HID { -TouchScreen::TouchScreen(Core::HID::HIDCore& hid_core_, - TouchScreenSharedMemoryFormat& touch_shared_memory) - : ControllerBase{hid_core_}, shared_memory{touch_shared_memory}, - touchscreen_width(Layout::ScreenUndocked::Width), +TouchScreen::TouchScreen(Core::HID::HIDCore& hid_core_) + : ControllerBase{hid_core_}, touchscreen_width(Layout::ScreenUndocked::Width), touchscreen_height(Layout::ScreenUndocked::Height) { console = hid_core.GetEmulatedConsole(); } @@ -28,6 +27,14 @@ void TouchScreen::OnInit() {} void TouchScreen::OnRelease() {} void TouchScreen::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + TouchScreenSharedMemoryFormat& shared_memory = data->shared_memory_format->touch_screen; shared_memory.touch_screen_lifo.timestamp = core_timing.GetGlobalTimeNs().count(); if (!IsControllerActivated()) { diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 5b6305bfc..945d359be 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -18,8 +18,7 @@ struct TouchScreenSharedMemoryFormat; class TouchScreen final : public ControllerBase { public: - explicit TouchScreen(Core::HID::HIDCore& hid_core_, - TouchScreenSharedMemoryFormat& touch_shared_memory); + explicit TouchScreen(Core::HID::HIDCore& hid_core_); ~TouchScreen() override; // Called when the controller is initialized @@ -35,7 +34,6 @@ public: private: TouchScreenState next_state{}; - TouchScreenSharedMemoryFormat& shared_memory; Core::HID::EmulatedConsole* console = nullptr; std::array<Core::HID::TouchFinger, MAX_FINGERS> fingers{}; diff --git a/src/core/hle/service/hid/controllers/shared_memory_format.h b/src/core/hle/service/hid/controllers/types/shared_memory_format.h index 2986c113e..2986c113e 100644 --- a/src/core/hle/service/hid/controllers/shared_memory_format.h +++ b/src/core/hle/service/hid/controllers/types/shared_memory_format.h diff --git a/src/core/hle/service/hid/controllers/unique_pad.cpp b/src/core/hle/service/hid/controllers/unique_pad.cpp new file mode 100644 index 000000000..8230501a5 --- /dev/null +++ b/src/core/hle/service/hid/controllers/unique_pad.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/core_timing.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" +#include "core/hle/service/hid/controllers/unique_pad.h" + +namespace Service::HID { + +UniquePad::UniquePad(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {} + +UniquePad::~UniquePad() = default; + +void UniquePad::OnInit() {} + +void UniquePad::OnRelease() {} + +void UniquePad::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + if (!smart_update) { + return; + } + + const u64 aruid = applet_resource->GetActiveAruid(); + auto* data = applet_resource->GetAruidData(aruid); + + if (data == nullptr) { + return; + } + + auto& header = data->shared_memory_format->capture_button.header; + header.timestamp = core_timing.GetGlobalTimeNs().count(); + header.total_entry_count = 17; + header.entry_count = 0; + header.last_entry_index = 0; +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/unique_pad.h b/src/core/hle/service/hid/controllers/unique_pad.h new file mode 100644 index 000000000..966368264 --- /dev/null +++ b/src/core/hle/service/hid/controllers/unique_pad.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/service/hid/controllers/controller_base.h" + +namespace Service::HID { + +class UniquePad final : public ControllerBase { +public: + explicit UniquePad(Core::HID::HIDCore& hid_core_); + ~UniquePad() override; + + // Called when the controller is initialized + void OnInit() override; + + // When the controller is released + void OnRelease() override; + + // When the controller is requesting an update for the shared memory + void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; + +private: + bool smart_update{}; +}; +} // namespace Service::HID diff --git a/src/core/hle/service/hid/hidbus.cpp b/src/core/hle/service/hid/hidbus.cpp index d12f9beb0..ffa7e144d 100644 --- a/src/core/hle/service/hid/hidbus.cpp +++ b/src/core/hle/service/hid/hidbus.cpp @@ -49,10 +49,10 @@ HidBus::HidBus(Core::System& system_) // Register update callbacks hidbus_update_event = Core::Timing::CreateEvent( "Hidbus::UpdateCallback", - [this](std::uintptr_t user_data, s64 time, + [this](s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { const auto guard = LockService(); - UpdateHidbus(user_data, ns_late); + UpdateHidbus(ns_late); return std::nullopt; }); @@ -61,10 +61,10 @@ HidBus::HidBus(Core::System& system_) } HidBus::~HidBus() { - system.CoreTiming().UnscheduleEvent(hidbus_update_event, 0); + system.CoreTiming().UnscheduleEvent(hidbus_update_event); } -void HidBus::UpdateHidbus(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) { +void HidBus::UpdateHidbus(std::chrono::nanoseconds ns_late) { if (is_hidbus_enabled) { for (std::size_t i = 0; i < devices.size(); ++i) { if (!devices[i].is_device_initializated) { diff --git a/src/core/hle/service/hid/hidbus.h b/src/core/hle/service/hid/hidbus.h index c29b5e882..85a1df133 100644 --- a/src/core/hle/service/hid/hidbus.h +++ b/src/core/hle/service/hid/hidbus.h @@ -108,7 +108,7 @@ private: void DisableJoyPollingReceiveMode(HLERequestContext& ctx); void SetStatusManagerType(HLERequestContext& ctx); - void UpdateHidbus(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); + void UpdateHidbus(std::chrono::nanoseconds ns_late); std::optional<std::size_t> GetDeviceIndexFromHandle(BusHandle handle) const; template <typename T> diff --git a/src/core/hle/service/hid/resource_manager.cpp b/src/core/hle/service/hid/resource_manager.cpp index 6c6cbd802..84b4be3ed 100644 --- a/src/core/hle/service/hid/resource_manager.cpp +++ b/src/core/hle/service/hid/resource_manager.cpp @@ -10,18 +10,23 @@ #include "core/hle/service/ipc_helpers.h" #include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/controllers/capture_button.h" #include "core/hle/service/hid/controllers/console_six_axis.h" +#include "core/hle/service/hid/controllers/debug_mouse.h" #include "core/hle/service/hid/controllers/debug_pad.h" +#include "core/hle/service/hid/controllers/digitizer.h" #include "core/hle/service/hid/controllers/gesture.h" +#include "core/hle/service/hid/controllers/home_button.h" #include "core/hle/service/hid/controllers/keyboard.h" #include "core/hle/service/hid/controllers/mouse.h" #include "core/hle/service/hid/controllers/npad.h" #include "core/hle/service/hid/controllers/palma.h" #include "core/hle/service/hid/controllers/seven_six_axis.h" -#include "core/hle/service/hid/controllers/shared_memory_format.h" #include "core/hle/service/hid/controllers/six_axis.h" -#include "core/hle/service/hid/controllers/stubbed.h" +#include "core/hle/service/hid/controllers/sleep_button.h" #include "core/hle/service/hid/controllers/touchscreen.h" +#include "core/hle/service/hid/controllers/types/shared_memory_format.h" +#include "core/hle/service/hid/controllers/unique_pad.h" namespace Service::HID { @@ -46,42 +51,13 @@ void ResourceManager::Initialize() { } system.HIDCore().ReloadInputDevices(); - is_initialized = true; -} - -void ResourceManager::InitializeController(u64 aruid) { - SharedMemoryFormat* shared_memory = nullptr; - const auto result = applet_resource->GetSharedMemoryFormat(&shared_memory, aruid); - if (result.IsError()) { - return; - } - - debug_pad = std::make_shared<DebugPad>(system.HIDCore(), shared_memory->debug_pad); - mouse = std::make_shared<Mouse>(system.HIDCore(), shared_memory->mouse); - debug_mouse = std::make_shared<DebugMouse>(system.HIDCore(), shared_memory->debug_mouse); - keyboard = std::make_shared<Keyboard>(system.HIDCore(), shared_memory->keyboard); - unique_pad = std::make_shared<UniquePad>(system.HIDCore(), shared_memory->unique_pad.header); - npad = std::make_shared<NPad>(system.HIDCore(), shared_memory->npad, service_context); - gesture = std::make_shared<Gesture>(system.HIDCore(), shared_memory->gesture); - touch_screen = std::make_shared<TouchScreen>(system.HIDCore(), shared_memory->touch_screen); - palma = std::make_shared<Palma>(system.HIDCore(), service_context); + InitializeHidCommonSampler(); + InitializeTouchScreenSampler(); + InitializeConsoleSixAxisSampler(); + InitializeAHidSampler(); - home_button = std::make_shared<HomeButton>(system.HIDCore(), shared_memory->home_button.header); - sleep_button = - std::make_shared<SleepButton>(system.HIDCore(), shared_memory->sleep_button.header); - capture_button = - std::make_shared<CaptureButton>(system.HIDCore(), shared_memory->capture_button.header); - digitizer = std::make_shared<Digitizer>(system.HIDCore(), shared_memory->digitizer.header); - - six_axis = std::make_shared<SixAxis>(system.HIDCore(), npad); - console_six_axis = std::make_shared<ConsoleSixAxis>(system.HIDCore(), shared_memory->console); - seven_six_axis = std::make_shared<SevenSixAxis>(system); - - // Homebrew doesn't try to activate some controllers, so we activate them by default - npad->Activate(); - six_axis->Activate(); - touch_screen->Activate(); + is_initialized = true; } std::shared_ptr<AppletResource> ResourceManager::GetAppletResource() const { @@ -165,16 +141,65 @@ Result ResourceManager::CreateAppletResource(u64 aruid) { if (result.IsError()) { return result; } + + // Homebrew doesn't try to activate some controllers, so we activate them by default + npad->Activate(); + six_axis->Activate(); + touch_screen->Activate(); + return GetNpad()->Activate(aruid); } Result ResourceManager::CreateAppletResourceImpl(u64 aruid) { std::scoped_lock lock{shared_mutex}; - const auto result = applet_resource->CreateAppletResource(aruid); - if (result.IsSuccess()) { - InitializeController(aruid); - } - return result; + return applet_resource->CreateAppletResource(aruid); +} + +void ResourceManager::InitializeHidCommonSampler() { + debug_pad = std::make_shared<DebugPad>(system.HIDCore()); + mouse = std::make_shared<Mouse>(system.HIDCore()); + debug_mouse = std::make_shared<DebugMouse>(system.HIDCore()); + keyboard = std::make_shared<Keyboard>(system.HIDCore()); + unique_pad = std::make_shared<UniquePad>(system.HIDCore()); + npad = std::make_shared<NPad>(system.HIDCore(), service_context); + gesture = std::make_shared<Gesture>(system.HIDCore()); + home_button = std::make_shared<HomeButton>(system.HIDCore()); + sleep_button = std::make_shared<SleepButton>(system.HIDCore()); + capture_button = std::make_shared<CaptureButton>(system.HIDCore()); + digitizer = std::make_shared<Digitizer>(system.HIDCore()); + + palma = std::make_shared<Palma>(system.HIDCore(), service_context); + six_axis = std::make_shared<SixAxis>(system.HIDCore(), npad); + + debug_pad->SetAppletResource(applet_resource); + digitizer->SetAppletResource(applet_resource); + keyboard->SetAppletResource(applet_resource); + npad->SetAppletResource(applet_resource); + six_axis->SetAppletResource(applet_resource); + mouse->SetAppletResource(applet_resource); + debug_mouse->SetAppletResource(applet_resource); + home_button->SetAppletResource(applet_resource); + sleep_button->SetAppletResource(applet_resource); + capture_button->SetAppletResource(applet_resource); +} + +void ResourceManager::InitializeTouchScreenSampler() { + gesture = std::make_shared<Gesture>(system.HIDCore()); + touch_screen = std::make_shared<TouchScreen>(system.HIDCore()); + + touch_screen->SetAppletResource(applet_resource); + gesture->SetAppletResource(applet_resource); +} + +void ResourceManager::InitializeConsoleSixAxisSampler() { + console_six_axis = std::make_shared<ConsoleSixAxis>(system.HIDCore()); + seven_six_axis = std::make_shared<SevenSixAxis>(system); + + console_six_axis->SetAppletResource(applet_resource); +} + +void ResourceManager::InitializeAHidSampler() { + // TODO } Result ResourceManager::RegisterCoreAppletResource() { @@ -227,8 +252,7 @@ void ResourceManager::EnableTouchScreen(u64 aruid, bool is_enabled) { applet_resource->EnableTouchScreen(aruid, is_enabled); } -void ResourceManager::UpdateControllers(std::uintptr_t user_data, - std::chrono::nanoseconds ns_late) { +void ResourceManager::UpdateControllers(std::chrono::nanoseconds ns_late) { auto& core_timing = system.CoreTiming(); debug_pad->OnUpdate(core_timing); digitizer->OnUpdate(core_timing); @@ -241,20 +265,19 @@ void ResourceManager::UpdateControllers(std::uintptr_t user_data, capture_button->OnUpdate(core_timing); } -void ResourceManager::UpdateNpad(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) { +void ResourceManager::UpdateNpad(std::chrono::nanoseconds ns_late) { auto& core_timing = system.CoreTiming(); npad->OnUpdate(core_timing); } -void ResourceManager::UpdateMouseKeyboard(std::uintptr_t user_data, - std::chrono::nanoseconds ns_late) { +void ResourceManager::UpdateMouseKeyboard(std::chrono::nanoseconds ns_late) { auto& core_timing = system.CoreTiming(); mouse->OnUpdate(core_timing); debug_mouse->OnUpdate(core_timing); keyboard->OnUpdate(core_timing); } -void ResourceManager::UpdateMotion(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) { +void ResourceManager::UpdateMotion(std::chrono::nanoseconds ns_late) { auto& core_timing = system.CoreTiming(); six_axis->OnUpdate(core_timing); seven_six_axis->OnUpdate(core_timing); @@ -273,34 +296,34 @@ IAppletResource::IAppletResource(Core::System& system_, std::shared_ptr<Resource // Register update callbacks npad_update_event = Core::Timing::CreateEvent( "HID::UpdatePadCallback", - [this, resource](std::uintptr_t user_data, s64 time, std::chrono::nanoseconds ns_late) - -> std::optional<std::chrono::nanoseconds> { + [this, resource]( + s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { const auto guard = LockService(); - resource->UpdateNpad(user_data, ns_late); + resource->UpdateNpad(ns_late); return std::nullopt; }); default_update_event = Core::Timing::CreateEvent( "HID::UpdateDefaultCallback", - [this, resource](std::uintptr_t user_data, s64 time, std::chrono::nanoseconds ns_late) - -> std::optional<std::chrono::nanoseconds> { + [this, resource]( + s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { const auto guard = LockService(); - resource->UpdateControllers(user_data, ns_late); + resource->UpdateControllers(ns_late); return std::nullopt; }); mouse_keyboard_update_event = Core::Timing::CreateEvent( "HID::UpdateMouseKeyboardCallback", - [this, resource](std::uintptr_t user_data, s64 time, std::chrono::nanoseconds ns_late) - -> std::optional<std::chrono::nanoseconds> { + [this, resource]( + s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { const auto guard = LockService(); - resource->UpdateMouseKeyboard(user_data, ns_late); + resource->UpdateMouseKeyboard(ns_late); return std::nullopt; }); motion_update_event = Core::Timing::CreateEvent( "HID::UpdateMotionCallback", - [this, resource](std::uintptr_t user_data, s64 time, std::chrono::nanoseconds ns_late) - -> std::optional<std::chrono::nanoseconds> { + [this, resource]( + s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { const auto guard = LockService(); - resource->UpdateMotion(user_data, ns_late); + resource->UpdateMotion(ns_late); return std::nullopt; }); @@ -314,10 +337,10 @@ IAppletResource::IAppletResource(Core::System& system_, std::shared_ptr<Resource } IAppletResource::~IAppletResource() { - system.CoreTiming().UnscheduleEvent(npad_update_event, 0); - system.CoreTiming().UnscheduleEvent(default_update_event, 0); - system.CoreTiming().UnscheduleEvent(mouse_keyboard_update_event, 0); - system.CoreTiming().UnscheduleEvent(motion_update_event, 0); + system.CoreTiming().UnscheduleEvent(npad_update_event); + system.CoreTiming().UnscheduleEvent(default_update_event); + system.CoreTiming().UnscheduleEvent(mouse_keyboard_update_event); + system.CoreTiming().UnscheduleEvent(motion_update_event); resource_manager->FreeAppletResourceId(aruid); } diff --git a/src/core/hle/service/hid/resource_manager.h b/src/core/hle/service/hid/resource_manager.h index 5ad7cb564..70d9b6550 100644 --- a/src/core/hle/service/hid/resource_manager.h +++ b/src/core/hle/service/hid/resource_manager.h @@ -20,24 +20,23 @@ class KSharedMemory; namespace Service::HID { class AppletResource; +class CaptureButton; class Controller_Stubbed; class ConsoleSixAxis; +class DebugMouse; class DebugPad; +class Digitizer; class Gesture; +class HomeButton; class Keyboard; class Mouse; class NPad; class Palma; class SevenSixAxis; class SixAxis; +class SleepButton; class TouchScreen; - -using CaptureButton = Controller_Stubbed; -using DebugMouse = Mouse; -using Digitizer = Controller_Stubbed; -using HomeButton = Controller_Stubbed; -using SleepButton = Controller_Stubbed; -using UniquePad = Controller_Stubbed; +class UniquePad; class ResourceManager { @@ -46,7 +45,6 @@ public: ~ResourceManager(); void Initialize(); - void InitializeController(u64 aruid); std::shared_ptr<AppletResource> GetAppletResource() const; std::shared_ptr<CaptureButton> GetCaptureButton() const; @@ -81,13 +79,17 @@ public: void EnablePadInput(u64 aruid, bool is_enabled); void EnableTouchScreen(u64 aruid, bool is_enabled); - void UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); - void UpdateNpad(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); - void UpdateMouseKeyboard(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); - void UpdateMotion(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); + void UpdateControllers(std::chrono::nanoseconds ns_late); + void UpdateNpad(std::chrono::nanoseconds ns_late); + void UpdateMouseKeyboard(std::chrono::nanoseconds ns_late); + void UpdateMotion(std::chrono::nanoseconds ns_late); private: Result CreateAppletResourceImpl(u64 aruid); + void InitializeHidCommonSampler(); + void InitializeTouchScreenSampler(); + void InitializeConsoleSixAxisSampler(); + void InitializeAHidSampler(); bool is_initialized{false}; diff --git a/src/core/hle/service/jit/jit.cpp b/src/core/hle/service/jit/jit.cpp index a94d05e19..77aa6d7d1 100644 --- a/src/core/hle/service/jit/jit.cpp +++ b/src/core/hle/service/jit/jit.cpp @@ -4,11 +4,11 @@ #include "core/arm/debug.h" #include "core/arm/symbols.h" #include "core/core.h" -#include "core/hle/kernel/k_code_memory.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/result.h" #include "core/hle/service/ipc_helpers.h" #include "core/hle/service/jit/jit.h" +#include "core/hle/service/jit/jit_code_memory.h" #include "core/hle/service/jit/jit_context.h" #include "core/hle/service/server_manager.h" #include "core/hle/service/service.h" @@ -23,10 +23,12 @@ struct CodeRange { class IJitEnvironment final : public ServiceFramework<IJitEnvironment> { public: - explicit IJitEnvironment(Core::System& system_, Kernel::KProcess& process_, CodeRange user_rx, - CodeRange user_ro) - : ServiceFramework{system_, "IJitEnvironment"}, process{&process_}, - context{process->GetMemory()} { + explicit IJitEnvironment(Core::System& system_, + Kernel::KScopedAutoObject<Kernel::KProcess>&& process_, + CodeMemory&& user_rx_, CodeMemory&& user_ro_) + : ServiceFramework{system_, "IJitEnvironment"}, process{std::move(process_)}, + user_rx{std::move(user_rx_)}, user_ro{std::move(user_ro_)}, + context{system_.ApplicationMemory()} { // clang-format off static const FunctionInfo functions[] = { {0, &IJitEnvironment::GenerateCode, "GenerateCode"}, @@ -39,10 +41,13 @@ public: RegisterHandlers(functions); // Identity map user code range into sysmodule context - configuration.user_ro_memory = user_ro; - configuration.user_rx_memory = user_rx; - configuration.sys_ro_memory = user_ro; - configuration.sys_rx_memory = user_rx; + configuration.user_rx_memory.size = user_rx.GetSize(); + configuration.user_rx_memory.offset = user_rx.GetAddress(); + configuration.user_ro_memory.size = user_ro.GetSize(); + configuration.user_ro_memory.offset = user_ro.GetAddress(); + + configuration.sys_rx_memory = configuration.user_rx_memory; + configuration.sys_ro_memory = configuration.user_ro_memory; } void GenerateCode(HLERequestContext& ctx) { @@ -318,6 +323,8 @@ private: } Kernel::KScopedAutoObject<Kernel::KProcess> process; + CodeMemory user_rx; + CodeMemory user_ro; GuestCallbacks callbacks; JITConfiguration configuration; JITContext context; @@ -335,6 +342,7 @@ public: RegisterHandlers(functions); } +private: void CreateJitEnvironment(HLERequestContext& ctx) { LOG_DEBUG(Service_JIT, "called"); @@ -380,20 +388,35 @@ public: return; } - const CodeRange user_rx{ - .offset = GetInteger(rx_mem->GetSourceAddress()), - .size = parameters.rx_size, - }; + CodeMemory rx, ro; + Result res; - const CodeRange user_ro{ - .offset = GetInteger(ro_mem->GetSourceAddress()), - .size = parameters.ro_size, - }; + res = rx.Initialize(*process, *rx_mem, parameters.rx_size, + Kernel::Svc::MemoryPermission::ReadExecute, generate_random); + if (R_FAILED(res)) { + LOG_ERROR(Service_JIT, "rx_mem could not be mapped for handle=0x{:08X}", rx_mem_handle); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(res); + return; + } + + res = ro.Initialize(*process, *ro_mem, parameters.ro_size, + Kernel::Svc::MemoryPermission::Read, generate_random); + if (R_FAILED(res)) { + LOG_ERROR(Service_JIT, "ro_mem could not be mapped for handle=0x{:08X}", ro_mem_handle); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(res); + return; + } IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); - rb.PushIpcInterface<IJitEnvironment>(system, *process, user_rx, user_ro); + rb.PushIpcInterface<IJitEnvironment>(system, std::move(process), std::move(rx), + std::move(ro)); } + +private: + std::mt19937_64 generate_random{}; }; void LoopProcess(Core::System& system) { diff --git a/src/core/hle/service/jit/jit_code_memory.cpp b/src/core/hle/service/jit/jit_code_memory.cpp new file mode 100644 index 000000000..2b480488a --- /dev/null +++ b/src/core/hle/service/jit/jit_code_memory.cpp @@ -0,0 +1,54 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/hle/service/jit/jit_code_memory.h" + +namespace Service::JIT { + +Result CodeMemory::Initialize(Kernel::KProcess& process, Kernel::KCodeMemory& code_memory, + size_t size, Kernel::Svc::MemoryPermission perm, + std::mt19937_64& generate_random) { + auto& page_table = process.GetPageTable(); + const u64 alias_code_start = + GetInteger(page_table.GetAliasCodeRegionStart()) / Kernel::PageSize; + const u64 alias_code_size = page_table.GetAliasCodeRegionSize() / Kernel::PageSize; + + // NOTE: This will retry indefinitely until mapping the code memory succeeds. + while (true) { + // Generate a new trial address. + const u64 mapped_address = + (alias_code_start + (generate_random() % alias_code_size)) * Kernel::PageSize; + + // Try to map the address + R_TRY_CATCH(code_memory.MapToOwner(mapped_address, size, perm)) { + R_CATCH(Kernel::ResultInvalidMemoryRegion) { + // If we could not map here, retry. + continue; + } + } + R_END_TRY_CATCH; + + // Set members. + m_code_memory = std::addressof(code_memory); + m_size = size; + m_address = mapped_address; + m_perm = perm; + + // Open a new reference to the code memory. + m_code_memory->Open(); + + // We succeeded. + R_SUCCEED(); + } +} + +void CodeMemory::Finalize() { + if (m_code_memory) { + R_ASSERT(m_code_memory->UnmapFromOwner(m_address, m_size)); + m_code_memory->Close(); + } + + m_code_memory = nullptr; +} + +} // namespace Service::JIT diff --git a/src/core/hle/service/jit/jit_code_memory.h b/src/core/hle/service/jit/jit_code_memory.h new file mode 100644 index 000000000..6376d4c4e --- /dev/null +++ b/src/core/hle/service/jit/jit_code_memory.h @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <random> + +#include "core/hle/kernel/k_code_memory.h" + +namespace Service::JIT { + +class CodeMemory { +public: + YUZU_NON_COPYABLE(CodeMemory); + + explicit CodeMemory() = default; + + CodeMemory(CodeMemory&& rhs) { + std::swap(m_code_memory, rhs.m_code_memory); + std::swap(m_size, rhs.m_size); + std::swap(m_address, rhs.m_address); + std::swap(m_perm, rhs.m_perm); + } + + ~CodeMemory() { + this->Finalize(); + } + +public: + Result Initialize(Kernel::KProcess& process, Kernel::KCodeMemory& code_memory, size_t size, + Kernel::Svc::MemoryPermission perm, std::mt19937_64& generate_random); + void Finalize(); + + size_t GetSize() const { + return m_size; + } + + u64 GetAddress() const { + return m_address; + } + +private: + Kernel::KCodeMemory* m_code_memory{}; + size_t m_size{}; + u64 m_address{}; + Kernel::Svc::MemoryPermission m_perm{}; +}; + +} // namespace Service::JIT diff --git a/src/core/hle/service/nvnflinger/nvnflinger.cpp b/src/core/hle/service/nvnflinger/nvnflinger.cpp index 6352b09a9..aa8aaa2d9 100644 --- a/src/core/hle/service/nvnflinger/nvnflinger.cpp +++ b/src/core/hle/service/nvnflinger/nvnflinger.cpp @@ -67,7 +67,7 @@ Nvnflinger::Nvnflinger(Core::System& system_, HosBinderDriverServer& hos_binder_ // Schedule the screen composition events multi_composition_event = Core::Timing::CreateEvent( "ScreenComposition", - [this](std::uintptr_t, s64 time, + [this](s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { vsync_signal.Set(); return std::chrono::nanoseconds(GetNextTicks()); @@ -75,7 +75,7 @@ Nvnflinger::Nvnflinger(Core::System& system_, HosBinderDriverServer& hos_binder_ single_composition_event = Core::Timing::CreateEvent( "ScreenComposition", - [this](std::uintptr_t, s64 time, + [this](s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { const auto lock_guard = Lock(); Compose(); @@ -93,11 +93,11 @@ Nvnflinger::Nvnflinger(Core::System& system_, HosBinderDriverServer& hos_binder_ Nvnflinger::~Nvnflinger() { if (system.IsMulticore()) { - system.CoreTiming().UnscheduleEvent(multi_composition_event, {}); + system.CoreTiming().UnscheduleEvent(multi_composition_event); vsync_thread.request_stop(); vsync_signal.Set(); } else { - system.CoreTiming().UnscheduleEvent(single_composition_event, {}); + system.CoreTiming().UnscheduleEvent(single_composition_event); } ShutdownLayers(); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index c7eb32c19..8176a41be 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -10,6 +10,7 @@ #include "common/assert.h" #include "common/atomic_ops.h" #include "common/common_types.h" +#include "common/heap_tracker.h" #include "common/logging/log.h" #include "common/page_table.h" #include "common/scope_exit.h" @@ -52,10 +53,18 @@ struct Memory::Impl { } else { current_page_table->fastmem_arena = nullptr; } + +#ifdef __linux__ + heap_tracker.emplace(system.DeviceMemory().buffer); + buffer = std::addressof(*heap_tracker); +#else + buffer = std::addressof(system.DeviceMemory().buffer); +#endif } void MapMemoryRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size, - Common::PhysicalAddress target, Common::MemoryPermission perms) { + Common::PhysicalAddress target, Common::MemoryPermission perms, + bool separate_heap) { ASSERT_MSG((size & YUZU_PAGEMASK) == 0, "non-page aligned size: {:016X}", size); ASSERT_MSG((base & YUZU_PAGEMASK) == 0, "non-page aligned base: {:016X}", GetInteger(base)); ASSERT_MSG(target >= DramMemoryMap::Base, "Out of bounds target: {:016X}", @@ -64,19 +73,20 @@ struct Memory::Impl { Common::PageType::Memory); if (current_page_table->fastmem_arena) { - system.DeviceMemory().buffer.Map(GetInteger(base), - GetInteger(target) - DramMemoryMap::Base, size, perms); + buffer->Map(GetInteger(base), GetInteger(target) - DramMemoryMap::Base, size, perms, + separate_heap); } } - void UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size) { + void UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size, + bool separate_heap) { ASSERT_MSG((size & YUZU_PAGEMASK) == 0, "non-page aligned size: {:016X}", size); ASSERT_MSG((base & YUZU_PAGEMASK) == 0, "non-page aligned base: {:016X}", GetInteger(base)); MapPages(page_table, base / YUZU_PAGESIZE, size / YUZU_PAGESIZE, 0, Common::PageType::Unmapped); if (current_page_table->fastmem_arena) { - system.DeviceMemory().buffer.Unmap(GetInteger(base), size); + buffer->Unmap(GetInteger(base), size, separate_heap); } } @@ -89,11 +99,6 @@ struct Memory::Impl { return; } - const bool is_r = True(perms & Common::MemoryPermission::Read); - const bool is_w = True(perms & Common::MemoryPermission::Write); - const bool is_x = - True(perms & Common::MemoryPermission::Execute) && Settings::IsNceEnabled(); - u64 protect_bytes{}; u64 protect_begin{}; for (u64 addr = vaddr; addr < vaddr + size; addr += YUZU_PAGESIZE) { @@ -102,8 +107,7 @@ struct Memory::Impl { switch (page_type) { case Common::PageType::RasterizerCachedMemory: if (protect_bytes > 0) { - system.DeviceMemory().buffer.Protect(protect_begin, protect_bytes, is_r, is_w, - is_x); + buffer->Protect(protect_begin, protect_bytes, perms); protect_bytes = 0; } break; @@ -116,7 +120,7 @@ struct Memory::Impl { } if (protect_bytes > 0) { - system.DeviceMemory().buffer.Protect(protect_begin, protect_bytes, is_r, is_w, is_x); + buffer->Protect(protect_begin, protect_bytes, perms); } } @@ -486,7 +490,9 @@ struct Memory::Impl { } if (current_page_table->fastmem_arena) { - system.DeviceMemory().buffer.Protect(vaddr, size, !debug, !debug); + const auto perm{debug ? Common::MemoryPermission{} + : Common::MemoryPermission::ReadWrite}; + buffer->Protect(vaddr, size, perm); } // Iterate over a contiguous CPU address space, marking/unmarking the region. @@ -543,9 +549,14 @@ struct Memory::Impl { } if (current_page_table->fastmem_arena) { - const bool is_read_enable = - !Settings::values.use_reactive_flushing.GetValue() || !cached; - system.DeviceMemory().buffer.Protect(vaddr, size, is_read_enable, !cached); + Common::MemoryPermission perm{}; + if (!Settings::values.use_reactive_flushing.GetValue() || !cached) { + perm |= Common::MemoryPermission::Read; + } + if (!cached) { + perm |= Common::MemoryPermission::Write; + } + buffer->Protect(vaddr, size, perm); } // Iterate over a contiguous CPU address space, which corresponds to the specified GPU @@ -856,6 +867,13 @@ struct Memory::Impl { std::array<GPUDirtyState, Core::Hardware::NUM_CPU_CORES> rasterizer_write_areas{}; std::span<Core::GPUDirtyMemoryManager> gpu_dirty_managers; std::mutex sys_core_guard; + + std::optional<Common::HeapTracker> heap_tracker; +#ifdef __linux__ + Common::HeapTracker* buffer{}; +#else + Common::HostMemory* buffer{}; +#endif }; Memory::Memory(Core::System& system_) : system{system_} { @@ -873,12 +891,14 @@ void Memory::SetCurrentPageTable(Kernel::KProcess& process) { } void Memory::MapMemoryRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size, - Common::PhysicalAddress target, Common::MemoryPermission perms) { - impl->MapMemoryRegion(page_table, base, size, target, perms); + Common::PhysicalAddress target, Common::MemoryPermission perms, + bool separate_heap) { + impl->MapMemoryRegion(page_table, base, size, target, perms, separate_heap); } -void Memory::UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size) { - impl->UnmapRegion(page_table, base, size); +void Memory::UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size, + bool separate_heap) { + impl->UnmapRegion(page_table, base, size, separate_heap); } void Memory::ProtectRegion(Common::PageTable& page_table, Common::ProcessAddress vaddr, u64 size, @@ -1048,7 +1068,9 @@ void Memory::FlushRegion(Common::ProcessAddress dest_addr, size_t size) { } bool Memory::InvalidateNCE(Common::ProcessAddress vaddr, size_t size) { - bool mapped = true; + [[maybe_unused]] bool mapped = true; + [[maybe_unused]] bool rasterizer = false; + u8* const ptr = impl->GetPointerImpl( GetInteger(vaddr), [&] { @@ -1056,8 +1078,26 @@ bool Memory::InvalidateNCE(Common::ProcessAddress vaddr, size_t size) { GetInteger(vaddr)); mapped = false; }, - [&] { impl->system.GPU().InvalidateRegion(GetInteger(vaddr), size); }); + [&] { + impl->system.GPU().InvalidateRegion(GetInteger(vaddr), size); + rasterizer = true; + }); + +#ifdef __linux__ + if (!rasterizer && mapped) { + impl->buffer->DeferredMapSeparateHeap(GetInteger(vaddr)); + } +#endif + return mapped && ptr != nullptr; } +bool Memory::InvalidateSeparateHeap(void* fault_address) { +#ifdef __linux__ + return impl->buffer->DeferredMapSeparateHeap(static_cast<u8*>(fault_address)); +#else + return false; +#endif +} + } // namespace Core::Memory diff --git a/src/core/memory.h b/src/core/memory.h index c1879e78f..dddfaf4a4 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -86,7 +86,8 @@ public: * @param perms The permissions to map the memory with. */ void MapMemoryRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size, - Common::PhysicalAddress target, Common::MemoryPermission perms); + Common::PhysicalAddress target, Common::MemoryPermission perms, + bool separate_heap); /** * Unmaps a region of the emulated process address space. @@ -95,7 +96,8 @@ public: * @param base The address to begin unmapping at. * @param size The amount of bytes to unmap. */ - void UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size); + void UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size, + bool separate_heap); /** * Protects a region of the emulated process address space with the new permissions. @@ -486,6 +488,7 @@ public: void SetGPUDirtyManagers(std::span<Core::GPUDirtyMemoryManager> managers); void InvalidateRegion(Common::ProcessAddress dest_addr, size_t size); bool InvalidateNCE(Common::ProcessAddress vaddr, size_t size); + bool InvalidateSeparateHeap(void* fault_address); void FlushRegion(Common::ProcessAddress dest_addr, size_t size); private: @@ -683,7 +686,8 @@ public: } else { this->m_memory.WriteBlockUnsafe(this->m_addr, this->data(), this->size_bytes()); } - } else if constexpr (FLAGS & GuestMemoryFlags::Safe) { + } else if constexpr ((FLAGS & GuestMemoryFlags::Safe) || + (FLAGS & GuestMemoryFlags::Cached)) { this->m_memory.InvalidateRegion(this->m_addr, this->size_bytes()); } } diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp index 3fc4024dc..7bc5b5ae5 100644 --- a/src/core/memory/cheat_engine.cpp +++ b/src/core/memory/cheat_engine.cpp @@ -190,15 +190,15 @@ CheatEngine::CheatEngine(System& system_, std::vector<CheatEntry> cheats_, } CheatEngine::~CheatEngine() { - core_timing.UnscheduleEvent(event, 0); + core_timing.UnscheduleEvent(event); } void CheatEngine::Initialize() { event = Core::Timing::CreateEvent( "CheatEngine::FrameCallback::" + Common::HexToString(metadata.main_nso_build_id), - [this](std::uintptr_t user_data, s64 time, + [this](s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { - FrameCallback(user_data, ns_late); + FrameCallback(ns_late); return std::nullopt; }); core_timing.ScheduleLoopingEvent(CHEAT_ENGINE_NS, CHEAT_ENGINE_NS, event); @@ -239,7 +239,7 @@ void CheatEngine::Reload(std::vector<CheatEntry> reload_cheats) { MICROPROFILE_DEFINE(Cheat_Engine, "Add-Ons", "Cheat Engine", MP_RGB(70, 200, 70)); -void CheatEngine::FrameCallback(std::uintptr_t, std::chrono::nanoseconds ns_late) { +void CheatEngine::FrameCallback(std::chrono::nanoseconds ns_late) { if (is_pending_reload.exchange(false)) { vm.LoadProgram(cheats); } diff --git a/src/core/memory/cheat_engine.h b/src/core/memory/cheat_engine.h index 284abdd28..ced2168d1 100644 --- a/src/core/memory/cheat_engine.h +++ b/src/core/memory/cheat_engine.h @@ -70,7 +70,7 @@ public: void Reload(std::vector<CheatEntry> reload_cheats); private: - void FrameCallback(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); + void FrameCallback(std::chrono::nanoseconds ns_late); DmntCheatVm vm; CheatProcessMetadata metadata; diff --git a/src/core/tools/freezer.cpp b/src/core/tools/freezer.cpp index 98ebbbf32..9d42c726e 100644 --- a/src/core/tools/freezer.cpp +++ b/src/core/tools/freezer.cpp @@ -51,18 +51,17 @@ void MemoryWriteWidth(Core::Memory::Memory& memory, u32 width, VAddr addr, u64 v Freezer::Freezer(Core::Timing::CoreTiming& core_timing_, Core::Memory::Memory& memory_) : core_timing{core_timing_}, memory{memory_} { - event = Core::Timing::CreateEvent( - "MemoryFreezer::FrameCallback", - [this](std::uintptr_t user_data, s64 time, - std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> { - FrameCallback(user_data, ns_late); - return std::nullopt; - }); + event = Core::Timing::CreateEvent("MemoryFreezer::FrameCallback", + [this](s64 time, std::chrono::nanoseconds ns_late) + -> std::optional<std::chrono::nanoseconds> { + FrameCallback(ns_late); + return std::nullopt; + }); core_timing.ScheduleEvent(memory_freezer_ns, event); } Freezer::~Freezer() { - core_timing.UnscheduleEvent(event, 0); + core_timing.UnscheduleEvent(event); } void Freezer::SetActive(bool is_active) { @@ -159,7 +158,7 @@ Freezer::Entries::const_iterator Freezer::FindEntry(VAddr address) const { [address](const Entry& entry) { return entry.address == address; }); } -void Freezer::FrameCallback(std::uintptr_t, std::chrono::nanoseconds ns_late) { +void Freezer::FrameCallback(std::chrono::nanoseconds ns_late) { if (!IsActive()) { LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); return; diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h index 0d6df5217..2efbc11f3 100644 --- a/src/core/tools/freezer.h +++ b/src/core/tools/freezer.h @@ -77,7 +77,7 @@ private: Entries::iterator FindEntry(VAddr address); Entries::const_iterator FindEntry(VAddr address) const; - void FrameCallback(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); + void FrameCallback(std::chrono::nanoseconds ns_late); void FillEntryReads(); std::atomic_bool active{false}; diff --git a/src/frontend_common/config.cpp b/src/frontend_common/config.cpp index d9f99148b..51576b4ee 100644 --- a/src/frontend_common/config.cpp +++ b/src/frontend_common/config.cpp @@ -403,59 +403,63 @@ void Config::SavePlayerValues(const std::size_t player_index) { // No custom profile selected return; } - WriteSetting(std::string(player_prefix).append("profile_name"), player.profile_name, - std::make_optional(std::string(""))); + WriteStringSetting(std::string(player_prefix).append("profile_name"), player.profile_name, + std::make_optional(std::string(""))); } - WriteSetting(std::string(player_prefix).append("type"), static_cast<u8>(player.controller_type), - std::make_optional(static_cast<u8>(Settings::ControllerType::ProController))); + WriteIntegerSetting( + std::string(player_prefix).append("type"), static_cast<u8>(player.controller_type), + std::make_optional(static_cast<u8>(Settings::ControllerType::ProController))); if (!player_prefix.empty() || !Settings::IsConfiguringGlobal()) { - WriteSetting(std::string(player_prefix).append("connected"), player.connected, - std::make_optional(player_index == 0)); - WriteSetting(std::string(player_prefix).append("vibration_enabled"), - player.vibration_enabled, std::make_optional(true)); - WriteSetting(std::string(player_prefix).append("vibration_strength"), - player.vibration_strength, std::make_optional(100)); - WriteSetting(std::string(player_prefix).append("body_color_left"), player.body_color_left, - std::make_optional(Settings::JOYCON_BODY_NEON_BLUE)); - WriteSetting(std::string(player_prefix).append("body_color_right"), player.body_color_right, - std::make_optional(Settings::JOYCON_BODY_NEON_RED)); - WriteSetting(std::string(player_prefix).append("button_color_left"), - player.button_color_left, - std::make_optional(Settings::JOYCON_BUTTONS_NEON_BLUE)); - WriteSetting(std::string(player_prefix).append("button_color_right"), - player.button_color_right, - std::make_optional(Settings::JOYCON_BUTTONS_NEON_RED)); + WriteBooleanSetting(std::string(player_prefix).append("connected"), player.connected, + std::make_optional(player_index == 0)); + WriteIntegerSetting(std::string(player_prefix).append("vibration_enabled"), + player.vibration_enabled, std::make_optional(true)); + WriteIntegerSetting(std::string(player_prefix).append("vibration_strength"), + player.vibration_strength, std::make_optional(100)); + WriteIntegerSetting(std::string(player_prefix).append("body_color_left"), + player.body_color_left, + std::make_optional(Settings::JOYCON_BODY_NEON_BLUE)); + WriteIntegerSetting(std::string(player_prefix).append("body_color_right"), + player.body_color_right, + std::make_optional(Settings::JOYCON_BODY_NEON_RED)); + WriteIntegerSetting(std::string(player_prefix).append("button_color_left"), + player.button_color_left, + std::make_optional(Settings::JOYCON_BUTTONS_NEON_BLUE)); + WriteIntegerSetting(std::string(player_prefix).append("button_color_right"), + player.button_color_right, + std::make_optional(Settings::JOYCON_BUTTONS_NEON_RED)); } } void Config::SaveTouchscreenValues() { const auto& touchscreen = Settings::values.touchscreen; - WriteSetting(std::string("touchscreen_enabled"), touchscreen.enabled, std::make_optional(true)); + WriteBooleanSetting(std::string("touchscreen_enabled"), touchscreen.enabled, + std::make_optional(true)); - WriteSetting(std::string("touchscreen_angle"), touchscreen.rotation_angle, - std::make_optional(static_cast<u32>(0))); - WriteSetting(std::string("touchscreen_diameter_x"), touchscreen.diameter_x, - std::make_optional(static_cast<u32>(15))); - WriteSetting(std::string("touchscreen_diameter_y"), touchscreen.diameter_y, - std::make_optional(static_cast<u32>(15))); + WriteIntegerSetting(std::string("touchscreen_angle"), touchscreen.rotation_angle, + std::make_optional(static_cast<u32>(0))); + WriteIntegerSetting(std::string("touchscreen_diameter_x"), touchscreen.diameter_x, + std::make_optional(static_cast<u32>(15))); + WriteIntegerSetting(std::string("touchscreen_diameter_y"), touchscreen.diameter_y, + std::make_optional(static_cast<u32>(15))); } void Config::SaveMotionTouchValues() { BeginArray(std::string("touch_from_button_maps")); for (std::size_t p = 0; p < Settings::values.touch_from_button_maps.size(); ++p) { SetArrayIndex(static_cast<int>(p)); - WriteSetting(std::string("name"), Settings::values.touch_from_button_maps[p].name, - std::make_optional(std::string("default"))); + WriteStringSetting(std::string("name"), Settings::values.touch_from_button_maps[p].name, + std::make_optional(std::string("default"))); BeginArray(std::string("entries")); for (std::size_t q = 0; q < Settings::values.touch_from_button_maps[p].buttons.size(); ++q) { SetArrayIndex(static_cast<int>(q)); - WriteSetting(std::string("bind"), - Settings::values.touch_from_button_maps[p].buttons[q]); + WriteStringSetting(std::string("bind"), + Settings::values.touch_from_button_maps[p].buttons[q]); } EndArray(); // entries } @@ -520,16 +524,16 @@ void Config::SaveCoreValues() { void Config::SaveDataStorageValues() { BeginGroup(Settings::TranslateCategory(Settings::Category::DataStorage)); - WriteSetting(std::string("nand_directory"), FS::GetYuzuPathString(FS::YuzuPath::NANDDir), - std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::NANDDir))); - WriteSetting(std::string("sdmc_directory"), FS::GetYuzuPathString(FS::YuzuPath::SDMCDir), - std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::SDMCDir))); - WriteSetting(std::string("load_directory"), FS::GetYuzuPathString(FS::YuzuPath::LoadDir), - std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::LoadDir))); - WriteSetting(std::string("dump_directory"), FS::GetYuzuPathString(FS::YuzuPath::DumpDir), - std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::DumpDir))); - WriteSetting(std::string("tas_directory"), FS::GetYuzuPathString(FS::YuzuPath::TASDir), - std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::TASDir))); + WriteStringSetting(std::string("nand_directory"), FS::GetYuzuPathString(FS::YuzuPath::NANDDir), + std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::NANDDir))); + WriteStringSetting(std::string("sdmc_directory"), FS::GetYuzuPathString(FS::YuzuPath::SDMCDir), + std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::SDMCDir))); + WriteStringSetting(std::string("load_directory"), FS::GetYuzuPathString(FS::YuzuPath::LoadDir), + std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::LoadDir))); + WriteStringSetting(std::string("dump_directory"), FS::GetYuzuPathString(FS::YuzuPath::DumpDir), + std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::DumpDir))); + WriteStringSetting(std::string("tas_directory"), FS::GetYuzuPathString(FS::YuzuPath::TASDir), + std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::TASDir))); WriteCategory(Settings::Category::DataStorage); @@ -540,7 +544,7 @@ void Config::SaveDebuggingValues() { BeginGroup(Settings::TranslateCategory(Settings::Category::Debugging)); // Intentionally not using the QT default setting as this is intended to be changed in the ini - WriteSetting(std::string("record_frame_times"), Settings::values.record_frame_times); + WriteBooleanSetting(std::string("record_frame_times"), Settings::values.record_frame_times); WriteCategory(Settings::Category::Debugging); WriteCategory(Settings::Category::DebuggingGraphics); @@ -564,11 +568,13 @@ void Config::SaveDisabledAddOnValues() { BeginArray(std::string("")); for (const auto& elem : Settings::values.disabled_addons) { SetArrayIndex(i); - WriteSetting(std::string("title_id"), elem.first, std::make_optional(static_cast<u64>(0))); + WriteIntegerSetting(std::string("title_id"), elem.first, + std::make_optional(static_cast<u64>(0))); BeginArray(std::string("disabled")); for (std::size_t j = 0; j < elem.second.size(); ++j) { SetArrayIndex(static_cast<int>(j)); - WriteSetting(std::string("d"), elem.second[j], std::make_optional(std::string(""))); + WriteStringSetting(std::string("d"), elem.second[j], + std::make_optional(std::string(""))); } EndArray(); // disabled ++i; @@ -609,8 +615,8 @@ void Config::SaveRendererValues() { void Config::SaveScreenshotValues() { BeginGroup(Settings::TranslateCategory(Settings::Category::Screenshots)); - WriteSetting(std::string("screenshot_path"), - FS::GetYuzuPathString(FS::YuzuPath::ScreenshotsDir)); + WriteStringSetting(std::string("screenshot_path"), + FS::GetYuzuPathString(FS::YuzuPath::ScreenshotsDir)); WriteCategory(Settings::Category::Screenshots); EndGroup(); @@ -746,46 +752,70 @@ bool Config::Exists(const std::string& section, const std::string& key) const { return !value.empty(); } -template <typename Type> -void Config::WriteSetting(const std::string& key, const Type& value, - const std::optional<Type>& default_value, - const std::optional<bool>& use_global) { - std::string full_key = GetFullKey(key, false); +void Config::WriteBooleanSetting(const std::string& key, const bool& value, + const std::optional<bool>& default_value, + const std::optional<bool>& use_global) { + std::optional<std::string> string_default = std::nullopt; + if (default_value.has_value()) { + string_default = std::make_optional(ToString(default_value.value())); + } + WritePreparedSetting(key, AdjustOutputString(ToString(value)), string_default, use_global); +} - std::string saved_value; - std::string string_default; - if constexpr (std::is_same_v<Type, std::string>) { - saved_value.append(AdjustOutputString(value)); - if (default_value.has_value()) { - string_default.append(AdjustOutputString(default_value.value())); - } - } else { - saved_value.append(AdjustOutputString(ToString(value))); - if (default_value.has_value()) { - string_default.append(ToString(default_value.value())); - } +template <typename T> +std::enable_if_t<std::is_integral_v<T>> Config::WriteIntegerSetting( + const std::string& key, const T& value, const std::optional<T>& default_value, + const std::optional<bool>& use_global) { + std::optional<std::string> string_default = std::nullopt; + if (default_value.has_value()) { + string_default = std::make_optional(ToString(default_value.value())); + } + WritePreparedSetting(key, AdjustOutputString(ToString(value)), string_default, use_global); +} + +void Config::WriteDoubleSetting(const std::string& key, const double& value, + const std::optional<double>& default_value, + const std::optional<bool>& use_global) { + std::optional<std::string> string_default = std::nullopt; + if (default_value.has_value()) { + string_default = std::make_optional(ToString(default_value.value())); } + WritePreparedSetting(key, AdjustOutputString(ToString(value)), string_default, use_global); +} - if (default_value.has_value() && use_global.has_value()) { +void Config::WriteStringSetting(const std::string& key, const std::string& value, + const std::optional<std::string>& default_value, + const std::optional<bool>& use_global) { + std::optional string_default = default_value; + if (default_value.has_value()) { + string_default.value().append(AdjustOutputString(default_value.value())); + } + WritePreparedSetting(key, AdjustOutputString(value), string_default, use_global); +} + +void Config::WritePreparedSetting(const std::string& key, const std::string& adjusted_value, + const std::optional<std::string>& adjusted_default_value, + const std::optional<bool>& use_global) { + std::string full_key = GetFullKey(key, false); + if (adjusted_default_value.has_value() && use_global.has_value()) { if (!global) { - WriteSettingInternal(std::string(full_key).append("\\global"), - ToString(use_global.value())); + WriteString(std::string(full_key).append("\\global"), ToString(use_global.value())); } if (global || use_global.value() == false) { - WriteSettingInternal(std::string(full_key).append("\\default"), - ToString(string_default == saved_value)); - WriteSettingInternal(full_key, saved_value); + WriteString(std::string(full_key).append("\\default"), + ToString(adjusted_default_value == adjusted_value)); + WriteString(full_key, adjusted_value); } - } else if (default_value.has_value() && !use_global.has_value()) { - WriteSettingInternal(std::string(full_key).append("\\default"), - ToString(string_default == saved_value)); - WriteSettingInternal(full_key, saved_value); + } else if (adjusted_default_value.has_value() && !use_global.has_value()) { + WriteString(std::string(full_key).append("\\default"), + ToString(adjusted_default_value == adjusted_value)); + WriteString(full_key, adjusted_value); } else { - WriteSettingInternal(full_key, saved_value); + WriteString(full_key, adjusted_value); } } -void Config::WriteSettingInternal(const std::string& key, const std::string& value) { +void Config::WriteString(const std::string& key, const std::string& value) { config->SetValue(GetSection().c_str(), key.c_str(), value.c_str()); } @@ -861,17 +891,17 @@ void Config::WriteSettingGeneric(const Settings::BasicSetting* const setting) { std::string key = AdjustKey(setting->GetLabel()); if (setting->Switchable()) { if (!global) { - WriteSetting(std::string(key).append("\\use_global"), setting->UsingGlobal()); + WriteBooleanSetting(std::string(key).append("\\use_global"), setting->UsingGlobal()); } if (global || !setting->UsingGlobal()) { - WriteSetting(std::string(key).append("\\default"), - setting->ToString() == setting->DefaultToString()); - WriteSetting(key, setting->ToString()); + WriteBooleanSetting(std::string(key).append("\\default"), + setting->ToString() == setting->DefaultToString()); + WriteStringSetting(key, setting->ToString()); } } else if (global) { - WriteSetting(std::string(key).append("\\default"), - setting->ToString() == setting->DefaultToString()); - WriteSetting(key, setting->ToString()); + WriteBooleanSetting(std::string(key).append("\\default"), + setting->ToString() == setting->DefaultToString()); + WriteStringSetting(key, setting->ToString()); } } diff --git a/src/frontend_common/config.h b/src/frontend_common/config.h index b3812af17..0c4d505b8 100644 --- a/src/frontend_common/config.h +++ b/src/frontend_common/config.h @@ -154,11 +154,20 @@ protected: * @param use_global Specifies if the custom or global config should be in use, for custom * configs */ - template <typename Type = int> - void WriteSetting(const std::string& key, const Type& value, - const std::optional<Type>& default_value = std::nullopt, - const std::optional<bool>& use_global = std::nullopt); - void WriteSettingInternal(const std::string& key, const std::string& value); + void WriteBooleanSetting(const std::string& key, const bool& value, + const std::optional<bool>& default_value = std::nullopt, + const std::optional<bool>& use_global = std::nullopt); + template <typename T> + std::enable_if_t<std::is_integral_v<T>> WriteIntegerSetting( + const std::string& key, const T& value, + const std::optional<T>& default_value = std::nullopt, + const std::optional<bool>& use_global = std::nullopt); + void WriteDoubleSetting(const std::string& key, const double& value, + const std::optional<double>& default_value = std::nullopt, + const std::optional<bool>& use_global = std::nullopt); + void WriteStringSetting(const std::string& key, const std::string& value, + const std::optional<std::string>& default_value = std::nullopt, + const std::optional<bool>& use_global = std::nullopt); void ReadCategory(Settings::Category category); void WriteCategory(Settings::Category category); @@ -175,8 +184,10 @@ protected: return value_ ? "true" : "false"; } else if constexpr (std::is_same_v<T, u64>) { return std::to_string(static_cast<u64>(value_)); - } else { + } else if constexpr (std::is_same_v<T, s64>) { return std::to_string(static_cast<s64>(value_)); + } else { + return std::to_string(value_); } } @@ -197,9 +208,13 @@ protected: const bool global; private: - inline static std::array<char, 19> special_characters = {'!', '#', '$', '%', '^', '&', '*', - '|', ';', '\'', '\"', ',', '<', '.', - '>', '?', '`', '~', '='}; + void WritePreparedSetting(const std::string& key, const std::string& adjusted_value, + const std::optional<std::string>& adjusted_default_value, + const std::optional<bool>& use_global); + void WriteString(const std::string& key, const std::string& value); + + inline static std::array<char, 18> special_characters = { + '!', '#', '$', '%', '^', '&', '*', '|', ';', '\'', '\"', ',', '<', '>', '?', '`', '~', '='}; struct ConfigArray { std::string name; diff --git a/src/tests/common/host_memory.cpp b/src/tests/common/host_memory.cpp index 1a28e862b..cb040c942 100644 --- a/src/tests/common/host_memory.cpp +++ b/src/tests/common/host_memory.cpp @@ -12,6 +12,7 @@ using namespace Common::Literals; static constexpr size_t VIRTUAL_SIZE = 1ULL << 39; static constexpr size_t BACKING_SIZE = 4_GiB; static constexpr auto PERMS = Common::MemoryPermission::ReadWrite; +static constexpr auto HEAP = false; TEST_CASE("HostMemory: Initialize and deinitialize", "[common]") { { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); } @@ -20,7 +21,7 @@ TEST_CASE("HostMemory: Initialize and deinitialize", "[common]") { TEST_CASE("HostMemory: Simple map", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x5000, 0x8000, 0x1000, PERMS); + mem.Map(0x5000, 0x8000, 0x1000, PERMS, HEAP); volatile u8* const data = mem.VirtualBasePointer() + 0x5000; data[0] = 50; @@ -29,8 +30,8 @@ TEST_CASE("HostMemory: Simple map", "[common]") { TEST_CASE("HostMemory: Simple mirror map", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x5000, 0x3000, 0x2000, PERMS); - mem.Map(0x8000, 0x4000, 0x1000, PERMS); + mem.Map(0x5000, 0x3000, 0x2000, PERMS, HEAP); + mem.Map(0x8000, 0x4000, 0x1000, PERMS, HEAP); volatile u8* const mirror_a = mem.VirtualBasePointer() + 0x5000; volatile u8* const mirror_b = mem.VirtualBasePointer() + 0x8000; @@ -40,116 +41,116 @@ TEST_CASE("HostMemory: Simple mirror map", "[common]") { TEST_CASE("HostMemory: Simple unmap", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x5000, 0x3000, 0x2000, PERMS); + mem.Map(0x5000, 0x3000, 0x2000, PERMS, HEAP); volatile u8* const data = mem.VirtualBasePointer() + 0x5000; data[75] = 50; REQUIRE(data[75] == 50); - mem.Unmap(0x5000, 0x2000); + mem.Unmap(0x5000, 0x2000, HEAP); } TEST_CASE("HostMemory: Simple unmap and remap", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x5000, 0x3000, 0x2000, PERMS); + mem.Map(0x5000, 0x3000, 0x2000, PERMS, HEAP); volatile u8* const data = mem.VirtualBasePointer() + 0x5000; data[0] = 50; REQUIRE(data[0] == 50); - mem.Unmap(0x5000, 0x2000); + mem.Unmap(0x5000, 0x2000, HEAP); - mem.Map(0x5000, 0x3000, 0x2000, PERMS); + mem.Map(0x5000, 0x3000, 0x2000, PERMS, HEAP); REQUIRE(data[0] == 50); - mem.Map(0x7000, 0x2000, 0x5000, PERMS); + mem.Map(0x7000, 0x2000, 0x5000, PERMS, HEAP); REQUIRE(data[0x3000] == 50); } TEST_CASE("HostMemory: Nieche allocation", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x0000, 0, 0x20000, PERMS); - mem.Unmap(0x0000, 0x4000); - mem.Map(0x1000, 0, 0x2000, PERMS); - mem.Map(0x3000, 0, 0x1000, PERMS); - mem.Map(0, 0, 0x1000, PERMS); + mem.Map(0x0000, 0, 0x20000, PERMS, HEAP); + mem.Unmap(0x0000, 0x4000, HEAP); + mem.Map(0x1000, 0, 0x2000, PERMS, HEAP); + mem.Map(0x3000, 0, 0x1000, PERMS, HEAP); + mem.Map(0, 0, 0x1000, PERMS, HEAP); } TEST_CASE("HostMemory: Full unmap", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x8000, 0, 0x4000, PERMS); - mem.Unmap(0x8000, 0x4000); - mem.Map(0x6000, 0, 0x16000, PERMS); + mem.Map(0x8000, 0, 0x4000, PERMS, HEAP); + mem.Unmap(0x8000, 0x4000, HEAP); + mem.Map(0x6000, 0, 0x16000, PERMS, HEAP); } TEST_CASE("HostMemory: Right out of bounds unmap", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x0000, 0, 0x4000, PERMS); - mem.Unmap(0x2000, 0x4000); - mem.Map(0x2000, 0x80000, 0x4000, PERMS); + mem.Map(0x0000, 0, 0x4000, PERMS, HEAP); + mem.Unmap(0x2000, 0x4000, HEAP); + mem.Map(0x2000, 0x80000, 0x4000, PERMS, HEAP); } TEST_CASE("HostMemory: Left out of bounds unmap", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x8000, 0, 0x4000, PERMS); - mem.Unmap(0x6000, 0x4000); - mem.Map(0x8000, 0, 0x2000, PERMS); + mem.Map(0x8000, 0, 0x4000, PERMS, HEAP); + mem.Unmap(0x6000, 0x4000, HEAP); + mem.Map(0x8000, 0, 0x2000, PERMS, HEAP); } TEST_CASE("HostMemory: Multiple placeholder unmap", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x0000, 0, 0x4000, PERMS); - mem.Map(0x4000, 0, 0x1b000, PERMS); - mem.Unmap(0x3000, 0x1c000); - mem.Map(0x3000, 0, 0x20000, PERMS); + mem.Map(0x0000, 0, 0x4000, PERMS, HEAP); + mem.Map(0x4000, 0, 0x1b000, PERMS, HEAP); + mem.Unmap(0x3000, 0x1c000, HEAP); + mem.Map(0x3000, 0, 0x20000, PERMS, HEAP); } TEST_CASE("HostMemory: Unmap between placeholders", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x0000, 0, 0x4000, PERMS); - mem.Map(0x4000, 0, 0x4000, PERMS); - mem.Unmap(0x2000, 0x4000); - mem.Map(0x2000, 0, 0x4000, PERMS); + mem.Map(0x0000, 0, 0x4000, PERMS, HEAP); + mem.Map(0x4000, 0, 0x4000, PERMS, HEAP); + mem.Unmap(0x2000, 0x4000, HEAP); + mem.Map(0x2000, 0, 0x4000, PERMS, HEAP); } TEST_CASE("HostMemory: Unmap to origin", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x4000, 0, 0x4000, PERMS); - mem.Map(0x8000, 0, 0x4000, PERMS); - mem.Unmap(0x4000, 0x4000); - mem.Map(0, 0, 0x4000, PERMS); - mem.Map(0x4000, 0, 0x4000, PERMS); + mem.Map(0x4000, 0, 0x4000, PERMS, HEAP); + mem.Map(0x8000, 0, 0x4000, PERMS, HEAP); + mem.Unmap(0x4000, 0x4000, HEAP); + mem.Map(0, 0, 0x4000, PERMS, HEAP); + mem.Map(0x4000, 0, 0x4000, PERMS, HEAP); } TEST_CASE("HostMemory: Unmap to right", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x4000, 0, 0x4000, PERMS); - mem.Map(0x8000, 0, 0x4000, PERMS); - mem.Unmap(0x8000, 0x4000); - mem.Map(0x8000, 0, 0x4000, PERMS); + mem.Map(0x4000, 0, 0x4000, PERMS, HEAP); + mem.Map(0x8000, 0, 0x4000, PERMS, HEAP); + mem.Unmap(0x8000, 0x4000, HEAP); + mem.Map(0x8000, 0, 0x4000, PERMS, HEAP); } TEST_CASE("HostMemory: Partial right unmap check bindings", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x4000, 0x10000, 0x4000, PERMS); + mem.Map(0x4000, 0x10000, 0x4000, PERMS, HEAP); volatile u8* const ptr = mem.VirtualBasePointer() + 0x4000; ptr[0x1000] = 17; - mem.Unmap(0x6000, 0x2000); + mem.Unmap(0x6000, 0x2000, HEAP); REQUIRE(ptr[0x1000] == 17); } TEST_CASE("HostMemory: Partial left unmap check bindings", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x4000, 0x10000, 0x4000, PERMS); + mem.Map(0x4000, 0x10000, 0x4000, PERMS, HEAP); volatile u8* const ptr = mem.VirtualBasePointer() + 0x4000; ptr[0x3000] = 19; ptr[0x3fff] = 12; - mem.Unmap(0x4000, 0x2000); + mem.Unmap(0x4000, 0x2000, HEAP); REQUIRE(ptr[0x3000] == 19); REQUIRE(ptr[0x3fff] == 12); @@ -157,13 +158,13 @@ TEST_CASE("HostMemory: Partial left unmap check bindings", "[common]") { TEST_CASE("HostMemory: Partial middle unmap check bindings", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x4000, 0x10000, 0x4000, PERMS); + mem.Map(0x4000, 0x10000, 0x4000, PERMS, HEAP); volatile u8* const ptr = mem.VirtualBasePointer() + 0x4000; ptr[0x0000] = 19; ptr[0x3fff] = 12; - mem.Unmap(0x1000, 0x2000); + mem.Unmap(0x1000, 0x2000, HEAP); REQUIRE(ptr[0x0000] == 19); REQUIRE(ptr[0x3fff] == 12); @@ -171,14 +172,14 @@ TEST_CASE("HostMemory: Partial middle unmap check bindings", "[common]") { TEST_CASE("HostMemory: Partial sparse middle unmap and check bindings", "[common]") { HostMemory mem(BACKING_SIZE, VIRTUAL_SIZE); - mem.Map(0x4000, 0x10000, 0x2000, PERMS); - mem.Map(0x6000, 0x20000, 0x2000, PERMS); + mem.Map(0x4000, 0x10000, 0x2000, PERMS, HEAP); + mem.Map(0x6000, 0x20000, 0x2000, PERMS, HEAP); volatile u8* const ptr = mem.VirtualBasePointer() + 0x4000; ptr[0x0000] = 19; ptr[0x3fff] = 12; - mem.Unmap(0x5000, 0x2000); + mem.Unmap(0x5000, 0x2000, HEAP); REQUIRE(ptr[0x0000] == 19); REQUIRE(ptr[0x3fff] == 12); diff --git a/src/tests/core/core_timing.cpp b/src/tests/core/core_timing.cpp index f08afbf9a..81898a1d3 100644 --- a/src/tests/core/core_timing.cpp +++ b/src/tests/core/core_timing.cpp @@ -16,20 +16,16 @@ namespace { // Numbers are chosen randomly to make sure the correct one is given. -constexpr std::array<u64, 5> CB_IDS{{42, 144, 93, 1026, UINT64_C(0xFFFF7FFFF7FFFF)}}; constexpr std::array<u64, 5> calls_order{{2, 0, 1, 4, 3}}; std::array<s64, 5> delays{}; - -std::bitset<CB_IDS.size()> callbacks_ran_flags; +std::bitset<5> callbacks_ran_flags; u64 expected_callback = 0; template <unsigned int IDX> -std::optional<std::chrono::nanoseconds> HostCallbackTemplate(std::uintptr_t user_data, s64 time, +std::optional<std::chrono::nanoseconds> HostCallbackTemplate(s64 time, std::chrono::nanoseconds ns_late) { - static_assert(IDX < CB_IDS.size(), "IDX out of range"); + static_assert(IDX < callbacks_ran_flags.size(), "IDX out of range"); callbacks_ran_flags.set(IDX); - REQUIRE(CB_IDS[IDX] == user_data); - REQUIRE(CB_IDS[IDX] == CB_IDS[calls_order[expected_callback]]); delays[IDX] = ns_late.count(); ++expected_callback; return std::nullopt; @@ -76,7 +72,7 @@ TEST_CASE("CoreTiming[BasicOrder]", "[core]") { const u64 order = calls_order[i]; const auto future_ns = std::chrono::nanoseconds{static_cast<s64>(i * one_micro + 100)}; - core_timing.ScheduleEvent(future_ns, events[order], CB_IDS[order]); + core_timing.ScheduleEvent(future_ns, events[order]); } /// test pause REQUIRE(callbacks_ran_flags.none()); @@ -118,7 +114,7 @@ TEST_CASE("CoreTiming[BasicOrderNoPausing]", "[core]") { for (std::size_t i = 0; i < events.size(); i++) { const u64 order = calls_order[i]; const auto future_ns = std::chrono::nanoseconds{static_cast<s64>(i * one_micro + 100)}; - core_timing.ScheduleEvent(future_ns, events[order], CB_IDS[order]); + core_timing.ScheduleEvent(future_ns, events[order]); } const u64 end = core_timing.GetGlobalTimeNs().count(); diff --git a/src/video_core/engines/maxwell_dma.cpp b/src/video_core/engines/maxwell_dma.cpp index 422d4d859..56fbff306 100644 --- a/src/video_core/engines/maxwell_dma.cpp +++ b/src/video_core/engines/maxwell_dma.cpp @@ -228,7 +228,7 @@ void MaxwellDMA::CopyBlockLinearToPitch() { Core::Memory::GpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer( memory_manager, src_operand.address, src_size, &read_buffer); - Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite> + Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::UnsafeReadCachedWrite> tmp_write_buffer(memory_manager, dst_operand.address, dst_size, &write_buffer); UnswizzleSubrect(tmp_write_buffer, tmp_read_buffer, bytes_per_pixel, width, height, depth, @@ -292,7 +292,7 @@ void MaxwellDMA::CopyPitchToBlockLinear() { GPUVAddr dst_addr = regs.offset_out; Core::Memory::GpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer( memory_manager, src_addr, src_size, &read_buffer); - Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite> + Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::UnsafeReadCachedWrite> tmp_write_buffer(memory_manager, dst_addr, dst_size, &write_buffer); // If the input is linear and the output is tiled, swizzle the input and copy it over. diff --git a/src/video_core/renderer_vulkan/vk_present_manager.cpp b/src/video_core/renderer_vulkan/vk_present_manager.cpp index 5e7518d96..792ed9615 100644 --- a/src/video_core/renderer_vulkan/vk_present_manager.cpp +++ b/src/video_core/renderer_vulkan/vk_present_manager.cpp @@ -329,7 +329,7 @@ void PresentManager::CopyToSwapchainImpl(Frame* frame) { // to account for that. const bool is_suboptimal = swapchain.NeedsRecreation(); const bool size_changed = - swapchain.GetWidth() != frame->width || swapchain.GetHeight() != frame->height; + swapchain.GetWidth() < frame->width || swapchain.GetHeight() < frame->height; if (is_suboptimal || size_changed) { RecreateSwapchain(frame); } diff --git a/src/yuzu/configuration/qt_config.cpp b/src/yuzu/configuration/qt_config.cpp index a71000b72..6aca71d7c 100644 --- a/src/yuzu/configuration/qt_config.cpp +++ b/src/yuzu/configuration/qt_config.cpp @@ -348,43 +348,45 @@ void QtConfig::SaveQtPlayerValues(const std::size_t player_index) { for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) { const std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]); - WriteSetting(std::string(player_prefix).append(Settings::NativeButton::mapping[i]), - player.buttons[i], std::make_optional(default_param)); + WriteStringSetting(std::string(player_prefix).append(Settings::NativeButton::mapping[i]), + player.buttons[i], std::make_optional(default_param)); } for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( default_analogs[i][0], default_analogs[i][1], default_analogs[i][2], default_analogs[i][3], default_stick_mod[i], 0.5f); - WriteSetting(std::string(player_prefix).append(Settings::NativeAnalog::mapping[i]), - player.analogs[i], std::make_optional(default_param)); + WriteStringSetting(std::string(player_prefix).append(Settings::NativeAnalog::mapping[i]), + player.analogs[i], std::make_optional(default_param)); } for (int i = 0; i < Settings::NativeMotion::NumMotions; ++i) { const std::string default_param = InputCommon::GenerateKeyboardParam(default_motions[i]); - WriteSetting(std::string(player_prefix).append(Settings::NativeMotion::mapping[i]), - player.motions[i], std::make_optional(default_param)); + WriteStringSetting(std::string(player_prefix).append(Settings::NativeMotion::mapping[i]), + player.motions[i], std::make_optional(default_param)); } } void QtConfig::SaveDebugControlValues() { for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) { const std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]); - WriteSetting(std::string("debug_pad_").append(Settings::NativeButton::mapping[i]), - Settings::values.debug_pad_buttons[i], std::make_optional(default_param)); + WriteStringSetting(std::string("debug_pad_").append(Settings::NativeButton::mapping[i]), + Settings::values.debug_pad_buttons[i], + std::make_optional(default_param)); } for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( default_analogs[i][0], default_analogs[i][1], default_analogs[i][2], default_analogs[i][3], default_stick_mod[i], 0.5f); - WriteSetting(std::string("debug_pad_").append(Settings::NativeAnalog::mapping[i]), - Settings::values.debug_pad_analogs[i], std::make_optional(default_param)); + WriteStringSetting(std::string("debug_pad_").append(Settings::NativeAnalog::mapping[i]), + Settings::values.debug_pad_analogs[i], + std::make_optional(default_param)); } } void QtConfig::SaveHidbusValues() { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( 0, 0, default_ringcon_analogs[0], default_ringcon_analogs[1], 0, 0.05f); - WriteSetting(std::string("ring_controller"), Settings::values.ringcon_analogs, - std::make_optional(default_param)); + WriteStringSetting(std::string("ring_controller"), Settings::values.ringcon_analogs, + std::make_optional(default_param)); } void QtConfig::SaveQtControlValues() { @@ -409,19 +411,20 @@ void QtConfig::SavePathValues() { WriteCategory(Settings::Category::Paths); - WriteSetting(std::string("romsPath"), UISettings::values.roms_path); + WriteStringSetting(std::string("romsPath"), UISettings::values.roms_path); BeginArray(std::string("gamedirs")); for (int i = 0; i < UISettings::values.game_dirs.size(); ++i) { SetArrayIndex(i); const auto& game_dir = UISettings::values.game_dirs[i]; - WriteSetting(std::string("path"), game_dir.path); - WriteSetting(std::string("deep_scan"), game_dir.deep_scan, std::make_optional(false)); - WriteSetting(std::string("expanded"), game_dir.expanded, std::make_optional(true)); + WriteStringSetting(std::string("path"), game_dir.path); + WriteBooleanSetting(std::string("deep_scan"), game_dir.deep_scan, + std::make_optional(false)); + WriteBooleanSetting(std::string("expanded"), game_dir.expanded, std::make_optional(true)); } EndArray(); - WriteSetting(std::string("recentFiles"), - UISettings::values.recent_files.join(QStringLiteral(", ")).toStdString()); + WriteStringSetting(std::string("recentFiles"), + UISettings::values.recent_files.join(QStringLiteral(", ")).toStdString()); EndGroup(); } @@ -438,14 +441,14 @@ void QtConfig::SaveShortcutValues() { BeginGroup(group); BeginGroup(name); - WriteSetting(std::string("KeySeq"), shortcut.keyseq, - std::make_optional(default_hotkey.keyseq)); - WriteSetting(std::string("Controller_KeySeq"), shortcut.controller_keyseq, - std::make_optional(default_hotkey.controller_keyseq)); - WriteSetting(std::string("Context"), shortcut.context, - std::make_optional(default_hotkey.context)); - WriteSetting(std::string("Repeat"), shortcut.repeat, - std::make_optional(default_hotkey.repeat)); + WriteStringSetting(std::string("KeySeq"), shortcut.keyseq, + std::make_optional(default_hotkey.keyseq)); + WriteStringSetting(std::string("Controller_KeySeq"), shortcut.controller_keyseq, + std::make_optional(default_hotkey.controller_keyseq)); + WriteIntegerSetting(std::string("Context"), shortcut.context, + std::make_optional(default_hotkey.context)); + WriteBooleanSetting(std::string("Repeat"), shortcut.repeat, + std::make_optional(default_hotkey.repeat)); EndGroup(); // name EndGroup(); // group @@ -460,9 +463,10 @@ void QtConfig::SaveUIValues() { WriteCategory(Settings::Category::Ui); WriteCategory(Settings::Category::UiGeneral); - WriteSetting(std::string("theme"), UISettings::values.theme, - std::make_optional(std::string( - UISettings::themes[static_cast<size_t>(UISettings::default_theme)].second))); + WriteStringSetting( + std::string("theme"), UISettings::values.theme, + std::make_optional(std::string( + UISettings::themes[static_cast<size_t>(UISettings::default_theme)].second))); SaveUIGamelistValues(); SaveUILayoutValues(); @@ -482,7 +486,7 @@ void QtConfig::SaveUIGamelistValues() { BeginArray(std::string("favorites")); for (int i = 0; i < UISettings::values.favorited_ids.size(); i++) { SetArrayIndex(i); - WriteSetting(std::string("program_id"), UISettings::values.favorited_ids[i]); + WriteIntegerSetting(std::string("program_id"), UISettings::values.favorited_ids[i]); } EndArray(); // favorites @@ -506,14 +510,15 @@ void QtConfig::SaveMultiplayerValues() { BeginArray(std::string("username_ban_list")); for (std::size_t i = 0; i < UISettings::values.multiplayer_ban_list.first.size(); ++i) { SetArrayIndex(static_cast<int>(i)); - WriteSetting(std::string("username"), UISettings::values.multiplayer_ban_list.first[i]); + WriteStringSetting(std::string("username"), + UISettings::values.multiplayer_ban_list.first[i]); } EndArray(); // username_ban_list BeginArray(std::string("ip_ban_list")); for (std::size_t i = 0; i < UISettings::values.multiplayer_ban_list.second.size(); ++i) { SetArrayIndex(static_cast<int>(i)); - WriteSetting(std::string("ip"), UISettings::values.multiplayer_ban_list.second[i]); + WriteStringSetting(std::string("ip"), UISettings::values.multiplayer_ban_list.second[i]); } EndArray(); // ip_ban_list diff --git a/src/yuzu_cmd/sdl_config.cpp b/src/yuzu_cmd/sdl_config.cpp index 39fd8050c..e81bf5d45 100644 --- a/src/yuzu_cmd/sdl_config.cpp +++ b/src/yuzu_cmd/sdl_config.cpp @@ -213,43 +213,45 @@ void SdlConfig::SaveSdlPlayerValues(const std::size_t player_index) { for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) { const std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]); - WriteSetting(std::string(player_prefix).append(Settings::NativeButton::mapping[i]), - player.buttons[i], std::make_optional(default_param)); + WriteStringSetting(std::string(player_prefix).append(Settings::NativeButton::mapping[i]), + player.buttons[i], std::make_optional(default_param)); } for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( default_analogs[i][0], default_analogs[i][1], default_analogs[i][2], default_analogs[i][3], default_stick_mod[i], 0.5f); - WriteSetting(std::string(player_prefix).append(Settings::NativeAnalog::mapping[i]), - player.analogs[i], std::make_optional(default_param)); + WriteStringSetting(std::string(player_prefix).append(Settings::NativeAnalog::mapping[i]), + player.analogs[i], std::make_optional(default_param)); } for (int i = 0; i < Settings::NativeMotion::NumMotions; ++i) { const std::string default_param = InputCommon::GenerateKeyboardParam(default_motions[i]); - WriteSetting(std::string(player_prefix).append(Settings::NativeMotion::mapping[i]), - player.motions[i], std::make_optional(default_param)); + WriteStringSetting(std::string(player_prefix).append(Settings::NativeMotion::mapping[i]), + player.motions[i], std::make_optional(default_param)); } } void SdlConfig::SaveDebugControlValues() { for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) { const std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]); - WriteSetting(std::string("debug_pad_").append(Settings::NativeButton::mapping[i]), - Settings::values.debug_pad_buttons[i], std::make_optional(default_param)); + WriteStringSetting(std::string("debug_pad_").append(Settings::NativeButton::mapping[i]), + Settings::values.debug_pad_buttons[i], + std::make_optional(default_param)); } for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( default_analogs[i][0], default_analogs[i][1], default_analogs[i][2], default_analogs[i][3], default_stick_mod[i], 0.5f); - WriteSetting(std::string("debug_pad_").append(Settings::NativeAnalog::mapping[i]), - Settings::values.debug_pad_analogs[i], std::make_optional(default_param)); + WriteStringSetting(std::string("debug_pad_").append(Settings::NativeAnalog::mapping[i]), + Settings::values.debug_pad_analogs[i], + std::make_optional(default_param)); } } void SdlConfig::SaveHidbusValues() { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( 0, 0, default_ringcon_analogs[0], default_ringcon_analogs[1], 0, 0.05f); - WriteSetting(std::string("ring_controller"), Settings::values.ringcon_analogs, - std::make_optional(default_param)); + WriteStringSetting(std::string("ring_controller"), Settings::values.ringcon_analogs, + std::make_optional(default_param)); } std::vector<Settings::BasicSetting*>& SdlConfig::FindRelevantList(Settings::Category category) { |