Replace legacy code on macOS builds (#11428)

* Fix macOS build error due to CGDisplayStreamCreate being deprecated.

---------

Co-authored-by: FischLu <randomDe@proton.me>
This commit is contained in:
FischLu 2024-11-10 15:18:04 +01:00 committed by GitHub
parent 6e81451f64
commit ca9b88fae8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 21 deletions

View File

@ -386,7 +386,7 @@ target_link_libraries(keepassxc_gui
${sshagent_LIB}) ${sshagent_LIB})
if(APPLE) 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) if(Qt5MacExtras_FOUND)
target_link_libraries(keepassxc_gui Qt5::MacExtras) target_link_libraries(keepassxc_gui Qt5::MacExtras)
endif() endif()

View File

@ -1,7 +1,7 @@
set(autotype_mac_SOURCES AutoTypeMac.cpp) set(autotype_mac_SOURCES AutoTypeMac.cpp)
add_library(keepassxc-autotype-cocoa MODULE ${autotype_mac_SOURCES}) 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) target_link_libraries(keepassxc-autotype-cocoa ${PROGNAME} Qt5::Core Qt5::Widgets)
install(TARGETS keepassxc-autotype-cocoa install(TARGETS keepassxc-autotype-cocoa

View File

@ -19,6 +19,9 @@
#import "AppKitImpl.h" #import "AppKitImpl.h"
#import <QWindow> #import <QWindow>
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#if __clang_major__ >= 13 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_3
#import <ScreenCaptureKit/ScreenCaptureKit.h>
#endif
@implementation AppKitImpl @implementation AppKitImpl
@ -183,26 +186,35 @@
// //
- (bool) enableScreenRecording - (bool) enableScreenRecording
{ {
#if __clang_major__ >= 9 && MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 #if __clang_major__ >= 13 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_3
if (@available(macOS 10.15, *)) { if (@available(macOS 12.3, *)) {
// Request screen recording permission on macOS 10.15+ __block BOOL hasPermission = NO;
// This is necessary to get the current window title dispatch_semaphore_t sema = dispatch_semaphore_create(0);
CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, nil,
^(CGDisplayStreamFrameStatus status, uint64_t displayTime, // Attempt to use SCShareableContent to check for screen recording permission
IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) { [SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent * _Nullable content,
Q_UNUSED(status); NSError * _Nullable error) {
Q_UNUSED(displayTime); Q_UNUSED(error);
Q_UNUSED(frameSurface); if (content) {
Q_UNUSED(updateRef); // Successfully obtained content, indicating permission is granted
}); hasPermission = YES;
if (stream) {
CFRelease(stream);
} else { } else {
return NO; // 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_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC);
dispatch_semaphore_wait(sema, timeout);
// Return the final result
return hasPermission;
} }
#endif #endif
return YES; return YES; // Return YES for macOS versions that do not support ScreenCaptureKit
} }
- (void) toggleForegroundApp:(bool) foreground - (void) toggleForegroundApp:(bool) foreground