]> git.sesse.net Git - vlc/blob - src/config/core.c
update module LIST file.
[vlc] / src / config / core.c
1 /*****************************************************************************
2  * core.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
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/vlc.h>
29 #include "../libvlc.h"
30 #include "vlc_keys.h"
31 #include "vlc_charset.h"
32 #include "vlc_configuration.h"
33
34 #include <errno.h>                                                  /* errno */
35 #include <assert.h>
36 #include <limits.h>
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>                                          /* getuid() */
40 #endif
41
42 #if defined(HAVE_GETPWUID)
43 #   include <pwd.h>                                            /* getpwuid() */
44 #endif
45
46 #if defined( HAVE_SYS_STAT_H )
47 #   include <sys/stat.h>
48 #endif
49 #if defined( HAVE_SYS_TYPES_H )
50 #   include <sys/types.h>
51 #endif
52 #if defined( WIN32 )
53 #   if !defined( UNDER_CE )
54 #       include <direct.h>
55 #   endif
56 #include <tchar.h>
57 #endif
58
59 #include "configuration.h"
60 #include "modules/modules.h"
61
62 static inline char *strdupnull (const char *src)
63 {
64     return src ? strdup (src) : NULL;
65 }
66
67 /* Item types that use a string value (i.e. serialized in the module cache) */
68 int IsConfigStringType (int type)
69 {
70     static const unsigned char config_types[] =
71     {
72         CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
73         CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
74         CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT
75     };
76
77     /* NOTE: this needs to be changed if we ever get more than 255 types */
78     return memchr (config_types, type, sizeof (config_types)) != NULL;
79 }
80
81
82 int IsConfigIntegerType (int type)
83 {
84     static const unsigned char config_types[] =
85     {
86         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
87         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
88     };
89
90     return memchr (config_types, type, sizeof (config_types)) != NULL;
91 }
92
93
94 /*****************************************************************************
95  * config_GetType: get the type of a variable (bool, int, float, string)
96  *****************************************************************************
97  * This function is used to get the type of a variable from its name.
98  * Beware, this is quite slow.
99  *****************************************************************************/
100 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
101 {
102     module_config_t *p_config;
103     int i_type;
104
105     p_config = config_FindConfig( p_this, psz_name );
106
107     /* sanity checks */
108     if( !p_config )
109     {
110         return 0;
111     }
112
113     switch( p_config->i_type )
114     {
115     case CONFIG_ITEM_BOOL:
116         i_type = VLC_VAR_BOOL;
117         break;
118
119     case CONFIG_ITEM_INTEGER:
120     case CONFIG_ITEM_KEY:
121         i_type = VLC_VAR_INTEGER;
122         break;
123
124     case CONFIG_ITEM_FLOAT:
125         i_type = VLC_VAR_FLOAT;
126         break;
127
128     case CONFIG_ITEM_MODULE:
129     case CONFIG_ITEM_MODULE_CAT:
130     case CONFIG_ITEM_MODULE_LIST:
131     case CONFIG_ITEM_MODULE_LIST_CAT:
132         i_type = VLC_VAR_MODULE;
133         break;
134
135     case CONFIG_ITEM_STRING:
136         i_type = VLC_VAR_STRING;
137         break;
138
139     case CONFIG_ITEM_PASSWORD:
140         i_type = VLC_VAR_STRING;
141         break;
142
143     case CONFIG_ITEM_FILE:
144         i_type = VLC_VAR_FILE;
145         break;
146
147     case CONFIG_ITEM_DIRECTORY:
148         i_type = VLC_VAR_DIRECTORY;
149         break;
150
151     default:
152         i_type = 0;
153         break;
154     }
155
156     return i_type;
157 }
158
159 /*****************************************************************************
160  * config_GetInt: get the value of an int variable
161  *****************************************************************************
162  * This function is used to get the value of variables which are internally
163  * represented by an integer (CONFIG_ITEM_INTEGER and
164  * CONFIG_ITEM_BOOL).
165  *****************************************************************************/
166 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
167 {
168     module_config_t *p_config;
169
170     p_config = config_FindConfig( p_this, psz_name );
171
172     /* sanity checks */
173     if( !p_config )
174     {
175         msg_Err( p_this, "option %s does not exist", psz_name );
176         return -1;
177     }
178
179     if (!IsConfigIntegerType (p_config->i_type))
180     {
181         msg_Err( p_this, "option %s does not refer to an int", psz_name );
182         return -1;
183     }
184
185     return p_config->value.i;
186 }
187
188 /*****************************************************************************
189  * config_GetFloat: get the value of a float variable
190  *****************************************************************************
191  * This function is used to get the value of variables which are internally
192  * represented by a float (CONFIG_ITEM_FLOAT).
193  *****************************************************************************/
194 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
195 {
196     module_config_t *p_config;
197
198     p_config = config_FindConfig( p_this, psz_name );
199
200     /* sanity checks */
201     if( !p_config )
202     {
203         msg_Err( p_this, "option %s does not exist", psz_name );
204         return -1;
205     }
206
207     if (!IsConfigFloatType (p_config->i_type))
208     {
209         msg_Err( p_this, "option %s does not refer to a float", psz_name );
210         return -1;
211     }
212
213     return p_config->value.f;
214 }
215
216 /*****************************************************************************
217  * config_GetPsz: get the string value of a string variable
218  *****************************************************************************
219  * This function is used to get the value of variables which are internally
220  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
221  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
222  *
223  * Important note: remember to free() the returned char* because it's a
224  *   duplicate of the actual value. It isn't safe to return a pointer to the
225  *   actual value as it can be modified at any time.
226  *****************************************************************************/
227 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
228 {
229     module_config_t *p_config;
230
231     p_config = config_FindConfig( p_this, psz_name );
232
233     /* sanity checks */
234     if( !p_config )
235     {
236         msg_Err( p_this, "option %s does not exist", psz_name );
237         return NULL;
238     }
239
240     if (!IsConfigStringType (p_config->i_type))
241     {
242         msg_Err( p_this, "option %s does not refer to a string", psz_name );
243         return NULL;
244     }
245
246     /* return a copy of the string */
247     vlc_mutex_lock( p_config->p_lock );
248     char *psz_value = strdupnull (p_config->value.psz);
249     vlc_mutex_unlock( p_config->p_lock );
250
251     return psz_value;
252 }
253
254 /*****************************************************************************
255  * config_PutPsz: set the string value of a string variable
256  *****************************************************************************
257  * This function is used to set the value of variables which are internally
258  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
259  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
260  *****************************************************************************/
261 void __config_PutPsz( vlc_object_t *p_this,
262                       const char *psz_name, const char *psz_value )
263 {
264     module_config_t *p_config;
265     vlc_value_t oldval, val;
266
267     p_config = config_FindConfig( p_this, psz_name );
268
269
270     /* sanity checks */
271     if( !p_config )
272     {
273         msg_Warn( p_this, "option %s does not exist", psz_name );
274         return;
275     }
276
277     if (!IsConfigStringType (p_config->i_type))
278     {
279         msg_Err( p_this, "option %s does not refer to a string", psz_name );
280         return;
281     }
282
283     vlc_mutex_lock( p_config->p_lock );
284
285     /* backup old value */
286     oldval.psz_string = (char *)p_config->value.psz;
287
288     if ((psz_value != NULL) && *psz_value)
289         p_config->value.psz = strdup (psz_value);
290     else
291         p_config->value.psz = NULL;
292
293     p_config->b_dirty = VLC_TRUE;
294
295     val.psz_string = (char *)p_config->value.psz;
296
297     vlc_mutex_unlock( p_config->p_lock );
298
299     if( p_config->pf_callback )
300     {
301         p_config->pf_callback( p_this, psz_name, oldval, val,
302                                p_config->p_callback_data );
303     }
304
305     /* free old string */
306     free( oldval.psz_string );
307 }
308
309 /*****************************************************************************
310  * config_PutInt: set the integer value of an int variable
311  *****************************************************************************
312  * This function is used to set the value of variables which are internally
313  * represented by an integer (CONFIG_ITEM_INTEGER and
314  * CONFIG_ITEM_BOOL).
315  *****************************************************************************/
316 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
317 {
318     module_config_t *p_config;
319     vlc_value_t oldval, val;
320
321     p_config = config_FindConfig( p_this, psz_name );
322
323     /* sanity checks */
324     if( !p_config )
325     {
326         msg_Warn( p_this, "option %s does not exist", psz_name );
327         return;
328     }
329
330     if (!IsConfigIntegerType (p_config->i_type))
331     {
332         msg_Err( p_this, "option %s does not refer to an int", psz_name );
333         return;
334     }
335
336     /* backup old value */
337     oldval.i_int = p_config->value.i;
338
339     /* if i_min == i_max == 0, then do not use them */
340     if ((p_config->min.i == 0) && (p_config->max.i == 0))
341     {
342         p_config->value.i = i_value;
343     }
344     else if (i_value < p_config->min.i)
345     {
346         p_config->value.i = p_config->min.i;
347     }
348     else if (i_value > p_config->max.i)
349     {
350         p_config->value.i = p_config->max.i;
351     }
352     else
353     {
354         p_config->value.i = i_value;
355     }
356
357     p_config->b_dirty = VLC_TRUE;
358
359     val.i_int = p_config->value.i;
360
361     if( p_config->pf_callback )
362     {
363         p_config->pf_callback( p_this, psz_name, oldval, val,
364                                p_config->p_callback_data );
365     }
366 }
367
368 /*****************************************************************************
369  * config_PutFloat: set the value of a float variable
370  *****************************************************************************
371  * This function is used to set the value of variables which are internally
372  * represented by a float (CONFIG_ITEM_FLOAT).
373  *****************************************************************************/
374 void __config_PutFloat( vlc_object_t *p_this,
375                         const char *psz_name, float f_value )
376 {
377     module_config_t *p_config;
378     vlc_value_t oldval, val;
379
380     p_config = config_FindConfig( p_this, psz_name );
381
382     /* sanity checks */
383     if( !p_config )
384     {
385         msg_Warn( p_this, "option %s does not exist", psz_name );
386         return;
387     }
388
389     if (!IsConfigFloatType (p_config->i_type))
390     {
391         msg_Err( p_this, "option %s does not refer to a float", psz_name );
392         return;
393     }
394
395     /* backup old value */
396     oldval.f_float = p_config->value.f;
397
398     /* if f_min == f_max == 0, then do not use them */
399     if ((p_config->min.f == 0) && (p_config->max.f == 0))
400     {
401         p_config->value.f = f_value;
402     }
403     else if (f_value < p_config->min.f)
404     {
405         p_config->value.f = p_config->min.f;
406     }
407     else if (f_value > p_config->max.f)
408     {
409         p_config->value.f = p_config->max.f;
410     }
411     else
412     {
413         p_config->value.f = f_value;
414     }
415
416     p_config->b_dirty = VLC_TRUE;
417
418     val.f_float = p_config->value.f;
419
420     if( p_config->pf_callback )
421     {
422         p_config->pf_callback( p_this, psz_name, oldval, val,
423                                p_config->p_callback_data );
424     }
425 }
426
427 /*****************************************************************************
428  * config_FindConfig: find the config structure associated with an option.
429  *****************************************************************************
430  * FIXME: This function really needs to be optimized.
431  * FIXME: And now even more.
432  *****************************************************************************/
433 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
434 {
435     vlc_list_t *p_list;
436     int i_index;
437
438     if( !psz_name ) return NULL;
439
440     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
441
442     for( i_index = 0; i_index < p_list->i_count; i_index++ )
443     {
444         module_config_t *p_item, *p_end;
445         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
446
447         if( !p_parser->i_config_items )
448             continue;
449
450         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
451              p_item < p_end;
452              p_item++ )
453         {
454             if( p_item->i_type & CONFIG_HINT )
455                 /* ignore hints */
456                 continue;
457             if( !strcmp( psz_name, p_item->psz_name )
458              || ( p_item->psz_oldname
459               && !strcmp( psz_name, p_item->psz_oldname ) ) )
460             {
461                 vlc_list_release( p_list );
462                 return p_item;
463             }
464         }
465     }
466
467     vlc_list_release( p_list );
468
469     return NULL;
470 }
471
472 /*****************************************************************************
473  * config_Free: frees a duplicated module's configuration data.
474  *****************************************************************************
475  * This function frees all the data duplicated by config_Duplicate.
476  *****************************************************************************/
477 void config_Free( module_t *p_module )
478 {
479     int i;
480
481     for (size_t j = 0; j < p_module->confsize; j++)
482     {
483         module_config_t *p_item = p_module->p_config + j;
484
485         free( p_item->psz_type );
486         free( p_item->psz_name );
487         free( p_item->psz_text );
488         free( p_item->psz_longtext );
489         free( p_item->psz_oldname );
490
491         if (IsConfigStringType (p_item->i_type))
492         {
493             free (p_item->value.psz);
494             free (p_item->orig.psz);
495             free (p_item->saved.psz);
496         }
497
498         if( p_item->ppsz_list )
499             for( i = 0; i < p_item->i_list; i++ )
500                 free( p_item->ppsz_list[i] );
501         if( p_item->ppsz_list_text )
502             for( i = 0; i < p_item->i_list; i++ )
503                 free( p_item->ppsz_list_text[i] );
504         free( p_item->ppsz_list );
505         free( p_item->ppsz_list_text );
506         free( p_item->pi_list );
507
508         if( p_item->i_action )
509         {
510             for( i = 0; i < p_item->i_action; i++ )
511             {
512                 free( p_item->ppsz_action_text[i] );
513             }
514             free( p_item->ppf_action );
515             free( p_item->ppsz_action_text );
516         }
517     }
518
519     free (p_module->p_config);
520     p_module->p_config = NULL;
521 }
522
523 /*****************************************************************************
524  * config_SetCallbacks: sets callback functions in the duplicate p_config.
525  *****************************************************************************
526  * Unfortunatly we cannot work directly with the module's config data as
527  * this module might be unloaded from memory at any time (remember HideModule).
528  * This is why we need to duplicate callbacks each time we reload the module.
529  *****************************************************************************/
530 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
531                           size_t n )
532 {
533     for (size_t i = 0; i < n; i++)
534     {
535         p_new->pf_callback = p_orig->pf_callback;
536         p_new++;
537         p_orig++;
538     }
539 }
540
541 /*****************************************************************************
542  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
543  *****************************************************************************
544  * We simply undo what we did in config_SetCallbacks.
545  *****************************************************************************/
546 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
547 {
548     for (size_t i = 0; i < n; i++)
549     {
550         p_new->pf_callback = NULL;
551         p_new++;
552     }
553 }
554
555 /*****************************************************************************
556  * config_ResetAll: reset the configuration data for all the modules.
557  *****************************************************************************/
558 void __config_ResetAll( vlc_object_t *p_this )
559 {
560     int i_index;
561     vlc_list_t *p_list;
562     module_t *p_module;
563
564     /* Acquire config file lock */
565     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
566
567     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
568
569     for( i_index = 0; i_index < p_list->i_count; i_index++ )
570     {
571         p_module = (module_t *)p_list->p_values[i_index].p_object ;
572         if( p_module->b_submodule ) continue;
573
574         for (size_t i = 0; i < p_module->confsize; i++ )
575         {
576             if (IsConfigIntegerType (p_module->p_config[i].i_type))
577                 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
578             else
579             if (IsConfigFloatType (p_module->p_config[i].i_type))
580                 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
581             else
582             if (IsConfigStringType (p_module->p_config[i].i_type))
583             {
584                 free ((char *)p_module->p_config[i].value.psz);
585                 p_module->p_config[i].value.psz =
586                         strdupnull (p_module->p_config[i].orig.psz);
587             }
588         }
589     }
590
591     vlc_list_release( p_list );
592     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
593 }
594
595 /**
596  * config_GetDataDir: find directory where shared data is installed
597  *
598  * @return a string (always succeeds).
599  */
600 const char *config_GetDataDir( void )
601 {
602 #if defined (WIN32) || defined (UNDER_CE)
603     return vlc_global()->psz_vlcpath;
604 #elif defined(__APPLE__) || defined (SYS_BEOS)
605     static char path[PATH_MAX] = "";
606
607     if( *path == '\0' )
608     {
609         snprintf( path, sizeof( path ), "%s/share",
610                   vlc_global()->psz_vlcpath );
611         path[sizeof( path ) - 1] = '\0';
612     }
613     return path;
614 #else
615     return DATA_PATH;
616 #endif
617 }
618
619 /*****************************************************************************
620  * config_GetHomeDir, config_GetUserDir: find the user's home directory.
621  *****************************************************************************
622  * This function will try by different ways to find the user's home path.
623  * Note that this function is not reentrant, it should be called only once
624  * at the beginning of main where the result will be stored for later use.
625  *****************************************************************************/
626 static char *GetDir( vlc_bool_t b_appdata )
627 {
628     const char *psz_localhome = NULL;
629
630 #if defined(WIN32) && !defined(UNDER_CE)
631     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
632                                                LPWSTR );
633 #ifndef CSIDL_FLAG_CREATE
634 #   define CSIDL_FLAG_CREATE 0x8000
635 #endif
636 #ifndef CSIDL_APPDATA
637 #   define CSIDL_APPDATA 0x1A
638 #endif
639 #ifndef CSIDL_PROFILE
640 #   define CSIDL_PROFILE 0x28
641 #endif
642 #ifndef SHGFP_TYPE_CURRENT
643 #   define SHGFP_TYPE_CURRENT 0
644 #endif
645
646     HINSTANCE shfolder_dll;
647     SHGETFOLDERPATH SHGetFolderPath ;
648
649     /* load the shfolder dll to retrieve SHGetFolderPath */
650     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
651     {
652         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
653                                                   _T("SHGetFolderPathW") );
654         if ( SHGetFolderPath != NULL )
655         {
656             wchar_t whomedir[MAX_PATH];
657
658             /* get the "Application Data" folder for the current user */
659             if( S_OK == SHGetFolderPath( NULL,
660                                          (b_appdata ? CSIDL_APPDATA :
661                                            CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
662                                          NULL, SHGFP_TYPE_CURRENT,
663                                          whomedir ) )
664             {
665                 FreeLibrary( shfolder_dll );
666                 return FromWide( whomedir );
667             }
668         }
669         FreeLibrary( shfolder_dll );
670     }
671
672 #elif defined(UNDER_CE)
673
674 #ifndef CSIDL_APPDATA
675 #   define CSIDL_APPDATA 0x1A
676 #endif
677
678     wchar_t whomedir[MAX_PATH];
679
680     /* get the "Application Data" folder for the current user */
681     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
682         return FromWide( whomedir );
683 #endif
684
685     psz_localhome = getenv( "HOME" );
686     if( psz_localhome == NULL )
687     {
688 #if defined(HAVE_GETPWUID)
689         struct passwd *p_pw;
690         (void)b_appdata;
691
692         if( ( p_pw = getpwuid( getuid() ) ) != NULL )
693             psz_localhome = p_pw->pw_dir;
694         else
695 #endif
696         {
697             psz_localhome = getenv( "TMP" );
698             if( psz_localhome == NULL )
699                 psz_localhome = "/tmp";
700         }
701     }
702
703     return FromLocaleDup( psz_localhome );
704 }
705
706 /**
707  * Get the user's home directory
708  */
709 char *config_GetHomeDir( void )
710 {
711     return GetDir( VLC_FALSE );
712 }
713
714 /**
715  * Get the user's main data and config directory:
716  *   - on windows that's the App Data directory;
717  *   - on other OSes it's the same as the home directory.
718  */
719 char *config_GetUserDir( void ); /* XXX why does gcc wants a declaration ?
720                                   * --funman */
721 char *config_GetUserDir( void )
722 {
723     return GetDir( VLC_TRUE );
724 }
725
726 /**
727  * Get the user's VLC configuration directory
728  */
729 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
730 {
731     char *psz_dir;
732 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
733     char *psz_parent = config_GetUserDir();
734     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
735     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
736         return NULL;
737     return psz_dir;
738 #else
739     /* XDG Base Directory Specification - Version 0.6 */
740     char *psz_env = getenv( "XDG_CONFIG_HOME" );
741     if( psz_env )
742     {
743         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
744             return NULL;
745         return psz_dir;
746     }
747     psz_env = getenv( "HOME" );
748     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
749     if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
750         return NULL;
751     return psz_dir;
752 #endif
753 }
754
755 /**
756  * Get the user's VLC data directory
757  * (used for stuff like the skins, custom lua modules, ...)
758  */
759 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
760 {
761     char *psz_dir;
762 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
763     char *psz_parent = config_GetUserDir();
764     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
765     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
766         return NULL;
767     return psz_dir;
768 #else
769     /* XDG Base Directory Specification - Version 0.6 */
770     char *psz_env = getenv( "XDG_DATA_HOME" );
771     if( psz_env )
772     {
773         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
774             return NULL;
775         return psz_dir;
776     }
777     psz_env = getenv( "HOME" );
778     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
779     if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
780         return NULL;
781     return psz_dir;
782 #endif
783 }
784
785 /**
786  * Get the user's VLC cache directory
787  * (used for stuff like the modules cache, the album art cache, ...)
788  */
789 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
790 {
791     char *psz_dir;
792 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
793     char *psz_parent = config_GetUserDir();
794     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
795     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
796         return NULL;
797     return psz_dir;
798 #else
799     /* XDG Base Directory Specification - Version 0.6 */
800     char *psz_env = getenv( "XDG_CACHE_HOME" );
801     if( psz_env )
802     {
803         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
804             return NULL;
805         return psz_dir;
806     }
807     psz_env = getenv( "HOME" );
808     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
809     if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
810         return NULL;
811     return psz_dir;
812 #endif
813 }