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.

167 lines
4.6KB

  1. /*
  2. * Carla macOS utils
  3. * Copyright (C) 2018-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifdef CARLA_OS_MAC
  18. #include "CarlaMacUtils.hpp"
  19. #include "CarlaString.hpp"
  20. #include <sys/xattr.h>
  21. #ifdef __MAC_10_12
  22. # define Component CocoaComponent
  23. # define MemoryBlock CocoaMemoryBlock
  24. # define Point CocoaPoint
  25. #endif
  26. #import <Cocoa/Cocoa.h>
  27. #import <Foundation/Foundation.h>
  28. #undef Component
  29. #undef MemoryBlock
  30. #undef Point
  31. CARLA_BACKEND_START_NAMESPACE
  32. // --------------------------------------------------------------------------------------------------------------------
  33. void initStandaloneApplication()
  34. {
  35. [[NSApplication sharedApplication] retain];
  36. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  37. [NSApp activateIgnoringOtherApps:YES];
  38. }
  39. const char* findBinaryInBundle(const char* const bundleDir)
  40. {
  41. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)bundleDir, (CFIndex)strlen(bundleDir), true);
  42. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, nullptr);
  43. const CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  44. CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr, nullptr);
  45. const CFURLRef exeRef = CFBundleCopyExecutableURL(bundleRef);
  46. CARLA_SAFE_ASSERT_RETURN(exeRef != nullptr, nullptr);
  47. const CFURLRef absoluteURL = CFURLCopyAbsoluteURL(exeRef);
  48. CARLA_SAFE_ASSERT_RETURN(absoluteURL != nullptr, nullptr);
  49. const NSString* strRef = (NSString*)CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
  50. CARLA_SAFE_ASSERT_RETURN(strRef != nullptr, nullptr);
  51. static CarlaString ret;
  52. ret = [strRef UTF8String];
  53. CFRelease(absoluteURL);
  54. CFRelease(exeRef);
  55. CFRelease(bundleRef);
  56. CFRelease(urlRef);
  57. return ret.buffer();
  58. }
  59. bool removeFileFromQuarantine(const char* const filename)
  60. {
  61. return removexattr(filename, "com.apple.quarantine", 0) == 0;
  62. }
  63. // --------------------------------------------------------------------------------------------------------------------
  64. AutoNSAutoreleasePool::AutoNSAutoreleasePool()
  65. : pool([NSAutoreleasePool new]) {}
  66. AutoNSAutoreleasePool::~AutoNSAutoreleasePool()
  67. {
  68. NSAutoreleasePool* rpool = (NSAutoreleasePool*)pool;
  69. [rpool drain];
  70. }
  71. // --------------------------------------------------------------------------------------------------------------------
  72. struct BundleLoader::PrivateData {
  73. CFBundleRef ref;
  74. CFBundleRefNum refNum;
  75. PrivateData() noexcept
  76. : ref(nullptr),
  77. refNum(0) {}
  78. ~PrivateData()
  79. {
  80. if (ref == nullptr)
  81. return;
  82. #pragma clang diagnostic push
  83. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  84. CFBundleCloseBundleResourceMap(ref, refNum);
  85. #pragma clang diagnostic pop
  86. if (CFGetRetainCount(ref) == 1)
  87. CFBundleUnloadExecutable(ref);
  88. if (CFGetRetainCount(ref) > 0)
  89. CFRelease(ref);
  90. }
  91. bool load(const char* const filename)
  92. {
  93. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)std::strlen(filename), true);
  94. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
  95. ref = CFBundleCreate(kCFAllocatorDefault, urlRef);
  96. CFRelease(urlRef);
  97. CARLA_SAFE_ASSERT_RETURN(ref != nullptr, false);
  98. if (! CFBundleLoadExecutable(ref))
  99. {
  100. CFRelease(ref);
  101. ref = nullptr;
  102. return false;
  103. }
  104. #pragma clang diagnostic push
  105. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  106. refNum = CFBundleOpenBundleResourceMap(ref);
  107. #pragma clang diagnostic pop
  108. return true;
  109. }
  110. };
  111. BundleLoader::BundleLoader()
  112. : pData(new PrivateData){}
  113. BundleLoader::~BundleLoader()
  114. {
  115. delete pData;
  116. }
  117. bool BundleLoader::load(const char* const filename)
  118. {
  119. return pData->load(filename);
  120. }
  121. CFBundleRef BundleLoader::getRef() const noexcept
  122. {
  123. return pData->ref;
  124. }
  125. // --------------------------------------------------------------------------------------------------------------------
  126. CARLA_BACKEND_END_NAMESPACE
  127. #endif // CARLA_OS_MAC