]> git.sesse.net Git - vlc/blob - src/posix/darwin_dirs.c
posix: drop support for non-UTF-8 operating systems
[vlc] / src / posix / darwin_dirs.c
1 /*****************************************************************************
2  * darwin_dirs.c: Mac OS X directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * Copyright © 2007-2012 Rémi Denis-Courmont
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <CoreFoundation/CoreFoundation.h>
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include "../libvlc.h"
34 #include <vlc_configuration.h>
35 #include "config/configuration.h"
36
37 #include <libgen.h>
38 #include <dlfcn.h>
39 #include <mach-o/dyld.h>
40
41 #ifndef MAXPATHLEN
42 # define MAXPATHLEN 1024
43 #endif
44
45 static char *configdir = NULL;
46
47 static pthread_once_t once = PTHREAD_ONCE_INIT;
48
49 static void init_dirs( void )
50 {
51     configdir = config_GetUserDir(VLC_CONFIG_DIR);
52 }
53
54 const char *config_GetConfDir( void )
55 {
56     pthread_once(&once, init_dirs);
57     return configdir;
58 }
59
60 static char *config_GetLibPath (void)
61 {
62     /* Get the full program path and name */
63     /* First try to see if we are linked to the framework */
64     for (unsigned i = 0; i < _dyld_image_count(); i++)
65     {
66         const char *psz_img_name = _dyld_get_image_name(i);
67         const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
68
69         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
70          * as well as "VLCKit.framework/Versions/A/VLCKit" and
71          * "VLC.framework/Versions/B/VLCKit" */
72         if( p != NULL )
73         {
74             /* Look for the next forward slash */
75             p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
76             p += strcspn( p, "/" );
77
78             /* If the string ends with VLC then we've found a winner */
79             if ( !strcmp( p, "/VLCKit" ) )
80                 return strdup( psz_img_name );
81         }
82
83         /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
84          * link to VLCKit. */
85         size_t len = strlen(psz_img_name);
86         if( len >= 3 && !strcmp( psz_img_name + len - 3, "VLC") )
87             return strdup( psz_img_name );
88     }
89
90     /* We are not linked to the VLC.framework, let's use dladdr to figure
91      * libvlc path */
92     Dl_info info;
93     if( dladdr(system_Init, &info) )
94         return strdup(dirname( info.dli_fname ));
95
96     char path[MAXPATHLEN+1];
97     uint32_t path_len = sizeof(path) - 1;
98
99     if ( !_NSGetExecutablePath(path, &path_len) )
100          return strdup(path);
101     return NULL;
102 }
103
104 char *config_GetLibDir (void)
105 {
106     char *path = config_GetLibPath ();
107     if (path != NULL)
108     {
109         char *p = strrchr (path, '/');
110         if (p != NULL)
111         {
112             *p = '\0';
113             return path;
114         }
115         free (path);
116     }
117
118     /* should never happen */
119     abort ();
120 }
121
122 char *config_GetDataDirDefault (void)
123 {
124     char *vlcpath = config_GetLibDir ();
125     char *datadir;
126
127     if (asprintf (&datadir, "%s/share", vlcpath) == -1)
128         datadir = NULL;
129
130     free (vlcpath);
131     return datadir;
132 }
133
134 static char *config_GetHomeDir (void)
135 {
136     const char *home = getenv ("HOME");
137
138     if (home == NULL)
139         home = "/tmp";
140
141     return strdup (home);
142 }
143
144 static char *getAppDependentDir(vlc_userdir_t type)
145 {
146     const char *psz_path;
147     switch (type)
148     {
149         case VLC_CONFIG_DIR:
150             psz_path = "%s/Library/Preferences/%s";
151             break;
152         case VLC_TEMPLATES_DIR:
153         case VLC_DATA_DIR:
154             psz_path = "%s/Library/Application Support/%s";
155             break;
156         case VLC_CACHE_DIR:
157             psz_path = "%s/Library/Caches/%s";
158             break;
159         default:
160             assert(0);
161             break;
162     }
163
164     // Default fallback
165     const char *name = "org.videolan.vlc";
166
167     CFBundleRef mainBundle = CFBundleGetMainBundle();
168     if (mainBundle)
169     {
170         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
171         if (identifierAsNS)
172         {
173             char identifier[256];
174             Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
175             if (ret)
176                 name = identifier;            
177         }
178     }
179
180     char *psz_parent = config_GetHomeDir ();
181     char *psz_dir;
182     if( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
183         psz_dir = NULL;
184     free(psz_parent);
185
186     return psz_dir;    
187 }
188
189 char *config_GetUserDir (vlc_userdir_t type)
190 {
191     const char *psz_path;
192     switch (type)
193     {
194         case VLC_CONFIG_DIR:
195         case VLC_TEMPLATES_DIR:
196         case VLC_DATA_DIR:
197         case VLC_CACHE_DIR:
198             return getAppDependentDir(type);
199
200         case VLC_DESKTOP_DIR:
201             psz_path = "%s/Desktop";
202             break;
203         case VLC_DOWNLOAD_DIR:
204             psz_path = "%s/Downloads";
205             break;
206         case VLC_DOCUMENTS_DIR:
207             psz_path = "%s/Documents";
208             break;
209         case VLC_MUSIC_DIR:
210             psz_path = "%s/Music";
211             break;
212         case VLC_PICTURES_DIR:
213             psz_path = "%s/Pictures";
214             break;
215         case VLC_VIDEOS_DIR:
216             psz_path = "%s/Movies";
217             break;
218         case VLC_PUBLICSHARE_DIR:
219             psz_path = "%s/Public";
220             break;
221         case VLC_HOME_DIR:
222         default:
223             psz_path = "%s";
224     }
225     char *psz_parent = config_GetHomeDir ();
226     char *psz_dir;
227     if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
228         psz_dir = NULL;
229     free(psz_parent);
230     return psz_dir;
231 }