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 = 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 = 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 = 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 )
560 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
565 /* Acquire config file lock */
566 vlc_mutex_lock( &priv->config_lock );
568 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
570 for( i_index = 0; i_index < p_list->i_count; i_index++ )
572 p_module = (module_t *)p_list->p_values[i_index].p_object ;
573 if( p_module->b_submodule ) continue;
575 for (size_t i = 0; i < p_module->confsize; i++ )
577 if (IsConfigIntegerType (p_module->p_config[i].i_type))
578 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
580 if (IsConfigFloatType (p_module->p_config[i].i_type))
581 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
583 if (IsConfigStringType (p_module->p_config[i].i_type))
585 free ((char *)p_module->p_config[i].value.psz);
586 p_module->p_config[i].value.psz =
587 strdupnull (p_module->p_config[i].orig.psz);
592 vlc_list_release( p_list );
593 vlc_mutex_unlock( &priv->config_lock );
597 * config_GetDataDir: find directory where shared data is installed
599 * @return a string (always succeeds).
601 const char *config_GetDataDir( void )
603 #if defined (WIN32) || defined (UNDER_CE)
604 return vlc_global()->psz_vlcpath;
605 #elif defined(__APPLE__) || defined (SYS_BEOS)
606 static char path[PATH_MAX] = "";
610 snprintf( path, sizeof( path ), "%s/share",
611 vlc_global()->psz_vlcpath );
612 path[sizeof( path ) - 1] = '\0';
620 /*****************************************************************************
621 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
622 *****************************************************************************
623 * This function will try by different ways to find the user's home path.
624 * Note that this function is not reentrant, it should be called only once
625 * at the beginning of main where the result will be stored for later use.
626 *****************************************************************************/
627 static char *GetDir( bool b_appdata )
629 const char *psz_localhome = NULL;
631 #if defined(WIN32) && !defined(UNDER_CE)
632 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
634 #ifndef CSIDL_FLAG_CREATE
635 # define CSIDL_FLAG_CREATE 0x8000
637 #ifndef CSIDL_APPDATA
638 # define CSIDL_APPDATA 0x1A
640 #ifndef CSIDL_PROFILE
641 # define CSIDL_PROFILE 0x28
643 #ifndef SHGFP_TYPE_CURRENT
644 # define SHGFP_TYPE_CURRENT 0
647 HINSTANCE shfolder_dll;
648 SHGETFOLDERPATH SHGetFolderPath ;
650 /* load the shfolder dll to retrieve SHGetFolderPath */
651 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
653 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
654 _T("SHGetFolderPathW") );
655 if ( SHGetFolderPath != NULL )
657 wchar_t whomedir[MAX_PATH];
659 /* get the "Application Data" folder for the current user */
660 if( S_OK == SHGetFolderPath( NULL,
661 (b_appdata ? CSIDL_APPDATA :
662 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
663 NULL, SHGFP_TYPE_CURRENT,
666 FreeLibrary( shfolder_dll );
667 return FromWide( whomedir );
670 FreeLibrary( shfolder_dll );
673 #elif defined(UNDER_CE)
675 #ifndef CSIDL_APPDATA
676 # define CSIDL_APPDATA 0x1A
679 wchar_t whomedir[MAX_PATH];
681 /* get the "Application Data" folder for the current user */
682 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
683 return FromWide( whomedir );
686 psz_localhome = getenv( "HOME" );
687 if( psz_localhome == NULL )
689 #if defined(HAVE_GETPWUID)
693 if( ( p_pw = getpwuid( getuid() ) ) != NULL )
694 psz_localhome = p_pw->pw_dir;
698 psz_localhome = getenv( "TMP" );
699 if( psz_localhome == NULL )
700 psz_localhome = "/tmp";
704 return FromLocaleDup( psz_localhome );
708 * Get the user's home directory
710 char *config_GetHomeDir( void )
712 return GetDir( false );
716 * Get the user's main data and config directory:
717 * - on windows that's the App Data directory;
718 * - on other OSes it's the same as the home directory.
720 char *config_GetUserDir( void ); /* XXX why does gcc wants a declaration ?
722 char *config_GetUserDir( void )
724 return GetDir( true );
727 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
730 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
731 char *psz_parent = config_GetUserDir();
733 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
737 (void)xdg_name; (void)xdg_default;
739 char var[sizeof ("XDG__HOME") + strlen (xdg_name)], *psz_env;
741 /* XDG Base Directory Specification - Version 0.6 */
742 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
743 psz_env = FromLocaleDup (getenv (var));
746 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
751 psz_env = FromLocaleDup (getenv ("HOME"));
752 /* not part of XDG spec but we want a sensible fallback */
754 psz_env = config_GetHomeDir();
755 if( asprintf( &psz_dir, "%s/%s/vlc", psz_env, xdg_default ) == -1 )
765 * Get the user's VLC configuration directory
767 char *config_GetConfigDir( void )
769 return config_GetFooDir ("CONFIG", ".config");
773 * Get the user's VLC data directory
774 * (used for stuff like the skins, custom lua modules, ...)
776 char *config_GetUserDataDir( void )
778 return config_GetFooDir ("DATA", ".local/share");
782 * Get the user's VLC cache directory
783 * (used for stuff like the modules cache, the album art cache, ...)
785 char *config_GetCacheDir( void )
787 return config_GetFooDir ("CACHE", ".cache");