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.

CarlaMacUtils.cpp 4.3KB

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