From 5f8ddef3e03bf543e221479e9c842af4dd7be325 Mon Sep 17 00:00:00 2001
From: Dmugetsu <diegoavilafacebook75@gmail.com>
Date: Fri, 3 Jan 2025 22:43:08 -0600
Subject: [PATCH] Motion Controller toggle Added under input settings

---
 src/common/config.cpp          | 11 +++++++++++
 src/common/config.h            |  2 ++
 src/input/controller.cpp       | 31 +++++++++++++++----------------
 src/qt_gui/settings_dialog.cpp |  5 ++++-
 src/qt_gui/settings_dialog.ui  |  7 +++++++
 5 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/src/common/config.cpp b/src/common/config.cpp
index de73ac1a305..60ec851cfd7 100644
--- a/src/common/config.cpp
+++ b/src/common/config.cpp
@@ -47,6 +47,7 @@ static std::string updateChannel;
 static std::string backButtonBehavior = "left";
 static bool useSpecialPad = false;
 static int specialPadClass = 1;
+static bool isMotionControlsEnabled = true;
 static bool isDebugDump = false;
 static bool isShaderDebug = false;
 static bool isShowSplash = false;
@@ -176,6 +177,10 @@ int getSpecialPadClass() {
     return specialPadClass;
 }
 
+bool getIsMotionControlsEnabled() {
+    return isMotionControlsEnabled;
+}
+
 bool debugDump() {
     return isDebugDump;
 }
@@ -372,6 +377,10 @@ void setSpecialPadClass(int type) {
     specialPadClass = type;
 }
 
+void setIsMotionControlsEnabled(bool use) {
+    isMotionControlsEnabled = use;
+}
+
 void setSeparateUpdateEnabled(bool use) {
     separateupdatefolder = use;
 }
@@ -600,6 +609,7 @@ void load(const std::filesystem::path& path) {
         backButtonBehavior = toml::find_or<std::string>(input, "backButtonBehavior", "left");
         useSpecialPad = toml::find_or<bool>(input, "useSpecialPad", false);
         specialPadClass = toml::find_or<int>(input, "specialPadClass", 1);
+        isMotionControlsEnabled = toml::find_or<bool>(input, "isMotionControlsEnabled", true);
     }
 
     if (data.contains("GPU")) {
@@ -715,6 +725,7 @@ void save(const std::filesystem::path& path) {
     data["Input"]["backButtonBehavior"] = backButtonBehavior;
     data["Input"]["useSpecialPad"] = useSpecialPad;
     data["Input"]["specialPadClass"] = specialPadClass;
+    data["Input"]["isMotionControlsEnabled"] = isMotionControlsEnabled;
     data["GPU"]["screenWidth"] = screenWidth;
     data["GPU"]["screenHeight"] = screenHeight;
     data["GPU"]["nullGpu"] = isNullGpu;
diff --git a/src/common/config.h b/src/common/config.h
index f3b81fe20b2..ee8986fb428 100644
--- a/src/common/config.h
+++ b/src/common/config.h
@@ -38,6 +38,7 @@ int getCursorHideTimeout();
 std::string getBackButtonBehavior();
 bool getUseSpecialPad();
 int getSpecialPadClass();
+bool getIsMotionControlsEnabled();
 
 u32 getScreenWidth();
 u32 getScreenHeight();
@@ -84,6 +85,7 @@ void setCursorHideTimeout(int newcursorHideTimeout);
 void setBackButtonBehavior(const std::string& type);
 void setUseSpecialPad(bool use);
 void setSpecialPadClass(int type);
+void setIsMotionControlsEnabled(bool use);
 
 void setLogType(const std::string& type);
 void setLogFilter(const std::string& type);
diff --git a/src/input/controller.cpp b/src/input/controller.cpp
index efef90f6828..98075d80ded 100644
--- a/src/input/controller.cpp
+++ b/src/input/controller.cpp
@@ -9,6 +9,7 @@
 
 #include <SDL3/SDL.h>
 #include "common/assert.h"
+#include "common/config.h"
 #include "common/logging/log.h"
 #include "common/path_util.h"
 #include "core/libraries/kernel/time.h"
@@ -196,11 +197,6 @@ void GameController::CalculateOrientation(Libraries::Pad::OrbisFVector3& acceler
     gz += Kp * ez + Ki * eInt[2];
 
     //// Integrate rate of change of quaternion
-    // float pa = q2, pb = q3, pc = q4;
-    // q1 += (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltaTime);
-    // q2 += (pa * gx + pb * gz - pc * gy) * (0.5f * deltaTime);
-    // q3 += (pb * gy - pa * gz + pc * gx) * (0.5f * deltaTime);
-    // q4 += (pc * gz + pa * gy - pb * gx) * (0.5f * deltaTime);
     q1 += (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltaTime);
     q2 += (q1 * gx + q3 * gz - q4 * gy) * (0.5f * deltaTime);
     q3 += (q1 * gy - q2 * gz + q4 * gx) * (0.5f * deltaTime);
@@ -254,18 +250,21 @@ void GameController::TryOpenSDLController() {
         int gamepad_count;
         SDL_JoystickID* gamepads = SDL_GetGamepads(&gamepad_count);
         m_sdl_gamepad = gamepad_count > 0 ? SDL_OpenGamepad(gamepads[0]) : nullptr;
-        if (SDL_SetGamepadSensorEnabled(m_sdl_gamepad, SDL_SENSOR_GYRO, true)) {
-            gyro_poll_rate = SDL_GetGamepadSensorDataRate(m_sdl_gamepad, SDL_SENSOR_GYRO);
-            LOG_INFO(Input, "Gyro initialized, poll rate: {}", gyro_poll_rate);
-        } else {
-            LOG_ERROR(Input, "Failed to initialize gyro controls for gamepad");
-        }
-        if (SDL_SetGamepadSensorEnabled(m_sdl_gamepad, SDL_SENSOR_ACCEL, true)) {
-            accel_poll_rate = SDL_GetGamepadSensorDataRate(m_sdl_gamepad, SDL_SENSOR_ACCEL);
-            LOG_INFO(Input, "Accel initialized, poll rate: {}", accel_poll_rate);
-        } else {
-            LOG_ERROR(Input, "Failed to initialize accel controls for gamepad");
+        if (Config::getIsMotionControlsEnabled()) {
+            if (SDL_SetGamepadSensorEnabled(m_sdl_gamepad, SDL_SENSOR_GYRO, true)) {
+                gyro_poll_rate = SDL_GetGamepadSensorDataRate(m_sdl_gamepad, SDL_SENSOR_GYRO);
+                LOG_INFO(Input, "Gyro initialized, poll rate: {}", gyro_poll_rate);
+            } else {
+                LOG_ERROR(Input, "Failed to initialize gyro controls for gamepad");
+            }
+            if (SDL_SetGamepadSensorEnabled(m_sdl_gamepad, SDL_SENSOR_ACCEL, true)) {
+                accel_poll_rate = SDL_GetGamepadSensorDataRate(m_sdl_gamepad, SDL_SENSOR_ACCEL);
+                LOG_INFO(Input, "Accel initialized, poll rate: {}", accel_poll_rate);
+            } else {
+                LOG_ERROR(Input, "Failed to initialize accel controls for gamepad");
+            }
         }
+
         SDL_free(gamepads);
 
         SetLightBarRGB(0, 0, 255);
diff --git a/src/qt_gui/settings_dialog.cpp b/src/qt_gui/settings_dialog.cpp
index 43c3b8fe7c0..eca6ee51daa 100644
--- a/src/qt_gui/settings_dialog.cpp
+++ b/src/qt_gui/settings_dialog.cpp
@@ -341,6 +341,8 @@ void SettingsDialog::LoadValuesFromConfig() {
         toml::find_or<std::string>(data, "Input", "backButtonBehavior", "left"));
     int index = ui->backButtonBehaviorComboBox->findData(backButtonBehavior);
     ui->backButtonBehaviorComboBox->setCurrentIndex(index != -1 ? index : 0);
+    ui->motionControlsCheckBox->setChecked(
+        toml::find_or<bool>(data, "Input", "isMotionControlsEnabled", true));
 
     ui->removeFolderButton->setEnabled(!ui->gameFoldersListWidget->selectedItems().isEmpty());
     ResetInstallFolders();
@@ -534,6 +536,7 @@ void SettingsDialog::UpdateSettings() {
 
     const QVector<std::string> TouchPadIndex = {"left", "center", "right", "none"};
     Config::setBackButtonBehavior(TouchPadIndex[ui->backButtonBehaviorComboBox->currentIndex()]);
+    Config::setIsMotionControlsEnabled(ui->motionControlsCheckBox->isChecked());
     Config::setFullscreenMode(ui->fullscreenCheckBox->isChecked());
     Config::setisTrophyPopupDisabled(ui->disableTrophycheckBox->isChecked());
     Config::setPlayBGM(ui->playBGMCheckBox->isChecked());
@@ -602,4 +605,4 @@ void SettingsDialog::ResetInstallFolders() {
         }
         Config::setGameInstallDirs(settings_install_dirs_config);
     }
-}
+}
\ No newline at end of file
diff --git a/src/qt_gui/settings_dialog.ui b/src/qt_gui/settings_dialog.ui
index 5b1e6947722..f73538eaafd 100644
--- a/src/qt_gui/settings_dialog.ui
+++ b/src/qt_gui/settings_dialog.ui
@@ -820,6 +820,13 @@
                  </layout>
                 </widget>
                </item>
+				  <item>
+					  <widget class="QCheckBox" name="motionControlsCheckBox"> 
+						  <property name="text">
+							  <string>Enable Motion Controls</string>
+						  </property>
+					  </widget>
+				  </item>
                <item>
                 <widget class="QWidget" name="controllerWidgetSpacer" native="true">
                  <property name="enabled">