Mercurial > minori
diff src/sys/osx/filesystem.cc @ 202:71832ffe425a
animia: re-add kvm fd source
this is all being merged from my wildly out-of-date laptop. SORRY!
in other news, I edited the CI file to install the wayland client
as well, so the linux CI build might finally get wayland stuff.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 02 Jan 2024 06:05:06 -0500 |
parents | c4ca035c565d |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sys/osx/filesystem.cc Tue Jan 02 06:05:06 2024 -0500 @@ -0,0 +1,51 @@ +#include "sys/osx/filesystem.h" + +#include <CoreFoundation/CoreFoundation.h> +#include <objc/runtime.h> + +#include <string> + +/* These constants are defined in Foundation but not + * exposed to CoreFoundation users. +*/ +static constexpr unsigned long NSApplicationSupportDirectory = 14; +static constexpr unsigned long NSUserDomainMask = 1; + +extern "C" { + CFArrayRef NSSearchPathForDirectoriesInDomains(unsigned long directory, unsigned long domainMask, BOOL expandTilde); +} + +namespace osx { + +bool GetApplicationSupportDirectory(std::string& result) { + // NSArray* strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, ON); + const CFArrayRef strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true); + if (!strings) + return false; + + // NSIndex index = [strings count]; + const CFIndex count = CFArrayGetCount(strings); + if (count < 1) { + CFRelease(strings); + return false; + } + + // NSString* string = [strings objectAtIndex: 0]; + const CFStringRef string = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(strings, 0)); + if (!string) { + CFRelease(strings); + return false; + } + + // result = [string UTF8String]; + result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1); + if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8)) { + CFRelease(strings); + return false; + } + result.resize(result.find_first_of('\0')); + + return true; +} + +} // namespace osx