]> git.sesse.net Git - vlc/blob - src/darwin/dirs.c
1041b6b92bfc3c9c39558a1b83a54deebd1a31de
[vlc] / src / 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 char *config_GetLibDir (void)
46 {
47     /* Get the full program path and name */
48     /* First try to see if we are linked to the framework */
49     for (unsigned i = 0; i < _dyld_image_count(); i++)
50     {
51         const char *psz_img_name = _dyld_get_image_name(i);
52         const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
53
54         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
55          * as well as "VLCKit.framework/Versions/A/VLCKit" and
56          * "VLC.framework/Versions/B/VLCKit" */
57         if (p != NULL) {
58             /* Look for the next forward slash */
59             p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
60             p += strcspn( p, "/" );
61
62             /* If the string ends with VLCKit then we've found a winner */
63             if (!strcmp( p, "/VLCKit"))
64                 return strdup( dirname(psz_img_name) );
65         }
66
67         /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
68          * link to VLCKit. */
69         size_t len = strlen(psz_img_name);
70         if (len >= 3 && !strcmp( psz_img_name + len - 3, "VLC"))
71             return strdup( dirname(psz_img_name) );
72
73         /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
74         if (len >= 10 && !strcmp( psz_img_name + len - 10, "VLC-Plugin"))
75             return strdup( dirname(psz_img_name) );
76     }
77
78     /* we are not part of any Mac-style package but were installed
79      * the UNIX way. let's trick-around a bit */
80     Dl_info info;
81     if (dladdr(system_Init, &info)) {
82         char *incompletepath = strdup(dirname( (char *)info.dli_fname ));
83         char *path = NULL;
84         asprintf(&path, "%s/"PACKAGE, incompletepath);
85         free(incompletepath);
86         printf("final path %s\n", path);
87         return path;
88     }
89
90     /* should never happen */
91     abort ();
92 }
93
94 char *config_GetDataDir (void)
95 {
96     const char *path = getenv ("VLC_DATA_PATH");
97     if (path)
98         return strdup (path);
99
100     char *vlcpath = config_GetLibDir ();
101     char *datadir;
102
103     if (asprintf (&datadir, "%s/share", vlcpath) == -1)
104         datadir = NULL;
105
106     free (vlcpath);
107     return datadir;
108 }
109
110 static char *config_GetHomeDir (void)
111 {
112     const char *home = getenv ("HOME");
113
114     if (home == NULL)
115         home = "/tmp";
116
117     return strdup (home);
118 }
119
120 static char *getAppDependentDir(vlc_userdir_t type)
121 {
122     const char *psz_path;
123     switch (type) {
124         case VLC_CONFIG_DIR:
125             psz_path = "%s/Library/Preferences/%s";
126             break;
127         case VLC_TEMPLATES_DIR:
128         case VLC_DATA_DIR:
129             psz_path = "%s/Library/Application Support/%s";
130             break;
131         case VLC_CACHE_DIR:
132             psz_path = "%s/Library/Caches/%s";
133             break;
134         default:
135             assert(0);
136             break;
137     }
138
139     // Default fallback
140     const char *name = "org.videolan.vlc";
141
142     CFBundleRef mainBundle = CFBundleGetMainBundle();
143     if (mainBundle) {
144         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
145         if (identifierAsNS) {
146             char identifier[256];
147             Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
148             if (ret)
149                 name = identifier;            
150         }
151     }
152
153     char *psz_parent = config_GetHomeDir ();
154     char *psz_dir;
155     if ( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
156         psz_dir = NULL;
157     free(psz_parent);
158
159     return psz_dir;    
160 }
161
162 char *config_GetUserDir (vlc_userdir_t type)
163 {
164     const char *psz_path;
165     switch (type) {
166         case VLC_CONFIG_DIR:
167         case VLC_TEMPLATES_DIR:
168         case VLC_DATA_DIR:
169         case VLC_CACHE_DIR:
170             return getAppDependentDir(type);
171
172         case VLC_DESKTOP_DIR:
173             psz_path = "%s/Desktop";
174             break;
175         case VLC_DOWNLOAD_DIR:
176             psz_path = "%s/Downloads";
177             break;
178         case VLC_DOCUMENTS_DIR:
179             psz_path = "%s/Documents";
180             break;
181         case VLC_MUSIC_DIR:
182             psz_path = "%s/Music";
183             break;
184         case VLC_PICTURES_DIR:
185             psz_path = "%s/Pictures";
186             break;
187         case VLC_VIDEOS_DIR:
188             psz_path = "%s/Movies";
189             break;
190         case VLC_PUBLICSHARE_DIR:
191             psz_path = "%s/Public";
192             break;
193         case VLC_HOME_DIR:
194         default:
195             psz_path = "%s";
196     }
197     char *psz_parent = config_GetHomeDir();
198     char *psz_dir;
199     if (asprintf( &psz_dir, psz_path, psz_parent ) == -1)
200         psz_dir = NULL;
201     free(psz_parent);
202     return psz_dir;
203 }