diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 123617e41..4ed5db1fe 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -302,6 +302,8 @@ file(GLOB MAIN_SOURCES Source/CommonFramework/Environment/HardwareValidation.h Source/CommonFramework/Environment/HardwareValidation_arm64.tpp Source/CommonFramework/Environment/HardwareValidation_x86.tpp + Source/CommonFramework/Environment/SystemSleep.cpp + Source/CommonFramework/Environment/SystemSleep.h Source/CommonFramework/Exceptions/FatalProgramException.cpp Source/CommonFramework/Exceptions/FatalProgramException.h Source/CommonFramework/Exceptions/OperationFailedException.cpp diff --git a/SerialPrograms/SerialPrograms.pro b/SerialPrograms/SerialPrograms.pro index 34674a6e5..692a34852 100644 --- a/SerialPrograms/SerialPrograms.pro +++ b/SerialPrograms/SerialPrograms.pro @@ -174,6 +174,7 @@ SOURCES += \ Source/CommonFramework/CrashDump.cpp \ Source/CommonFramework/Environment/Environment.cpp \ Source/CommonFramework/Environment/HardwareValidation.cpp \ + Source/CommonFramework/Environment/SystemSleep.cpp \ Source/CommonFramework/Exceptions/FatalProgramException.cpp \ Source/CommonFramework/Exceptions/OperationFailedException.cpp \ Source/CommonFramework/Exceptions/ProgramFinishedException.cpp \ @@ -1177,6 +1178,7 @@ HEADERS += \ Source/CommonFramework/Environment/HardwareValidation.h \ Source/CommonFramework/Environment/HardwareValidation_arm64.tpp \ Source/CommonFramework/Environment/HardwareValidation_x86.tpp \ + Source/CommonFramework/Environment/SystemSleep.h \ Source/CommonFramework/Exceptions/FatalProgramException.h \ Source/CommonFramework/Exceptions/OperationFailedException.h \ Source/CommonFramework/Exceptions/ProgramFinishedException.h \ diff --git a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h index c1aec51fc..951382f13 100644 --- a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h +++ b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h @@ -9,7 +9,9 @@ namespace PokemonAutomation{ - +// Check user hardware. +// If the hardware is too old, send a QMessageBox to tell user and return false. +// If the hardware may not be powerful enough, send a QMessageBox to inform user. bool check_hardware(); diff --git a/SerialPrograms/Source/CommonFramework/Environment/SystemSleep.cpp b/SerialPrograms/Source/CommonFramework/Environment/SystemSleep.cpp new file mode 100644 index 000000000..b27d979c8 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Environment/SystemSleep.cpp @@ -0,0 +1,70 @@ +/* OS Sleep + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + */ + +#include "SystemSleep.h" + +#include + +namespace PokemonAutomation{ + +#if defined(__APPLE__) + +// Code from https://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/8461182#8461182 + +SystemSleepController::SystemSleepController() + : m_prevention_succeeded(kIOReturnError), m_session_id(0) {} + +SystemSleepController::~SystemSleepController(){ + enable_sleep(); +} + +bool SystemSleepController::prevent_sleep(bool prevent){ + if (prevent){ + return disable_sleep(); + } else{ + return enable_sleep(); + } +} + +bool SystemSleepController::disable_sleep(){ + if (m_prevention_succeeded == kIOReturnSuccess){ + return true; + } + std::cout << "Disabling display sleep and OS sleep" << std::endl; + // kIOPMAssertionTypeNoDisplaySleep prevents display sleep, + // kIOPMAssertionTypeNoIdleSleep prevents idle sleep + + // NOTE: IOPMAssertionCreateWithName limits the string to 128 characters. + CFStringRef reasonForActivity = (CFStringRef) __builtin___CFStringMakeConstantString("SerialPrograms is running"); + + m_prevention_succeeded = kIOReturnError; + m_session_id = 0; + m_prevention_succeeded = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, + kIOPMAssertionLevelOn, reasonForActivity, &m_session_id); + if (m_prevention_succeeded != kIOReturnSuccess){ + m_session_id = 0; + std::cerr << "Cannot disable sleep. Error code " << m_prevention_succeeded << std::endl; + return false; + } + + std::cout << "Disabled display sleep and OS sleep" << std::endl; + return true; +} + +bool SystemSleepController::enable_sleep(){ + if (m_prevention_succeeded == kIOReturnSuccess){ + IOPMAssertionRelease(m_session_id); + m_session_id = 0; + m_prevention_succeeded = kIOReturnError; + std::cout << "Enabled display sleep and OS sleep." << std::endl; + } + return true; +} + +#endif // defined(__APPLE__) + +} + diff --git a/SerialPrograms/Source/CommonFramework/Environment/SystemSleep.h b/SerialPrograms/Source/CommonFramework/Environment/SystemSleep.h new file mode 100644 index 000000000..0c12fffd7 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Environment/SystemSleep.h @@ -0,0 +1,42 @@ +/* System Sleep + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + */ + +#ifndef PokemonAutomation_SystemSleep_H +#define PokemonAutomation_SystemSleep_H + +#if defined(__APPLE__) +#include +#endif + +namespace PokemonAutomation{ + + +#if defined(__APPLE__) + +// Call OS API to prevent screen saver from running and OS from going to sleep. +// Useful for running some programs like PokemonSV_VideoFastCodeEntry that require +// the screen to be constantly on. +class SystemSleepController{ +public: + SystemSleepController(); + ~SystemSleepController(); + + // Disable/Enable screen saver and OS sleep. + // Return whether the setting is successful. + bool prevent_sleep(bool prevent); + +private: + bool disable_sleep(); + bool enable_sleep(); + + IOReturn m_prevention_succeeded; + IOPMAssertionID m_session_id; +}; + +#endif + +} +#endif