1 /*****************************************************************************
2 * core.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 #include "../libvlc.h"
27 #include "vlc_charset.h"
29 #include <errno.h> /* errno */
36 # include <unistd.h> /* getuid() */
39 #ifdef HAVE_GETOPT_LONG
41 # include <getopt.h> /* getopt() */
44 # include "../extras/getopt.h"
47 #if defined(HAVE_GETPWUID)
48 # include <pwd.h> /* getpwuid() */
51 #if defined( HAVE_SYS_STAT_H )
52 # include <sys/stat.h>
54 #if defined( HAVE_SYS_TYPES_H )
55 # include <sys/types.h>
58 # if !defined( UNDER_CE )
65 #include "modules/modules.h"
67 static inline char *strdupnull (const char *src)
69 return src ? strdup (src) : NULL;
72 static inline char *_strdupnull (const char *src)
74 return src ? strdup (_(src)) : NULL;
77 /* Item types that use a string value (i.e. serialized in the module cache) */
78 int IsConfigStringType (int type)
80 static const unsigned char config_types[] =
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
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;
92 static int IsConfigIntegerType (int type)
94 static const unsigned char config_types[] =
96 CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
97 CONFIG_CATEGORY, CONFIG_SUBCATEGORY
100 return memchr (config_types, type, sizeof (config_types)) != NULL;
104 static inline int IsConfigFloatType (int type)
106 return type == CONFIG_ITEM_FLOAT;
110 /*****************************************************************************
111 * config_GetType: get the type of a variable (bool, int, float, string)
112 *****************************************************************************
113 * This function is used to get the type of a variable from its name.
114 * Beware, this is quite slow.
115 *****************************************************************************/
116 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
118 module_config_t *p_config;
121 p_config = config_FindConfig( p_this, psz_name );
129 switch( p_config->i_type )
131 case CONFIG_ITEM_BOOL:
132 i_type = VLC_VAR_BOOL;
135 case CONFIG_ITEM_INTEGER:
136 case CONFIG_ITEM_KEY:
137 i_type = VLC_VAR_INTEGER;
140 case CONFIG_ITEM_FLOAT:
141 i_type = VLC_VAR_FLOAT;
144 case CONFIG_ITEM_MODULE:
145 case CONFIG_ITEM_MODULE_CAT:
146 case CONFIG_ITEM_MODULE_LIST:
147 case CONFIG_ITEM_MODULE_LIST_CAT:
148 i_type = VLC_VAR_MODULE;
151 case CONFIG_ITEM_STRING:
152 i_type = VLC_VAR_STRING;
155 case CONFIG_ITEM_PASSWORD:
156 i_type = VLC_VAR_STRING;
159 case CONFIG_ITEM_FILE:
160 i_type = VLC_VAR_FILE;
163 case CONFIG_ITEM_DIRECTORY:
164 i_type = VLC_VAR_DIRECTORY;
175 /*****************************************************************************
176 * config_GetInt: get the value of an int variable
177 *****************************************************************************
178 * This function is used to get the value of variables which are internally
179 * represented by an integer (CONFIG_ITEM_INTEGER and
181 *****************************************************************************/
182 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
184 module_config_t *p_config;
186 p_config = config_FindConfig( p_this, psz_name );
191 msg_Err( p_this, "option %s does not exist", psz_name );
195 if (!IsConfigIntegerType (p_config->i_type))
197 msg_Err( p_this, "option %s does not refer to an int", psz_name );
201 return p_config->value.i;
204 /*****************************************************************************
205 * config_GetFloat: get the value of a float variable
206 *****************************************************************************
207 * This function is used to get the value of variables which are internally
208 * represented by a float (CONFIG_ITEM_FLOAT).
209 *****************************************************************************/
210 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
212 module_config_t *p_config;
214 p_config = config_FindConfig( p_this, psz_name );
219 msg_Err( p_this, "option %s does not exist", psz_name );
223 if (!IsConfigFloatType (p_config->i_type))
225 msg_Err( p_this, "option %s does not refer to a float", psz_name );
229 return p_config->value.f;
232 /*****************************************************************************
233 * config_GetPsz: get the string value of a string variable
234 *****************************************************************************
235 * This function is used to get the value of variables which are internally
236 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
237 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
239 * Important note: remember to free() the returned char* because it's a
240 * duplicate of the actual value. It isn't safe to return a pointer to the
241 * actual value as it can be modified at any time.
242 *****************************************************************************/
243 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
245 module_config_t *p_config;
247 p_config = config_FindConfig( p_this, psz_name );
252 msg_Err( p_this, "option %s does not exist", psz_name );
256 if (!IsConfigStringType (p_config->i_type))
258 msg_Err( p_this, "option %s does not refer to a string", psz_name );
262 /* return a copy of the string */
263 vlc_mutex_lock( p_config->p_lock );
264 char *psz_value = strdupnull (p_config->value.psz);
265 vlc_mutex_unlock( p_config->p_lock );
270 /*****************************************************************************
271 * config_PutPsz: set the string value of a string variable
272 *****************************************************************************
273 * This function is used to set the value of variables which are internally
274 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
275 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
276 *****************************************************************************/
277 void __config_PutPsz( vlc_object_t *p_this,
278 const char *psz_name, const char *psz_value )
280 module_config_t *p_config;
281 vlc_value_t oldval, val;
283 p_config = config_FindConfig( p_this, psz_name );
289 msg_Warn( p_this, "option %s does not exist", psz_name );
293 if (!IsConfigStringType (p_config->i_type))
295 msg_Err( p_this, "option %s does not refer to a string", psz_name );
299 vlc_mutex_lock( p_config->p_lock );
301 /* backup old value */
302 oldval.psz_string = (char *)p_config->value.psz;
304 if ((psz_value != NULL) && *psz_value)
305 p_config->value.psz = strdup (psz_value);
307 p_config->value.psz = NULL;
309 p_config->b_dirty = VLC_TRUE;
311 val.psz_string = (char *)p_config->value.psz;
313 vlc_mutex_unlock( p_config->p_lock );
315 if( p_config->pf_callback )
317 p_config->pf_callback( p_this, psz_name, oldval, val,
318 p_config->p_callback_data );
321 /* free old string */
322 if( oldval.psz_string ) free( oldval.psz_string );
325 /*****************************************************************************
326 * config_PutInt: set the integer value of an int variable
327 *****************************************************************************
328 * This function is used to set the value of variables which are internally
329 * represented by an integer (CONFIG_ITEM_INTEGER and
331 *****************************************************************************/
332 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
334 module_config_t *p_config;
335 vlc_value_t oldval, val;
337 p_config = config_FindConfig( p_this, psz_name );
342 msg_Warn( p_this, "option %s does not exist", psz_name );
346 if (!IsConfigIntegerType (p_config->i_type))
348 msg_Err( p_this, "option %s does not refer to an int", psz_name );
352 /* backup old value */
353 oldval.i_int = p_config->value.i;
355 /* if i_min == i_max == 0, then do not use them */
356 if ((p_config->min.i == 0) && (p_config->max.i == 0))
358 p_config->value.i = i_value;
360 else if (i_value < p_config->min.i)
362 p_config->value.i = p_config->min.i;
364 else if (i_value > p_config->max.i)
366 p_config->value.i = p_config->max.i;
370 p_config->value.i = i_value;
373 p_config->b_dirty = VLC_TRUE;
375 val.i_int = p_config->value.i;
377 if( p_config->pf_callback )
379 p_config->pf_callback( p_this, psz_name, oldval, val,
380 p_config->p_callback_data );
384 /*****************************************************************************
385 * config_PutFloat: set the value of a float variable
386 *****************************************************************************
387 * This function is used to set the value of variables which are internally
388 * represented by a float (CONFIG_ITEM_FLOAT).
389 *****************************************************************************/
390 void __config_PutFloat( vlc_object_t *p_this,
391 const char *psz_name, float f_value )
393 module_config_t *p_config;
394 vlc_value_t oldval, val;
396 p_config = config_FindConfig( p_this, psz_name );
401 msg_Warn( p_this, "option %s does not exist", psz_name );
405 if (!IsConfigFloatType (p_config->i_type))
407 msg_Err( p_this, "option %s does not refer to a float", psz_name );
411 /* backup old value */
412 oldval.f_float = p_config->value.f;
414 /* if f_min == f_max == 0, then do not use them */
415 if ((p_config->min.f == 0) && (p_config->max.f == 0))
417 p_config->value.f = f_value;
419 else if (f_value < p_config->min.f)
421 p_config->value.f = p_config->min.f;
423 else if (f_value > p_config->max.f)
425 p_config->value.f = p_config->max.f;
429 p_config->value.f = f_value;
432 p_config->b_dirty = VLC_TRUE;
434 val.f_float = p_config->value.f;
436 if( p_config->pf_callback )
438 p_config->pf_callback( p_this, psz_name, oldval, val,
439 p_config->p_callback_data );
443 /*****************************************************************************
444 * config_FindConfig: find the config structure associated with an option.
445 *****************************************************************************
446 * FIXME: This function really needs to be optimized.
447 * FIXME: And now even more.
448 *****************************************************************************/
449 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
454 if( !psz_name ) return NULL;
456 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
458 for( i_index = 0; i_index < p_list->i_count; i_index++ )
460 module_config_t *p_item, *p_end;
461 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
463 if( !p_parser->i_config_items )
466 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
470 if( p_item->i_type & CONFIG_HINT )
473 if( !strcmp( psz_name, p_item->psz_name ) )
475 vlc_list_release( p_list );
481 vlc_list_release( p_list );
486 /*****************************************************************************
487 * config_Duplicate: creates a duplicate of a module's configuration data.
488 *****************************************************************************
489 * Unfortunatly we cannot work directly with the module's config data as
490 * this module might be unloaded from memory at any time (remember HideModule).
491 * This is why we need to create an exact copy of the config data.
492 *****************************************************************************/
493 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
497 const module_config_t *p_item, *p_end = p_orig + n;
499 /* Calculate the structure length */
500 p_module->i_config_items = 0;
501 p_module->i_bool_items = 0;
503 for( p_item = p_orig; p_item < p_end; p_item++ )
505 if( p_item->i_type & CONFIG_ITEM )
507 p_module->i_config_items++;
510 if( p_item->i_type == CONFIG_ITEM_BOOL )
512 p_module->i_bool_items++;
516 /* Allocate memory */
517 p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
518 if( p_module->p_config == NULL )
520 msg_Err( p_module, "config error: can't duplicate p_config" );
523 p_module->confsize = n;
525 /* Do the duplication job */
526 for( size_t i = 0; i < n ; i++ )
528 p_module->p_config[i] = p_orig[i];
530 if (IsConfigIntegerType (p_module->p_config[i].i_type))
532 p_module->p_config[i].orig.i = p_orig[i].value.i;
533 p_module->p_config[i].saved.i = p_orig[i].value.i;
536 if (IsConfigFloatType (p_module->p_config[i].i_type))
538 p_module->p_config[i].orig.f = p_orig[i].value.f;
539 p_module->p_config[i].saved.f = p_orig[i].value.f;
542 if (IsConfigStringType (p_module->p_config[i].i_type))
544 p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
545 p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
546 p_module->p_config[i].saved.psz = NULL;
549 p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
550 p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
551 p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
552 p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
553 p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
555 p_module->p_config[i].p_lock = &p_module->object_lock;
557 /* duplicate the string list */
558 if( p_orig[i].i_list )
560 if( p_orig[i].ppsz_list )
562 p_module->p_config[i].ppsz_list =
563 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
564 if( p_module->p_config[i].ppsz_list )
566 for( j = 0; j < p_orig[i].i_list; j++ )
567 p_module->p_config[i].ppsz_list[j] =
568 strdupnull (p_orig[i].ppsz_list[j]);
569 p_module->p_config[i].ppsz_list[j] = NULL;
572 if( p_orig[i].ppsz_list_text )
574 p_module->p_config[i].ppsz_list_text =
575 calloc( (p_orig[i].i_list + 1), sizeof(char *) );
576 if( p_module->p_config[i].ppsz_list_text )
578 for( j = 0; j < p_orig[i].i_list; j++ )
579 p_module->p_config[i].ppsz_list_text[j] =
580 strdupnull (_(p_orig[i].ppsz_list_text[j]));
581 p_module->p_config[i].ppsz_list_text[j] = NULL;
584 if( p_orig[i].pi_list )
586 p_module->p_config[i].pi_list =
587 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
588 if( p_module->p_config[i].pi_list )
590 for( j = 0; j < p_orig[i].i_list; j++ )
591 p_module->p_config[i].pi_list[j] =
592 p_orig[i].pi_list[j];
597 /* duplicate the actions list */
598 if( p_orig[i].i_action )
602 p_module->p_config[i].ppf_action =
603 malloc( p_orig[i].i_action * sizeof(void *) );
604 p_module->p_config[i].ppsz_action_text =
605 malloc( p_orig[i].i_action * sizeof(char *) );
607 for( j = 0; j < p_orig[i].i_action; j++ )
609 p_module->p_config[i].ppf_action[j] =
610 p_orig[i].ppf_action[j];
611 p_module->p_config[i].ppsz_action_text[j] =
612 strdupnull (p_orig[i].ppsz_action_text[j]);
616 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
622 /*****************************************************************************
623 * config_Free: frees a duplicated module's configuration data.
624 *****************************************************************************
625 * This function frees all the data duplicated by config_Duplicate.
626 *****************************************************************************/
627 void config_Free( module_t *p_module )
631 for (size_t j = 0; j < p_module->confsize; j++)
633 module_config_t *p_item = p_module->p_config + j;
635 free( (char*) p_item->psz_type );
636 free( (char*) p_item->psz_name );
637 free( (char*) p_item->psz_current );
638 free( (char*) p_item->psz_text );
639 free( (char*) p_item->psz_longtext );
641 if (IsConfigStringType (p_item->i_type))
643 free ((char *)p_item->value.psz);
644 free ((char *)p_item->orig.psz);
645 free ((char *)p_item->saved.psz);
650 for( i = 0; i < p_item->i_list; i++ )
652 if( p_item->ppsz_list && p_item->ppsz_list[i] )
653 free( (char*) p_item->ppsz_list[i] );
654 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
655 free( (char*) p_item->ppsz_list_text[i] );
657 if( p_item->ppsz_list ) free( p_item->ppsz_list );
658 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
659 if( p_item->pi_list ) free( p_item->pi_list );
662 if( p_item->i_action )
664 for( i = 0; i < p_item->i_action; i++ )
666 free( (char*) p_item->ppsz_action_text[i] );
668 if( p_item->ppf_action ) free( p_item->ppf_action );
669 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
673 if (p_module->p_config != NULL)
675 free (p_module->p_config);
676 p_module->p_config = NULL;
680 /*****************************************************************************
681 * config_SetCallbacks: sets callback functions in the duplicate p_config.
682 *****************************************************************************
683 * Unfortunatly we cannot work directly with the module's config data as
684 * this module might be unloaded from memory at any time (remember HideModule).
685 * This is why we need to duplicate callbacks each time we reload the module.
686 *****************************************************************************/
687 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
690 for (size_t i = 0; i < n; i++)
692 p_new->pf_callback = p_orig->pf_callback;
698 /*****************************************************************************
699 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
700 *****************************************************************************
701 * We simply undo what we did in config_SetCallbacks.
702 *****************************************************************************/
703 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
705 for (size_t i = 0; i < n; i++)
707 p_new->pf_callback = NULL;
712 /*****************************************************************************
713 * config_ResetAll: reset the configuration data for all the modules.
714 *****************************************************************************/
715 void __config_ResetAll( vlc_object_t *p_this )
721 /* Acquire config file lock */
722 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
724 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
726 for( i_index = 0; i_index < p_list->i_count; i_index++ )
728 p_module = (module_t *)p_list->p_values[i_index].p_object ;
729 if( p_module->b_submodule ) continue;
731 for (size_t i = 0; i < p_module->confsize; i++ )
733 if (IsConfigIntegerType (p_module->p_config[i].i_type))
734 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
736 if (IsConfigFloatType (p_module->p_config[i].i_type))
737 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
739 if (IsConfigStringType (p_module->p_config[i].i_type))
741 free ((char *)p_module->p_config[i].value.psz);
742 p_module->p_config[i].value.psz =
743 strdupnull (p_module->p_config[i].orig.psz);
748 vlc_list_release( p_list );
749 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
753 * config_GetDataDir: find directory where shared data is installed
755 * @return a string (always succeeds).
757 const char *config_GetDataDir( void )
759 #if defined (WIN32) || defined (UNDER_CE)
760 return vlc_global()->psz_vlcpath;
761 #elif defined(__APPLE__) || defined (SYS_BEOS)
762 static char path[PATH_MAX] = "";
766 snprintf( path, sizeof( path ), "%s/share",
767 vlc_global()->psz_vlcpath );
768 path[sizeof( path ) - 1] = '\0';
776 /*****************************************************************************
777 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
778 *****************************************************************************
779 * This function will try by different ways to find the user's home path.
780 * Note that this function is not reentrant, it should be called only once
781 * at the beginning of main where the result will be stored for later use.
782 *****************************************************************************/
783 static char *GetDir( vlc_bool_t b_appdata )
785 const char *psz_localhome = NULL;
787 #if defined(WIN32) && !defined(UNDER_CE)
788 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
790 #ifndef CSIDL_FLAG_CREATE
791 # define CSIDL_FLAG_CREATE 0x8000
793 #ifndef CSIDL_APPDATA
794 # define CSIDL_APPDATA 0x1A
796 #ifndef CSIDL_PROFILE
797 # define CSIDL_PROFILE 0x28
799 #ifndef SHGFP_TYPE_CURRENT
800 # define SHGFP_TYPE_CURRENT 0
803 HINSTANCE shfolder_dll;
804 SHGETFOLDERPATH SHGetFolderPath ;
806 /* load the shfolder dll to retrieve SHGetFolderPath */
807 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
809 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
810 _T("SHGetFolderPathW") );
811 if ( SHGetFolderPath != NULL )
813 wchar_t whomedir[MAX_PATH];
815 /* get the "Application Data" folder for the current user */
816 if( S_OK == SHGetFolderPath( NULL,
817 (b_appdata ? CSIDL_APPDATA :
818 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
819 NULL, SHGFP_TYPE_CURRENT,
822 FreeLibrary( shfolder_dll );
823 return FromWide( whomedir );
826 FreeLibrary( shfolder_dll );
829 #elif defined(UNDER_CE)
831 #ifndef CSIDL_APPDATA
832 # define CSIDL_APPDATA 0x1A
835 wchar_t whomedir[MAX_PATH];
837 /* get the "Application Data" folder for the current user */
838 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
839 return FromWide( whomedir );
842 psz_localhome = getenv( "HOME" );
843 if( psz_localhome == NULL )
845 #if defined(HAVE_GETPWUID)
849 if( ( p_pw = getpwuid( getuid() ) ) != NULL )
850 psz_localhome = p_pw->pw_dir;
854 psz_localhome = getenv( "TMP" );
855 if( psz_localhome == NULL )
856 psz_localhome = "/tmp";
860 return FromLocaleDup( psz_localhome );
864 * Get the user's home directory
866 char *config_GetHomeDir( void )
868 return GetDir( VLC_FALSE );
872 * Get the user's main data and config directory:
873 * - on windows that's the App Data directory;
874 * - on other OSes it's the same as the home directory.
876 char *config_GetUserDir( void );
877 char *config_GetUserDir( void )
879 return GetDir( VLC_TRUE );
883 * Get the user's VLC configuration directory
885 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
888 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
889 char *psz_parent = config_GetUserDir();
890 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
891 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
895 /* XDG Base Directory Specification - Version 0.6 */
896 char *psz_env = getenv( "XDG_CONFIG_HOME" );
899 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
903 psz_env = getenv( "HOME" );
904 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
905 if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
912 * Get the user's VLC data directory
913 * (used for stuff like the skins, custom lua modules, ...)
915 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
918 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
919 char *psz_parent = config_GetUserDir();
920 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
921 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
925 /* XDG Base Directory Specification - Version 0.6 */
926 char *psz_env = getenv( "XDG_DATA_HOME" );
929 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
933 psz_env = getenv( "HOME" );
934 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
935 if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
942 * Get the user's VLC cache directory
943 * (used for stuff like the modules cache, the album art cache, ...)
945 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
948 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
949 char *psz_parent = config_GetUserDir();
950 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
951 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
955 /* XDG Base Directory Specification - Version 0.6 */
956 char *psz_env = getenv( "XDG_CACHE_HOME" );
959 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
963 psz_env = getenv( "HOME" );
964 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
965 if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
971 /* Adds an extra interface to the configuration */
972 void __config_AddIntf( vlc_object_t *p_this, const char *psz_intf )
976 char *psz_config, *psz_parser;
977 size_t i_len = strlen( psz_intf );
979 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
982 if( !strncmp( psz_intf, psz_parser, i_len ) )
987 psz_parser = strchr( psz_parser, ':' );
988 if( psz_parser ) psz_parser++; /* skip the ':' */
992 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
995 if( !strncmp( psz_intf, psz_parser, i_len ) )
1000 psz_parser = strchr( psz_parser, ':' );
1001 if( psz_parser ) psz_parser++; /* skip the ':' */
1004 /* interface not found in the config, let's add it */
1005 if( psz_config && strlen( psz_config ) > 0 )
1007 char *psz_newconfig;
1008 if( asprintf( &psz_newconfig, "%s:%s", psz_config, psz_intf ) != -1 )
1010 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1011 free( psz_newconfig );
1015 config_PutPsz( p_this->p_libvlc, "extraintf", psz_intf );
1020 /* Removes an extra interface from the configuration */
1021 void __config_RemoveIntf( vlc_object_t *p_this, const char *psz_intf )
1025 char *psz_config, *psz_parser;
1026 size_t i_len = strlen( psz_intf );
1028 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1031 if( !strncmp( psz_intf, psz_parser, i_len ) )
1033 char *psz_newconfig;
1034 char *psz_end = psz_parser + i_len;
1035 if( *psz_end == ':' ) psz_end++;
1037 if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1039 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1040 free( psz_newconfig );
1044 psz_parser = strchr( psz_parser, ':' );
1045 if( psz_parser ) psz_parser++; /* skip the ':' */
1049 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1052 if( !strncmp( psz_intf, psz_parser, i_len ) )
1054 char *psz_newconfig;
1055 char *psz_end = psz_parser + i_len;
1056 if( *psz_end == ':' ) psz_end++;
1058 if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1060 config_PutPsz( p_this->p_libvlc, "control", psz_newconfig );
1061 free( psz_newconfig );
1065 psz_parser = strchr( psz_parser, ':' );
1066 if( psz_parser ) psz_parser++; /* skip the ':' */
1072 * Returns VLC_TRUE if the specified extra interface is present in the
1073 * configuration, VLC_FALSE if not
1075 vlc_bool_t __config_ExistIntf( vlc_object_t *p_this, const char *psz_intf )
1079 char *psz_config, *psz_parser;
1080 size_t i_len = strlen( psz_intf );
1082 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1085 if( !strncmp( psz_parser, psz_intf, i_len ) )
1090 psz_parser = strchr( psz_parser, ':' );
1091 if( psz_parser ) psz_parser++; /* skip the ':' */
1095 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1098 if( !strncmp( psz_parser, psz_intf, i_len ) )
1103 psz_parser = strchr( psz_parser, ':' );
1104 if( psz_parser ) psz_parser++; /* skip the ':' */