Blender V5.0
GHOST_SystemPathsCocoa.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#import <AppKit/NSDocumentController.h>
6#import <Foundation/Foundation.h>
7
8#include <optional>
9#include <string>
10
11#include "GHOST_Debug.hh"
13
14/* --------------------------------------------------------------------
15 * Base directories retrieval.
16 */
17
18static const char *GetApplicationSupportDir(const char *versionstr,
19 const NSSearchPathDomainMask mask,
20 char *tempPath,
21 const std::size_t len_tempPath)
22{
23 @autoreleasepool {
24 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, mask, YES);
25
26 if (paths.count == 0) {
27 return nullptr;
28 }
29 NSString *basePath = [paths objectAtIndex:0];
30
31 snprintf(tempPath,
32 len_tempPath,
33 "%s/Blender/%s",
34 [basePath cStringUsingEncoding:NSASCIIStringEncoding],
35 versionstr);
36 }
37 return tempPath;
38}
39
40const char *GHOST_SystemPathsCocoa::getSystemDir(int /* version */, const char *versionstr) const
41{
42 static char tempPath[512] = "";
43 return GetApplicationSupportDir(versionstr, NSLocalDomainMask, tempPath, sizeof(tempPath));
44}
45
46const char *GHOST_SystemPathsCocoa::getUserDir(int /* version */, const char *versionstr) const
47{
48 static char tempPath[512] = "";
49 return GetApplicationSupportDir(versionstr, NSUserDomainMask, tempPath, sizeof(tempPath));
50}
51
54{
55 char tempPath[512] = "";
56 @autoreleasepool {
57 NSSearchPathDirectory ns_directory;
58
59 switch (type) {
61 ns_directory = NSDesktopDirectory;
62 break;
64 ns_directory = NSDocumentDirectory;
65 break;
67 ns_directory = NSDownloadsDirectory;
68 break;
70 ns_directory = NSMusicDirectory;
71 break;
73 ns_directory = NSPicturesDirectory;
74 break;
76 ns_directory = NSMoviesDirectory;
77 break;
79 ns_directory = NSCachesDirectory;
80 break;
81 default:
83 false,
84 "GHOST_SystemPathsCocoa::getUserSpecialDir(): Invalid enum value for type parameter");
85 return std::nullopt;
86 }
87
88 NSArray *paths = NSSearchPathForDirectoriesInDomains(ns_directory, NSUserDomainMask, YES);
89 if (paths.count == 0) {
90 return std::nullopt;
91 }
92 NSString *basePath = [paths objectAtIndex:0];
93
94 const char *basePath_cstr = [basePath cStringUsingEncoding:NSASCIIStringEncoding];
95 int basePath_len = strlen(basePath_cstr);
96
97 basePath_len = MIN(basePath_len, sizeof(tempPath) - 1);
98 memcpy(tempPath, basePath_cstr, basePath_len);
99 tempPath[basePath_len] = '\0';
100 }
101 if (!tempPath[0]) {
102 return std::nullopt;
103 }
104 return tempPath;
105}
106
108{
109 static char tempPath[512] = "";
110
111 @autoreleasepool {
112 NSString *basePath = [[NSBundle mainBundle] bundlePath];
113
114 if (basePath == nil) {
115 return nullptr;
116 }
117
118 const char *basePath_cstr = [basePath cStringUsingEncoding:NSASCIIStringEncoding];
119 int basePath_len = strlen(basePath_cstr);
120
121 basePath_len = MIN(basePath_len, sizeof(tempPath) - 1);
122 memcpy(tempPath, basePath_cstr, basePath_len);
123 tempPath[basePath_len] = '\0';
124 }
125 return tempPath;
126}
127
129{
130 @autoreleasepool {
131 NSURL *file_url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:filepath]];
132 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:file_url];
133 }
134}
#define GHOST_ASSERT(x, info)
static const char * GetApplicationSupportDir(const char *versionstr, const NSSearchPathDomainMask mask, char *tempPath, const std::size_t len_tempPath)
GHOST_TUserSpecialDirTypes
@ GHOST_kUserSpecialDirDesktop
@ GHOST_kUserSpecialDirMusic
@ GHOST_kUserSpecialDirPictures
@ GHOST_kUserSpecialDirVideos
@ GHOST_kUserSpecialDirDownloads
@ GHOST_kUserSpecialDirCaches
@ GHOST_kUserSpecialDirDocuments
#define MIN(_a, _b)
const char * getBinaryDir() const override
void addToSystemRecentFiles(const char *filepath) const override
const char * getUserDir(int version, const char *versionstr) const override
std::optional< std::string > getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const override
const char * getSystemDir(int version, const char *versionstr) const override
ccl_device_inline float2 mask(const MaskType mask, const float2 a)