]> git.sesse.net Git - vlc/blob - src/posix/darwin_dirs.c
6fc5d878face49a4999bf54989895c101b3d2c37
[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_charset.h>
35 #include <vlc_configuration.h>
36 #include "config/configuration.h"
37
38 #include <libgen.h>
39 #include <dlfcn.h>
40 #include <mach-o/dyld.h>
41
42 #ifndef MAXPATHLEN
43 # define MAXPATHLEN 1024
44 #endif
45
46 static char *configdir = NULL;
47
48 static pthread_once_t once = PTHREAD_ONCE_INIT;
49
50 static void init_dirs( void )
51 {
52     configdir = config_GetUserDir(VLC_CONFIG_DIR);
53 }
54
55 const char *config_GetConfDir( void )
56 {
57     pthread_once(&once, init_dirs);
58     return configdir;
59 }
60
61 static char *config_GetLibPath (void)
62 {
63     /* Get the full program path and name */
64     /* First try to see if we are linked to the framework */
65     for (unsigned i = 0; i < _dyld_image_count(); i++)
66     {
67         const char *psz_img_name = _dyld_get_image_name(i);
68         const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
69
70         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
71          * as well as "VLCKit.framework/Versions/A/VLCKit" and
72          * "VLC.framework/Versions/B/VLCKit" */
73         if( p != NULL )
74         {
75             /* Look for the next forward slash */
76             p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
77             p += strcspn( p, "/" );
78
79             /* If the string ends with VLC then we've found a winner */
80             if ( !strcmp( p, "/VLCKit" ) )
81                 return strdup( psz_img_name );
82         }
83
84         /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
85          * link to VLCKit. */
86         size_t len = strlen(psz_img_name);
87         if( len >= 3 && !strcmp( psz_img_name + len - 3, "VLC") )
88             return strdup( psz_img_name );
89     }
90
91     /* We are not linked to the VLC.framework, let's use dladdr to figure
92      * libvlc path */
93     Dl_info info;
94     if( dladdr(system_Init, &info) )
95         return strdup(dirname( info.dli_fname ));
96
97     char path[MAXPATHLEN+1];
98     uint32_t path_len = sizeof(path) - 1;
99
100     if ( !_NSGetExecutablePath(path, &path_len) )
101          return strdup(path);
102     return NULL;
103 }
104
105 char *config_GetLibDir (void)
106 {
107     char *path = config_GetLibPath ();
108     if (path != NULL)
109     {
110         char *p = strrchr (p, '/');
111         if (p != NULL)
112         {
113             *p = '\0';
114             return path;
115         }
116         free (path);
117     }
118
119     /* should never happen */
120     abort ();
121 }
122
123 char *config_GetDataDirDefault (void)
124 {
125     char *vlcpath = config_GetLibDir ();
126     char *datadir;
127
128     if (asprintf (&datadir, "%s/share", vlcpath) == -1)
129         datadir = NULL;
130
131     free (vlcpath);
132     return datadir;
133 }
134
135 static char *config_GetHomeDir (void)
136 {
137     const char *home = getenv ("HOME");
138
139     if (home == NULL)
140         home = "/tmp";
141
142     return FromLocaleDup (home);
143 }
144
145 static char *getAppDependentDir(vlc_userdir_t type)
146 {
147     const char *psz_path;
148     switch (type)
149     {
150         case VLC_CONFIG_DIR:
151             psz_path = "%s/Library/Preferences/%s";
152             break;
153         case VLC_TEMPLATES_DIR:
154         case VLC_DATA_DIR:
155             psz_path = "%s/Library/Application Support/%s";
156             break;
157         case VLC_CACHE_DIR:
158             psz_path = "%s/Library/Caches/%s";
159             break;
160         default:
161             assert(0);
162             break;
163     }
164
165     // Default fallback
166     const char *name = "org.videolan.vlc";
167
168     CFBundleRef mainBundle = CFBundleGetMainBundle();
169     if (mainBundle)
170     {
171         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
172         if (identifierAsNS)
173         {
174             char identifier[256];
175             Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
176             if (ret)
177                 name = identifier;            
178         }
179     }
180
181     char *psz_parent = config_GetHomeDir ();
182     char *psz_dir;
183     if( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
184         psz_dir = NULL;
185     free(psz_parent);
186
187     return psz_dir;    
188 }
189
190 char *config_GetUserDir (vlc_userdir_t type)
191 {
192     const char *psz_path;
193     switch (type)
194     {
195         case VLC_CONFIG_DIR:
196         case VLC_TEMPLATES_DIR:
197         case VLC_DATA_DIR:
198         case VLC_CACHE_DIR:
199             return getAppDependentDir(type);
200
201         case VLC_DESKTOP_DIR:
202             psz_path = "%s/Desktop";
203             break;
204         case VLC_DOWNLOAD_DIR:
205             psz_path = "%s/Downloads";
206             break;
207         case VLC_DOCUMENTS_DIR:
208             psz_path = "%s/Documents";
209             break;
210         case VLC_MUSIC_DIR:
211             psz_path = "%s/Music";
212             break;
213         case VLC_PICTURES_DIR:
214             psz_path = "%s/Pictures";
215             break;
216         case VLC_VIDEOS_DIR:
217             psz_path = "%s/Movies";
218             break;
219         case VLC_PUBLICSHARE_DIR:
220             psz_path = "%s/Public";
221             break;
222         case VLC_HOME_DIR:
223         default:
224             psz_path = "%s";
225     }
226     char *psz_parent = config_GetHomeDir ();
227     char *psz_dir;
228     if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
229         psz_dir = NULL;
230     free(psz_parent);
231     return psz_dir;
232 }