Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.1KB

  1. // SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "CarlaDefines.h"
  4. #ifdef CARLA_OS_MAC
  5. #include "CarlaMacUtils.hpp"
  6. #include "distrho/extra/String.hpp"
  7. #include <sys/xattr.h>
  8. #ifdef __MAC_10_12
  9. # define Component CocoaComponent
  10. # define MemoryBlock CocoaMemoryBlock
  11. # define Point CocoaPoint
  12. #endif
  13. #import <Cocoa/Cocoa.h>
  14. #import <Foundation/Foundation.h>
  15. #undef Component
  16. #undef MemoryBlock
  17. #undef Point
  18. CARLA_BACKEND_START_NAMESPACE
  19. // --------------------------------------------------------------------------------------------------------------------
  20. void initStandaloneApplication()
  21. {
  22. [[NSApplication sharedApplication] retain];
  23. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  24. [NSApp activateIgnoringOtherApps:YES];
  25. }
  26. const char* findBinaryInBundle(const char* const bundleDir)
  27. {
  28. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)bundleDir, (CFIndex)strlen(bundleDir), true);
  29. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, nullptr);
  30. const CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  31. CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr, nullptr);
  32. const CFURLRef exeRef = CFBundleCopyExecutableURL(bundleRef);
  33. CARLA_SAFE_ASSERT_RETURN(exeRef != nullptr, nullptr);
  34. const CFURLRef absoluteURL = CFURLCopyAbsoluteURL(exeRef);
  35. CARLA_SAFE_ASSERT_RETURN(absoluteURL != nullptr, nullptr);
  36. const NSString* strRef = (NSString*)CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
  37. CARLA_SAFE_ASSERT_RETURN(strRef != nullptr, nullptr);
  38. static String ret;
  39. ret = [strRef UTF8String];
  40. CFRelease(absoluteURL);
  41. CFRelease(exeRef);
  42. CFRelease(bundleRef);
  43. CFRelease(urlRef);
  44. return ret.buffer();
  45. }
  46. bool removeFileFromQuarantine(const char* const filename)
  47. {
  48. return removexattr(filename, "com.apple.quarantine", 0) == 0;
  49. }
  50. // --------------------------------------------------------------------------------------------------------------------
  51. AutoNSAutoreleasePool::AutoNSAutoreleasePool()
  52. : pool([NSAutoreleasePool new]) {}
  53. AutoNSAutoreleasePool::~AutoNSAutoreleasePool()
  54. {
  55. NSAutoreleasePool* rpool = (NSAutoreleasePool*)pool;
  56. [rpool drain];
  57. }
  58. // --------------------------------------------------------------------------------------------------------------------
  59. struct BundleLoader::PrivateData {
  60. CFBundleRef ref;
  61. CFBundleRefNum refNum;
  62. PrivateData() noexcept
  63. : ref(nullptr),
  64. refNum(0) {}
  65. ~PrivateData()
  66. {
  67. if (ref == nullptr)
  68. return;
  69. #pragma clang diagnostic push
  70. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  71. CFBundleCloseBundleResourceMap(ref, refNum);
  72. #pragma clang diagnostic pop
  73. if (CFGetRetainCount(ref) == 1)
  74. CFBundleUnloadExecutable(ref);
  75. if (CFGetRetainCount(ref) > 0)
  76. CFRelease(ref);
  77. }
  78. bool load(const char* const filename)
  79. {
  80. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)std::strlen(filename), true);
  81. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
  82. ref = CFBundleCreate(kCFAllocatorDefault, urlRef);
  83. CFRelease(urlRef);
  84. CARLA_SAFE_ASSERT_RETURN(ref != nullptr, false);
  85. if (! CFBundleLoadExecutable(ref))
  86. {
  87. CFRelease(ref);
  88. ref = nullptr;
  89. return false;
  90. }
  91. #pragma clang diagnostic push
  92. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  93. refNum = CFBundleOpenBundleResourceMap(ref);
  94. #pragma clang diagnostic pop
  95. return true;
  96. }
  97. };
  98. BundleLoader::BundleLoader()
  99. : pData(new PrivateData){}
  100. BundleLoader::~BundleLoader()
  101. {
  102. delete pData;
  103. }
  104. bool BundleLoader::load(const char* const filename)
  105. {
  106. return pData->load(filename);
  107. }
  108. CFBundleRef BundleLoader::getRef() const noexcept
  109. {
  110. return pData->ref;
  111. }
  112. // --------------------------------------------------------------------------------------------------------------------
  113. CARLA_BACKEND_END_NAMESPACE
  114. #endif // CARLA_OS_MAC