1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java Thu Aug 23 14:32:30 2018 -0400
1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java Thu Aug 23 14:05:25 2018 -0700
1.3 @@ -54,6 +54,14 @@
1.4 private static final int SDL_SYSTEM_CURSOR_NO = 10;
1.5 private static final int SDL_SYSTEM_CURSOR_HAND = 11;
1.6
1.7 + protected static final int SDL_ORIENTATION_UNKNOWN = 0;
1.8 + protected static final int SDL_ORIENTATION_LANDSCAPE = 1;
1.9 + protected static final int SDL_ORIENTATION_LANDSCAPE_FLIPPED = 2;
1.10 + protected static final int SDL_ORIENTATION_PORTRAIT = 3;
1.11 + protected static final int SDL_ORIENTATION_PORTRAIT_FLIPPED = 4;
1.12 +
1.13 + protected static int mCurrentOrientation;
1.14 +
1.15 // Handle the state of the native layer
1.16 public enum NativeState {
1.17 INIT, RESUMED, PAUSED
1.18 @@ -250,6 +258,10 @@
1.19 mLayout = new RelativeLayout(this);
1.20 mLayout.addView(mSurface);
1.21
1.22 + // Get our current screen orientation and pass it down.
1.23 + mCurrentOrientation = SDLActivity.getCurrentOrientation();
1.24 + SDLActivity.onNativeOrientationChanged(mCurrentOrientation);
1.25 +
1.26 setContentView(mLayout);
1.27
1.28 setWindowStyle(false);
1.29 @@ -304,6 +316,32 @@
1.30 SDLActivity.handleNativeState();
1.31 }
1.32
1.33 + public static int getCurrentOrientation() {
1.34 + final Context context = SDLActivity.getContext();
1.35 + final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
1.36 +
1.37 + int result = SDL_ORIENTATION_UNKNOWN;
1.38 +
1.39 + switch (display.getRotation()) {
1.40 + case Surface.ROTATION_0:
1.41 + result = SDL_ORIENTATION_PORTRAIT;
1.42 + break;
1.43 +
1.44 + case Surface.ROTATION_90:
1.45 + result = SDL_ORIENTATION_LANDSCAPE;
1.46 + break;
1.47 +
1.48 + case Surface.ROTATION_180:
1.49 + result = SDL_ORIENTATION_PORTRAIT_FLIPPED;
1.50 + break;
1.51 +
1.52 + case Surface.ROTATION_270:
1.53 + result = SDL_ORIENTATION_LANDSCAPE_FLIPPED;
1.54 + break;
1.55 + }
1.56 +
1.57 + return result;
1.58 + }
1.59
1.60 @Override
1.61 public void onWindowFocusChanged(boolean hasFocus) {
1.62 @@ -628,6 +666,7 @@
1.63 public static native void onNativeSurfaceDestroyed();
1.64 public static native String nativeGetHint(String name);
1.65 public static native void nativeSetenv(String name, String value);
1.66 + public static native void onNativeOrientationChanged(int orientation);
1.67
1.68 /**
1.69 * This method is called by SDL using JNI.
1.70 @@ -1748,28 +1787,45 @@
1.71 @Override
1.72 public void onSensorChanged(SensorEvent event) {
1.73 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
1.74 +
1.75 + // Since we may have an orientation set, we won't receive onConfigurationChanged events.
1.76 + // We thus should check here.
1.77 + int newOrientation = SDLActivity.SDL_ORIENTATION_UNKNOWN;
1.78 +
1.79 float x, y;
1.80 switch (mDisplay.getRotation()) {
1.81 case Surface.ROTATION_90:
1.82 x = -event.values[1];
1.83 y = event.values[0];
1.84 + newOrientation = SDLActivity.SDL_ORIENTATION_LANDSCAPE;
1.85 break;
1.86 case Surface.ROTATION_270:
1.87 x = event.values[1];
1.88 y = -event.values[0];
1.89 + newOrientation = SDLActivity.SDL_ORIENTATION_LANDSCAPE_FLIPPED;
1.90 break;
1.91 case Surface.ROTATION_180:
1.92 x = -event.values[1];
1.93 y = -event.values[0];
1.94 + newOrientation = SDLActivity.SDL_ORIENTATION_PORTRAIT_FLIPPED;
1.95 break;
1.96 default:
1.97 x = event.values[0];
1.98 y = event.values[1];
1.99 + newOrientation = SDLActivity.SDL_ORIENTATION_PORTRAIT;
1.100 break;
1.101 }
1.102 +
1.103 + if (newOrientation != SDLActivity.mCurrentOrientation) {
1.104 + SDLActivity.mCurrentOrientation = newOrientation;
1.105 + SDLActivity.onNativeOrientationChanged(newOrientation);
1.106 + }
1.107 +
1.108 SDLActivity.onNativeAccel(-x / SensorManager.GRAVITY_EARTH,
1.109 y / SensorManager.GRAVITY_EARTH,
1.110 event.values[2] / SensorManager.GRAVITY_EARTH);
1.111 +
1.112 +
1.113 }
1.114 }
1.115
2.1 --- a/src/core/android/SDL_android.c Thu Aug 23 14:32:30 2018 -0400
2.2 +++ b/src/core/android/SDL_android.c Thu Aug 23 14:05:25 2018 -0700
2.3 @@ -135,6 +135,10 @@
2.4 JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeEnvironmentVariablesSet)(
2.5 JNIEnv* env, jclass cls);
2.6
2.7 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
2.8 + JNIEnv* env, jclass cls,
2.9 + jint orientation);
2.10 +
2.11 /* Java class SDLInputConnection */
2.12 JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
2.13 JNIEnv* env, jclass cls,
2.14 @@ -535,6 +539,14 @@
2.15 Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
2.16 }
2.17
2.18 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
2.19 + JNIEnv *env, jclass jcls,
2.20 + jint orientation)
2.21 +{
2.22 + SDL_VideoDisplay *display = SDL_GetDisplay(0);
2.23 + SDL_SendDisplayEvent(display, SDL_DISPLAYEVENT_ORIENTATION, orientation);
2.24 +}
2.25 +
2.26 /* Paddown */
2.27 JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)(
2.28 JNIEnv* env, jclass jcls,