# Android screen orientation issues (2.0.9)

**URL:** <https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262>\
**Category:** SDL Development\
**Created:** [June 10, 2019, 2:02pm UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262 "2019-06-10T14:02:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aa-dav](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@aa-dav](https://discourse.libsdl.org/u/aa-dav)\
**Post date:** [June 10, 2019, 2:02pm UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262/1 "2019-06-10T14:02:27Z")

</div>

Hello! Sorry for my english, I am not native speaker.  
Current code seems to contain several errors:

1. SDL\_GetDisplayOrientation( … ) returns 0 (UNKNOWN) after application start.  
I do not know why, but:

> // Get our current screen orientation and pass it down.  
> mCurrentOrientation = SDLActivity.getCurrentOrientation();  
> SDLActivity.onNativeOrientationChanged(mCurrentOrientation);

in onCreate() doesn’t do anything. And later:

> ```
> if ( SDLActivity.mCurrentOrientation) { 
> SDLActivity.mCurrentOrientation = newOrientation;
> SDLActivity.onNativeOrientationChanged(newOrientation);
> }
> 
> ```

mCurrentOrientation blocks event from C-code and it doesn’t see correct value of orientation until physical flip of device (generation of event after changed orientation).  
I think it’s better to check cached value in C-code, not in Java.

1. enum

> typedef enum  
> {  
> SDL\_ORIENTATION\_UNKNOWN,  
> SDL\_ORIENTATION\_LANDSCAPE,  
> SDL\_ORIENTATION\_LANDSCAPE\_FLIPPED,  
> SDL\_ORIENTATION\_PORTRAIT,  
> SDL\_ORIENTATION\_PORTRAIT\_FLIPPED  
> } SDL\_DisplayOrientation;

and (or?) it’s usage are misleading. These values are mapped to Android “rotation from default position” in SDLActivity.java:

> ```
> switch (display.getRotation()) {
> case Surface.ROTATION_0:
> result = SDL_ORIENTATION_PORTRAIT;
> break;
> 
> case Surface.ROTATION_90:
> result = SDL_ORIENTATION_LANDSCAPE;
> break;
> 
> case Surface.ROTATION_180:
> result = SDL_ORIENTATION_PORTRAIT_FLIPPED;
> break;
> 
> case Surface.ROTATION_270:
> result = SDL_ORIENTATION_LANDSCAPE_FLIPPED;
> break;
> }
> 
> ```

but this is wrong - default position (ROTATION\_0) is vertical for smartphones and all these values become misleading. I think it’s better to change meaning of enum values to Android “rotation degrees” because this is important for accelerator orientation determination only.  
Also mapping to joistick (also in SDLActivity.java):

> ```
> switch (mDisplay.getRotation()) {
> case Surface.ROTATION_90:
> x = -event.values[1];
> y = event.values[0];
> newOrientation = SDLActivity.SDL_ORIENTATION_LANDSCAPE;
> break;
> case Surface.ROTATION_270:
> x = event.values[1];
> y = -event.values[0];
> newOrientation = SDLActivity.SDL_ORIENTATION_LANDSCAPE_FLIPPED;
> break;
> case Surface.ROTATION_180:
> x = -event.values[1];
> y = -event.values[0];
> newOrientation = SDLActivity.SDL_ORIENTATION_PORTRAIT_FLIPPED;
> break;
> default:
> x = event.values[0];
> y = event.values[1];
> newOrientation = SDLActivity.SDL_ORIENTATION_PORTRAIT;
> break;
> }
> 
> ```

is wrong for direction ROTATION\_180 - it must be opposite of ROTATION\_0 (default here) by signs only, but x and y are swapped also. This is incorrect. Must be:

> ```
> case Surface.ROTATION_180:
> x = -event.values[0];
> y = -event.values[1];
> 
> ```

Hope this helps to improve next version of SDL2.  
I very appreciate your work! Super great job! Thank you very much!

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [June 11, 2019, 8:59am UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262/2 "2019-06-11T08:59:18Z")

</div>

Hi,  
Can you put some log in the java getCurrentOrientation()  
like:

```java
Log.v(TAG, "SDL getCurrentOrientation: getRotation()=" + display.getRotation() + " result=" + result);

```

and also something similar in onSensorChanged()

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [June 11, 2019, 9:02am UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262/3 "2019-06-11T09:02:26Z")

</div>

I’ve pushed your fix for ROTATION\_180 thanks

> **[SDL: changeset 12830:370a2c6f5a1d](https://hg.libsdl.org/SDL/rev/370a2c6f5a1d)**
>
> Android: fix coordinates for Surface.ROTATION\_180

---

<div class="post-metadata">

**Author:** ![aa-dav](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@aa-dav](https://discourse.libsdl.org/u/aa-dav)\
**Post date:** [June 11, 2019, 9:33am UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262/4 "2019-06-11T09:33:41Z")

</div>

onSensorChanged() gets correct value of orientation, but it cannot pass it to C-code because in the start (onCreate) this correct value was cached in mCurrentOrientation and later it blocks by “if” inside onSensorChanged:

```auto
            if ( newOrientation != SDLActivity.mCurrentOrientation) { // no way
                SDLActivity.mCurrentOrientation = newOrientation;
                SDLActivity.onNativeOrientationChanged(newOrientation); // c-code is not called
            }

```

I think real problem is inside onCreate:

```auto
// Get our current screen orientation and pass it down.
mCurrentOrientation = SDLActivity.getCurrentOrientation();
SDLActivity.onNativeOrientationChanged(mCurrentOrientation);

```

And again - mCurrentOrientation gets correct value of current screen orientation.  
But it looks like code inside onNativeOrientationChanged don’t apply it to C-variable at this point for some reason.  
Maybe at this moment some inner structures of C-code are not initialized? I do not know.  
I’l try to inspect it by myself, but I may have problems with understanding of inner structure of SDL.

---

<div class="post-metadata">

**Author:** ![aa-dav](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@aa-dav](https://discourse.libsdl.org/u/aa-dav)\
**Post date:** [June 11, 2019, 9:55am UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262/5 "2019-06-11T09:55:31Z")

</div>

Well, I found it - first time screenOrientation is initialized in onCreate and it is passed to  
SDLActivity.onNativeOrientationChanged:

```auto
 JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeOrientationChanged)(
                                     JNIEnv *env, jclass jcls,
                                     jint orientation)
 {
     SDL_VideoDisplay *display = SDL_GetDisplay(0);
     SDL_SendDisplayEvent(display, SDL_DISPLAYEVENT_ORIENTATION, orientation);
 }

```

display gets nullptr and event is rejected inside SDL\_SendDisplayEvent.  
I suppose displays are not initialized at this point.  
This is why cached values in C and Java differs after startup and after that cached value in Java blocks C-code from receiving correct value until device is physically flipped.  
My temporary fix is not suitable for release product, so I hope this information will help to make it cool.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [June 11, 2019, 11:01am UTC](https://discourse.libsdl.org/t/android-screen-orientation-issues-2-0-9/26262/6 "2019-06-11T11:01:59Z")

</div>

Ok you aren’t using the latest SDL source code, because this has been fixed:

> **[SDL: changeset 12625:e58468e48179](https://hg.libsdl.org/SDL/rev/e58468e48179)**
>
> Fixed initial display orientation at Android app start

  
Get head by cloning with mercurial: [https://hg.libsdl.org/SDL](https://hg.libsdl.org/SDL)
