view src/sys/osx/filesystem.cc @ 198:bc1ae1810855

dep/animia: switch from using classes to global functions the old idea was ok, but sort of hackish; this method doesn't use classes at all, and this way (especially important!) we can do wayland stuff AND x11 at the same time, which wasn't really possible without stupid workarounds in the other method
author Paper <mrpapersonic@gmail.com>
date Sun, 24 Dec 2023 02:59:42 -0500
parents c4ca035c565d
children
line wrap: on
line source

#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