From 316c5b0c5ad3942d4713c9de09acad994e40bcb2 Mon Sep 17 00:00:00 2001 From: FischLu Date: Sat, 2 Nov 2024 23:08:48 +0100 Subject: [PATCH] Fix macOS build error due to CGDisplayStreamCreate being deprecated. --- src/CMakeLists.txt | 2 +- src/autotype/mac/CMakeLists.txt | 2 +- src/gui/osutils/macutils/AppKitImpl.mm | 49 +++++++++++++++----------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 67aada93fe..ee83fac327 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -386,7 +386,7 @@ target_link_libraries(keepassxc_gui ${sshagent_LIB}) if(APPLE) - target_link_libraries(keepassxc_gui "-framework Foundation -framework AppKit -framework Carbon -framework Security -framework LocalAuthentication") + target_link_libraries(keepassxc_gui "-framework Foundation -framework AppKit -framework Carbon -framework Security -framework LocalAuthentication -framework ScreenCaptureKit") if(Qt5MacExtras_FOUND) target_link_libraries(keepassxc_gui Qt5::MacExtras) endif() diff --git a/src/autotype/mac/CMakeLists.txt b/src/autotype/mac/CMakeLists.txt index e0df901fdd..ae1f5187f5 100644 --- a/src/autotype/mac/CMakeLists.txt +++ b/src/autotype/mac/CMakeLists.txt @@ -1,7 +1,7 @@ set(autotype_mac_SOURCES AutoTypeMac.cpp) add_library(keepassxc-autotype-cocoa MODULE ${autotype_mac_SOURCES}) -set_target_properties(keepassxc-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon") +set_target_properties(keepassxc-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon -framework ScreenCaptureKit") target_link_libraries(keepassxc-autotype-cocoa ${PROGNAME} Qt5::Core Qt5::Widgets) install(TARGETS keepassxc-autotype-cocoa diff --git a/src/gui/osutils/macutils/AppKitImpl.mm b/src/gui/osutils/macutils/AppKitImpl.mm index f2692bd6ef..bd770fca9d 100644 --- a/src/gui/osutils/macutils/AppKitImpl.mm +++ b/src/gui/osutils/macutils/AppKitImpl.mm @@ -19,7 +19,9 @@ #import "AppKitImpl.h" #import #import - +#if __clang_major__ >= 13 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_3 +#import +#endif @implementation AppKitImpl - (id) initWithObject:(AppKit*)appkit @@ -181,28 +183,33 @@ - (bool) enableAccessibility // // Check if screen recording is enabled, may show an popup asking for permissions // -- (bool) enableScreenRecording +- (bool)enableScreenRecording { -#if __clang_major__ >= 9 && MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 - if (@available(macOS 10.15, *)) { - // Request screen recording permission on macOS 10.15+ - // This is necessary to get the current window title - CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, nil, - ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, - IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) { - Q_UNUSED(status); - Q_UNUSED(displayTime); - Q_UNUSED(frameSurface); - Q_UNUSED(updateRef); - }); - if (stream) { - CFRelease(stream); - } else { - return NO; - } - } +#if __clang_major__ >= 13 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_3 + __block BOOL hasPermission = NO; + dispatch_semaphore_t sema = dispatch_semaphore_create(0); + + // Attempt to use SCShareableContent to check for screen recording permission + [SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent * _Nullable content, + NSError * _Nullable error) { + if (content) { + // Successfully obtained content, indicating permission is granted + hasPermission = YES; + } else { + // No permission or other error occurred + hasPermission = NO; + } + // Notify the semaphore that the asynchronous task is complete + dispatch_semaphore_signal(sema); + }]; + + // Wait for the asynchronous callback to complete + dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); + + // Return the final result + return hasPermission; #endif - return YES; + return YES; // Return YES for macOS versions that do not support ScreenCaptureKit } - (void) toggleForegroundApp:(bool) foreground