We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4246

Summary: Android: orientation between portrait<->landscape doesn't work
Product: SDL Reporter: Trent Gamblin <trent>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: rtrussell, sezeroz, sylvain.becker
Version: HG 2.0   
Hardware: x86_64   
OS: Linux   
Attachments: Fix for orientation just working between portrait<->landscape
Documentation fix (Add Android)
patch

Description Trent Gamblin 2018-09-03 07:18:10 UTC
All is fine if you want only landscape or only portrait, but if you want fullSensor it doesn't work. It will start in the correct orientation but not switch after that. I think the problem may be in SDL_androidwindow.c the resizable window flag is toggled off after creating the window, which is later checked in the Java code. My personal temporary fix was to comment all the SENSOR/hints stuff in SDLActivity.java and just stick in an orientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; Another possible fix is to check for multiple hints and if they're all included then set it to UNSPECIFIED.

If you have a preference on a way to fix this (I have a feeling removing the unset of RESIZABLE would cause problems) I can send a patch.
Comment 1 Sam Lantinga 2018-09-03 19:51:43 UTC
Yes, a patch checking for all hints is fine.

Thanks!
Comment 2 Trent Gamblin 2018-09-03 20:22:57 UTC
Created attachment 3294 [details]
Fix for orientation just working between portrait<->landscape

Here's a patch. I can change if it's not OK. Thanks.
Comment 3 Trent Gamblin 2018-09-03 20:29:41 UTC
Created attachment 3295 [details]
Documentation fix (Add Android)

Here's a small documentation fix too. This previously only mentioned iOS so confused me a bit.
Comment 4 Trent Gamblin 2018-09-08 22:25:18 UTC
Any changes you'd like to see to these patches? I know it's a bit funky, but can't just set orientation to UNSPECIFIED because that's equal to -1 according to Android docs and my testing.
Comment 5 Sam Lantinga 2018-10-03 22:47:09 UTC
If your window is not resizable then you're not actually allowed to set an orientation that changes the display size. What's your use case where this works?
Comment 6 Trent Gamblin 2018-10-03 22:56:24 UTC
The window is resizable. The internal SDL code of the Android driver toggles that flag off for some reason though. I figured it does it for a reason so it was best not to mess with it. See SDL_androidwindow.c line 175.
Comment 7 Trent Gamblin 2018-10-04 00:01:12 UTC
If you meant how does it work with that flag toggled off, honestly I don't know the internals of SDL much but it just works with the same code as on desktops in my game.
Comment 8 Sylvain 2018-10-09 13:00:22 UTC
Created attachment 3356 [details]
patch

A reworked patch for setting the orientation. it also doesn't keep the RESIZABLE flags.

- The hint is used, as documentation states it, to "explicitly control which UI orientations are allowed".
- RESIZABLE allows to have both landscape and portrait, provided it's not restricted by the hint.

But there is another issue, this time with SDL_WINDOW_FULLSCREEN, because it exchanges w/h and mislead the choice of orientation is nothing is specified.

(I think the issue is there: https://hg.libsdl.org/SDL/file/18f3294b230e/src/video/SDL_video.c#l1512 )
Comment 9 Richard Russell 2020-01-12 12:46:40 UTC
This bug seems not to be fixed in SDL 2.0.10.  Both patches are effective for me (I don't use the hint), so what is the current status as regards incorporation in a release?
Comment 10 Sylvain 2020-01-14 16:30:37 UTC
Hi Richard:

Which one of the 2/3 patches is working for you ?

I you don't use the hint, only the part clearing the RESIZEABLE flag should matter ?
Comment 11 Richard Russell 2020-01-17 10:02:15 UTC
(In reply to Sylvain from comment #10)
> Which one of the 2/3 patches is working for you ?

I used your patch because I need the 'full sensor' mode of operation.  But since I don't use the hint anyway, most of your code is redundant in my case.

> I you don't use the hint, only the part clearing the RESIZEABLE flag should
> matter ?

I'm not sure, I don't think I tried that.
Comment 12 Sylvain 2020-01-17 11:04:26 UTC
Ok thanks for the feedback ! 
I've commit the patch: https://hg.libsdl.org/SDL/rev/094b752ee72d
Comment 13 Richard Russell 2020-01-19 12:24:12 UTC
(In reply to Sylvain from comment #10)
> I you don't use the hint, only the part clearing the RESIZEABLE flag should
> matter ?

You're absolutely right.  I've reverted SDLActivity.java to the original SDL2.0.10 version and orientation changes are still working correctly for me, with the change to SDL_androidwindow.c so it no longer clears the SDL_WINDOW_RESIZABLE flag.  I should have tried that change first!

So I'm afraid my experience doesn't verify whether your patch is effective, in the presence of the hint.  In my application I prefer to set the allowed orientation(s) in AndroidManifest.xml anyway.