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.

123 lines
3.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. #import <Cocoa/Cocoa.h>
  22. CARLA_BACKEND_START_NAMESPACE
  23. // --------------------------------------------------------------------------------------------------------------------
  24. void initStandaloneApplication()
  25. {
  26. [[NSApplication sharedApplication] retain];
  27. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  28. [NSApp activateIgnoringOtherApps:YES];
  29. }
  30. const char* findBinaryInBundle(const char* const bundleDir)
  31. {
  32. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)bundleDir, (CFIndex)strlen(bundleDir), true);
  33. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, nullptr);
  34. const CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  35. CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr, nullptr);
  36. const CFURLRef exeRef = CFBundleCopyExecutableURL(bundleRef);
  37. CARLA_SAFE_ASSERT_RETURN(exeRef != nullptr, nullptr);
  38. const CFURLRef absoluteURL = CFURLCopyAbsoluteURL(exeRef);
  39. CARLA_SAFE_ASSERT_RETURN(absoluteURL != nullptr, nullptr);
  40. const NSString* strRef = (NSString*)CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
  41. CARLA_SAFE_ASSERT_RETURN(strRef != nullptr, nullptr);
  42. static CarlaString ret;
  43. ret = [strRef UTF8String];
  44. CFRelease(absoluteURL);
  45. CFRelease(exeRef);
  46. CFRelease(bundleRef);
  47. CFRelease(urlRef);
  48. return ret.buffer();
  49. }
  50. bool removeFileFromQuarantine(const char* const filename)
  51. {
  52. return removexattr(filename, "com.apple.quarantine", 0) == 0;
  53. }
  54. // --------------------------------------------------------------------------------------------------------------------
  55. AutoNSAutoreleasePool::AutoNSAutoreleasePool()
  56. : pool([NSAutoreleasePool new]) {}
  57. AutoNSAutoreleasePool::~AutoNSAutoreleasePool()
  58. {
  59. NSAutoreleasePool* rpool = (NSAutoreleasePool*)pool;
  60. [rpool drain];
  61. }
  62. // --------------------------------------------------------------------------------------------------------------------
  63. BundleLoader::BundleLoader() noexcept
  64. : ref(nullptr),
  65. refNum(0) {}
  66. BundleLoader::~BundleLoader()
  67. {
  68. if (ref == nullptr)
  69. return;
  70. CFBundleCloseBundleResourceMap(ref, refNum);
  71. CFBundleUnloadExecutable(ref);
  72. CFRelease(ref);
  73. }
  74. bool BundleLoader::load(const char* const filename)
  75. {
  76. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)std::strlen(filename), true);
  77. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
  78. ref = CFBundleCreate(kCFAllocatorDefault, urlRef);
  79. CFRelease(urlRef);
  80. CARLA_SAFE_ASSERT_RETURN(ref != nullptr, false);
  81. if (! CFBundleLoadExecutable(ref))
  82. {
  83. CFRelease(ref);
  84. ref = nullptr;
  85. return false;
  86. }
  87. refNum = CFBundleOpenBundleResourceMap(ref);
  88. return true;
  89. }
  90. // --------------------------------------------------------------------------------------------------------------------
  91. CARLA_BACKEND_END_NAMESPACE
  92. #endif // CARLA_OS_MAC