mirror of https://github.com/stenzek/duckstation
FrontendCommon: Add PlaySoundAsync()
parent
cba4bb4ab2
commit
a4eb5f1d5d
@ -1,7 +0,0 @@
|
||||
#include "common/window_info.h"
|
||||
|
||||
namespace FrontendCommon
|
||||
{
|
||||
void SuspendScreensaver(const WindowInfo& wi);
|
||||
void ResumeScreensaver();
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
#include "common/window_info.h"
|
||||
|
||||
namespace FrontendCommon {
|
||||
void SuspendScreensaver(const WindowInfo& wi);
|
||||
void ResumeScreensaver();
|
||||
|
||||
/// Abstracts platform-specific code for asynchronously playing a sound.
|
||||
/// On Windows, this will use PlaySound(). On Linux, it will shell out to aplay. On MacOS, it uses NSSound.
|
||||
bool PlaySoundAsync(const char* path);
|
||||
} // namespace FrontendCommon
|
||||
@ -0,0 +1,77 @@
|
||||
#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;
|
||||
static WindowInfo s_screensaver_suspender;
|
||||
|
||||
void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
|
||||
{
|
||||
if (s_screensaver_suspended &&
|
||||
(s_screensaver_suspender.type != wi.type || s_screensaver_suspender.window_handle != wi.window_handle))
|
||||
ResumeScreensaver();
|
||||
|
||||
if (!SetScreensaverInhibitMacOS(true))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to suspend screensaver.");
|
||||
return;
|
||||
}
|
||||
|
||||
Log_InfoPrintf("Screensaver suspended by 0x%" PRIx64 ".",
|
||||
static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
|
||||
s_screensaver_suspended = true;
|
||||
s_screensaver_suspender = wi;
|
||||
}
|
||||
|
||||
void FrontendCommon::ResumeScreensaver()
|
||||
{
|
||||
if (!s_screensaver_suspended)
|
||||
return;
|
||||
|
||||
if (!SetScreensaverInhibitMacOS(false))
|
||||
Log_ErrorPrint("Failed to resume screensaver.");
|
||||
else
|
||||
Log_InfoPrint("Screensaver resumed.");
|
||||
|
||||
s_screensaver_suspended = false;
|
||||
s_screensaver_suspender = {};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
#include "common/log.h"
|
||||
#include "common/string.h"
|
||||
#include "common/string_util.h"
|
||||
#include "platform_misc.h"
|
||||
#include <cinttypes>
|
||||
Log_SetChannel(FrontendCommon);
|
||||
|
||||
#include "common/windows_headers.h"
|
||||
#include <mmsystem.h>
|
||||
|
||||
static bool SetScreensaverInhibitWin32(bool inhibit)
|
||||
{
|
||||
if (SetThreadExecutionState(ES_CONTINUOUS | (inhibit ? (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED) : 0)) == NULL)
|
||||
{
|
||||
Log_ErrorPrintf("SetThreadExecutionState() failed: %d", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool s_screensaver_suspended;
|
||||
static WindowInfo s_screensaver_suspender;
|
||||
|
||||
void FrontendCommon::SuspendScreensaver(const WindowInfo& wi)
|
||||
{
|
||||
if (s_screensaver_suspended &&
|
||||
(s_screensaver_suspender.type != wi.type || s_screensaver_suspender.window_handle != wi.window_handle))
|
||||
ResumeScreensaver();
|
||||
|
||||
if (!SetScreensaverInhibitWin32(true))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to suspend screensaver.");
|
||||
return;
|
||||
}
|
||||
|
||||
Log_InfoPrintf("Screensaver suspended by 0x%" PRIx64 ".",
|
||||
static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
|
||||
s_screensaver_suspended = true;
|
||||
s_screensaver_suspender = wi;
|
||||
}
|
||||
|
||||
void FrontendCommon::ResumeScreensaver()
|
||||
{
|
||||
if (!s_screensaver_suspended)
|
||||
return;
|
||||
|
||||
if (!SetScreensaverInhibitWin32(false))
|
||||
Log_ErrorPrint("Failed to resume screensaver.");
|
||||
else
|
||||
Log_InfoPrint("Screensaver resumed.");
|
||||
|
||||
s_screensaver_suspended = false;
|
||||
s_screensaver_suspender = {};
|
||||
}
|
||||
|
||||
bool FrontendCommon::PlaySoundAsync(const char* path)
|
||||
{
|
||||
const std::wstring wpath(StringUtil::UTF8StringToWideString(path));
|
||||
return PlaySoundW(wpath.c_str(), NULL, SND_ASYNC | SND_NODEFAULT);
|
||||
}
|
||||
Loading…
Reference in New Issue