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