Duckstation/src/util/platform_misc_mac.mm

150 lines
3.6 KiB
Plaintext
Raw Normal View History

// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "metal_layer.h"
2022-09-21 12:44:52 +00:00
#include "platform_misc.h"
2023-08-23 12:06:48 +00:00
#include "window_info.h"
2022-09-21 12:44:52 +00:00
#include "common/log.h"
2023-09-20 13:49:14 +00:00
#include "common/small_string.h"
2023-08-23 12:06:48 +00:00
#include <Cocoa/Cocoa.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <QuartzCore/QuartzCore.h>
2022-09-21 12:44:52 +00:00
#include <cinttypes>
#include <optional>
#include <sys/sysctl.h>
#include <vector>
2022-09-21 12:44:52 +00:00
2023-08-23 12:06:48 +00:00
Log_SetChannel(PlatformMisc);
#if __has_feature(objc_arc)
#error ARC should not be enabled.
#endif
2022-09-21 12:44:52 +00:00
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)
{
2024-05-23 10:55:28 +00:00
ERROR_LOG("IOPMAssertionCreateWithName() failed");
2022-09-21 12:44:52 +00:00
return false;
}
return true;
}
else
{
IOPMAssertionRelease(s_prevent_idle_assertion);
s_prevent_idle_assertion = kIOPMNullAssertionID;
return true;
}
}
static bool s_screensaver_suspended;
2023-08-23 12:06:48 +00:00
void PlatformMisc::SuspendScreensaver()
2022-09-21 12:44:52 +00:00
{
if (s_screensaver_suspended)
2022-09-21 12:44:52 +00:00
if (!SetScreensaverInhibitMacOS(true))
{
2024-05-23 10:55:28 +00:00
ERROR_LOG("Failed to suspend screensaver.");
return;
}
2022-09-21 12:44:52 +00:00
s_screensaver_suspended = true;
}
2023-08-23 12:06:48 +00:00
void PlatformMisc::ResumeScreensaver()
2022-09-21 12:44:52 +00:00
{
if (!s_screensaver_suspended)
return;
if (!SetScreensaverInhibitMacOS(false))
2024-05-23 10:55:28 +00:00
ERROR_LOG("Failed to resume screensaver.");
2022-09-21 12:44:52 +00:00
s_screensaver_suspended = false;
}
template<typename T>
static std::optional<T> sysctlbyname(const char* name)
{
T output = 0;
size_t output_size = sizeof(output);
if (sysctlbyname(name, &output, &output_size, nullptr, 0) != 0)
return std::nullopt;
return output;
}
size_t PlatformMisc::GetRuntimePageSize()
{
return sysctlbyname<u32>("hw.pagesize").value_or(0);
}
size_t PlatformMisc::GetRuntimeCacheLineSize()
{
return static_cast<size_t>(std::max<s64>(sysctlbyname<s64>("hw.cachelinesize").value_or(0), 0));
}
2023-08-23 12:06:48 +00:00
bool PlatformMisc::PlaySoundAsync(const char* path)
2022-09-21 12:44:52 +00:00
{
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;
}
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
2023-08-24 08:02:55 +00:00
{
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
bool ret;
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]() { ret = CreateMetalLayer(wi); });
2023-08-24 08:02:55 +00:00
return ret;
}
2023-08-24 08:02:55 +00:00
CAMetalLayer* layer = [CAMetalLayer layer];
if (layer == nil)
{
2024-05-23 10:55:28 +00:00
ERROR_LOG("Failed to create CAMetalLayer");
2023-08-24 08:02:55 +00:00
return false;
}
2023-08-24 08:02:55 +00:00
NSView* view = (__bridge NSView*)wi->window_handle;
[view setWantsLayer:TRUE];
[view setLayer:layer];
[layer setContentsScale:[[[view window] screen] backingScaleFactor]];
2023-08-24 08:02:55 +00:00
wi->surface_handle = (__bridge void*)layer;
return true;
}
void CocoaTools::DestroyMetalLayer(WindowInfo* wi)
2023-08-24 08:02:55 +00:00
{
if (!wi->surface_handle)
return;
2023-08-24 08:02:55 +00:00
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
dispatch_sync(dispatch_get_main_queue(), [wi]() { DestroyMetalLayer(wi); });
return;
}
2023-08-24 08:02:55 +00:00
NSView* view = (__bridge NSView*)wi->window_handle;
CAMetalLayer* layer = (__bridge CAMetalLayer*)wi->surface_handle;
[view setLayer:nil];
[view setWantsLayer:NO];
[layer release];
}