codec: dvbsub: remove usage of bs_show
[vlc.git] / src / darwin / dirs.c
blobde95ef31dc4779cfdc246fcb163fc24d63d22fe5
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
7 * Authors: Rémi Denis-Courmont
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * Pierre d'Herbemont <pdherbemont # videolan org>
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.
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.
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 "../libvlc.h"
33 #include <libgen.h>
34 #include <dlfcn.h>
35 #include <mach-o/dyld.h>
37 #include <CoreFoundation/CoreFoundation.h>
39 char *config_GetLibDir (void)
41 /* Get the full program path and name */
42 /* First try to see if we are linked to the framework */
43 for (unsigned i = 0; i < _dyld_image_count(); i++)
45 const char *psz_img_name = _dyld_get_image_name(i);
46 const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
48 /* Check for "VLCKit.framework/Versions/Current/VLCKit",
49 * as well as "VLCKit.framework/Versions/A/VLCKit" and
50 * "VLC.framework/Versions/B/VLCKit" */
51 if (p != NULL) {
52 /* Look for the next forward slash */
53 p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
54 p += strcspn( p, "/" );
56 /* If the string ends with VLCKit then we've found a winner */
57 if (!strcmp( p, "/VLCKit"))
58 return strdup( dirname(psz_img_name) );
61 /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
62 * link to VLCKit. */
63 size_t len = strlen(psz_img_name);
64 if (len >= 3 && !strcmp( psz_img_name + len - 3, "VLC"))
65 return strdup( dirname(psz_img_name) );
67 /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
68 if (len >= 10 && !strcmp( psz_img_name + len - 10, "VLC-Plugin"))
69 return strdup( dirname(psz_img_name) );
71 /* Do we end by "VLC for iOS"? so we are the iOS app */
72 if (len >= 11 && !strcmp( psz_img_name + len - 11, "VLC for iOS"))
73 return strdup( dirname(psz_img_name) );
75 /* Do we end by "VLC-TV"? so we are the tvOS app */
76 if (len >= 6 && !strcmp( psz_img_name + len - 6, "VLC-TV"))
77 return strdup( dirname(psz_img_name) );
80 /* we are not part of any Mac-style package but were installed
81 * the UNIX way. let's trick-around a bit */
82 Dl_info info;
83 if (dladdr(system_Init, &info)) {
84 char *incompletepath = strdup(dirname( (char *)info.dli_fname ));
85 char *path = NULL;
86 asprintf(&path, "%s/"PACKAGE, incompletepath);
87 free(incompletepath);
88 return path;
91 /* should never happen */
92 abort ();
95 static char *config_GetDataDir(void)
97 const char *path = getenv ("VLC_DATA_PATH");
98 if (path)
99 return strdup (path);
101 char *vlcpath = config_GetLibDir ();
102 char *datadir;
104 if (asprintf (&datadir, "%s/share", vlcpath) == -1)
105 datadir = NULL;
107 free (vlcpath);
108 return datadir;
111 char *config_GetSysPath(vlc_sysdir_t type, const char *filename)
113 char *dir = NULL;
115 switch (type)
117 case VLC_PKG_DATA_DIR:
118 dir = config_GetDataDir();
119 break;
120 case VLC_PKG_LIB_DIR:
121 case VLC_PKG_LIBEXEC_DIR:
122 dir = config_GetLibDir();
123 break;
124 case VLC_SYSDATA_DIR:
125 break;
126 case VLC_LOCALE_DIR:
127 dir = config_GetSysPath(VLC_PKG_DATA_DIR, "locale");
128 break;
129 default:
130 vlc_assert_unreachable();
133 if (filename == NULL || unlikely(dir == NULL))
134 return dir;
136 char *path;
137 asprintf(&path, "%s/%s", dir, filename);
138 free(dir);
139 return path;
142 static char *config_GetHomeDir (void)
144 const char *home = getenv ("HOME");
146 if (home == NULL)
147 home = "/tmp";
149 return strdup (home);
152 static char *getAppDependentDir(vlc_userdir_t type)
154 const char *psz_path;
155 switch (type) {
156 case VLC_CONFIG_DIR:
157 psz_path = "%s/Library/Preferences/%s";
158 break;
159 case VLC_TEMPLATES_DIR:
160 case VLC_USERDATA_DIR:
161 psz_path = "%s/Library/Application Support/%s";
162 break;
163 case VLC_CACHE_DIR:
164 psz_path = "%s/Library/Caches/%s";
165 break;
166 default:
167 vlc_assert_unreachable();
168 break;
171 // Default fallback
172 const char *fallback = "org.videolan.vlc";
173 char *name = NULL;
175 CFBundleRef mainBundle = CFBundleGetMainBundle();
176 if (mainBundle) {
177 CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
178 if (identifierAsNS) {
179 CFIndex len = CFStringGetLength(identifierAsNS);
180 CFIndex size = CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8);
181 char *identifier = calloc(len + 1, sizeof(char));
182 if (identifier != NULL) {
183 Boolean ret = CFStringGetCString(identifierAsNS, identifier, size, kCFStringEncodingUTF8);
184 if (ret)
185 name = identifier;
186 else
187 free(identifier);
192 char *psz_parent = config_GetHomeDir ();
193 char *psz_dir;
194 if ( asprintf( &psz_dir, psz_path, psz_parent, (name) ? name : fallback) == -1 )
195 psz_dir = NULL;
196 free(psz_parent);
197 free(name);
199 return psz_dir;
202 char *config_GetUserDir (vlc_userdir_t type)
204 const char *psz_path;
205 switch (type) {
206 case VLC_CONFIG_DIR:
207 case VLC_TEMPLATES_DIR:
208 case VLC_USERDATA_DIR:
209 case VLC_CACHE_DIR:
210 return getAppDependentDir(type);
212 case VLC_DESKTOP_DIR:
213 psz_path = "%s/Desktop";
214 break;
215 case VLC_DOWNLOAD_DIR:
216 psz_path = "%s/Downloads";
217 break;
218 case VLC_DOCUMENTS_DIR:
219 psz_path = "%s/Documents";
220 break;
221 case VLC_MUSIC_DIR:
222 psz_path = "%s/Music";
223 break;
224 case VLC_PICTURES_DIR:
225 psz_path = "%s/Pictures";
226 break;
227 case VLC_VIDEOS_DIR:
228 psz_path = "%s/Movies";
229 break;
230 case VLC_PUBLICSHARE_DIR:
231 psz_path = "%s/Public";
232 break;
233 case VLC_HOME_DIR:
234 default:
235 psz_path = "%s";
237 char *psz_parent = config_GetHomeDir();
238 char *psz_dir;
239 if (asprintf( &psz_dir, psz_path, psz_parent ) == -1)
240 psz_dir = NULL;
241 free(psz_parent);
242 return psz_dir;