2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2022-09-21 12:44:52 +00:00
|
|
|
#include "platform_misc.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
#include "common/string.h"
|
|
|
|
#include <IOKit/pwr_mgt/IOPMLib.h>
|
|
|
|
#include <cinttypes>
|
|
|
|
Log_SetChannel(FrontendCommon);
|
|
|
|
|
|
|
|
#import <AppKit/AppKit.h>
|
|
|
|
|
|
|
|
static IOPMAssertionID s_prevent_idle_assertion = kIOPMNullAssertionID;
|
|
|
|
|
|
|
|
static bool SetScreensaverInhibitMacOS(bool inhibit)
|
|
|
|
{
|
|
|
|
if (inhibit)
|
|
|
|
{
|
|
|
|
const CFStringRef reason = CFSTR("System Running");
|
|
|
|
if (IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, reason,
|
|
|
|
&s_prevent_idle_assertion) != kIOReturnSuccess)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("IOPMAssertionCreateWithName() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IOPMAssertionRelease(s_prevent_idle_assertion);
|
|
|
|
s_prevent_idle_assertion = kIOPMNullAssertionID;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool s_screensaver_suspended;
|
|
|
|
|
2022-11-18 08:14:39 +00:00
|
|
|
void FrontendCommon::SuspendScreensaver()
|
2022-09-21 12:44:52 +00:00
|
|
|
{
|
2022-11-18 08:14:39 +00:00
|
|
|
if (s_screensaver_suspended)
|
2022-09-21 12:44:52 +00:00
|
|
|
|
|
|
|
if (!SetScreensaverInhibitMacOS(true))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to suspend screensaver.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_screensaver_suspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrontendCommon::ResumeScreensaver()
|
|
|
|
{
|
|
|
|
if (!s_screensaver_suspended)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!SetScreensaverInhibitMacOS(false))
|
|
|
|
Log_ErrorPrint("Failed to resume screensaver.");
|
|
|
|
|
|
|
|
s_screensaver_suspended = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FrontendCommon::PlaySoundAsync(const char* path)
|
|
|
|
{
|
|
|
|
NSString* nspath = [[NSString alloc] initWithUTF8String:path];
|
|
|
|
NSSound* sound = [[NSSound alloc] initWithContentsOfFile:nspath byReference:YES];
|
|
|
|
const bool result = [sound play];
|
|
|
|
[sound release];
|
|
|
|
[nspath release];
|
|
|
|
return result;
|
|
|
|
}
|