VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / utils / CarlaMacUtils.cpp
blob6eed101b9e6d71ee49df686d19fc383795c8d4e7
1 /*
2 * Carla macOS utils
3 * Copyright (C) 2018-2023 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaDefines.h"
20 #ifdef CARLA_OS_MAC
22 #include "CarlaMacUtils.hpp"
23 #include "CarlaString.hpp"
25 #include <sys/xattr.h>
27 #ifdef __MAC_10_12
28 # define Component CocoaComponent
29 # define MemoryBlock CocoaMemoryBlock
30 # define Point CocoaPoint
31 #endif
33 #import <Cocoa/Cocoa.h>
34 #import <Foundation/Foundation.h>
36 #undef Component
37 #undef MemoryBlock
38 #undef Point
40 CARLA_BACKEND_START_NAMESPACE
42 // --------------------------------------------------------------------------------------------------------------------
44 void initStandaloneApplication()
46 [[NSApplication sharedApplication] retain];
47 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
48 [NSApp activateIgnoringOtherApps:YES];
51 const char* findBinaryInBundle(const char* const bundleDir)
53 const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)bundleDir, (CFIndex)strlen(bundleDir), true);
54 CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, nullptr);
56 const CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
57 CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr, nullptr);
59 const CFURLRef exeRef = CFBundleCopyExecutableURL(bundleRef);
60 CARLA_SAFE_ASSERT_RETURN(exeRef != nullptr, nullptr);
62 const CFURLRef absoluteURL = CFURLCopyAbsoluteURL(exeRef);
63 CARLA_SAFE_ASSERT_RETURN(absoluteURL != nullptr, nullptr);
65 const NSString* strRef = (NSString*)CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
66 CARLA_SAFE_ASSERT_RETURN(strRef != nullptr, nullptr);
68 static CarlaString ret;
69 ret = [strRef UTF8String];
71 CFRelease(absoluteURL);
72 CFRelease(exeRef);
73 CFRelease(bundleRef);
74 CFRelease(urlRef);
76 return ret.buffer();
79 bool removeFileFromQuarantine(const char* const filename)
81 return removexattr(filename, "com.apple.quarantine", 0) == 0;
84 // --------------------------------------------------------------------------------------------------------------------
86 AutoNSAutoreleasePool::AutoNSAutoreleasePool()
87 : pool([NSAutoreleasePool new]) {}
89 AutoNSAutoreleasePool::~AutoNSAutoreleasePool()
91 NSAutoreleasePool* rpool = (NSAutoreleasePool*)pool;
92 [rpool drain];
95 // --------------------------------------------------------------------------------------------------------------------
97 struct BundleLoader::PrivateData {
98 CFBundleRef ref;
99 CFBundleRefNum refNum;
101 PrivateData() noexcept
102 : ref(nullptr),
103 refNum(0) {}
105 ~PrivateData()
107 if (ref == nullptr)
108 return;
110 #pragma clang diagnostic push
111 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
112 CFBundleCloseBundleResourceMap(ref, refNum);
113 #pragma clang diagnostic pop
115 if (CFGetRetainCount(ref) == 1)
116 CFBundleUnloadExecutable(ref);
118 if (CFGetRetainCount(ref) > 0)
119 CFRelease(ref);
122 bool load(const char* const filename)
124 const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)std::strlen(filename), true);
125 CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
127 ref = CFBundleCreate(kCFAllocatorDefault, urlRef);
128 CFRelease(urlRef);
129 CARLA_SAFE_ASSERT_RETURN(ref != nullptr, false);
131 if (! CFBundleLoadExecutable(ref))
133 CFRelease(ref);
134 ref = nullptr;
135 return false;
138 #pragma clang diagnostic push
139 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
140 refNum = CFBundleOpenBundleResourceMap(ref);
141 #pragma clang diagnostic pop
142 return true;
146 BundleLoader::BundleLoader()
147 : pData(new PrivateData){}
149 BundleLoader::~BundleLoader()
151 delete pData;
154 bool BundleLoader::load(const char* const filename)
156 return pData->load(filename);
159 CFBundleRef BundleLoader::getRef() const noexcept
161 return pData->ref;
164 // --------------------------------------------------------------------------------------------------------------------
166 CARLA_BACKEND_END_NAMESPACE
168 #endif // CARLA_OS_MAC