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 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 static char *config_GetHomeDir (void)
113 const char *home
= getenv ("HOME");
118 return strdup (home
);
121 static char *getAppDependentDir(vlc_userdir_t type
)
123 const char *psz_path
;
126 psz_path
= "%s/Library/Preferences/%s";
128 case VLC_TEMPLATES_DIR
:
130 psz_path
= "%s/Library/Application Support/%s";
133 psz_path
= "%s/Library/Caches/%s";
136 vlc_assert_unreachable();
141 const char *name
= "org.videolan.vlc";
143 CFBundleRef mainBundle
= CFBundleGetMainBundle();
145 CFStringRef identifierAsNS
= CFBundleGetIdentifier(mainBundle
);
146 if (identifierAsNS
) {
147 char identifier
[256];
148 Boolean ret
= CFStringGetCString(identifierAsNS
, identifier
, sizeof(identifier
), kCFStringEncodingUTF8
);
154 char *psz_parent
= config_GetHomeDir ();
156 if ( asprintf( &psz_dir
, psz_path
, psz_parent
, name
) == -1 )
163 char *config_GetUserDir (vlc_userdir_t type
)
165 const char *psz_path
;
168 case VLC_TEMPLATES_DIR
:
171 return getAppDependentDir(type
);
173 case VLC_DESKTOP_DIR
:
174 psz_path
= "%s/Desktop";
176 case VLC_DOWNLOAD_DIR
:
177 psz_path
= "%s/Downloads";
179 case VLC_DOCUMENTS_DIR
:
180 psz_path
= "%s/Documents";
183 psz_path
= "%s/Music";
185 case VLC_PICTURES_DIR
:
186 psz_path
= "%s/Pictures";
189 psz_path
= "%s/Movies";
191 case VLC_PUBLICSHARE_DIR
:
192 psz_path
= "%s/Public";
198 char *psz_parent
= config_GetHomeDir();
200 if (asprintf( &psz_dir
, psz_path
, psz_parent
) == -1)