]> git.sesse.net Git - vlc/blob - src/darwin/dirs.c
decoder: add input_DecoderFlush()
[vlc] / src / darwin / dirs.c
1 /*****************************************************************************
2  * darwin_dirs.c: Mac OS X directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2014 VLC authors and VideoLAN
5  * Copyright (C) 2007-2012 Rémi Denis-Courmont
6  *
7  * Authors: Rémi Denis-Courmont
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *          Pierre d'Herbemont <pdherbemont # videolan org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #include <CoreFoundation/CoreFoundation.h>
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #include "../libvlc.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 char *config_GetLibDir (void)
47 {
48     /* Get the full program path and name */
49     /* First try to see if we are linked to the framework */
50     for (unsigned i = 0; i < _dyld_image_count(); i++)
51     {
52         const char *psz_img_name = _dyld_get_image_name(i);
53         const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
54
55         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
56          * as well as "VLCKit.framework/Versions/A/VLCKit" and
57          * "VLC.framework/Versions/B/VLCKit" */
58         if (p != NULL) {
59             /* Look for the next forward slash */
60             p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
61             p += strcspn( p, "/" );
62
63             /* If the string ends with VLCKit then we've found a winner */
64             if (!strcmp( p, "/VLCKit"))
65                 return strdup( dirname(psz_img_name) );
66         }
67
68         /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
69          * link to VLCKit. */
70         size_t len = strlen(psz_img_name);
71         if (len >= 3 && !strcmp( psz_img_name + len - 3, "VLC"))
72             return strdup( dirname(psz_img_name) );
73
74         /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
75         if (len >= 10 && !strcmp( psz_img_name + len - 10, "VLC-Plugin"))
76             return strdup( dirname(psz_img_name) );
77     }
78
79     /* we are not part of any Mac-style package but were installed
80      * the UNIX way. let's trick-around a bit */
81     Dl_info info;
82     if (dladdr(system_Init, &info)) {
83         char *incompletepath = strdup(dirname( (char *)info.dli_fname ));
84         char *path = NULL;
85         asprintf(&path, "%s/"PACKAGE, incompletepath);
86         free(incompletepath);
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             vlc_assert_unreachable();
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 }