]> git.sesse.net Git - vlc/blob - src/config/dirs.c
Update _WIN32_IE to IE 5.01
[vlc] / src / config / dirs.c
1 /*****************************************************************************
2  * dirs.c: directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * Copyright © 2007-2008 Rémi Denis-Courmont
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #if defined( WIN32 )
31 # ifndef _WIN32_IE
32 #  define _WIN32_IE 0x0501
33 # endif
34 # include <w32api.h>
35 #ifndef UNDER_CE
36 # include <direct.h>
37 #endif
38 # include <shlobj.h>
39 #else
40 # include <unistd.h>
41 # include <pwd.h>
42 #endif
43
44 #include "../libvlc.h"
45 #include "configuration.h"
46 #include <vlc_charset.h>
47 #include <vlc_configuration.h>
48
49 #include <errno.h>                                                  /* errno */
50 #include <assert.h>
51 #include <limits.h>
52
53 #if defined( WIN32 )
54 # define DIR_SHARE ""
55 #else
56 # define DIR_SHARE "share"
57 #endif
58
59
60 /**
61  * config_GetDataDir: find directory where shared data is installed
62  *
63  * @return a string (always succeeds).
64  */
65 const char *config_GetDataDir( void )
66 {
67 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
68     static char path[PATH_MAX] = "";
69
70     if( *path == '\0' )
71     {
72         snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE, psz_vlcpath );
73         path[sizeof( path ) - 1] = '\0';
74     }
75     return path;
76 #else
77     return DATA_PATH;
78 #endif
79 }
80
81 static const char *GetDir( bool b_appdata, bool b_common_appdata )
82 {
83     /* FIXME: a full memory page here - quite a waste... */
84     static char homedir[PATH_MAX] = "";
85
86 #if defined (WIN32)
87     wchar_t wdir[MAX_PATH];
88
89 # if defined (UNDER_CE)
90     /*There are some errors in cegcc headers*/
91 #undef SHGetSpecialFolderPath
92     BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
93     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
94 # else
95     /* Get the "Application Data" folder for the current user */
96     if( S_OK == SHGetFolderPathW( NULL,
97               ( b_appdata ? CSIDL_APPDATA :
98                ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
99               | CSIDL_FLAG_CREATE,
100                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
101 # endif
102     {
103         static char appdir[PATH_MAX] = "";
104         static char comappdir[PATH_MAX] = "";
105         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
106                              b_appdata ? appdir :
107                              (b_common_appdata ? comappdir :homedir),
108                               PATH_MAX, NULL, NULL);
109         return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
110     }
111 #else
112     (void)b_appdata;
113     (void)b_common_appdata;
114 #endif
115
116 #ifdef LIBVLC_USE_PTHREAD
117     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
118     pthread_mutex_lock (&lock);
119 #endif
120
121     if (!*homedir)
122     {
123         const char *psz_localhome = getenv( "HOME" );
124 #if defined(HAVE_GETPWUID_R)
125         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
126         if (psz_localhome == NULL)
127         {
128             struct passwd pw, *res;
129
130             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
131                 psz_localhome = pw.pw_dir;
132         }
133 #endif
134         if (psz_localhome == NULL)
135             psz_localhome = getenv( "TMP" );
136         if (psz_localhome == NULL)
137             psz_localhome = "/tmp";
138
139         const char *uhomedir = FromLocale (psz_localhome);
140         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
141         homedir[sizeof (homedir) - 1] = '\0';
142         LocaleFree (uhomedir);
143     }
144 #ifdef LIBVLC_USE_PTHREAD
145     pthread_mutex_unlock (&lock);
146 #endif
147     return homedir;
148 }
149
150 /**
151  * Determines the system configuration directory.
152  *
153  * @return a string (always succeeds).
154  */
155 const char *config_GetConfDir( void )
156 {
157 #if defined (WIN32)
158     return GetDir( false, true );
159 #elif defined(__APPLE__) || defined (SYS_BEOS)
160     static char path[PATH_MAX] = "";
161
162     if( *path == '\0' )
163     {
164         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
165                   psz_vlcpath );
166         path[sizeof( path ) - 1] = '\0';
167     }
168     return path;
169 #else
170     return SYSCONFDIR;
171 #endif
172 }
173
174 /**
175  * Get the user's home directory
176  */
177 const char *config_GetHomeDir( void )
178 {
179     return GetDir (false, false);
180 }
181
182 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
183 {
184     char *psz_dir;
185 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
186     const char *psz_parent = GetDir (true, false);
187
188     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
189         psz_dir = NULL;
190
191     (void)xdg_name; (void)xdg_default;
192 #else
193     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
194     /* XDG Base Directory Specification - Version 0.6 */
195     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
196
197     const char *psz_home = getenv (var);
198     psz_home = psz_home ? FromLocale (psz_home) : NULL;
199     if( psz_home )
200     {
201         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
202             psz_dir = NULL;
203         LocaleFree (psz_home);
204         return psz_dir;
205     }
206
207     /* Try HOME, then fallback to non-XDG dirs */
208     psz_home = config_GetHomeDir();
209     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
210         psz_dir = NULL;
211 #endif
212     return psz_dir;
213 }
214
215 /**
216  * Get the user's VLC configuration directory
217  */
218 char *config_GetUserConfDir( void )
219 {
220     return config_GetFooDir ("CONFIG", ".config");
221 }
222
223 /**
224  * Get the user's VLC data directory
225  * (used for stuff like the skins, custom lua modules, ...)
226  */
227 char *config_GetUserDataDir( void )
228 {
229     return config_GetFooDir ("DATA", ".local/share");
230 }
231
232 /**
233  * Get the user's VLC cache directory
234  * (used for stuff like the modules cache, the album art cache, ...)
235  */
236 char *config_GetCacheDir( void )
237 {
238 #if defined(__APPLE__)
239     char *psz_dir;
240     const char *psz_parent = GetDir (true, false);
241
242     if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
243         psz_dir = NULL;
244
245     return psz_dir;
246 #else
247     return config_GetFooDir ("CACHE", ".cache");
248 #endif
249 }