Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit ad7452a

Browse files
authored
Merge pull request #1834 from b10z/ForceOrientation
Force orientation for Android
2 parents dadaa54 + 4b006ac commit ad7452a

File tree

7 files changed

+59
-6
lines changed

7 files changed

+59
-6
lines changed

libswirl/android/Android.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_getControllers(JNIEnv
116116

117117
JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_setupMic(JNIEnv *env,jobject obj,jobject sip) __attribute__((visibility("default")));
118118

119-
SETTINGS_ACCESSORS(Nosound, aica.NoSound, jboolean)
120-
SETTINGS_ACCESSORS(Widescreen, rend.WideScreen, jboolean)
119+
SETTINGS_ACCESSORS(Nosound, aica.NoSound, jboolean);
120+
SETTINGS_ACCESSORS(Widescreen, rend.WideScreen, jboolean);
121121
SETTINGS_ACCESSORS(VirtualGamepadVibration, input.VirtualGamepadVibration, jint);
122+
SETTINGS_ACCESSORS(ScreenOrientation, rend.ScreenOrientation, jint);
122123

123124
JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_screenDpi(JNIEnv *env,jobject obj, jint screenDpi) __attribute__((visibility("default")));
124125
JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_guiOpenSettings(JNIEnv *env,jobject obj) __attribute__((visibility("default")));
@@ -725,4 +726,4 @@ void android_RecreateView()
725726
JNIEnv *env = jvm_attacher.getEnv();
726727
jmethodID RecreateViewMID = env->GetMethodID(env->GetObjectClass(g_activity), "RecreateView", "()V");
727728
env->CallVoidMethod(g_activity, RecreateViewMID);
728-
}
729+
}

libswirl/gui/gui_settings_general.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ void gui_settings_general()
4141
ImGui::SameLine();
4242
gui_ShowHelpMarker("The language as configured in the Dreamcast BIOS");
4343

44+
#ifdef _ANDROID
45+
const char *orientation[] = { "Auto", "Force Portrait", "Force Landscape" };
46+
if (ImGui::BeginCombo("Orientation", orientation[settings.rend.ScreenOrientation], ImGuiComboFlags_None))
47+
{
48+
for (int i = 0; i < IM_ARRAYSIZE(orientation); i++)
49+
{
50+
bool is_selected = settings.rend.ScreenOrientation == i;
51+
if (ImGui::Selectable(orientation[i], &is_selected))
52+
settings.rend.ScreenOrientation = i;
53+
if (is_selected)
54+
ImGui::SetItemDefaultFocus();
55+
}
56+
ImGui::EndCombo();
57+
}
58+
ImGui::SameLine();
59+
gui_ShowHelpMarker("Select type of orientation");
60+
#endif
4461
const char *broadcast[] = { "NTSC", "PAL", "PAL/M", "PAL/N", "Default" };
4562
if (ImGui::BeginCombo("Broadcast", broadcast[settings.dreamcast.broadcast], ImGuiComboFlags_None))
4663
{

libswirl/libswirl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ void InitSettings()
309309
settings.rend.Fog = true;
310310
settings.rend.FloatVMUs = false;
311311
settings.rend.Rotate90 = false;
312+
settings.rend.ScreenOrientation = 0; //default is 0 (Auto Rotation)
312313

313314
settings.pvr.ta_skip = 0;
314315
settings.pvr.backend = "auto";
@@ -405,6 +406,7 @@ void LoadSettings(bool game_specific)
405406
settings.rend.Fog = cfgLoadBool(config_section, "rend.Fog", settings.rend.Fog);
406407
settings.rend.FloatVMUs = cfgLoadBool(config_section, "rend.FloatVMUs", settings.rend.FloatVMUs);
407408
settings.rend.Rotate90 = cfgLoadBool(config_section, "rend.Rotate90", settings.rend.Rotate90);
409+
settings.rend.ScreenOrientation = cfgLoadInt(config_section, "rend.ScreenOrientation", settings.rend.ScreenOrientation); //Load Orientation
408410

409411
settings.pvr.ta_skip = cfgLoadInt(config_section, "ta.skip", settings.pvr.ta_skip);
410412
settings.pvr.backend = cfgLoadStr(config_section, "pvr.backend", settings.pvr.backend.c_str());
@@ -563,6 +565,7 @@ void SaveSettings()
563565
cfgSaveBool("config", "rend.Fog", settings.rend.Fog);
564566
cfgSaveBool("config", "rend.FloatVMUs", settings.rend.FloatVMUs);
565567
cfgSaveBool("config", "rend.Rotate90", settings.rend.Rotate90);
568+
cfgSaveInt("config", "rend.ScreenOrientation", settings.rend.ScreenOrientation);
566569
cfgSaveInt("config", "ta.skip", settings.pvr.ta_skip);
567570
cfgSaveStr("config", "pvr.backend", settings.pvr.backend.c_str());
568571

libswirl/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ using namespace std;
359359
#define naked __attribute__((naked))
360360
#endif
361361

362-
363362
// NOTE: Always inline for macOS builds or it causes duplicate symbol linker errors
364363
#if DEBUG && HOST_OS != OS_DARWIN
365364
//force
@@ -486,6 +485,7 @@ struct settings_t
486485
bool Fog;
487486
bool FloatVMUs;
488487
bool Rotate90; // Rotate the screen 90 deg CC
488+
int ScreenOrientation; //Force Screen Orientation value here: 1=Force Portrait, 2=Force Landscape, 3=AutoRotate
489489
} rend;
490490

491491
struct

reicast/android-studio/reicast/src/main/java/com/reicast/emulator/BaseGLActivity.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import android.content.DialogInterface;
77
import android.content.Intent;
88
import android.content.SharedPreferences;
9+
import android.content.pm.ActivityInfo;
910
import android.content.pm.PackageManager;
11+
import android.content.res.Configuration;
1012
import android.net.Uri;
1113
import android.os.Build;
1214
import android.os.Bundle;
@@ -21,6 +23,7 @@
2123
import android.view.InputDevice;
2224
import android.view.KeyEvent;
2325
import android.view.MotionEvent;
26+
import android.view.Surface;
2427
import android.view.View;
2528
import android.view.ViewConfiguration;
2629

@@ -60,7 +63,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6063
prefs = PreferenceManager.getDefaultSharedPreferences(this);
6164

6265
Emulator app = (Emulator)getApplicationContext();
63-
app.getConfigurationPrefs();
6466
Emulator.setCurrentActivity(this);
6567

6668
OuyaController.init(this);
@@ -103,8 +105,11 @@ public void onClick(DialogInterface dialog,int id) {
103105

104106
InputDeviceManager.getInstance().startListening(getApplicationContext());
105107
register(this);
106-
108+
app.getConfigurationPrefs(); //when this method is on top, the values of screenOrientation, vibrationDuration and noSound did not load as they should. moved here fixes all these problems.
107109
audioBackend = new AudioBackend();
110+
setOrientation(); //Setting device orientation as the application starts.
111+
112+
108113

109114
// When viewing a resource, pass its URI to the native code for opening
110115
Intent intent = getIntent();
@@ -134,6 +139,26 @@ private void setStorageDirectories()
134139
JNIdc.setExternalStorageDirectories(pathList.toArray());
135140
}
136141

142+
public void setOrientation(){ //forcing orientation, depending on the value of Emulator.screenOrientation
143+
Log.i("reicast", Integer.toString(Emulator.screenOrientation));
144+
if ((Emulator.screenOrientation == 1)){
145+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
146+
}else if ((Emulator.screenOrientation == 2)){
147+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
148+
}else{
149+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
150+
}
151+
}
152+
153+
@Override
154+
public void onConfigurationChanged(Configuration newConfig) { //as the device orientation changes, the device checks the config and apply the Orientation if possible (this is used in order to get revert_landscape working as well in realtime, without restart or Save changes)
155+
super.onConfigurationChanged(newConfig);
156+
setOrientation();
157+
158+
159+
}
160+
161+
137162
@Override
138163
protected void onDestroy() {
139164
super.onDestroy();

reicast/android-studio/reicast/src/main/java/com/reicast/emulator/Emulator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Emulator extends Application {
2323

2424
public static boolean nosound = false;
2525
public static int vibrationDuration = 20;
26+
public static int screenOrientation = 0; //Default value is 0
2627

2728
public static int maple_devices[] = {
2829
MDT_None,
@@ -44,6 +45,7 @@ public class Emulator extends Application {
4445
public void getConfigurationPrefs() {
4546
Emulator.nosound = JNIdc.getNosound();
4647
Emulator.vibrationDuration = JNIdc.getVirtualGamepadVibration();
48+
Emulator.screenOrientation = JNIdc.getScreenOrientation(); //Loading screenOrientation
4749
JNIdc.getControllers(maple_devices, maple_expansion_devices);
4850
}
4951

@@ -53,10 +55,12 @@ public void getConfigurationPrefs() {
5355
*/
5456
public void SaveAndroidSettings(String homeDirectory)
5557
{
58+
5659
Log.i("reicast", "SaveAndroidSettings: saving preferences");
5760
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
5861
Emulator.nosound = JNIdc.getNosound();
5962
Emulator.vibrationDuration = JNIdc.getVirtualGamepadVibration();
63+
Emulator.screenOrientation = JNIdc.getScreenOrientation();
6064
JNIdc.getControllers(maple_devices, maple_expansion_devices);
6165

6266
prefs.edit()
@@ -67,6 +71,8 @@ public void SaveAndroidSettings(String homeDirectory)
6771
if (micPluggedIn() && currentActivity instanceof BaseGLActivity) {
6872
((BaseGLActivity)currentActivity).requestRecordAudioPermission();
6973
}
74+
currentActivity.setOrientation(); //As the settings are saved, setOrientation sets the new Orientation of the device
75+
7076
}
7177

7278
public void LaunchFromUrl(String url) {

reicast/android-studio/reicast/src/main/java/com/reicast/emulator/emu/JNIdc.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public final class JNIdc
2929

3030
public static native void setupMic(SipEmulator sip);
3131
public static native boolean getNosound();
32+
public static native int getScreenOrientation(); //Using SETTINGS_ACCESSORS to get the value of ScreenOrientation
3233

3334
public static native int getVirtualGamepadVibration();
3435

0 commit comments

Comments
 (0)