input: source: add a refcount
[vlc.git] / src / darwin / dirs.m
blob3a300a5824b9c276b99b45c3a0d1a81fdf0a6ec2
1 /*****************************************************************************
2  * darwin_dirs.c: Darwin directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2016 VLC authors and VideoLAN
5  * Copyright (C) 2007-2012 Rémi Denis-Courmont
6  *
7  * Authors: Rémi Denis-Courmont
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *          Pierre d'Herbemont <pdherbemont # videolan org>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_charset.h>
32 #include "../libvlc.h"
34 #include <libgen.h>
35 #include <dlfcn.h>
37 #include <Foundation/Foundation.h>
39 static bool config_isBundle()
41     NSBundle *bundle = [NSBundle mainBundle];
42     NSString *bundlePath = bundle.bundlePath;
43     return [bundlePath hasSuffix:@".app"] || [bundlePath hasSuffix:@".framework"];
46 static char *config_getLibraryDirReal(const char *fallback)
48     const char *dir = getenv("VLC_LIB_PATH");
49     if (dir) {
50         return strdup(dir);
51     }
53     if (config_isBundle()) {
54         NSBundle *bundle = [NSBundle mainBundle];
55         NSString *path = bundle.privateFrameworksPath;
56         if (!path)
57             return NULL;
59         return strdup(path.UTF8String);
60     }
62     if (fallback)
63         return strdup(fallback);
65     return NULL;
68 static char *config_getDataDirReal(const char *fallback)
70     const char *dir = getenv("VLC_DATA_PATH");
71     if (dir) {
72         return strdup(dir);
73     }
75     if (config_isBundle()) {
76         NSBundle *bundle = [NSBundle mainBundle];
77         NSString *path = bundle.resourcePath;
78         if (!path)
79             return NULL;
81         path = [path stringByAppendingPathComponent:@"share"];
82         return strdup(path.UTF8String);
83     }
85     if (fallback)
86         return strdup(fallback);
88     return NULL;
91 char *config_GetLibDir(void)
93     return config_getLibraryDirReal(LIBDIR);
96 char *config_GetSysPath(vlc_sysdir_t type, const char *filename)
98     char *dir = NULL;
99     switch (type)
100     {
101         case VLC_PKG_DATA_DIR:
102             dir = config_getDataDirReal(PKGDATADIR);
103             break;
105         case VLC_PKG_LIB_DIR:
106             dir = config_getLibraryDirReal(PKGLIBDIR);
107             break;
108         case VLC_LIB_DIR:
109             dir = config_getLibraryDirReal(LIBDIR);
110             break;
113         case VLC_PKG_LIBEXEC_DIR:
114             dir = config_getLibraryDirReal(PKGLIBEXECDIR);
115             break;
116         case VLC_LIBEXEC_DIR:
117             dir = config_getLibraryDirReal(LIBEXECDIR);
118             break;
120         case VLC_LOCALE_DIR:
121             dir = config_getDataDirReal(NULL);
122             if (dir) {
123                 NSString *result = [NSString stringWithFormat:@"%s/locale", dir];
124                 free(dir);
125                 dir = strdup(result.UTF8String);
126                 break;
127             }
129             dir = strdup(LOCALEDIR);
130             break;
132         case VLC_SYSDATA_DIR:
133             break;
134         default:
135             vlc_assert_unreachable();
136     }
138     if (filename == NULL || unlikely(dir == NULL))
139         return dir;
141     char *path;
142     asprintf(&path, "%s/%s", dir, filename);
143     free(dir);
144     return path;
147 static char *config_GetHomeDir (void)
149     const char *home = getenv ("HOME");
151     if (home == NULL)
152         home = "/tmp";
154     return strdup (home);
157 static char *getAppDependentDir(vlc_userdir_t type)
159     NSString *formatString;
160     switch (type) {
161         case VLC_CONFIG_DIR:
162             formatString = @"%s/Library/Preferences/%@";
163             break;
164         case VLC_TEMPLATES_DIR:
165         case VLC_USERDATA_DIR:
166             formatString = @"%s/Library/Application Support/%@";
167             break;
168         case VLC_CACHE_DIR:
169             formatString = @"%s/Library/Caches/%@";
170             break;
171         default:
172             vlc_assert_unreachable();
173             break;
174     }
176     // Default fallback
177     NSString *identifier = @"org.videolan.vlc";
178     NSBundle *mainBundle = [NSBundle mainBundle];
179     if (mainBundle) {
180         NSString *bundleId = mainBundle.bundleIdentifier;
181         if (bundleId)
182             identifier = bundleId;
183     }
185     char *homeDir = config_GetHomeDir();
186     NSString *result = [NSString stringWithFormat:formatString, homeDir, identifier];
187     free(homeDir);
188     return strdup(result.UTF8String);
191 char *config_GetUserDir (vlc_userdir_t type)
193     const char *psz_path;
194     switch (type) {
195         case VLC_CONFIG_DIR:
196         case VLC_TEMPLATES_DIR:
197         case VLC_USERDATA_DIR:
198         case VLC_CACHE_DIR:
199             return getAppDependentDir(type);
201         case VLC_DESKTOP_DIR:
202             psz_path = "%s/Desktop";
203             break;
204         case VLC_DOWNLOAD_DIR:
205             psz_path = "%s/Downloads";
206             break;
207         case VLC_DOCUMENTS_DIR:
208             psz_path = "%s/Documents";
209             break;
210         case VLC_MUSIC_DIR:
211             psz_path = "%s/Music";
212             break;
213         case VLC_PICTURES_DIR:
214             psz_path = "%s/Pictures";
215             break;
216         case VLC_VIDEOS_DIR:
217             psz_path = "%s/Movies";
218             break;
219         case VLC_PUBLICSHARE_DIR:
220             psz_path = "%s/Public";
221             break;
222         case VLC_HOME_DIR:
223         default:
224             psz_path = "%s";
225     }
226     char *psz_parent = config_GetHomeDir();
227     char *psz_dir;
228     if (asprintf( &psz_dir, psz_path, psz_parent ) == -1)
229         psz_dir = NULL;
230     free(psz_parent);
231     return psz_dir;