SDL: Use the correct orientation transformation based on whether the device is naturally landscape or portrait

From 9eb5eab0adb032336ebaf5d980d5a246977e8272 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 17 Jun 2023 08:04:34 -0700
Subject: [PATCH] Use the correct orientation transformation based on whether
 the device is naturally landscape or portrait

---
 src/joystick/SDL_gamepad.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index 88e6efee1ef4..7091c11a2d79 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -377,15 +377,27 @@ static void RecenterGamepad(SDL_Gamepad *gamepad)
  */
 static void AdjustSensorOrientation(float *src, float *dst)
 {
-    /* When a phone is rotated left and laid flat, the axes change
-       orientation as follows:
-        -X to +X becomes +Z to -Z
-        -Y to +Y becomes +X to -X
-        -Z to +Z becomes -Y to +Y
-    */
-    dst[0] = -src[1];
-    dst[1] = src[2];
-    dst[2] = -src[0];
+    if (SDL_GetDisplayNaturalOrientation(SDL_GetPrimaryDisplay()) == SDL_ORIENTATION_LANDSCAPE) {
+        /* When a device in landscape orientation is laid flat, the axes change
+           orientation as follows:
+            -X to +X becomes -X to +X
+            -Y to +Y becomes +Z to -Z
+            -Z to +Z becomes -Y to +Y
+        */
+        dst[0] = src[0];
+        dst[1] = src[2];
+        dst[2] = -src[1];
+    } else {
+        /* When a device in portrait orientation is rotated left and laid flat,
+           the axes change orientation as follows:
+            -X to +X becomes +Z to -Z
+            -Y to +Y becomes +X to -X
+            -Z to +Z becomes -Y to +Y
+        */
+        dst[0] = -src[1];
+        dst[1] = src[2];
+        dst[2] = -src[0];
+    }
 }
 
 /*