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 <vlc_charset.h>
32 #include "../libvlc.h"
36 #include <mach-o/dyld.h>
38 #include <CoreFoundation/CoreFoundation.h>
40 char *config_GetLibDir (void)
42 /* Get the full program path and name */
43 /* First try to see if we are linked to the framework */
44 for (unsigned i
= 0; i
< _dyld_image_count(); i
++)
46 const char *psz_img_name
= _dyld_get_image_name(i
);
47 const char *p
= strstr( psz_img_name
, "VLCKit.framework/Versions/" );
49 /* Check for "VLCKit.framework/Versions/Current/VLCKit",
50 * as well as "VLCKit.framework/Versions/A/VLCKit" and
51 * "VLC.framework/Versions/B/VLCKit" */
53 /* Look for the next forward slash */
54 p
+= 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
55 p
+= strcspn( p
, "/" );
57 /* If the string ends with VLCKit then we've found a winner */
58 if (!strcmp( p
, "/VLCKit"))
59 return strdup( dirname(psz_img_name
) );
62 /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
64 size_t len
= strlen(psz_img_name
);
65 if (len
>= 3 && !strcmp( psz_img_name
+ len
- 3, "VLC"))
66 return strdup( dirname(psz_img_name
) );
68 /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
69 if (len
>= 10 && !strcmp( psz_img_name
+ len
- 10, "VLC-Plugin"))
70 return strdup( dirname(psz_img_name
) );
72 /* Do we end by "VLC for iOS"? so we are the iOS app */
73 if (len
>= 11 && !strcmp( psz_img_name
+ len
- 11, "VLC for iOS"))
74 return strdup( dirname(psz_img_name
) );
76 /* Do we end by "VLC-TV"? so we are the tvOS app */
77 if (len
>= 6 && !strcmp( psz_img_name
+ len
- 6, "VLC-TV"))
78 return strdup( dirname(psz_img_name
) );
81 /* we are not part of any Mac-style package but were installed
82 * the UNIX way. let's trick-around a bit */
84 if (dladdr(system_Init
, &info
)) {
85 char *incompletepath
= strdup(dirname( (char *)info
.dli_fname
));
87 asprintf(&path
, "%s/"PACKAGE
, incompletepath
);
92 /* should never happen */
96 static char *config_GetDataDir(void)
98 const char *path
= getenv ("VLC_DATA_PATH");
100 return strdup (path
);
102 char *vlcpath
= config_GetLibDir ();
105 if (asprintf (&datadir
, "%s/share", vlcpath
) == -1)
112 char *config_GetSysPath(vlc_sysdir_t type
, const char *filename
)
118 case VLC_PKG_DATA_DIR
:
119 dir
= config_GetDataDir();
121 case VLC_PKG_LIB_DIR
:
122 case VLC_PKG_LIBEXEC_DIR
:
123 dir
= config_GetLibDir();
125 case VLC_SYSDATA_DIR
:
128 dir
= config_GetSysPath(VLC_PKG_DATA_DIR
, "locale");
131 vlc_assert_unreachable();
134 if (filename
== NULL
|| unlikely(dir
== NULL
))
138 asprintf(&path
, "%s/%s", dir
, filename
);
143 static char *config_GetHomeDir (void)
145 const char *home
= getenv ("HOME");
150 return strdup (home
);
153 static char *getAppDependentDir(vlc_userdir_t type
)
155 const char *psz_path
;
158 psz_path
= "%s/Library/Preferences/%s";
160 case VLC_TEMPLATES_DIR
:
161 case VLC_USERDATA_DIR
:
162 psz_path
= "%s/Library/Application Support/%s";
165 psz_path
= "%s/Library/Caches/%s";
168 vlc_assert_unreachable();
173 const char *fallback
= "org.videolan.vlc";
176 CFBundleRef mainBundle
= CFBundleGetMainBundle();
178 CFStringRef identifierAsNS
= CFBundleGetIdentifier(mainBundle
);
180 name
= FromCFString(identifierAsNS
, kCFStringEncodingUTF8
);
183 char *psz_parent
= config_GetHomeDir ();
185 if ( asprintf( &psz_dir
, psz_path
, psz_parent
, (name
) ? name
: fallback
) == -1 )
193 char *config_GetUserDir (vlc_userdir_t type
)
195 const char *psz_path
;
198 case VLC_TEMPLATES_DIR
:
199 case VLC_USERDATA_DIR
:
201 return getAppDependentDir(type
);
203 case VLC_DESKTOP_DIR
:
204 psz_path
= "%s/Desktop";
206 case VLC_DOWNLOAD_DIR
:
207 psz_path
= "%s/Downloads";
209 case VLC_DOCUMENTS_DIR
:
210 psz_path
= "%s/Documents";
213 psz_path
= "%s/Music";
215 case VLC_PICTURES_DIR
:
216 psz_path
= "%s/Pictures";
219 psz_path
= "%s/Movies";
221 case VLC_PUBLICSHARE_DIR
:
222 psz_path
= "%s/Public";
228 char *psz_parent
= config_GetHomeDir();
230 if (asprintf( &psz_dir
, psz_path
, psz_parent
) == -1)