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.

157 lines
4.2KB

  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. CFBundleCloseBundleResourceMap(ref, refNum);
  83. CFBundleUnloadExecutable(ref);
  84. CFRelease(ref);
  85. }
  86. bool load(const char* const filename)
  87. {
  88. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)std::strlen(filename), true);
  89. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
  90. ref = CFBundleCreate(kCFAllocatorDefault, urlRef);
  91. CFRelease(urlRef);
  92. CARLA_SAFE_ASSERT_RETURN(ref != nullptr, false);
  93. if (! CFBundleLoadExecutable(ref))
  94. {
  95. CFRelease(ref);
  96. ref = nullptr;
  97. return false;
  98. }
  99. refNum = CFBundleOpenBundleResourceMap(ref);
  100. return true;
  101. }
  102. };
  103. BundleLoader::BundleLoader()
  104. : pData(new PrivateData){}
  105. BundleLoader::~BundleLoader()
  106. {
  107. delete pData;
  108. }
  109. bool BundleLoader::load(const char* const filename)
  110. {
  111. return pData->load(filename);
  112. }
  113. CFBundleRef BundleLoader::getRef() const noexcept
  114. {
  115. return pData->ref;
  116. }
  117. // --------------------------------------------------------------------------------------------------------------------
  118. CARLA_BACKEND_END_NAMESPACE
  119. #endif // CARLA_OS_MAC