dirs.c 6.06 KB
Newer Older
1
/*****************************************************************************
2
 * darwin_dirs.c: Mac OS X directories configuration
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
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
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
10
11
12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13
14
15
16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
17
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
20
21
22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
 *****************************************************************************/

25
26
#include <CoreFoundation/CoreFoundation.h>

27
28
29
30
31
32
33
34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>

#include "../libvlc.h"
#include <vlc_configuration.h>
Pierre Ynard's avatar
Pierre Ynard committed
35
#include "config/configuration.h"
36

37
38
39
40
#include <libgen.h>
#include <dlfcn.h>
#include <mach-o/dyld.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
41
42
43
44
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif

45
static char *config_GetLibPath (void)
46
{
47
48
49
50
51
52
    /* Get the full program path and name */
    /* First try to see if we are linked to the framework */
    for (unsigned i = 0; i < _dyld_image_count(); i++)
    {
        const char *psz_img_name = _dyld_get_image_name(i);
        const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        /* Check for "VLCKit.framework/Versions/Current/VLCKit",
         * as well as "VLCKit.framework/Versions/A/VLCKit" and
         * "VLC.framework/Versions/B/VLCKit" */
        if( p != NULL )
        {
            /* Look for the next forward slash */
            p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
            p += strcspn( p, "/" );

            /* If the string ends with VLC then we've found a winner */
            if ( !strcmp( p, "/VLCKit" ) )
                return strdup( psz_img_name );
        }

        /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
         * link to VLCKit. */
        size_t len = strlen(psz_img_name);
        if( len >= 3 && !strcmp( psz_img_name + len - 3, "VLC") )
            return strdup( psz_img_name );
    }

    /* We are not linked to the VLC.framework, let's use dladdr to figure
     * libvlc path */
    Dl_info info;
    if( dladdr(system_Init, &info) )
        return strdup(dirname( info.dli_fname ));

    char path[MAXPATHLEN+1];
    uint32_t path_len = sizeof(path) - 1;

    if ( !_NSGetExecutablePath(path, &path_len) )
         return strdup(path);
    return NULL;
87
88
}

89
char *config_GetLibDir (void)
90
{
91
92
93
    char *path = config_GetLibPath ();
    if (path != NULL)
    {
Rafaël Carré's avatar
Rafaël Carré committed
94
        char *p = strrchr (path, '/');
95
96
97
98
99
100
101
102
103
        if (p != NULL)
        {
            *p = '\0';
            return path;
        }
        free (path);
    }

    /* should never happen */
104
105
106
    abort ();
}

107
char *config_GetDataDir (void)
108
{
109
110
111
112
    const char *path = getenv ("VLC_DATA_PATH");
    if (path)
        return strdup (path);

113
114
115
116
117
118
119
120
121
122
    char *vlcpath = config_GetLibDir ();
    char *datadir;

    if (asprintf (&datadir, "%s/share", vlcpath) == -1)
        datadir = NULL;

    free (vlcpath);
    return datadir;
}

123
124
125
126
127
128
129
static char *config_GetHomeDir (void)
{
    const char *home = getenv ("HOME");

    if (home == NULL)
        home = "/tmp";

130
    return strdup (home);
131
132
}

133
static char *getAppDependentDir(vlc_userdir_t type)
134
{
135
    const char *psz_path;
136
137
138
    switch (type)
    {
        case VLC_CONFIG_DIR:
139
            psz_path = "%s/Library/Preferences/%s";
140
141
            break;
        case VLC_TEMPLATES_DIR:
142
        case VLC_DATA_DIR:
143
144
145
146
147
148
149
            psz_path = "%s/Library/Application Support/%s";
            break;
        case VLC_CACHE_DIR:
            psz_path = "%s/Library/Caches/%s";
            break;
        default:
            assert(0);
150
            break;
151
152
    }

153
154
155
    // Default fallback
    const char *name = "org.videolan.vlc";

156
    CFBundleRef mainBundle = CFBundleGetMainBundle();
157
158
159
160
161
162
163
164
165
166
167
168
    if (mainBundle)
    {
        CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
        if (identifierAsNS)
        {
            char identifier[256];
            Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
            if (ret)
                name = identifier;            
        }
    }

169
170
    char *psz_parent = config_GetHomeDir ();
    char *psz_dir;
171
    if( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        psz_dir = NULL;
    free(psz_parent);

    return psz_dir;    
}

char *config_GetUserDir (vlc_userdir_t type)
{
    const char *psz_path;
    switch (type)
    {
        case VLC_CONFIG_DIR:
        case VLC_TEMPLATES_DIR:
        case VLC_DATA_DIR:
        case VLC_CACHE_DIR:
            return getAppDependentDir(type);

189
        case VLC_DESKTOP_DIR:
190
191
            psz_path = "%s/Desktop";
            break;
192
        case VLC_DOWNLOAD_DIR:
193
194
            psz_path = "%s/Downloads";
            break;
195
        case VLC_DOCUMENTS_DIR:
196
197
            psz_path = "%s/Documents";
            break;
198
        case VLC_MUSIC_DIR:
199
200
            psz_path = "%s/Music";
            break;
201
        case VLC_PICTURES_DIR:
202
203
            psz_path = "%s/Pictures";
            break;
204
        case VLC_VIDEOS_DIR:
205
206
207
208
209
210
211
212
            psz_path = "%s/Movies";
            break;
        case VLC_PUBLICSHARE_DIR:
            psz_path = "%s/Public";
            break;
        case VLC_HOME_DIR:
        default:
            psz_path = "%s";
213
    }
214
215
    char *psz_parent = config_GetHomeDir ();
    char *psz_dir;
216
217
    if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
        psz_dir = NULL;
218
    free(psz_parent);
219
    return psz_dir;
220
}