-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gin
committed
Sep 21, 2023
1 parent
7a492c5
commit e5955e8
Showing
5 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
SerialPrograms/Source/CommonFramework/Environment/SystemSleep.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
SerialPrograms/Source/CommonFramework/Environment/SystemSleep.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |