2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2022-05-25 21:43:11 +00:00
|
|
|
|
|
|
|
#if ! __has_feature(objc_arc)
|
|
|
|
#error "Compile this with -fobjc-arc"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "CocoaTools.h"
|
|
|
|
#include "Console.h"
|
2023-12-26 11:25:10 +00:00
|
|
|
#include "HostSys.h"
|
2022-05-25 21:43:11 +00:00
|
|
|
#include "WindowInfo.h"
|
2022-12-05 06:55:17 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <mutex>
|
2022-05-25 22:32:10 +00:00
|
|
|
#include <vector>
|
2022-05-25 21:43:11 +00:00
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#include <QuartzCore/QuartzCore.h>
|
|
|
|
|
2022-05-25 22:32:10 +00:00
|
|
|
// MARK: - Metal Layers
|
|
|
|
|
2024-01-25 00:48:52 +00:00
|
|
|
static NSString*_Nonnull NSStringFromStringView(std::string_view sv)
|
|
|
|
{
|
|
|
|
return [[NSString alloc] initWithBytes:sv.data() length:sv.size() encoding:NSUTF8StringEncoding];
|
|
|
|
}
|
|
|
|
|
2022-05-25 21:43:11 +00:00
|
|
|
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
|
|
|
|
{
|
|
|
|
if (![NSThread isMainThread])
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]{ ret = CreateMetalLayer(wi); });
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMetalLayer* layer = [CAMetalLayer layer];
|
|
|
|
if (!layer)
|
|
|
|
{
|
|
|
|
Console.Error("Failed to create Metal layer.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSView* view = (__bridge NSView*)wi->window_handle;
|
|
|
|
[view setWantsLayer:YES];
|
|
|
|
[view setLayer:layer];
|
|
|
|
[layer setContentsScale:[[[view window] screen] backingScaleFactor]];
|
|
|
|
// Store the layer pointer, that way MoltenVK doesn't call [NSView layer] outside the main thread.
|
|
|
|
wi->surface_handle = (__bridge_retained void*)layer;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocoaTools::DestroyMetalLayer(WindowInfo* wi)
|
|
|
|
{
|
|
|
|
if (![NSThread isMainThread])
|
|
|
|
{
|
|
|
|
dispatch_sync_f(dispatch_get_main_queue(), wi, [](void* ctx){ DestroyMetalLayer(static_cast<WindowInfo*>(ctx)); });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSView* view = (__bridge NSView*)wi->window_handle;
|
|
|
|
CAMetalLayer* layer = (__bridge_transfer CAMetalLayer*)wi->surface_handle;
|
|
|
|
if (!layer)
|
|
|
|
return;
|
|
|
|
wi->surface_handle = nullptr;
|
|
|
|
[view setLayer:nil];
|
|
|
|
[view setWantsLayer:NO];
|
|
|
|
}
|
2022-05-25 22:32:10 +00:00
|
|
|
|
|
|
|
// MARK: - Theme Change Handlers
|
|
|
|
|
|
|
|
@interface PCSX2KVOHelper : NSObject
|
|
|
|
|
|
|
|
- (void)addCallback:(void*)ctx run:(void(*)(void*))callback;
|
|
|
|
- (void)removeCallback:(void*)ctx;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PCSX2KVOHelper
|
|
|
|
{
|
|
|
|
std::vector<std::pair<void*, void(*)(void*)>> _callbacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addCallback:(void*)ctx run:(void(*)(void*))callback
|
|
|
|
{
|
|
|
|
_callbacks.push_back(std::make_pair(ctx, callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeCallback:(void*)ctx
|
|
|
|
{
|
|
|
|
auto new_end = std::remove_if(_callbacks.begin(), _callbacks.end(), [ctx](const auto& entry){
|
|
|
|
return ctx == entry.first;
|
|
|
|
});
|
|
|
|
_callbacks.erase(new_end, _callbacks.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
|
|
|
|
{
|
|
|
|
for (const auto& callback : _callbacks)
|
|
|
|
callback.second(callback.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static PCSX2KVOHelper* s_themeChangeHandler;
|
|
|
|
|
|
|
|
void CocoaTools::AddThemeChangeHandler(void* ctx, void(handler)(void* ctx))
|
|
|
|
{
|
|
|
|
assert([NSThread isMainThread]);
|
|
|
|
if (!s_themeChangeHandler)
|
|
|
|
{
|
|
|
|
s_themeChangeHandler = [[PCSX2KVOHelper alloc] init];
|
|
|
|
NSApplication* app = [NSApplication sharedApplication];
|
|
|
|
[app addObserver:s_themeChangeHandler
|
|
|
|
forKeyPath:@"effectiveAppearance"
|
|
|
|
options:0
|
|
|
|
context:nil];
|
|
|
|
}
|
|
|
|
[s_themeChangeHandler addCallback:ctx run:handler];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocoaTools::RemoveThemeChangeHandler(void* ctx)
|
|
|
|
{
|
|
|
|
assert([NSThread isMainThread]);
|
|
|
|
[s_themeChangeHandler removeCallback:ctx];
|
|
|
|
}
|
2022-04-18 13:35:14 +00:00
|
|
|
|
|
|
|
// MARK: - Sound playback
|
|
|
|
|
|
|
|
bool Common::PlaySoundAsync(const char* path)
|
|
|
|
{
|
|
|
|
NSString* nspath = [[NSString alloc] initWithUTF8String:path];
|
|
|
|
NSSound* sound = [[NSSound alloc] initWithContentsOfFile:nspath byReference:YES];
|
|
|
|
return [sound play];
|
|
|
|
}
|
2022-12-05 06:55:17 +00:00
|
|
|
|
|
|
|
// MARK: - Updater
|
|
|
|
|
2024-05-14 21:55:58 +00:00
|
|
|
std::optional<std::string> CocoaTools::GetBundlePath()
|
|
|
|
{
|
|
|
|
std::optional<std::string> ret;
|
|
|
|
@autoreleasepool {
|
|
|
|
NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
|
|
|
|
if (url)
|
|
|
|
ret = std::string([url fileSystemRepresentation]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-12-05 06:55:17 +00:00
|
|
|
std::optional<std::string> CocoaTools::GetNonTranslocatedBundlePath()
|
|
|
|
{
|
|
|
|
// See https://objective-see.com/blog/blog_0x15.html
|
|
|
|
|
|
|
|
NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
|
|
|
|
if (!url)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
if (void* handle = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY))
|
|
|
|
{
|
|
|
|
auto IsTranslocatedURL = reinterpret_cast<Boolean(*)(CFURLRef path, bool* isTranslocated, CFErrorRef*__nullable error)>(dlsym(handle, "SecTranslocateIsTranslocatedURL"));
|
|
|
|
auto CreateOriginalPathForURL = reinterpret_cast<CFURLRef __nullable(*)(CFURLRef translocatedPath, CFErrorRef*__nullable error)>(dlsym(handle, "SecTranslocateCreateOriginalPathForURL"));
|
|
|
|
bool is_translocated = false;
|
|
|
|
if (IsTranslocatedURL)
|
|
|
|
IsTranslocatedURL((__bridge CFURLRef)url, &is_translocated, nullptr);
|
|
|
|
if (is_translocated)
|
|
|
|
{
|
|
|
|
if (CFURLRef actual = CreateOriginalPathForURL((__bridge CFURLRef)url, nullptr))
|
|
|
|
url = (__bridge_transfer NSURL*)actual;
|
|
|
|
}
|
|
|
|
dlclose(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string([url fileSystemRepresentation]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> CocoaTools::MoveToTrash(std::string_view file)
|
|
|
|
{
|
2024-01-25 00:48:52 +00:00
|
|
|
NSURL* url = [NSURL fileURLWithPath:NSStringFromStringView(file)];
|
2022-12-05 06:55:17 +00:00
|
|
|
NSURL* new_url;
|
|
|
|
if (![[NSFileManager defaultManager] trashItemAtURL:url resultingItemURL:&new_url error:nil])
|
|
|
|
return std::nullopt;
|
|
|
|
return std::string([new_url fileSystemRepresentation]);
|
|
|
|
}
|
|
|
|
|
2023-06-21 07:51:58 +00:00
|
|
|
bool CocoaTools::DelayedLaunch(std::string_view file)
|
2022-12-05 06:55:17 +00:00
|
|
|
{
|
2023-06-21 07:51:58 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
NSTask* task = [NSTask new];
|
|
|
|
[task setExecutableURL:[NSURL fileURLWithPath:@"/bin/sh"]];
|
|
|
|
[task setEnvironment:@{
|
|
|
|
@"WAITPID": [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]],
|
2024-01-25 00:48:52 +00:00
|
|
|
@"LAUNCHAPP": NSStringFromStringView(file),
|
2022-12-05 06:55:17 +00:00
|
|
|
}];
|
2023-06-21 07:51:58 +00:00
|
|
|
[task setArguments:@[@"-c", @"while /bin/ps -p $WAITPID > /dev/null; do /bin/sleep 0.1; done; exec /usr/bin/open \"$LAUNCHAPP\";"]];
|
|
|
|
return [task launchAndReturnError:nil];
|
2022-12-05 06:55:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-09 07:15:41 +00:00
|
|
|
|
|
|
|
// MARK: - Directory Services
|
|
|
|
|
|
|
|
bool CocoaTools::ShowInFinder(std::string_view file)
|
|
|
|
{
|
2024-01-25 00:48:52 +00:00
|
|
|
return [[NSWorkspace sharedWorkspace] selectFile:NSStringFromStringView(file)
|
2023-09-09 07:15:41 +00:00
|
|
|
inFileViewerRootedAtPath:nil];
|
|
|
|
}
|