packetizer: hxxx: fix DirectTV extraction
[vlc.git] / src / darwin / dirs.c
blob32acc4e0dcb46f7d8c2c08e3e64f220b19fb6e2c
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 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 static char *config_GetHomeDir (void)
113 const char *home = getenv ("HOME");
115 if (home == NULL)
116 home = "/tmp";
118 return strdup (home);
121 static char *getAppDependentDir(vlc_userdir_t type)
123 const char *psz_path;
124 switch (type) {
125 case VLC_CONFIG_DIR:
126 psz_path = "%s/Library/Preferences/%s";
127 break;
128 case VLC_TEMPLATES_DIR:
129 case VLC_DATA_DIR:
130 psz_path = "%s/Library/Application Support/%s";
131 break;
132 case VLC_CACHE_DIR:
133 psz_path = "%s/Library/Caches/%s";
134 break;
135 default:
136 vlc_assert_unreachable();
137 break;
140 // Default fallback
141 const char *name = "org.videolan.vlc";
143 CFBundleRef mainBundle = CFBundleGetMainBundle();
144 if (mainBundle) {
145 CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
146 if (identifierAsNS) {
147 char identifier[256];
148 Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
149 if (ret)
150 name = identifier;
154 char *psz_parent = config_GetHomeDir ();
155 char *psz_dir;
156 if ( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
157 psz_dir = NULL;
158 free(psz_parent);
160 return psz_dir;
163 char *config_GetUserDir (vlc_userdir_t type)
165 const char *psz_path;
166 switch (type) {
167 case VLC_CONFIG_DIR:
168 case VLC_TEMPLATES_DIR:
169 case VLC_DATA_DIR:
170 case VLC_CACHE_DIR:
171 return getAppDependentDir(type);
173 case VLC_DESKTOP_DIR:
174 psz_path = "%s/Desktop";
175 break;
176 case VLC_DOWNLOAD_DIR:
177 psz_path = "%s/Downloads";
178 break;
179 case VLC_DOCUMENTS_DIR:
180 psz_path = "%s/Documents";
181 break;
182 case VLC_MUSIC_DIR:
183 psz_path = "%s/Music";
184 break;
185 case VLC_PICTURES_DIR:
186 psz_path = "%s/Pictures";
187 break;
188 case VLC_VIDEOS_DIR:
189 psz_path = "%s/Movies";
190 break;
191 case VLC_PUBLICSHARE_DIR:
192 psz_path = "%s/Public";
193 break;
194 case VLC_HOME_DIR:
195 default:
196 psz_path = "%s";
198 char *psz_parent = config_GetHomeDir();
199 char *psz_dir;
200 if (asprintf( &psz_dir, psz_path, psz_parent ) == -1)
201 psz_dir = NULL;
202 free(psz_parent);
203 return psz_dir;