Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android: Add screen orientation presets to layout settings #142

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import io.github.mandarine3ds.mandarine.utils.ThemeUtil
import io.github.mandarine3ds.mandarine.viewmodel.EmulationViewModel
import io.github.mandarine3ds.mandarine.utils.NetPlayManager
import io.github.mandarine3ds.mandarine.dialogs.NetPlayDialog
import io.github.mandarine3ds.mandarine.features.settings.model.IntSetting

class EmulationActivity : AppCompatActivity() {
private val preferences: SharedPreferences
Expand Down Expand Up @@ -79,7 +80,7 @@ class EmulationActivity : AppCompatActivity() {
NativeLibrary.enableAdrenoTurboMode(BooleanSetting.ADRENO_GPU_BOOST.boolean)

binding = ActivityEmulationBinding.inflate(layoutInflater)
screenAdjustmentUtil = ScreenAdjustmentUtil(windowManager, settingsViewModel.settings)
screenAdjustmentUtil = ScreenAdjustmentUtil(this, windowManager, settingsViewModel.settings)
hotkeyUtility = HotkeyUtility(screenAdjustmentUtil, this)
setContentView(binding.root)

Expand Down Expand Up @@ -113,6 +114,8 @@ class EmulationActivity : AppCompatActivity() {

isEmulationRunning = true
instance = this

applyOrientationSettings() // Check for orientation settings at startup
}

// On some devices, the system bars will not disappear on first boot or after some
Expand All @@ -121,6 +124,7 @@ class EmulationActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
enableFullscreenImmersive()
applyOrientationSettings() // Check for orientation settings changes on runtime
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
Expand Down Expand Up @@ -230,6 +234,11 @@ class EmulationActivity : AppCompatActivity() {
}
}

private fun applyOrientationSettings() {
val orientationOption = IntSetting.ORIENTATION_OPTION.int
screenAdjustmentUtil.changeActivityOrientation(orientationOption)
}

// Gets button presses
@Suppress("DEPRECATION")
@SuppressLint("GestureBackNavigation")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Citra Emulator Project
// Copyright 2025 Citra Project / Mandarine Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

Expand All @@ -11,9 +11,16 @@ import io.github.mandarine3ds.mandarine.features.settings.model.IntSetting
import io.github.mandarine3ds.mandarine.features.settings.model.Settings
import io.github.mandarine3ds.mandarine.features.settings.utils.SettingsFile
import io.github.mandarine3ds.mandarine.utils.EmulationMenuSettings
import android.content.Context
import android.content.pm.ActivityInfo
import android.app.Activity
import io.github.mandarine3ds.mandarine.R

class ScreenAdjustmentUtil(private val windowManager: WindowManager,
private val settings: Settings) {
class ScreenAdjustmentUtil(
private val context: Context,
private val windowManager: WindowManager,
private val settings: Settings,
) {
fun swapScreen() {
val isEnabled = !EmulationMenuSettings.swapScreens
EmulationMenuSettings.swapScreens = isEnabled
Expand All @@ -26,9 +33,9 @@ class ScreenAdjustmentUtil(private val windowManager: WindowManager,
}

fun cycleLayouts() {
// TODO: figure out how to pull these from R.array
val landscapeValues = intArrayOf(2, 1, 3, 4, 0, 5)
val portraitValues = intArrayOf(0, 1)
val landscapeValues = context.resources.getIntArray(R.array.landscapeValues)
val portraitValues = context.resources.getIntArray(R.array.portraitValues)

if (NativeLibrary.isPortraitMode) {
val currentLayout = IntSetting.PORTRAIT_SCREEN_LAYOUT.int
val pos = portraitValues.indexOf(currentLayout)
Expand All @@ -55,4 +62,11 @@ class ScreenAdjustmentUtil(private val windowManager: WindowManager,
NativeLibrary.reloadSettings()
NativeLibrary.updateFramebuffer(NativeLibrary.isPortraitMode)
}

fun changeActivityOrientation(orientationOption: Int) {
val activity = context as? Activity ?: return
IntSetting.ORIENTATION_OPTION.int = orientationOption
settings.saveSetting(IntSetting.ORIENTATION_OPTION, SettingsFile.FILE_NAME_CONFIG)
activity.requestedOrientation = orientationOption
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Citra Emulator Project
// Copyright 2025 Citra Project / Mandarine Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

Expand Down Expand Up @@ -69,6 +69,7 @@ enum class IntSetting(
FORCE_HW_VERTEX_SHADERS("force_hw_vertex_shaders", Settings.SECTION_RENDERER, 0),
DISABLE_SURFACE_TEXTURE_COPY("disable_surface_texture_copy", Settings.SECTION_RENDERER, 0),
DISABLE_FLUSH_CPU_WRITE("disable_flush_cpu_write", Settings.SECTION_RENDERER, 0),
ORIENTATION_OPTION("screen_orientation", Settings.SECTION_LAYOUT, 2),
USE_ARTIC_BASE_CONTROLLER("use_artic_base_controller", Settings.SECTION_CONTROLS, 0);

override var int: Int = defaultValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Citra Emulator Project
// Copyright 2025 Citra Project / Mandarine Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

Expand Down Expand Up @@ -998,6 +998,17 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView)
private fun addLayoutSettings(sl: ArrayList<SettingsItem>) {
settingsActivity.setToolbarTitle(settingsActivity.getString(R.string.preferences_layout))
sl.apply {
add(
SingleChoiceSetting(
IntSetting.ORIENTATION_OPTION,
R.string.layout_screen_orientation,
0,
R.array.screenOrientations,
R.array.screenOrientationValues,
IntSetting.ORIENTATION_OPTION.key,
IntSetting.ORIENTATION_OPTION.defaultValue
)
)
add(
SingleChoiceSetting(
IntSetting.SCREEN_LAYOUT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
retainInstance = true
emulationState = EmulationState(game.path)
emulationActivity = requireActivity() as EmulationActivity
screenAdjustmentUtil =
ScreenAdjustmentUtil(emulationActivity.windowManager, settingsViewModel.settings)
screenAdjustmentUtil = ScreenAdjustmentUtil(requireContext(), requireActivity().windowManager, settingsViewModel.settings)
EmulationLifecycleUtil.addShutdownHook(hook = { emulationState.stop() })
EmulationLifecycleUtil.addPauseResumeHook(hook = { togglePause() })
}
Expand Down
10 changes: 9 additions & 1 deletion src/android/app/src/main/jni/default_ini.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Citra Emulator Project
// Copyright 2025 Citra Project / Mandarine Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

Expand Down Expand Up @@ -239,6 +239,14 @@ custom_bottom_y =
custom_bottom_width =
custom_bottom_height =

# Orientation option for the emulator
# 2 (default): Automatic
# 0: Landscape
# 8: Landscape (Flipped)
# 1: Portrait
# 9: Portrait (Flipped)
screen_orientation =

# Layout for the portrait mode
# 0 (default): Top and bottom screens at top, full width
# 1: Custom Layout
Expand Down
28 changes: 28 additions & 0 deletions src/android/app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,32 @@
<item>12</item>
</integer-array>

<integer-array name="landscapeValues">
<item>2</item>
<item>1</item>
<item>3</item>
<item>4</item>
<item>0</item>
<item>5</item>
</integer-array>
<integer-array name="portraitValues">
<item>0</item>
<item>1</item>
</integer-array>

<string-array name="screenOrientations">
<item>@string/layout_screen_orientation_auto_sensor</item>
<item>@string/layout_screen_orientation_landscape</item>
<item>@string/layout_screen_orientation_landscape_reverse</item>
<item>@string/layout_screen_orientation_portrait</item>
<item>@string/layout_screen_orientation_portrait_reverse</item>
</string-array>
<integer-array name="screenOrientationValues">
<item>2</item>
<item>0</item>
<item>8</item>
<item>1</item>
<item>9</item>
</integer-array>

</resources>
8 changes: 8 additions & 0 deletions src/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@
<string name="audio_input_type">Audio Input Device</string>
<string name="sound_output_mode">Sound Output Mode</string>

<!-- Layout settings strings -->
<string name="layout_screen_orientation">Screen Orientation</string>
<string name="layout_screen_orientation_auto_sensor">Automatic</string>
<string name="layout_screen_orientation_landscape">Landscape</string>
<string name="layout_screen_orientation_landscape_reverse">Reverse Landscape</string>
<string name="layout_screen_orientation_portrait">Portrait</string>
<string name="layout_screen_orientation_portrait_reverse">Reverse Portrait</string>

<!-- Miscellaneous -->
<string name="clear">Clear</string>
<string name="slider_default">Default</string>
Expand Down
Loading