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 *****************************************************************************/
29 #include "../libvlc.h"
31 #include "vlc_charset.h"
32 #include "vlc_configuration.h"
34 #include <errno.h> /* errno */
39 # include <unistd.h> /* getuid() */
42 #ifdef HAVE_GETOPT_LONG
44 # include <getopt.h> /* getopt() */
47 # include "../extras/getopt.h"
50 #if defined(HAVE_GETPWUID)
51 # include <pwd.h> /* getpwuid() */
54 #if defined( HAVE_SYS_STAT_H )
55 # include <sys/stat.h>
57 #if defined( HAVE_SYS_TYPES_H )
58 # include <sys/types.h>
61 # if !defined( UNDER_CE )
67 #include "configuration.h"
68 #include "modules/modules.h"
70 static inline char *strdupnull (const char *src)
72 return src ? strdup (src) : NULL;
75 static inline char *_strdupnull (const char *src)
77 return src ? strdup (_(src)) : NULL;
80 /* Item types that use a string value (i.e. serialized in the module cache) */
81 int IsConfigStringType (int type)
83 static const unsigned char config_types[] =
85 CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
86 CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
87 CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT
90 /* NOTE: this needs to be changed if we ever get more than 255 types */
91 return memchr (config_types, type, sizeof (config_types)) != NULL;
95 int IsConfigIntegerType (int type)
97 static const unsigned char config_types[] =
99 CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
100 CONFIG_CATEGORY, CONFIG_SUBCATEGORY
103 return memchr (config_types, type, sizeof (config_types)) != NULL;
107 /*****************************************************************************
108 * config_GetType: get the type of a variable (bool, int, float, string)
109 *****************************************************************************
110 * This function is used to get the type of a variable from its name.
111 * Beware, this is quite slow.
112 *****************************************************************************/
113 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
115 module_config_t *p_config;
118 p_config = config_FindConfig( p_this, psz_name );
126 switch( p_config->i_type )
128 case CONFIG_ITEM_BOOL:
129 i_type = VLC_VAR_BOOL;
132 case CONFIG_ITEM_INTEGER:
133 case CONFIG_ITEM_KEY:
134 i_type = VLC_VAR_INTEGER;
137 case CONFIG_ITEM_FLOAT:
138 i_type = VLC_VAR_FLOAT;
141 case CONFIG_ITEM_MODULE:
142 case CONFIG_ITEM_MODULE_CAT:
143 case CONFIG_ITEM_MODULE_LIST:
144 case CONFIG_ITEM_MODULE_LIST_CAT:
145 i_type = VLC_VAR_MODULE;
148 case CONFIG_ITEM_STRING:
149 i_type = VLC_VAR_STRING;
152 case CONFIG_ITEM_PASSWORD:
153 i_type = VLC_VAR_STRING;
156 case CONFIG_ITEM_FILE:
157 i_type = VLC_VAR_FILE;
160 case CONFIG_ITEM_DIRECTORY:
161 i_type = VLC_VAR_DIRECTORY;
172 /*****************************************************************************
173 * config_GetInt: get the value of an int variable
174 *****************************************************************************
175 * This function is used to get the value of variables which are internally
176 * represented by an integer (CONFIG_ITEM_INTEGER and
178 *****************************************************************************/
179 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
181 module_config_t *p_config;
183 p_config = config_FindConfig( p_this, psz_name );
188 msg_Err( p_this, "option %s does not exist", psz_name );
192 if (!IsConfigIntegerType (p_config->i_type))
194 msg_Err( p_this, "option %s does not refer to an int", psz_name );
198 return p_config->value.i;
201 /*****************************************************************************
202 * config_GetFloat: get the value of a float variable
203 *****************************************************************************
204 * This function is used to get the value of variables which are internally
205 * represented by a float (CONFIG_ITEM_FLOAT).
206 *****************************************************************************/
207 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
209 module_config_t *p_config;
211 p_config = config_FindConfig( p_this, psz_name );
216 msg_Err( p_this, "option %s does not exist", psz_name );
220 if (!IsConfigFloatType (p_config->i_type))
222 msg_Err( p_this, "option %s does not refer to a float", psz_name );
226 return p_config->value.f;
229 /*****************************************************************************
230 * config_GetPsz: get the string value of a string variable
231 *****************************************************************************
232 * This function is used to get the value of variables which are internally
233 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
234 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
236 * Important note: remember to free() the returned char* because it's a
237 * duplicate of the actual value. It isn't safe to return a pointer to the
238 * actual value as it can be modified at any time.
239 *****************************************************************************/
240 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
242 module_config_t *p_config;
244 p_config = config_FindConfig( p_this, psz_name );
249 msg_Err( p_this, "option %s does not exist", psz_name );
253 if (!IsConfigStringType (p_config->i_type))
255 msg_Err( p_this, "option %s does not refer to a string", psz_name );
259 /* return a copy of the string */
260 vlc_mutex_lock( p_config->p_lock );
261 char *psz_value = strdupnull (p_config->value.psz);
262 vlc_mutex_unlock( p_config->p_lock );
267 /*****************************************************************************
268 * config_PutPsz: set the string value of a string variable
269 *****************************************************************************
270 * This function is used to set the value of variables which are internally
271 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
272 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
273 *****************************************************************************/
274 void __config_PutPsz( vlc_object_t *p_this,
275 const char *psz_name, const char *psz_value )
277 module_config_t *p_config;
278 vlc_value_t oldval, val;
280 p_config = config_FindConfig( p_this, psz_name );
286 msg_Warn( p_this, "option %s does not exist", psz_name );
290 if (!IsConfigStringType (p_config->i_type))
292 msg_Err( p_this, "option %s does not refer to a string", psz_name );
296 vlc_mutex_lock( p_config->p_lock );
298 /* backup old value */
299 oldval.psz_string = (char *)p_config->value.psz;
301 if ((psz_value != NULL) && *psz_value)
302 p_config->value.psz = strdup (psz_value);
304 p_config->value.psz = NULL;
306 p_config->b_dirty = VLC_TRUE;
308 val.psz_string = (char *)p_config->value.psz;
310 vlc_mutex_unlock( p_config->p_lock );
312 if( p_config->pf_callback )
314 p_config->pf_callback( p_this, psz_name, oldval, val,
315 p_config->p_callback_data );
318 /* free old string */
319 if( oldval.psz_string ) free( oldval.psz_string );
322 /*****************************************************************************
323 * config_PutInt: set the integer value of an int variable
324 *****************************************************************************
325 * This function is used to set the value of variables which are internally
326 * represented by an integer (CONFIG_ITEM_INTEGER and
328 *****************************************************************************/
329 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
331 module_config_t *p_config;
332 vlc_value_t oldval, val;
334 p_config = config_FindConfig( p_this, psz_name );
339 msg_Warn( p_this, "option %s does not exist", psz_name );
343 if (!IsConfigIntegerType (p_config->i_type))
345 msg_Err( p_this, "option %s does not refer to an int", psz_name );
349 /* backup old value */
350 oldval.i_int = p_config->value.i;
352 /* if i_min == i_max == 0, then do not use them */
353 if ((p_config->min.i == 0) && (p_config->max.i == 0))
355 p_config->value.i = i_value;
357 else if (i_value < p_config->min.i)
359 p_config->value.i = p_config->min.i;
361 else if (i_value > p_config->max.i)
363 p_config->value.i = p_config->max.i;
367 p_config->value.i = i_value;
370 p_config->b_dirty = VLC_TRUE;
372 val.i_int = p_config->value.i;
374 if( p_config->pf_callback )
376 p_config->pf_callback( p_this, psz_name, oldval, val,
377 p_config->p_callback_data );
381 /*****************************************************************************
382 * config_PutFloat: set the value of a float variable
383 *****************************************************************************
384 * This function is used to set the value of variables which are internally
385 * represented by a float (CONFIG_ITEM_FLOAT).
386 *****************************************************************************/
387 void __config_PutFloat( vlc_object_t *p_this,
388 const char *psz_name, float f_value )
390 module_config_t *p_config;
391 vlc_value_t oldval, val;
393 p_config = config_FindConfig( p_this, psz_name );
398 msg_Warn( p_this, "option %s does not exist", psz_name );
402 if (!IsConfigFloatType (p_config->i_type))
404 msg_Err( p_this, "option %s does not refer to a float", psz_name );
408 /* backup old value */
409 oldval.f_float = p_config->value.f;
411 /* if f_min == f_max == 0, then do not use them */
412 if ((p_config->min.f == 0) && (p_config->max.f == 0))
414 p_config->value.f = f_value;
416 else if (f_value < p_config->min.f)
418 p_config->value.f = p_config->min.f;
420 else if (f_value > p_config->max.f)
422 p_config->value.f = p_config->max.f;
426 p_config->value.f = f_value;
429 p_config->b_dirty = VLC_TRUE;
431 val.f_float = p_config->value.f;
433 if( p_config->pf_callback )
435 p_config->pf_callback( p_this, psz_name, oldval, val,
436 p_config->p_callback_data );
440 /*****************************************************************************
441 * config_FindConfig: find the config structure associated with an option.
442 *****************************************************************************
443 * FIXME: This function really needs to be optimized.
444 * FIXME: And now even more.
445 *****************************************************************************/
446 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
451 if( !psz_name ) return NULL;
453 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
455 for( i_index = 0; i_index < p_list->i_count; i_index++ )
457 module_config_t *p_item, *p_end;
458 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
460 if( !p_parser->i_config_items )
463 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
467 if( p_item->i_type & CONFIG_HINT )
470 if( !strcmp( psz_name, p_item->psz_name )
471 || ( p_item->psz_oldname
472 && !strcmp( psz_name, p_item->psz_oldname ) ) )
474 vlc_list_release( p_list );
480 vlc_list_release( p_list );
485 /*****************************************************************************
486 * config_Free: frees a duplicated module's configuration data.
487 *****************************************************************************
488 * This function frees all the data duplicated by config_Duplicate.
489 *****************************************************************************/
490 void config_Free( module_t *p_module )
494 for (size_t j = 0; j < p_module->confsize; j++)
496 module_config_t *p_item = p_module->p_config + j;
498 free( p_item->psz_type );
499 free( p_item->psz_name );
500 free( p_item->psz_text );
501 free( p_item->psz_longtext );
502 free( p_item->psz_oldname );
504 if (IsConfigStringType (p_item->i_type))
506 free (p_item->value.psz);
507 free (p_item->orig.psz);
508 free (p_item->saved.psz);
511 if( p_item->ppsz_list )
512 for( i = 0; i < p_item->i_list; i++ )
513 free( p_item->ppsz_list[i] );
514 if( p_item->ppsz_list_text )
515 for( i = 0; i < p_item->i_list; i++ )
516 free( p_item->ppsz_list_text[i] );
517 free( p_item->ppsz_list );
518 free( p_item->ppsz_list_text );
519 free( p_item->pi_list );
521 if( p_item->i_action )
523 for( i = 0; i < p_item->i_action; i++ )
525 free( p_item->ppsz_action_text[i] );
527 free( p_item->ppf_action );
528 free( p_item->ppsz_action_text );
532 free (p_module->p_config);
533 p_module->p_config = NULL;
536 /*****************************************************************************
537 * config_SetCallbacks: sets callback functions in the duplicate p_config.
538 *****************************************************************************
539 * Unfortunatly we cannot work directly with the module's config data as
540 * this module might be unloaded from memory at any time (remember HideModule).
541 * This is why we need to duplicate callbacks each time we reload the module.
542 *****************************************************************************/
543 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
546 for (size_t i = 0; i < n; i++)
548 p_new->pf_callback = p_orig->pf_callback;
554 /*****************************************************************************
555 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
556 *****************************************************************************
557 * We simply undo what we did in config_SetCallbacks.
558 *****************************************************************************/
559 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
561 for (size_t i = 0; i < n; i++)
563 p_new->pf_callback = NULL;
568 /*****************************************************************************
569 * config_ResetAll: reset the configuration data for all the modules.
570 *****************************************************************************/
571 void __config_ResetAll( vlc_object_t *p_this )
577 /* Acquire config file lock */
578 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
580 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
582 for( i_index = 0; i_index < p_list->i_count; i_index++ )
584 p_module = (module_t *)p_list->p_values[i_index].p_object ;
585 if( p_module->b_submodule ) continue;
587 for (size_t i = 0; i < p_module->confsize; i++ )
589 if (IsConfigIntegerType (p_module->p_config[i].i_type))
590 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
592 if (IsConfigFloatType (p_module->p_config[i].i_type))
593 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
595 if (IsConfigStringType (p_module->p_config[i].i_type))
597 free ((char *)p_module->p_config[i].value.psz);
598 p_module->p_config[i].value.psz =
599 strdupnull (p_module->p_config[i].orig.psz);
604 vlc_list_release( p_list );
605 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
609 * config_GetDataDir: find directory where shared data is installed
611 * @return a string (always succeeds).
613 const char *config_GetDataDir( void )
615 #if defined (WIN32) || defined (UNDER_CE)
616 return vlc_global()->psz_vlcpath;
617 #elif defined(__APPLE__) || defined (SYS_BEOS)
618 static char path[PATH_MAX] = "";
622 snprintf( path, sizeof( path ), "%s/share",
623 vlc_global()->psz_vlcpath );
624 path[sizeof( path ) - 1] = '\0';
632 /*****************************************************************************
633 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
634 *****************************************************************************
635 * This function will try by different ways to find the user's home path.
636 * Note that this function is not reentrant, it should be called only once
637 * at the beginning of main where the result will be stored for later use.
638 *****************************************************************************/
639 static char *GetDir( vlc_bool_t b_appdata )
641 const char *psz_localhome = NULL;
643 #if defined(WIN32) && !defined(UNDER_CE)
644 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
646 #ifndef CSIDL_FLAG_CREATE
647 # define CSIDL_FLAG_CREATE 0x8000
649 #ifndef CSIDL_APPDATA
650 # define CSIDL_APPDATA 0x1A
652 #ifndef CSIDL_PROFILE
653 # define CSIDL_PROFILE 0x28
655 #ifndef SHGFP_TYPE_CURRENT
656 # define SHGFP_TYPE_CURRENT 0
659 HINSTANCE shfolder_dll;
660 SHGETFOLDERPATH SHGetFolderPath ;
662 /* load the shfolder dll to retrieve SHGetFolderPath */
663 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
665 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
666 _T("SHGetFolderPathW") );
667 if ( SHGetFolderPath != NULL )
669 wchar_t whomedir[MAX_PATH];
671 /* get the "Application Data" folder for the current user */
672 if( S_OK == SHGetFolderPath( NULL,
673 (b_appdata ? CSIDL_APPDATA :
674 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
675 NULL, SHGFP_TYPE_CURRENT,
678 FreeLibrary( shfolder_dll );
679 return FromWide( whomedir );
682 FreeLibrary( shfolder_dll );
685 #elif defined(UNDER_CE)
687 #ifndef CSIDL_APPDATA
688 # define CSIDL_APPDATA 0x1A
691 wchar_t whomedir[MAX_PATH];
693 /* get the "Application Data" folder for the current user */
694 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
695 return FromWide( whomedir );
698 psz_localhome = getenv( "HOME" );
699 if( psz_localhome == NULL )
701 #if defined(HAVE_GETPWUID)
705 if( ( p_pw = getpwuid( getuid() ) ) != NULL )
706 psz_localhome = p_pw->pw_dir;
710 psz_localhome = getenv( "TMP" );
711 if( psz_localhome == NULL )
712 psz_localhome = "/tmp";
716 return FromLocaleDup( psz_localhome );
720 * Get the user's home directory
722 char *config_GetHomeDir( void )
724 return GetDir( VLC_FALSE );
728 * Get the user's main data and config directory:
729 * - on windows that's the App Data directory;
730 * - on other OSes it's the same as the home directory.
732 char *config_GetUserDir( void )
734 return GetDir( VLC_TRUE );
738 * Get the user's VLC configuration directory
740 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
743 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
744 char *psz_parent = config_GetUserDir();
745 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
746 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
750 /* XDG Base Directory Specification - Version 0.6 */
751 char *psz_env = getenv( "XDG_CONFIG_HOME" );
754 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
758 psz_env = getenv( "HOME" );
759 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
760 if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
767 * Get the user's VLC data directory
768 * (used for stuff like the skins, custom lua modules, ...)
770 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
773 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
774 char *psz_parent = config_GetUserDir();
775 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
776 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
780 /* XDG Base Directory Specification - Version 0.6 */
781 char *psz_env = getenv( "XDG_DATA_HOME" );
784 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
788 psz_env = getenv( "HOME" );
789 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
790 if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
797 * Get the user's VLC cache directory
798 * (used for stuff like the modules cache, the album art cache, ...)
800 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
803 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
804 char *psz_parent = config_GetUserDir();
805 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
806 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
810 /* XDG Base Directory Specification - Version 0.6 */
811 char *psz_env = getenv( "XDG_CACHE_HOME" );
814 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
818 psz_env = getenv( "HOME" );
819 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
820 if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )