comparison 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
comparison
equal deleted inserted replaced
201:8f6f8dd2eb23 202:71832ffe425a
1 #include "sys/osx/filesystem.h"
2
3 #include <CoreFoundation/CoreFoundation.h>
4 #include <objc/runtime.h>
5
6 #include <string>
7
8 /* These constants are defined in Foundation but not
9 * exposed to CoreFoundation users.
10 */
11 static constexpr unsigned long NSApplicationSupportDirectory = 14;
12 static constexpr unsigned long NSUserDomainMask = 1;
13
14 extern "C" {
15 CFArrayRef NSSearchPathForDirectoriesInDomains(unsigned long directory, unsigned long domainMask, BOOL expandTilde);
16 }
17
18 namespace osx {
19
20 bool GetApplicationSupportDirectory(std::string& result) {
21 // NSArray* strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, ON);
22 const CFArrayRef strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
23 if (!strings)
24 return false;
25
26 // NSIndex index = [strings count];
27 const CFIndex count = CFArrayGetCount(strings);
28 if (count < 1) {
29 CFRelease(strings);
30 return false;
31 }
32
33 // NSString* string = [strings objectAtIndex: 0];
34 const CFStringRef string = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(strings, 0));
35 if (!string) {
36 CFRelease(strings);
37 return false;
38 }
39
40 // result = [string UTF8String];
41 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1);
42 if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8)) {
43 CFRelease(strings);
44 return false;
45 }
46 result.resize(result.find_first_of('\0'));
47
48 return true;
49 }
50
51 } // namespace osx