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_FindModule: find a specific module structure.
488 *****************************************************************************/
489 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
492 module_t *p_module, *p_result = NULL;
495 if( !psz_name ) return NULL;
497 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
499 for( i_index = 0; i_index < p_list->i_count; i_index++ )
501 p_module = (module_t *)p_list->p_values[i_index].p_object;
502 if( !strcmp( p_module->psz_object_name, psz_name ) )
509 vlc_list_release( p_list );
514 /*****************************************************************************
515 * config_Duplicate: creates a duplicate of a module's configuration data.
516 *****************************************************************************
517 * Unfortunatly we cannot work directly with the module's config data as
518 * this module might be unloaded from memory at any time (remember HideModule).
519 * This is why we need to create an exact copy of the config data.
520 *****************************************************************************/
521 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
525 const module_config_t *p_item, *p_end = p_orig + n;
527 /* Calculate the structure length */
528 p_module->i_config_items = 0;
529 p_module->i_bool_items = 0;
531 for( p_item = p_orig; p_item < p_end; p_item++ )
533 if( p_item->i_type & CONFIG_ITEM )
535 p_module->i_config_items++;
538 if( p_item->i_type == CONFIG_ITEM_BOOL )
540 p_module->i_bool_items++;
544 /* Allocate memory */
545 p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
546 if( p_module->p_config == NULL )
548 msg_Err( p_module, "config error: can't duplicate p_config" );
551 p_module->confsize = n;
553 /* Do the duplication job */
554 for( size_t i = 0; i < n ; i++ )
556 p_module->p_config[i] = p_orig[i];
558 if (IsConfigIntegerType (p_module->p_config[i].i_type))
560 p_module->p_config[i].orig.i = p_orig[i].value.i;
561 p_module->p_config[i].saved.i = p_orig[i].value.i;
564 if (IsConfigFloatType (p_module->p_config[i].i_type))
566 p_module->p_config[i].orig.f = p_orig[i].value.f;
567 p_module->p_config[i].saved.f = p_orig[i].value.f;
570 if (IsConfigStringType (p_module->p_config[i].i_type))
572 p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
573 p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
574 p_module->p_config[i].saved.psz = NULL;
577 p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
578 p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
579 p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
580 p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
581 p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
583 p_module->p_config[i].p_lock = &p_module->object_lock;
585 /* duplicate the string list */
586 if( p_orig[i].i_list )
588 if( p_orig[i].ppsz_list )
590 p_module->p_config[i].ppsz_list =
591 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
592 if( p_module->p_config[i].ppsz_list )
594 for( j = 0; j < p_orig[i].i_list; j++ )
595 p_module->p_config[i].ppsz_list[j] =
596 strdupnull (p_orig[i].ppsz_list[j]);
597 p_module->p_config[i].ppsz_list[j] = NULL;
600 if( p_orig[i].ppsz_list_text )
602 p_module->p_config[i].ppsz_list_text =
603 calloc( (p_orig[i].i_list + 1), sizeof(char *) );
604 if( p_module->p_config[i].ppsz_list_text )
606 for( j = 0; j < p_orig[i].i_list; j++ )
607 p_module->p_config[i].ppsz_list_text[j] =
608 strdupnull (_(p_orig[i].ppsz_list_text[j]));
609 p_module->p_config[i].ppsz_list_text[j] = NULL;
612 if( p_orig[i].pi_list )
614 p_module->p_config[i].pi_list =
615 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
616 if( p_module->p_config[i].pi_list )
618 for( j = 0; j < p_orig[i].i_list; j++ )
619 p_module->p_config[i].pi_list[j] =
620 p_orig[i].pi_list[j];
625 /* duplicate the actions list */
626 if( p_orig[i].i_action )
630 p_module->p_config[i].ppf_action =
631 malloc( p_orig[i].i_action * sizeof(void *) );
632 p_module->p_config[i].ppsz_action_text =
633 malloc( p_orig[i].i_action * sizeof(char *) );
635 for( j = 0; j < p_orig[i].i_action; j++ )
637 p_module->p_config[i].ppf_action[j] =
638 p_orig[i].ppf_action[j];
639 p_module->p_config[i].ppsz_action_text[j] =
640 strdupnull (p_orig[i].ppsz_action_text[j]);
644 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
650 /*****************************************************************************
651 * config_Free: frees a duplicated module's configuration data.
652 *****************************************************************************
653 * This function frees all the data duplicated by config_Duplicate.
654 *****************************************************************************/
655 void config_Free( module_t *p_module )
659 for (size_t j = 0; j < p_module->confsize; j++)
661 module_config_t *p_item = p_module->p_config + j;
663 free( (char*) p_item->psz_type );
664 free( (char*) p_item->psz_name );
665 free( (char*) p_item->psz_current );
666 free( (char*) p_item->psz_text );
667 free( (char*) p_item->psz_longtext );
669 if (IsConfigStringType (p_item->i_type))
671 free ((char *)p_item->value.psz);
672 free ((char *)p_item->orig.psz);
673 free ((char *)p_item->saved.psz);
678 for( i = 0; i < p_item->i_list; i++ )
680 if( p_item->ppsz_list && p_item->ppsz_list[i] )
681 free( (char*) p_item->ppsz_list[i] );
682 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
683 free( (char*) p_item->ppsz_list_text[i] );
685 if( p_item->ppsz_list ) free( p_item->ppsz_list );
686 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
687 if( p_item->pi_list ) free( p_item->pi_list );
690 if( p_item->i_action )
692 for( i = 0; i < p_item->i_action; i++ )
694 free( (char*) p_item->ppsz_action_text[i] );
696 if( p_item->ppf_action ) free( p_item->ppf_action );
697 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
701 if (p_module->p_config != NULL)
703 free (p_module->p_config);
704 p_module->p_config = NULL;
708 /*****************************************************************************
709 * config_SetCallbacks: sets callback functions in the duplicate p_config.
710 *****************************************************************************
711 * Unfortunatly we cannot work directly with the module's config data as
712 * this module might be unloaded from memory at any time (remember HideModule).
713 * This is why we need to duplicate callbacks each time we reload the module.
714 *****************************************************************************/
715 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
718 for (size_t i = 0; i < n; i++)
720 p_new->pf_callback = p_orig->pf_callback;
726 /*****************************************************************************
727 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
728 *****************************************************************************
729 * We simply undo what we did in config_SetCallbacks.
730 *****************************************************************************/
731 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
733 for (size_t i = 0; i < n; i++)
735 p_new->pf_callback = NULL;
740 /*****************************************************************************
741 * config_ResetAll: reset the configuration data for all the modules.
742 *****************************************************************************/
743 void __config_ResetAll( vlc_object_t *p_this )
749 /* Acquire config file lock */
750 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
752 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
754 for( i_index = 0; i_index < p_list->i_count; i_index++ )
756 p_module = (module_t *)p_list->p_values[i_index].p_object ;
757 if( p_module->b_submodule ) continue;
759 for (size_t i = 0; i < p_module->confsize; i++ )
761 if (IsConfigIntegerType (p_module->p_config[i].i_type))
762 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
764 if (IsConfigFloatType (p_module->p_config[i].i_type))
765 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
767 if (IsConfigStringType (p_module->p_config[i].i_type))
769 free ((char *)p_module->p_config[i].value.psz);
770 p_module->p_config[i].value.psz =
771 strdupnull (p_module->p_config[i].orig.psz);
776 vlc_list_release( p_list );
777 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
781 * config_GetDataDir: find directory where shared data is installed
783 * @return a string (always succeeds).
785 const char *config_GetDataDir( void )
787 #if defined (WIN32) || defined (UNDER_CE)
788 return vlc_global()->psz_vlcpath;
789 #elif defined(__APPLE__) || defined (SYS_BEOS)
790 static char path[PATH_MAX] = "";
794 snprintf( path, sizeof( path ), "%s/share",
795 vlc_global()->psz_vlcpath );
796 path[sizeof( path ) - 1] = '\0';
804 /*****************************************************************************
805 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
806 *****************************************************************************
807 * This function will try by different ways to find the user's home path.
808 * Note that this function is not reentrant, it should be called only once
809 * at the beginning of main where the result will be stored for later use.
810 *****************************************************************************/
811 static char *GetDir( vlc_bool_t b_appdata )
813 const char *psz_localhome = NULL;
815 #if defined(WIN32) && !defined(UNDER_CE)
816 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
818 #ifndef CSIDL_FLAG_CREATE
819 # define CSIDL_FLAG_CREATE 0x8000
821 #ifndef CSIDL_APPDATA
822 # define CSIDL_APPDATA 0x1A
824 #ifndef CSIDL_PROFILE
825 # define CSIDL_PROFILE 0x28
827 #ifndef SHGFP_TYPE_CURRENT
828 # define SHGFP_TYPE_CURRENT 0
831 HINSTANCE shfolder_dll;
832 SHGETFOLDERPATH SHGetFolderPath ;
834 /* load the shfolder dll to retrieve SHGetFolderPath */
835 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
837 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
838 _T("SHGetFolderPathW") );
839 if ( SHGetFolderPath != NULL )
841 wchar_t whomedir[MAX_PATH];
843 /* get the "Application Data" folder for the current user */
844 if( S_OK == SHGetFolderPath( NULL,
845 (b_appdata ? CSIDL_APPDATA :
846 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
847 NULL, SHGFP_TYPE_CURRENT,
850 FreeLibrary( shfolder_dll );
851 return FromWide( whomedir );
854 FreeLibrary( shfolder_dll );
857 #elif defined(UNDER_CE)
859 #ifndef CSIDL_APPDATA
860 # define CSIDL_APPDATA 0x1A
863 wchar_t whomedir[MAX_PATH];
865 /* get the "Application Data" folder for the current user */
866 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
867 return FromWide( whomedir );
870 psz_localhome = getenv( "HOME" );
871 if( psz_localhome == NULL )
873 #if defined(HAVE_GETPWUID)
877 if( ( p_pw = getpwuid( getuid() ) ) != NULL )
878 psz_localhome = p_pw->pw_dir;
882 psz_localhome = getenv( "TMP" );
883 if( psz_localhome == NULL )
884 psz_localhome = "/tmp";
888 return FromLocaleDup( psz_localhome );
892 * Get the user's home directory
894 char *config_GetHomeDir( void )
896 return GetDir( VLC_FALSE );
900 * Get the user's main data and config directory:
901 * - on windows that's the App Data directory;
902 * - on other OSes it's the same as the home directory.
904 char *config_GetUserDir( void );
905 char *config_GetUserDir( void )
907 return GetDir( VLC_TRUE );
911 * Get the user's VLC configuration directory
913 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
916 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
917 char *psz_parent = config_GetUserDir();
918 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
919 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
923 /* XDG Base Directory Specification - Version 0.6 */
924 char *psz_env = getenv( "XDG_CONFIG_HOME" );
927 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
931 psz_env = getenv( "HOME" );
932 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
933 if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
940 * Get the user's VLC data directory
941 * (used for stuff like the skins, custom lua modules, ...)
943 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
946 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
947 char *psz_parent = config_GetUserDir();
948 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
949 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
953 /* XDG Base Directory Specification - Version 0.6 */
954 char *psz_env = getenv( "XDG_DATA_HOME" );
957 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
961 psz_env = getenv( "HOME" );
962 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
963 if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
970 * Get the user's VLC cache directory
971 * (used for stuff like the modules cache, the album art cache, ...)
973 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
976 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
977 char *psz_parent = config_GetUserDir();
978 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
979 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
983 /* XDG Base Directory Specification - Version 0.6 */
984 char *psz_env = getenv( "XDG_CACHE_HOME" );
987 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
991 psz_env = getenv( "HOME" );
992 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
993 if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
999 /* Adds an extra interface to the configuration */
1000 void __config_AddIntf( vlc_object_t *p_this, const char *psz_intf )
1004 char *psz_config, *psz_parser;
1005 size_t i_len = strlen( psz_intf );
1007 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1010 if( !strncmp( psz_intf, psz_parser, i_len ) )
1015 psz_parser = strchr( psz_parser, ':' );
1016 if( psz_parser ) psz_parser++; /* skip the ':' */
1020 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1023 if( !strncmp( psz_intf, psz_parser, i_len ) )
1028 psz_parser = strchr( psz_parser, ':' );
1029 if( psz_parser ) psz_parser++; /* skip the ':' */
1032 /* interface not found in the config, let's add it */
1033 if( psz_config && strlen( psz_config ) > 0 )
1035 char *psz_newconfig;
1036 if( asprintf( &psz_newconfig, "%s:%s", psz_config, psz_intf ) != -1 )
1038 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1039 free( psz_newconfig );
1043 config_PutPsz( p_this->p_libvlc, "extraintf", psz_intf );
1048 /* Removes an extra interface from the configuration */
1049 void __config_RemoveIntf( vlc_object_t *p_this, const char *psz_intf )
1053 char *psz_config, *psz_parser;
1054 size_t i_len = strlen( psz_intf );
1056 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1059 if( !strncmp( psz_intf, psz_parser, i_len ) )
1061 char *psz_newconfig;
1062 char *psz_end = psz_parser + i_len;
1063 if( *psz_end == ':' ) psz_end++;
1065 if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1067 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1068 free( psz_newconfig );
1072 psz_parser = strchr( psz_parser, ':' );
1073 if( psz_parser ) psz_parser++; /* skip the ':' */
1077 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1080 if( !strncmp( psz_intf, psz_parser, i_len ) )
1082 char *psz_newconfig;
1083 char *psz_end = psz_parser + i_len;
1084 if( *psz_end == ':' ) psz_end++;
1086 if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1088 config_PutPsz( p_this->p_libvlc, "control", psz_newconfig );
1089 free( psz_newconfig );
1093 psz_parser = strchr( psz_parser, ':' );
1094 if( psz_parser ) psz_parser++; /* skip the ':' */
1100 * Returns VLC_TRUE if the specified extra interface is present in the
1101 * configuration, VLC_FALSE if not
1103 vlc_bool_t __config_ExistIntf( vlc_object_t *p_this, const char *psz_intf )
1107 char *psz_config, *psz_parser;
1108 size_t i_len = strlen( psz_intf );
1110 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1113 if( !strncmp( psz_parser, psz_intf, i_len ) )
1118 psz_parser = strchr( psz_parser, ':' );
1119 if( psz_parser ) psz_parser++; /* skip the ':' */
1123 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1126 if( !strncmp( psz_parser, psz_intf, i_len ) )
1131 psz_parser = strchr( psz_parser, ':' );
1132 if( psz_parser ) psz_parser++; /* skip the ':' */