From 0a9d15a9780f48e2142678ce1264cc73ff8f1b75 Mon Sep 17 00:00:00 2001 From: Sintendo Date: Sun, 8 Nov 2020 10:34:17 +0100 Subject: [PATCH] hiro/cocoa: Implement Application::setScreenSaver Based on code from Dolphin and SDL2. https://github.com/dolphin-emu/dolphin/blob/908d6f8fa0bb68a48ed3ce966567ebf76abcf919/Source/Core/UICommon/UICommon.cpp#L416 https://github.com/SDL-mirror/SDL/blob/cf1c19293687f10579c32ad2106eaead3d850f93/src/video/cocoa/SDL_cocoaevents.m#L514 --- hiro/cocoa/application.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/hiro/cocoa/application.cpp b/hiro/cocoa/application.cpp index 2afaeb9e..4303f7c0 100755 --- a/hiro/cocoa/application.cpp +++ b/hiro/cocoa/application.cpp @@ -1,4 +1,5 @@ #if defined(Hiro_Application) +#include @implementation CocoaDelegate : NSObject @@ -92,7 +93,24 @@ auto pApplication::quit() -> void { } auto pApplication::setScreenSaver(bool screenSaver) -> void { - //TODO: not implemented + static IOPMAssertionID powerAssertion = kIOPMNullAssertionID; + + // do nothing if current already matches desired state + bool current = powerAssertion == kIOPMNullAssertionID; + if(current == screenSaver) return; + + @autoreleasepool { + if(screenSaver) { + IOPMAssertionRelease(powerAssertion); + powerAssertion = kIOPMNullAssertionID; + } else { + string reason = {Application::state().name, " screensaver suppression"}; + NSString* assertionName = [NSString stringWithUTF8String:reason.data()]; + bool success = IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, + kIOPMAssertionLevelOn, (CFStringRef) assertionName, &powerAssertion) != kIOReturnSuccess; + if(success) powerAssertion = kIOPMNullAssertionID; + } + } } auto pApplication::initialize() -> void {