LMMS
Loading...
Searching...
No Matches
CarlaMacUtils.cpp
Go to the documentation of this file.
1/*
2 * Carla macOS utils
3 * Copyright (C) 2018-2023 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
18#include "CarlaDefines.h"
19
20#ifdef CARLA_OS_MAC
21
22#include "CarlaMacUtils.hpp"
23#include "CarlaString.hpp"
24
25#include <sys/xattr.h>
26
27#ifdef __MAC_10_12
28# define Component CocoaComponent
29# define MemoryBlock CocoaMemoryBlock
30# define Point CocoaPoint
31#endif
32
33#import <Cocoa/Cocoa.h>
34#import <Foundation/Foundation.h>
35
36#undef Component
37#undef MemoryBlock
38#undef Point
39
40CARLA_BACKEND_START_NAMESPACE
41
42// --------------------------------------------------------------------------------------------------------------------
43
44void initStandaloneApplication()
45{
46 [[NSApplication sharedApplication] retain];
47 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
48 [NSApp activateIgnoringOtherApps:YES];
49}
50
51const char* findBinaryInBundle(const char* const bundleDir)
52{
53 const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)bundleDir, (CFIndex)strlen(bundleDir), true);
54 CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, nullptr);
55
56 const CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
57 CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr, nullptr);
58
59 const CFURLRef exeRef = CFBundleCopyExecutableURL(bundleRef);
60 CARLA_SAFE_ASSERT_RETURN(exeRef != nullptr, nullptr);
61
62 const CFURLRef absoluteURL = CFURLCopyAbsoluteURL(exeRef);
63 CARLA_SAFE_ASSERT_RETURN(absoluteURL != nullptr, nullptr);
64
65 const NSString* strRef = (NSString*)CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
66 CARLA_SAFE_ASSERT_RETURN(strRef != nullptr, nullptr);
67
68 static CarlaString ret;
69 ret = [strRef UTF8String];
70
71 CFRelease(absoluteURL);
72 CFRelease(exeRef);
73 CFRelease(bundleRef);
74 CFRelease(urlRef);
75
76 return ret.buffer();
77}
78
79bool removeFileFromQuarantine(const char* const filename)
80{
81 return removexattr(filename, "com.apple.quarantine", 0) == 0;
82}
83
84// --------------------------------------------------------------------------------------------------------------------
85
86AutoNSAutoreleasePool::AutoNSAutoreleasePool()
87 : pool([NSAutoreleasePool new]) {}
88
89AutoNSAutoreleasePool::~AutoNSAutoreleasePool()
90{
91 NSAutoreleasePool* rpool = (NSAutoreleasePool*)pool;
92 [rpool drain];
93}
94
95// --------------------------------------------------------------------------------------------------------------------
96
97struct BundleLoader::PrivateData {
98 CFBundleRef ref;
99 CFBundleRefNum refNum;
100
101 PrivateData() noexcept
102 : ref(nullptr),
103 refNum(0) {}
104
105 ~PrivateData()
106 {
107 if (ref == nullptr)
108 return;
109
110 #pragma clang diagnostic push
111 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
112 CFBundleCloseBundleResourceMap(ref, refNum);
113 #pragma clang diagnostic pop
114
115 if (CFGetRetainCount(ref) == 1)
116 CFBundleUnloadExecutable(ref);
117
118 if (CFGetRetainCount(ref) > 0)
119 CFRelease(ref);
120 }
121
122 bool load(const char* const filename)
123 {
124 const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)std::strlen(filename), true);
125 CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
126
127 ref = CFBundleCreate(kCFAllocatorDefault, urlRef);
128 CFRelease(urlRef);
129 CARLA_SAFE_ASSERT_RETURN(ref != nullptr, false);
130
131 if (! CFBundleLoadExecutable(ref))
132 {
133 CFRelease(ref);
134 ref = nullptr;
135 return false;
136 }
137
138 #pragma clang diagnostic push
139 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
140 refNum = CFBundleOpenBundleResourceMap(ref);
141 #pragma clang diagnostic pop
142 return true;
143 }
144};
145
146BundleLoader::BundleLoader()
147 : pData(new PrivateData){}
148
149BundleLoader::~BundleLoader()
150{
151 delete pData;
152}
153
154bool BundleLoader::load(const char* const filename)
155{
156 return pData->load(filename);
157}
158
159CFBundleRef BundleLoader::getRef() const noexcept
160{
161 return pData->ref;
162}
163
164// --------------------------------------------------------------------------------------------------------------------
165
166CARLA_BACKEND_END_NAMESPACE
167
168#endif // CARLA_OS_MAC
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
#define noexcept
Definition DistrhoDefines.h:72
pool_t pool
Definition Util.cpp:167
static char filename[]
Definition features.c:5
#define const
Definition zconf.h:137