From 52675a41c32b6295e6c07a21c2ff72ae5c936729 Mon Sep 17 00:00:00 2001
From: niansa <tuxifan@posteo.de>
Date: Sat, 9 Mar 2024 04:23:16 +0100
Subject: [PATCH] Implemented some basic sleep functions

---
 .../service/am/service/global_state_controller.cpp   | 12 +++++++++---
 .../hle/service/am/service/global_state_controller.h |  1 +
 .../hle/service/am/service/home_menu_functions.cpp   |  8 +++++++-
 .../hle/service/am/service/home_menu_functions.h     |  1 +
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/core/hle/service/am/service/global_state_controller.cpp b/src/core/hle/service/am/service/global_state_controller.cpp
index 3e3b61b0a0..a985b1925d 100644
--- a/src/core/hle/service/am/service/global_state_controller.cpp
+++ b/src/core/hle/service/am/service/global_state_controller.cpp
@@ -14,7 +14,7 @@ IGlobalStateController::IGlobalStateController(Core::System& system_)
     static const FunctionInfo functions[] = {
         {0, nullptr, "RequestToEnterSleep"},
         {1, nullptr, "EnterSleep"},
-        {2, nullptr, "StartSleepSequence"},
+        {2, D<&IGlobalStateController::StartSleepSequence>, "StartSleepSequence"},
         {3, D<&IGlobalStateController::StartShutdownSequence>, "StartShutdownSequence"},
         {4, D<&IGlobalStateController::StartRebootSequence>, "StartRebootSequence"},
         {9, nullptr, "IsAutoPowerDownRequested"},
@@ -31,6 +31,14 @@ IGlobalStateController::IGlobalStateController(Core::System& system_)
     RegisterHandlers(functions);
 }
 
+IGlobalStateController::~IGlobalStateController() = default;
+
+Result IGlobalStateController::StartSleepSequence(u8 a) {
+    LOG_WARNING(Service_AM, "called, a={}", a);
+    system.Pause();
+    R_SUCCEED();
+}
+
 Result IGlobalStateController::StartShutdownSequence() {
     LOG_INFO(Service_AM, "called");
     system.Exit();
@@ -43,8 +51,6 @@ Result IGlobalStateController::StartRebootSequence() {
     R_SUCCEED();
 }
 
-IGlobalStateController::~IGlobalStateController() = default;
-
 Result IGlobalStateController::LoadAndApplyIdlePolicySettings() {
     LOG_WARNING(Service_AM, "(STUBBED) called");
     R_SUCCEED();
diff --git a/src/core/hle/service/am/service/global_state_controller.h b/src/core/hle/service/am/service/global_state_controller.h
index 83efb57dfc..e0f1d46cc1 100644
--- a/src/core/hle/service/am/service/global_state_controller.h
+++ b/src/core/hle/service/am/service/global_state_controller.h
@@ -18,6 +18,7 @@ public:
     ~IGlobalStateController() override;
 
 private:
+    Result StartSleepSequence(u8 a);
     Result StartShutdownSequence();
     Result StartRebootSequence();
     Result LoadAndApplyIdlePolicySettings();
diff --git a/src/core/hle/service/am/service/home_menu_functions.cpp b/src/core/hle/service/am/service/home_menu_functions.cpp
index 25f78beb5a..47a10dceb3 100644
--- a/src/core/hle/service/am/service/home_menu_functions.cpp
+++ b/src/core/hle/service/am/service/home_menu_functions.cpp
@@ -23,7 +23,7 @@ IHomeMenuFunctions::IHomeMenuFunctions(Core::System& system_, std::shared_ptr<Ap
         {21, D<&IHomeMenuFunctions::GetPopFromGeneralChannelEvent>, "GetPopFromGeneralChannelEvent"},
         {30, nullptr, "GetHomeButtonWriterLockAccessor"},
         {31, nullptr, "GetWriterLockAccessorEx"},
-        {40, nullptr, "IsSleepEnabled"},
+        {40, D<&IHomeMenuFunctions::IsSleepEnabled>, "IsSleepEnabled"},
         {41, D<&IHomeMenuFunctions::IsRebootEnabled>, "IsRebootEnabled"},
         {50, nullptr, "LaunchSystemApplet"},
         {51, nullptr, "LaunchStarter"},
@@ -64,6 +64,12 @@ Result IHomeMenuFunctions::GetPopFromGeneralChannelEvent(
     R_SUCCEED();
 }
 
+Result IHomeMenuFunctions::IsSleepEnabled(Out<bool> out_is_sleep_enbaled) {
+    LOG_INFO(Service_AM, "called");
+    *out_is_sleep_enbaled = true;
+    R_SUCCEED();
+}
+
 Result IHomeMenuFunctions::IsRebootEnabled(Out<bool> out_is_reboot_enbaled) {
     LOG_INFO(Service_AM, "called");
     *out_is_reboot_enbaled = true;
diff --git a/src/core/hle/service/am/service/home_menu_functions.h b/src/core/hle/service/am/service/home_menu_functions.h
index f56094aa9d..e393898e35 100644
--- a/src/core/hle/service/am/service/home_menu_functions.h
+++ b/src/core/hle/service/am/service/home_menu_functions.h
@@ -24,6 +24,7 @@ private:
     Result LockForeground();
     Result UnlockForeground();
     Result GetPopFromGeneralChannelEvent(OutCopyHandle<Kernel::KReadableEvent> out_event);
+    Result IsSleepEnabled(Out<bool> out_is_sleep_enbaled);
     Result IsRebootEnabled(Out<bool> out_is_reboot_enbaled);
     Result IsForceTerminateApplicationDisabledForDebug(
         Out<bool> out_is_force_terminate_application_disabled_for_debug);