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 #if defined(HAVE_GETPWUID)
43 # include <pwd.h> /* getpwuid() */
46 #if defined( HAVE_SYS_STAT_H )
47 # include <sys/stat.h>
49 #if defined( HAVE_SYS_TYPES_H )
50 # include <sys/types.h>
53 # if !defined( UNDER_CE )
59 #include "configuration.h"
60 #include "modules/modules.h"
62 static inline char *strdupnull (const char *src
)
64 return src
? strdup (src
) : NULL
;
67 /* Item types that use a string value (i.e. serialized in the module cache) */
68 int IsConfigStringType (int type
)
70 static const unsigned char config_types
[] =
72 CONFIG_ITEM_STRING
, CONFIG_ITEM_FILE
, CONFIG_ITEM_MODULE
,
73 CONFIG_ITEM_DIRECTORY
, CONFIG_ITEM_MODULE_CAT
, CONFIG_ITEM_PASSWORD
,
74 CONFIG_ITEM_MODULE_LIST
, CONFIG_ITEM_MODULE_LIST_CAT
77 /* NOTE: this needs to be changed if we ever get more than 255 types */
78 return memchr (config_types
, type
, sizeof (config_types
)) != NULL
;
82 int IsConfigIntegerType (int type
)
84 static const unsigned char config_types
[] =
86 CONFIG_ITEM_INTEGER
, CONFIG_ITEM_KEY
, CONFIG_ITEM_BOOL
,
87 CONFIG_CATEGORY
, CONFIG_SUBCATEGORY
90 return memchr (config_types
, type
, sizeof (config_types
)) != NULL
;
94 /*****************************************************************************
95 * config_GetType: get the type of a variable (bool, int, float, string)
96 *****************************************************************************
97 * This function is used to get the type of a variable from its name.
98 * Beware, this is quite slow.
99 *****************************************************************************/
100 int __config_GetType( vlc_object_t
*p_this
, const char *psz_name
)
102 module_config_t
*p_config
;
105 p_config
= config_FindConfig( p_this
, psz_name
);
113 switch( p_config
->i_type
)
115 case CONFIG_ITEM_BOOL
:
116 i_type
= VLC_VAR_BOOL
;
119 case CONFIG_ITEM_INTEGER
:
120 case CONFIG_ITEM_KEY
:
121 i_type
= VLC_VAR_INTEGER
;
124 case CONFIG_ITEM_FLOAT
:
125 i_type
= VLC_VAR_FLOAT
;
128 case CONFIG_ITEM_MODULE
:
129 case CONFIG_ITEM_MODULE_CAT
:
130 case CONFIG_ITEM_MODULE_LIST
:
131 case CONFIG_ITEM_MODULE_LIST_CAT
:
132 i_type
= VLC_VAR_MODULE
;
135 case CONFIG_ITEM_STRING
:
136 i_type
= VLC_VAR_STRING
;
139 case CONFIG_ITEM_PASSWORD
:
140 i_type
= VLC_VAR_STRING
;
143 case CONFIG_ITEM_FILE
:
144 i_type
= VLC_VAR_FILE
;
147 case CONFIG_ITEM_DIRECTORY
:
148 i_type
= VLC_VAR_DIRECTORY
;
159 /*****************************************************************************
160 * config_GetInt: get the value of an int variable
161 *****************************************************************************
162 * This function is used to get the value of variables which are internally
163 * represented by an integer (CONFIG_ITEM_INTEGER and
165 *****************************************************************************/
166 int __config_GetInt( vlc_object_t
*p_this
, const char *psz_name
)
168 module_config_t
*p_config
;
170 p_config
= config_FindConfig( p_this
, psz_name
);
175 msg_Err( p_this
, "option %s does not exist", psz_name
);
179 if (!IsConfigIntegerType (p_config
->i_type
))
181 msg_Err( p_this
, "option %s does not refer to an int", psz_name
);
185 return p_config
->value
.i
;
188 /*****************************************************************************
189 * config_GetFloat: get the value of a float variable
190 *****************************************************************************
191 * This function is used to get the value of variables which are internally
192 * represented by a float (CONFIG_ITEM_FLOAT).
193 *****************************************************************************/
194 float __config_GetFloat( vlc_object_t
*p_this
, const char *psz_name
)
196 module_config_t
*p_config
;
198 p_config
= config_FindConfig( p_this
, psz_name
);
203 msg_Err( p_this
, "option %s does not exist", psz_name
);
207 if (!IsConfigFloatType (p_config
->i_type
))
209 msg_Err( p_this
, "option %s does not refer to a float", psz_name
);
213 return p_config
->value
.f
;
216 /*****************************************************************************
217 * config_GetPsz: get the string value of a string variable
218 *****************************************************************************
219 * This function is used to get the value of variables which are internally
220 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
221 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
223 * Important note: remember to free() the returned char* because it's a
224 * duplicate of the actual value. It isn't safe to return a pointer to the
225 * actual value as it can be modified at any time.
226 *****************************************************************************/
227 char * __config_GetPsz( vlc_object_t
*p_this
, const char *psz_name
)
229 module_config_t
*p_config
;
231 p_config
= config_FindConfig( p_this
, psz_name
);
236 msg_Err( p_this
, "option %s does not exist", psz_name
);
240 if (!IsConfigStringType (p_config
->i_type
))
242 msg_Err( p_this
, "option %s does not refer to a string", psz_name
);
246 /* return a copy of the string */
247 vlc_mutex_lock( p_config
->p_lock
);
248 char *psz_value
= strdupnull (p_config
->value
.psz
);
249 vlc_mutex_unlock( p_config
->p_lock
);
254 /*****************************************************************************
255 * config_PutPsz: set the string value of a string variable
256 *****************************************************************************
257 * This function is used to set the value of variables which are internally
258 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
259 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
260 *****************************************************************************/
261 void __config_PutPsz( vlc_object_t
*p_this
,
262 const char *psz_name
, const char *psz_value
)
264 module_config_t
*p_config
;
265 vlc_value_t oldval
, val
;
267 p_config
= config_FindConfig( p_this
, psz_name
);
273 msg_Warn( p_this
, "option %s does not exist", psz_name
);
277 if (!IsConfigStringType (p_config
->i_type
))
279 msg_Err( p_this
, "option %s does not refer to a string", psz_name
);
283 vlc_mutex_lock( p_config
->p_lock
);
285 /* backup old value */
286 oldval
.psz_string
= (char *)p_config
->value
.psz
;
288 if ((psz_value
!= NULL
) && *psz_value
)
289 p_config
->value
.psz
= strdup (psz_value
);
291 p_config
->value
.psz
= NULL
;
293 p_config
->b_dirty
= VLC_TRUE
;
295 val
.psz_string
= (char *)p_config
->value
.psz
;
297 vlc_mutex_unlock( p_config
->p_lock
);
299 if( p_config
->pf_callback
)
301 p_config
->pf_callback( p_this
, psz_name
, oldval
, val
,
302 p_config
->p_callback_data
);
305 /* free old string */
306 free( oldval
.psz_string
);
309 /*****************************************************************************
310 * config_PutInt: set the integer value of an int variable
311 *****************************************************************************
312 * This function is used to set the value of variables which are internally
313 * represented by an integer (CONFIG_ITEM_INTEGER and
315 *****************************************************************************/
316 void __config_PutInt( vlc_object_t
*p_this
, const char *psz_name
, int i_value
)
318 module_config_t
*p_config
;
319 vlc_value_t oldval
, val
;
321 p_config
= config_FindConfig( p_this
, psz_name
);
326 msg_Warn( p_this
, "option %s does not exist", psz_name
);
330 if (!IsConfigIntegerType (p_config
->i_type
))
332 msg_Err( p_this
, "option %s does not refer to an int", psz_name
);
336 /* backup old value */
337 oldval
.i_int
= p_config
->value
.i
;
339 /* if i_min == i_max == 0, then do not use them */
340 if ((p_config
->min
.i
== 0) && (p_config
->max
.i
== 0))
342 p_config
->value
.i
= i_value
;
344 else if (i_value
< p_config
->min
.i
)
346 p_config
->value
.i
= p_config
->min
.i
;
348 else if (i_value
> p_config
->max
.i
)
350 p_config
->value
.i
= p_config
->max
.i
;
354 p_config
->value
.i
= i_value
;
357 p_config
->b_dirty
= VLC_TRUE
;
359 val
.i_int
= p_config
->value
.i
;
361 if( p_config
->pf_callback
)
363 p_config
->pf_callback( p_this
, psz_name
, oldval
, val
,
364 p_config
->p_callback_data
);
368 /*****************************************************************************
369 * config_PutFloat: set the value of a float variable
370 *****************************************************************************
371 * This function is used to set the value of variables which are internally
372 * represented by a float (CONFIG_ITEM_FLOAT).
373 *****************************************************************************/
374 void __config_PutFloat( vlc_object_t
*p_this
,
375 const char *psz_name
, float f_value
)
377 module_config_t
*p_config
;
378 vlc_value_t oldval
, val
;
380 p_config
= config_FindConfig( p_this
, psz_name
);
385 msg_Warn( p_this
, "option %s does not exist", psz_name
);
389 if (!IsConfigFloatType (p_config
->i_type
))
391 msg_Err( p_this
, "option %s does not refer to a float", psz_name
);
395 /* backup old value */
396 oldval
.f_float
= p_config
->value
.f
;
398 /* if f_min == f_max == 0, then do not use them */
399 if ((p_config
->min
.f
== 0) && (p_config
->max
.f
== 0))
401 p_config
->value
.f
= f_value
;
403 else if (f_value
< p_config
->min
.f
)
405 p_config
->value
.f
= p_config
->min
.f
;
407 else if (f_value
> p_config
->max
.f
)
409 p_config
->value
.f
= p_config
->max
.f
;
413 p_config
->value
.f
= f_value
;
416 p_config
->b_dirty
= VLC_TRUE
;
418 val
.f_float
= p_config
->value
.f
;
420 if( p_config
->pf_callback
)
422 p_config
->pf_callback( p_this
, psz_name
, oldval
, val
,
423 p_config
->p_callback_data
);
427 /*****************************************************************************
428 * config_FindConfig: find the config structure associated with an option.
429 *****************************************************************************
430 * FIXME: This function really needs to be optimized.
431 * FIXME: And now even more.
432 *****************************************************************************/
433 module_config_t
*config_FindConfig( vlc_object_t
*p_this
, const char *psz_name
)
438 if( !psz_name
) return NULL
;
440 p_list
= vlc_list_find( p_this
, VLC_OBJECT_MODULE
, FIND_ANYWHERE
);
442 for( i_index
= 0; i_index
< p_list
->i_count
; i_index
++ )
444 module_config_t
*p_item
, *p_end
;
445 module_t
*p_parser
= (module_t
*)p_list
->p_values
[i_index
].p_object
;
447 if( !p_parser
->i_config_items
)
450 for( p_item
= p_parser
->p_config
, p_end
= p_item
+ p_parser
->confsize
;
454 if( p_item
->i_type
& CONFIG_HINT
)
457 if( !strcmp( psz_name
, p_item
->psz_name
)
458 || ( p_item
->psz_oldname
459 && !strcmp( psz_name
, p_item
->psz_oldname
) ) )
461 vlc_list_release( p_list
);
467 vlc_list_release( p_list
);
472 /*****************************************************************************
473 * config_Free: frees a duplicated module's configuration data.
474 *****************************************************************************
475 * This function frees all the data duplicated by config_Duplicate.
476 *****************************************************************************/
477 void config_Free( module_t
*p_module
)
481 for (size_t j
= 0; j
< p_module
->confsize
; j
++)
483 module_config_t
*p_item
= p_module
->p_config
+ j
;
485 free( p_item
->psz_type
);
486 free( p_item
->psz_name
);
487 free( p_item
->psz_text
);
488 free( p_item
->psz_longtext
);
489 free( p_item
->psz_oldname
);
491 if (IsConfigStringType (p_item
->i_type
))
493 free (p_item
->value
.psz
);
494 free (p_item
->orig
.psz
);
495 free (p_item
->saved
.psz
);
498 if( p_item
->ppsz_list
)
499 for( i
= 0; i
< p_item
->i_list
; i
++ )
500 free( p_item
->ppsz_list
[i
] );
501 if( p_item
->ppsz_list_text
)
502 for( i
= 0; i
< p_item
->i_list
; i
++ )
503 free( p_item
->ppsz_list_text
[i
] );
504 free( p_item
->ppsz_list
);
505 free( p_item
->ppsz_list_text
);
506 free( p_item
->pi_list
);
508 if( p_item
->i_action
)
510 for( i
= 0; i
< p_item
->i_action
; i
++ )
512 free( p_item
->ppsz_action_text
[i
] );
514 free( p_item
->ppf_action
);
515 free( p_item
->ppsz_action_text
);
519 free (p_module
->p_config
);
520 p_module
->p_config
= NULL
;
523 /*****************************************************************************
524 * config_SetCallbacks: sets callback functions in the duplicate p_config.
525 *****************************************************************************
526 * Unfortunatly we cannot work directly with the module's config data as
527 * this module might be unloaded from memory at any time (remember HideModule).
528 * This is why we need to duplicate callbacks each time we reload the module.
529 *****************************************************************************/
530 void config_SetCallbacks( module_config_t
*p_new
, module_config_t
*p_orig
,
533 for (size_t i
= 0; i
< n
; i
++)
535 p_new
->pf_callback
= p_orig
->pf_callback
;
541 /*****************************************************************************
542 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
543 *****************************************************************************
544 * We simply undo what we did in config_SetCallbacks.
545 *****************************************************************************/
546 void config_UnsetCallbacks( module_config_t
*p_new
, size_t n
)
548 for (size_t i
= 0; i
< n
; i
++)
550 p_new
->pf_callback
= NULL
;
555 /*****************************************************************************
556 * config_ResetAll: reset the configuration data for all the modules.
557 *****************************************************************************/
558 void __config_ResetAll( vlc_object_t
*p_this
)
564 /* Acquire config file lock */
565 vlc_mutex_lock( &p_this
->p_libvlc
->config_lock
);
567 p_list
= vlc_list_find( p_this
, VLC_OBJECT_MODULE
, FIND_ANYWHERE
);
569 for( i_index
= 0; i_index
< p_list
->i_count
; i_index
++ )
571 p_module
= (module_t
*)p_list
->p_values
[i_index
].p_object
;
572 if( p_module
->b_submodule
) continue;
574 for (size_t i
= 0; i
< p_module
->confsize
; i
++ )
576 if (IsConfigIntegerType (p_module
->p_config
[i
].i_type
))
577 p_module
->p_config
[i
].value
.i
= p_module
->p_config
[i
].orig
.i
;
579 if (IsConfigFloatType (p_module
->p_config
[i
].i_type
))
580 p_module
->p_config
[i
].value
.f
= p_module
->p_config
[i
].orig
.f
;
582 if (IsConfigStringType (p_module
->p_config
[i
].i_type
))
584 free ((char *)p_module
->p_config
[i
].value
.psz
);
585 p_module
->p_config
[i
].value
.psz
=
586 strdupnull (p_module
->p_config
[i
].orig
.psz
);
591 vlc_list_release( p_list
);
592 vlc_mutex_unlock( &p_this
->p_libvlc
->config_lock
);
596 * config_GetDataDir: find directory where shared data is installed
598 * @return a string (always succeeds).
600 const char *config_GetDataDir( void )
602 #if defined (WIN32) || defined (UNDER_CE)
603 return vlc_global()->psz_vlcpath
;
604 #elif defined(__APPLE__) || defined (SYS_BEOS)
605 static char path
[PATH_MAX
] = "";
609 snprintf( path
, sizeof( path
), "%s/share",
610 vlc_global()->psz_vlcpath
);
611 path
[sizeof( path
) - 1] = '\0';
619 /*****************************************************************************
620 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
621 *****************************************************************************
622 * This function will try by different ways to find the user's home path.
623 * Note that this function is not reentrant, it should be called only once
624 * at the beginning of main where the result will be stored for later use.
625 *****************************************************************************/
626 static char *GetDir( vlc_bool_t b_appdata
)
628 const char *psz_localhome
= NULL
;
630 #if defined(WIN32) && !defined(UNDER_CE)
631 typedef HRESULT (WINAPI
*SHGETFOLDERPATH
)( HWND
, int, HANDLE
, DWORD
,
633 #ifndef CSIDL_FLAG_CREATE
634 # define CSIDL_FLAG_CREATE 0x8000
636 #ifndef CSIDL_APPDATA
637 # define CSIDL_APPDATA 0x1A
639 #ifndef CSIDL_PROFILE
640 # define CSIDL_PROFILE 0x28
642 #ifndef SHGFP_TYPE_CURRENT
643 # define SHGFP_TYPE_CURRENT 0
646 HINSTANCE shfolder_dll
;
647 SHGETFOLDERPATH SHGetFolderPath
;
649 /* load the shfolder dll to retrieve SHGetFolderPath */
650 if( ( shfolder_dll
= LoadLibrary( _T("SHFolder.dll") ) ) != NULL
)
652 SHGetFolderPath
= (void *)GetProcAddress( shfolder_dll
,
653 _T("SHGetFolderPathW") );
654 if ( SHGetFolderPath
!= NULL
)
656 wchar_t whomedir
[MAX_PATH
];
658 /* get the "Application Data" folder for the current user */
659 if( S_OK
== SHGetFolderPath( NULL
,
660 (b_appdata
? CSIDL_APPDATA
:
661 CSIDL_PROFILE
) | CSIDL_FLAG_CREATE
,
662 NULL
, SHGFP_TYPE_CURRENT
,
665 FreeLibrary( shfolder_dll
);
666 return FromWide( whomedir
);
669 FreeLibrary( shfolder_dll
);
672 #elif defined(UNDER_CE)
674 #ifndef CSIDL_APPDATA
675 # define CSIDL_APPDATA 0x1A
678 wchar_t whomedir
[MAX_PATH
];
680 /* get the "Application Data" folder for the current user */
681 if( SHGetSpecialFolderPath( NULL
, whomedir
, CSIDL_APPDATA
, 1 ) )
682 return FromWide( whomedir
);
685 psz_localhome
= getenv( "HOME" );
686 if( psz_localhome
== NULL
)
688 #if defined(HAVE_GETPWUID)
692 if( ( p_pw
= getpwuid( getuid() ) ) != NULL
)
693 psz_localhome
= p_pw
->pw_dir
;
697 psz_localhome
= getenv( "TMP" );
698 if( psz_localhome
== NULL
)
699 psz_localhome
= "/tmp";
703 return FromLocaleDup( psz_localhome
);
707 * Get the user's home directory
709 char *config_GetHomeDir( void )
711 return GetDir( VLC_FALSE
);
715 * Get the user's main data and config directory:
716 * - on windows that's the App Data directory;
717 * - on other OSes it's the same as the home directory.
719 char *config_GetUserDir( void ); /* XXX why does gcc wants a declaration ?
721 char *config_GetUserDir( void )
723 return GetDir( VLC_TRUE
);
727 * Get the user's VLC configuration directory
729 char *config_GetConfigDir( libvlc_int_t
*p_libvlc
)
732 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
733 char *psz_parent
= config_GetUserDir();
734 if( !psz_parent
) psz_parent
= p_libvlc
->psz_homedir
;
735 if( asprintf( &psz_dir
, "%s" DIR_SEP CONFIG_DIR
, psz_parent
) == -1 )
739 /* XDG Base Directory Specification - Version 0.6 */
740 char *psz_env
= getenv( "XDG_CONFIG_HOME" );
743 if( asprintf( &psz_dir
, "%s/vlc", psz_env
) == -1 )
747 psz_env
= getenv( "HOME" );
748 if( !psz_env
) psz_env
= p_libvlc
->psz_homedir
; /* not part of XDG spec but we want a sensible fallback */
749 if( asprintf( &psz_dir
, "%s/.config/vlc", psz_env
) == -1 )
756 * Get the user's VLC data directory
757 * (used for stuff like the skins, custom lua modules, ...)
759 char *config_GetUserDataDir( libvlc_int_t
*p_libvlc
)
762 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
763 char *psz_parent
= config_GetUserDir();
764 if( !psz_parent
) psz_parent
= p_libvlc
->psz_homedir
;
765 if( asprintf( &psz_dir
, "%s" DIR_SEP CONFIG_DIR
, psz_parent
) == -1 )
769 /* XDG Base Directory Specification - Version 0.6 */
770 char *psz_env
= getenv( "XDG_DATA_HOME" );
773 if( asprintf( &psz_dir
, "%s/vlc", psz_env
) == -1 )
777 psz_env
= getenv( "HOME" );
778 if( !psz_env
) psz_env
= p_libvlc
->psz_homedir
; /* not part of XDG spec but we want a sensible fallback */
779 if( asprintf( &psz_dir
, "%s/.local/share/vlc", psz_env
) == -1 )
786 * Get the user's VLC cache directory
787 * (used for stuff like the modules cache, the album art cache, ...)
789 char *config_GetCacheDir( libvlc_int_t
*p_libvlc
)
792 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
793 char *psz_parent
= config_GetUserDir();
794 if( !psz_parent
) psz_parent
= p_libvlc
->psz_homedir
;
795 if( asprintf( &psz_dir
, "%s" DIR_SEP CONFIG_DIR
, psz_parent
) == -1 )
799 /* XDG Base Directory Specification - Version 0.6 */
800 char *psz_env
= getenv( "XDG_CACHE_HOME" );
803 if( asprintf( &psz_dir
, "%s/vlc", psz_env
) == -1 )
807 psz_env
= getenv( "HOME" );
808 if( !psz_env
) psz_env
= p_libvlc
->psz_homedir
; /* not part of XDG spec but we want a sensible fallback */
809 if( asprintf( &psz_dir
, "%s/.cache/vlc", psz_env
) == -1 )