Skip to content

Commit

Permalink
add macOS sleep controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Gin committed Sep 21, 2023
1 parent 7a492c5 commit e5955e8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
2 changes: 2 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/SerialPrograms.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand Down
70 changes: 70 additions & 0 deletions SerialPrograms/Source/CommonFramework/Environment/SystemSleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* OS Sleep
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#include "SystemSleep.h"

#include <iostream>

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__)

}

42 changes: 42 additions & 0 deletions SerialPrograms/Source/CommonFramework/Environment/SystemSleep.h
Original file line number Diff line number Diff line change
@@ -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 <IOKit/pwr_mgt/IOPMLib.h>
#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

0 comments on commit e5955e8

Please sign in to comment.