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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include "../libvlc.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" */
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
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 */
83 if (dladdr(system_Init
, &info
)) {
84 char *incompletepath
= strdup(dirname( (char *)info
.dli_fname
));
86 asprintf(&path
, "%s/"PACKAGE
, incompletepath
);
91 /* should never happen */
95 static char *config_GetDataDir(void)
97 const char *path
= getenv ("VLC_DATA_PATH");
101 char *vlcpath
= config_GetLibDir ();
104 if (asprintf (&datadir
, "%s/share", vlcpath
) == -1)
111 char *config_GetSysPath(vlc_sysdir_t type
, const char *filename
)
117 case VLC_PKG_DATA_DIR
:
118 dir
= config_GetDataDir();
120 case VLC_PKG_LIB_DIR
:
121 case VLC_PKG_LIBEXEC_DIR
:
122 dir
= config_GetLibDir();
124 case VLC_SYSDATA_DIR
:
127 dir
= config_GetSysPath(VLC_PKG_DATA_DIR
, "locale");
130 vlc_assert_unreachable();
133 if (filename
== NULL
|| unlikely(dir
== NULL
))
137 asprintf(&path
, "%s/%s", dir
, filename
);
142 static char *config_GetHomeDir (void)
144 const char *home
= getenv ("HOME");
149 return strdup (home
);
152 static char *getAppDependentDir(vlc_userdir_t type
)
154 const char *psz_path
;
157 psz_path
= "%s/Library/Preferences/%s";
159 case VLC_TEMPLATES_DIR
:
160 case VLC_USERDATA_DIR
:
161 psz_path
= "%s/Library/Application Support/%s";
164 psz_path
= "%s/Library/Caches/%s";
167 vlc_assert_unreachable();
172 const char *fallback
= "org.videolan.vlc";
175 CFBundleRef mainBundle
= CFBundleGetMainBundle();
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
);
192 char *psz_parent
= config_GetHomeDir ();
194 if ( asprintf( &psz_dir
, psz_path
, psz_parent
, (name
) ? name
: fallback
) == -1 )
202 char *config_GetUserDir (vlc_userdir_t type
)
204 const char *psz_path
;
207 case VLC_TEMPLATES_DIR
:
208 case VLC_USERDATA_DIR
:
210 return getAppDependentDir(type
);
212 case VLC_DESKTOP_DIR
:
213 psz_path
= "%s/Desktop";
215 case VLC_DOWNLOAD_DIR
:
216 psz_path
= "%s/Downloads";
218 case VLC_DOCUMENTS_DIR
:
219 psz_path
= "%s/Documents";
222 psz_path
= "%s/Music";
224 case VLC_PICTURES_DIR
:
225 psz_path
= "%s/Pictures";
228 psz_path
= "%s/Movies";
230 case VLC_PUBLICSHARE_DIR
:
231 psz_path
= "%s/Public";
237 char *psz_parent
= config_GetHomeDir();
239 if (asprintf( &psz_dir
, psz_path
, psz_parent
) == -1)