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"
22 #include "CarlaMacUtils.hpp"
23 #include "CarlaString.hpp"
25 #include <sys/xattr.h>
28 # define Component CocoaComponent
29 # define MemoryBlock CocoaMemoryBlock
30 # define Point CocoaPoint
33 #import <Cocoa/Cocoa.h>
34 #import <Foundation/Foundation.h>
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
);
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
;
95 // --------------------------------------------------------------------------------------------------------------------
97 struct BundleLoader::PrivateData
{
99 CFBundleRefNum refNum
;
101 PrivateData() noexcept
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)
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
);
129 CARLA_SAFE_ASSERT_RETURN(ref
!= nullptr, false);
131 if (! CFBundleLoadExecutable(ref
))
138 #pragma clang diagnostic push
139 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
140 refNum
= CFBundleOpenBundleResourceMap(ref
);
141 #pragma clang diagnostic pop
146 BundleLoader::BundleLoader()
147 : pData(new PrivateData
){}
149 BundleLoader::~BundleLoader()
154 bool BundleLoader::load(const char* const filename
)
156 return pData
->load(filename
);
159 CFBundleRef
BundleLoader::getRef() const noexcept
164 // --------------------------------------------------------------------------------------------------------------------
166 CARLA_BACKEND_END_NAMESPACE
168 #endif // CARLA_OS_MAC