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