diff --git a/internal/platform/implementation/windows/session_manager.cc b/internal/platform/implementation/windows/session_manager.cc index 1c22295e..09c207b9 100644 --- a/internal/platform/implementation/windows/session_manager.cc +++ b/internal/platform/implementation/windows/session_manager.cc @@ -167,6 +167,27 @@ bool SessionManager::IsScreenLocked() const { return (session_state == WTS_SESSIONSTATE_LOCK); } +bool SessionManager::PreventSleep() const { + EXECUTION_STATE execution_state = + SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); + if (execution_state == 0) { + NEARBY_LOGS(ERROR) << __func__ + << ": Failed to set execution state of the thread."; + return false; + } + return true; +} + +bool SessionManager::AllowSleep() const { + EXECUTION_STATE execution_state = SetThreadExecutionState(ES_CONTINUOUS); + if (execution_state == 0) { + NEARBY_LOGS(ERROR) << __func__ + << ": Failed to set execution state of the thread."; + return false; + } + return true; +} + void SessionManager::StartSession(absl::Notification& notification) { kSessionHwnd = CreateNearbyWindow(); if (kSessionHwnd == nullptr) { diff --git a/internal/platform/implementation/windows/session_manager.h b/internal/platform/implementation/windows/session_manager.h index 06d928b7..a5da2c26 100644 --- a/internal/platform/implementation/windows/session_manager.h +++ b/internal/platform/implementation/windows/session_manager.h @@ -43,6 +43,12 @@ class SessionManager { bool IsScreenLocked() const; + // Prevents the Windows device from sleeping state. + bool PreventSleep() const; + + // Allows the Windows device to sleep state. + bool AllowSleep() const; + private: void StartSession(absl::Notification& notification); void StopSession();