dirs.c 6.06 KB
Newer Older
1
/*****************************************************************************
2
 * darwin_dirs.c: Mac OS X directories configuration
3
 *****************************************************************************
4 5
 * Copyright (C) 2001-2014 VLC authors and VideoLAN
 * 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
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * 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
14 15 16 17
 * (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
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * 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.
24 25
 *****************************************************************************/

26 27
#include <CoreFoundation/CoreFoundation.h>

28 29 30 31 32 33 34 35
#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
36
#include "config/configuration.h"
37

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

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

46
char *config_GetLibDir (void)
47
{
48 49 50 51 52 53
    /* 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/" );
54

55 56 57
        /* Check for "VLCKit.framework/Versions/Current/VLCKit",
         * as well as "VLCKit.framework/Versions/A/VLCKit" and
         * "VLC.framework/Versions/B/VLCKit" */
58
        if (p != NULL) {
59 60 61 62
            /* Look for the next forward slash */
            p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
            p += strcspn( p, "/" );

63 64 65
            /* If the string ends with VLCKit then we've found a winner */
            if (!strcmp( p, "/VLCKit"))
                return strdup( dirname(psz_img_name) );
66 67 68 69 70
        }

        /* 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);
71 72
        if (len >= 3 && !strcmp( psz_img_name + len - 3, "VLC"))
            return strdup( dirname(psz_img_name) );
73 74

        /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
75 76
        if (len >= 10 && !strcmp( psz_img_name + len - 10, "VLC-Plugin"))
            return strdup( dirname(psz_img_name) );
77 78
    }

79 80
    /* we are not part of any Mac-style package but were installed
     * the UNIX way. let's trick-around a bit */
81
    Dl_info info;
82 83 84 85 86 87
    if (dladdr(system_Init, &info)) {
        char *incompletepath = strdup(dirname( (char *)info.dli_fname ));
        char *path = NULL;
        asprintf(&path, "%s/"PACKAGE, incompletepath);
        free(incompletepath);
        return path;
88 89 90
    }

    /* should never happen */
91 92 93
    abort ();
}

94
char *config_GetDataDir (void)
95
{
96 97 98 99
    const char *path = getenv ("VLC_DATA_PATH");
    if (path)
        return strdup (path);

100 101 102 103 104 105 106 107 108 109
    char *vlcpath = config_GetLibDir ();
    char *datadir;

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

    free (vlcpath);
    return datadir;
}

110 111 112 113 114 115 116
static char *config_GetHomeDir (void)
{
    const char *home = getenv ("HOME");

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

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 127
            break;
        case VLC_TEMPLATES_DIR:
128
        case VLC_DATA_DIR:
129 130 131 132 133 134
            psz_path = "%s/Library/Application Support/%s";
            break;
        case VLC_CACHE_DIR:
            psz_path = "%s/Library/Caches/%s";
            break;
        default:
135
            vlc_assert_unreachable();
136
            break;
137 138
    }

139 140 141
    // Default fallback
    const char *name = "org.videolan.vlc";

142
    CFBundleRef mainBundle = CFBundleGetMainBundle();
143
    if (mainBundle) {
144
        CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
145
        if (identifierAsNS) {
146 147 148 149 150 151 152
            char identifier[256];
            Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
            if (ret)
                name = identifier;            
        }
    }

153 154
    char *psz_parent = config_GetHomeDir ();
    char *psz_dir;
155
    if ( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
156 157 158 159 160 161 162 163 164
        psz_dir = NULL;
    free(psz_parent);

    return psz_dir;    
}

char *config_GetUserDir (vlc_userdir_t type)
{
    const char *psz_path;
165
    switch (type) {
166 167 168 169 170 171
        case VLC_CONFIG_DIR:
        case VLC_TEMPLATES_DIR:
        case VLC_DATA_DIR:
        case VLC_CACHE_DIR:
            return getAppDependentDir(type);

172
        case VLC_DESKTOP_DIR:
173 174
            psz_path = "%s/Desktop";
            break;
175
        case VLC_DOWNLOAD_DIR:
176 177
            psz_path = "%s/Downloads";
            break;
178
        case VLC_DOCUMENTS_DIR:
179 180
            psz_path = "%s/Documents";
            break;
181
        case VLC_MUSIC_DIR:
182 183
            psz_path = "%s/Music";
            break;
184
        case VLC_PICTURES_DIR:
185 186
            psz_path = "%s/Pictures";
            break;
187
        case VLC_VIDEOS_DIR:
188 189 190 191 192 193 194 195
            psz_path = "%s/Movies";
            break;
        case VLC_PUBLICSHARE_DIR:
            psz_path = "%s/Public";
            break;
        case VLC_HOME_DIR:
        default:
            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
}