]> git.sesse.net Git - vlc/blob - src/win32/dirs.c
udp: fix locking (fixes #14234) and cancellation
[vlc] / src / win32 / dirs.c
1 /*****************************************************************************
2  * dirs.c: directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2010 VLC authors and VideoLAN
5  * Copyright © 2007-2012 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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 #ifndef UNICODE
29 #define UNICODE
30 #endif
31 #include <vlc_common.h>
32
33 #ifdef __MINGW32__
34 # include <w32api.h>
35 #endif
36 #include <direct.h>
37 #include <shlobj.h>
38
39 #include "../libvlc.h"
40 #include <vlc_charset.h>
41 #include <vlc_configuration.h>
42 #include "config/configuration.h"
43
44 #include <assert.h>
45 #include <limits.h>
46
47 char *config_GetLibDir (void)
48 {
49 #if VLC_WINSTORE_APP
50     return NULL;
51 #else
52     /* Get our full path */
53     MEMORY_BASIC_INFORMATION mbi;
54     if (!VirtualQuery (config_GetLibDir, &mbi, sizeof(mbi)))
55         goto error;
56
57     wchar_t wpath[MAX_PATH];
58     if (!GetModuleFileName ((HMODULE) mbi.AllocationBase, wpath, MAX_PATH))
59         goto error;
60
61     wchar_t *file = wcsrchr (wpath, L'\\');
62     if (file == NULL)
63         goto error;
64     *file = L'\0';
65
66     return FromWide (wpath);
67 error:
68     abort ();
69 #endif
70 }
71
72 char *config_GetDataDir (void)
73 {
74     const char *path = getenv ("VLC_DATA_PATH");
75     return (path != NULL) ? strdup (path) : config_GetLibDir ();
76 }
77
78 static char *config_GetShellDir (int csidl)
79 {
80     wchar_t wdir[MAX_PATH];
81
82     if (SHGetFolderPathW (NULL, csidl | CSIDL_FLAG_CREATE,
83                           NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
84         return FromWide (wdir);
85     return NULL;
86 }
87
88 static char *config_GetAppDir (void)
89 {
90     char *psz_dir;
91     char *psz_parent = config_GetShellDir (CSIDL_APPDATA);
92
93     if (psz_parent == NULL
94      ||  asprintf (&psz_dir, "%s\\vlc", psz_parent) == -1)
95         psz_dir = NULL;
96     free (psz_parent);
97     return psz_dir;
98 }
99
100 #warning FIXME Use known folders on Vista and above
101 char *config_GetUserDir (vlc_userdir_t type)
102 {
103     switch (type)
104     {
105         case VLC_HOME_DIR:
106             return config_GetShellDir (CSIDL_PERSONAL);
107         case VLC_CONFIG_DIR:
108         case VLC_DATA_DIR:
109         case VLC_CACHE_DIR:
110             return config_GetAppDir ();
111
112         case VLC_DESKTOP_DIR:
113         case VLC_DOWNLOAD_DIR:
114         case VLC_TEMPLATES_DIR:
115         case VLC_PUBLICSHARE_DIR:
116         case VLC_DOCUMENTS_DIR:
117             return config_GetUserDir(VLC_HOME_DIR);
118         case VLC_MUSIC_DIR:
119             return config_GetShellDir (CSIDL_MYMUSIC);
120         case VLC_PICTURES_DIR:
121             return config_GetShellDir (CSIDL_MYPICTURES);
122         case VLC_VIDEOS_DIR:
123             return config_GetShellDir (CSIDL_MYVIDEO);
124     }
125     vlc_assert_unreachable ();
126 }