1 /*****************************************************************************
2 * configuration.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 )
64 #include "configuration.h"
65 #include "modules/modules.h"
67 static int ConfigStringToKey( const char * );
68 static char *ConfigKeyToString( int );
70 static inline char *strdupnull (const char *src)
77 static inline char *_strdupnull (const char *src)
81 return strdup (_(src));
84 /* Item types that use a string value (i.e. serialized in the module cache) */
85 int IsConfigStringType (int type)
87 static const unsigned char config_types[] =
89 CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
90 CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
91 CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT
94 /* NOTE: this needs to be changed if we ever get more than 255 types */
95 return memchr (config_types, type, sizeof (config_types)) != NULL;
99 static int IsConfigIntegerType (int type)
101 static const unsigned char config_types[] =
103 CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
104 CONFIG_CATEGORY, CONFIG_SUBCATEGORY
107 return memchr (config_types, type, sizeof (config_types)) != NULL;
111 static inline int IsConfigFloatType (int type)
113 return type == CONFIG_ITEM_FLOAT;
117 /*****************************************************************************
118 * config_GetType: get the type of a variable (bool, int, float, string)
119 *****************************************************************************
120 * This function is used to get the type of a variable from its name.
121 * Beware, this is quite slow.
122 *****************************************************************************/
123 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
125 module_config_t *p_config;
128 p_config = config_FindConfig( p_this, psz_name );
136 switch( p_config->i_type )
138 case CONFIG_ITEM_BOOL:
139 i_type = VLC_VAR_BOOL;
142 case CONFIG_ITEM_INTEGER:
143 case CONFIG_ITEM_KEY:
144 i_type = VLC_VAR_INTEGER;
147 case CONFIG_ITEM_FLOAT:
148 i_type = VLC_VAR_FLOAT;
151 case CONFIG_ITEM_MODULE:
152 case CONFIG_ITEM_MODULE_CAT:
153 case CONFIG_ITEM_MODULE_LIST:
154 case CONFIG_ITEM_MODULE_LIST_CAT:
155 i_type = VLC_VAR_MODULE;
158 case CONFIG_ITEM_STRING:
159 i_type = VLC_VAR_STRING;
162 case CONFIG_ITEM_PASSWORD:
163 i_type = VLC_VAR_STRING;
166 case CONFIG_ITEM_FILE:
167 i_type = VLC_VAR_FILE;
170 case CONFIG_ITEM_DIRECTORY:
171 i_type = VLC_VAR_DIRECTORY;
182 /*****************************************************************************
183 * config_GetInt: get the value of an int variable
184 *****************************************************************************
185 * This function is used to get the value of variables which are internally
186 * represented by an integer (CONFIG_ITEM_INTEGER and
188 *****************************************************************************/
189 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
191 module_config_t *p_config;
193 p_config = config_FindConfig( p_this, psz_name );
198 msg_Err( p_this, "option %s does not exist", psz_name );
202 if (!IsConfigIntegerType (p_config->i_type))
204 msg_Err( p_this, "option %s does not refer to an int", psz_name );
208 return p_config->value.i;
211 /*****************************************************************************
212 * config_GetFloat: get the value of a float variable
213 *****************************************************************************
214 * This function is used to get the value of variables which are internally
215 * represented by a float (CONFIG_ITEM_FLOAT).
216 *****************************************************************************/
217 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
219 module_config_t *p_config;
221 p_config = config_FindConfig( p_this, psz_name );
226 msg_Err( p_this, "option %s does not exist", psz_name );
230 if (!IsConfigFloatType (p_config->i_type))
232 msg_Err( p_this, "option %s does not refer to a float", psz_name );
236 return p_config->value.f;
239 /*****************************************************************************
240 * config_GetPsz: get the string value of a string variable
241 *****************************************************************************
242 * This function is used to get the value of variables which are internally
243 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
244 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
246 * Important note: remember to free() the returned char* because it's a
247 * duplicate of the actual value. It isn't safe to return a pointer to the
248 * actual value as it can be modified at any time.
249 *****************************************************************************/
250 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
252 module_config_t *p_config;
254 p_config = config_FindConfig( p_this, psz_name );
259 msg_Err( p_this, "option %s does not exist", psz_name );
263 if (!IsConfigStringType (p_config->i_type))
265 msg_Err( p_this, "option %s does not refer to a string", psz_name );
269 /* return a copy of the string */
270 vlc_mutex_lock( p_config->p_lock );
271 char *psz_value = strdupnull (p_config->value.psz);
272 vlc_mutex_unlock( p_config->p_lock );
277 /*****************************************************************************
278 * config_PutPsz: set the string value of a string variable
279 *****************************************************************************
280 * This function is used to set the value of variables which are internally
281 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
282 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
283 *****************************************************************************/
284 void __config_PutPsz( vlc_object_t *p_this,
285 const char *psz_name, const char *psz_value )
287 module_config_t *p_config;
288 vlc_value_t oldval, val;
290 p_config = config_FindConfig( p_this, psz_name );
296 msg_Warn( p_this, "option %s does not exist", psz_name );
300 if (!IsConfigStringType (p_config->i_type))
302 msg_Err( p_this, "option %s does not refer to a string", psz_name );
306 vlc_mutex_lock( p_config->p_lock );
308 /* backup old value */
309 oldval.psz_string = (char *)p_config->value.psz;
311 if ((psz_value != NULL) && *psz_value)
312 p_config->value.psz = strdup (psz_value);
314 p_config->value.psz = NULL;
316 p_config->b_dirty = VLC_TRUE;
318 val.psz_string = (char *)p_config->value.psz;
320 vlc_mutex_unlock( p_config->p_lock );
322 if( p_config->pf_callback )
324 p_config->pf_callback( p_this, psz_name, oldval, val,
325 p_config->p_callback_data );
328 /* free old string */
329 if( oldval.psz_string ) free( oldval.psz_string );
332 /*****************************************************************************
333 * config_PutInt: set the integer value of an int variable
334 *****************************************************************************
335 * This function is used to set the value of variables which are internally
336 * represented by an integer (CONFIG_ITEM_INTEGER and
338 *****************************************************************************/
339 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
341 module_config_t *p_config;
342 vlc_value_t oldval, val;
344 p_config = config_FindConfig( p_this, psz_name );
349 msg_Warn( p_this, "option %s does not exist", psz_name );
353 if (!IsConfigIntegerType (p_config->i_type))
355 msg_Err( p_this, "option %s does not refer to an int", psz_name );
359 /* backup old value */
360 oldval.i_int = p_config->value.i;
362 /* if i_min == i_max == 0, then do not use them */
363 if ((p_config->min.i == 0) && (p_config->max.i == 0))
365 p_config->value.i = i_value;
367 else if (i_value < p_config->min.i)
369 p_config->value.i = p_config->min.i;
371 else if (i_value > p_config->max.i)
373 p_config->value.i = p_config->max.i;
377 p_config->value.i = i_value;
380 p_config->b_dirty = VLC_TRUE;
382 val.i_int = p_config->value.i;
384 if( p_config->pf_callback )
386 p_config->pf_callback( p_this, psz_name, oldval, val,
387 p_config->p_callback_data );
391 /*****************************************************************************
392 * config_PutFloat: set the value of a float variable
393 *****************************************************************************
394 * This function is used to set the value of variables which are internally
395 * represented by a float (CONFIG_ITEM_FLOAT).
396 *****************************************************************************/
397 void __config_PutFloat( vlc_object_t *p_this,
398 const char *psz_name, float f_value )
400 module_config_t *p_config;
401 vlc_value_t oldval, val;
403 p_config = config_FindConfig( p_this, psz_name );
408 msg_Warn( p_this, "option %s does not exist", psz_name );
412 if (!IsConfigFloatType (p_config->i_type))
414 msg_Err( p_this, "option %s does not refer to a float", psz_name );
418 /* backup old value */
419 oldval.f_float = p_config->value.f;
421 /* if f_min == f_max == 0, then do not use them */
422 if ((p_config->min.f == 0) && (p_config->max.f == 0))
424 p_config->value.f = f_value;
426 else if (f_value < p_config->min.f)
428 p_config->value.f = p_config->min.f;
430 else if (f_value > p_config->max.f)
432 p_config->value.f = p_config->max.f;
436 p_config->value.f = f_value;
439 p_config->b_dirty = VLC_TRUE;
441 val.f_float = p_config->value.f;
443 if( p_config->pf_callback )
445 p_config->pf_callback( p_this, psz_name, oldval, val,
446 p_config->p_callback_data );
450 /*****************************************************************************
451 * config_FindConfig: find the config structure associated with an option.
452 *****************************************************************************
453 * FIXME: This function really needs to be optimized.
454 * FIXME: And now even more.
455 *****************************************************************************/
456 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
461 if( !psz_name ) return NULL;
463 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
465 for( i_index = 0; i_index < p_list->i_count; i_index++ )
467 module_config_t *p_item, *p_end;
468 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
470 if( !p_parser->i_config_items )
473 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
477 if( p_item->i_type & CONFIG_HINT )
480 if( !strcmp( psz_name, p_item->psz_name ) )
482 vlc_list_release( p_list );
488 vlc_list_release( p_list );
493 /*****************************************************************************
494 * config_FindModule: find a specific module structure.
495 *****************************************************************************/
496 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
499 module_t *p_module, *p_result = NULL;
502 if( !psz_name ) return NULL;
504 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
506 for( i_index = 0; i_index < p_list->i_count; i_index++ )
508 p_module = (module_t *)p_list->p_values[i_index].p_object;
509 if( !strcmp( p_module->psz_object_name, psz_name ) )
516 vlc_list_release( p_list );
521 /*****************************************************************************
522 * config_Duplicate: creates a duplicate of a module's configuration data.
523 *****************************************************************************
524 * Unfortunatly we cannot work directly with the module's config data as
525 * this module might be unloaded from memory at any time (remember HideModule).
526 * This is why we need to create an exact copy of the config data.
527 *****************************************************************************/
528 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
532 const module_config_t *p_item, *p_end = p_orig + n;
534 /* Calculate the structure length */
535 p_module->i_config_items = 0;
536 p_module->i_bool_items = 0;
538 for( p_item = p_orig; p_item < p_end; p_item++ )
540 if( p_item->i_type & CONFIG_ITEM )
542 p_module->i_config_items++;
545 if( p_item->i_type == CONFIG_ITEM_BOOL )
547 p_module->i_bool_items++;
551 /* Allocate memory */
552 p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
553 if( p_module->p_config == NULL )
555 msg_Err( p_module, "config error: can't duplicate p_config" );
558 p_module->confsize = n;
560 /* Do the duplication job */
561 for( size_t i = 0; i < n ; i++ )
563 p_module->p_config[i] = p_orig[i];
565 if (IsConfigIntegerType (p_module->p_config[i].i_type))
567 p_module->p_config[i].orig.i = p_orig[i].value.i;
568 p_module->p_config[i].saved.i = p_orig[i].value.i;
571 if (IsConfigFloatType (p_module->p_config[i].i_type))
573 p_module->p_config[i].orig.f = p_orig[i].value.f;
574 p_module->p_config[i].saved.f = p_orig[i].value.f;
577 if (IsConfigStringType (p_module->p_config[i].i_type))
579 p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
580 p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
581 p_module->p_config[i].saved.psz = NULL;
584 p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
585 p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
586 p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
587 p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
588 p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
590 p_module->p_config[i].p_lock = &p_module->object_lock;
592 /* duplicate the string list */
593 if( p_orig[i].i_list )
595 if( p_orig[i].ppsz_list )
597 p_module->p_config[i].ppsz_list =
598 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
599 if( p_module->p_config[i].ppsz_list )
601 for( j = 0; j < p_orig[i].i_list; j++ )
602 p_module->p_config[i].ppsz_list[j] =
603 strdupnull (p_orig[i].ppsz_list[j]);
604 p_module->p_config[i].ppsz_list[j] = NULL;
607 if( p_orig[i].ppsz_list_text )
609 p_module->p_config[i].ppsz_list_text =
610 calloc( (p_orig[i].i_list + 1), sizeof(char *) );
611 if( p_module->p_config[i].ppsz_list_text )
613 for( j = 0; j < p_orig[i].i_list; j++ )
614 p_module->p_config[i].ppsz_list_text[j] =
615 strdupnull (_(p_orig[i].ppsz_list_text[j]));
616 p_module->p_config[i].ppsz_list_text[j] = NULL;
619 if( p_orig[i].pi_list )
621 p_module->p_config[i].pi_list =
622 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
623 if( p_module->p_config[i].pi_list )
625 for( j = 0; j < p_orig[i].i_list; j++ )
626 p_module->p_config[i].pi_list[j] =
627 p_orig[i].pi_list[j];
632 /* duplicate the actions list */
633 if( p_orig[i].i_action )
637 p_module->p_config[i].ppf_action =
638 malloc( p_orig[i].i_action * sizeof(void *) );
639 p_module->p_config[i].ppsz_action_text =
640 malloc( p_orig[i].i_action * sizeof(char *) );
642 for( j = 0; j < p_orig[i].i_action; j++ )
644 p_module->p_config[i].ppf_action[j] =
645 p_orig[i].ppf_action[j];
646 p_module->p_config[i].ppsz_action_text[j] =
647 strdupnull (p_orig[i].ppsz_action_text[j]);
651 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
657 /*****************************************************************************
658 * config_Free: frees a duplicated module's configuration data.
659 *****************************************************************************
660 * This function frees all the data duplicated by config_Duplicate.
661 *****************************************************************************/
662 void config_Free( module_t *p_module )
666 for (size_t j = 0; j < p_module->confsize; j++)
668 module_config_t *p_item = p_module->p_config + j;
670 free( (char*) p_item->psz_type );
671 free( (char*) p_item->psz_name );
672 free( (char*) p_item->psz_current );
673 free( (char*) p_item->psz_text );
674 free( (char*) p_item->psz_longtext );
676 if (IsConfigStringType (p_item->i_type))
678 free ((char *)p_item->value.psz);
679 free ((char *)p_item->orig.psz);
680 free ((char *)p_item->saved.psz);
685 for( i = 0; i < p_item->i_list; i++ )
687 if( p_item->ppsz_list && p_item->ppsz_list[i] )
688 free( (char*) p_item->ppsz_list[i] );
689 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
690 free( (char*) p_item->ppsz_list_text[i] );
692 if( p_item->ppsz_list ) free( p_item->ppsz_list );
693 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
694 if( p_item->pi_list ) free( p_item->pi_list );
697 if( p_item->i_action )
699 for( i = 0; i < p_item->i_action; i++ )
701 free( (char*) p_item->ppsz_action_text[i] );
703 if( p_item->ppf_action ) free( p_item->ppf_action );
704 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
708 if (p_module->p_config != NULL)
710 free (p_module->p_config);
711 p_module->p_config = NULL;
715 /*****************************************************************************
716 * config_SetCallbacks: sets callback functions in the duplicate p_config.
717 *****************************************************************************
718 * Unfortunatly we cannot work directly with the module's config data as
719 * this module might be unloaded from memory at any time (remember HideModule).
720 * This is why we need to duplicate callbacks each time we reload the module.
721 *****************************************************************************/
722 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
725 for (size_t i = 0; i < n; i++)
727 p_new->pf_callback = p_orig->pf_callback;
733 /*****************************************************************************
734 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
735 *****************************************************************************
736 * We simply undo what we did in config_SetCallbacks.
737 *****************************************************************************/
738 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
740 for (size_t i = 0; i < n; i++)
742 p_new->pf_callback = NULL;
747 /*****************************************************************************
748 * config_ResetAll: reset the configuration data for all the modules.
749 *****************************************************************************/
750 void __config_ResetAll( vlc_object_t *p_this )
756 /* Acquire config file lock */
757 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
759 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
761 for( i_index = 0; i_index < p_list->i_count; i_index++ )
763 p_module = (module_t *)p_list->p_values[i_index].p_object ;
764 if( p_module->b_submodule ) continue;
766 for (size_t i = 0; i < p_module->confsize; i++ )
768 if (IsConfigIntegerType (p_module->p_config[i].i_type))
769 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
771 if (IsConfigFloatType (p_module->p_config[i].i_type))
772 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
774 if (IsConfigStringType (p_module->p_config[i].i_type))
776 free ((char *)p_module->p_config[i].value.psz);
777 p_module->p_config[i].value.psz =
778 strdupnull (p_module->p_config[i].orig.psz);
783 vlc_list_release( p_list );
784 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
788 static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
790 char *psz_filename = p_obj->p_libvlc->psz_configfile;
795 psz_filename = config_GetConfigFile( p_obj->p_libvlc );
798 msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
800 p_stream = utf8_fopen( psz_filename, mode );
801 if( p_stream == NULL && errno != ENOENT )
803 msg_Err( p_obj, "cannot open config file (%s): %m",
807 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
808 else if( p_stream == NULL && errno == ENOENT && mode[0] == 'r' )
810 /* This is the fallback for pre XDG Base Directory
811 * Specification configs */
813 if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
814 p_obj->p_libvlc->psz_homedir ) != -1 )
816 p_stream = utf8_fopen( psz_old, mode );
819 /* Old config file found. We want to write it at the
820 * new location now. */
821 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
822 "VLC will now use %s.", psz_old, psz_filename );
824 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
825 p_obj->p_libvlc->psz_homedir ) != -1 )
827 FILE *p_readme = utf8_fopen( psz_readme, "wt" );
830 fputs( "The VLC media player configuration folder has "
831 "moved to comply with the XDG Base "
832 "Directory Specification version 0.6. Your "
833 "configuration has been copied to the new "
834 "location (", p_readme );
835 fputs( p_obj->p_libvlc->psz_configdir, p_readme );
836 fputs( "). You can delete this directory and "
837 "all its contents.", p_readme );
847 else if( p_stream != NULL )
849 p_obj->p_libvlc->psz_configfile = psz_filename;
856 static int strtoi (const char *str)
862 l = strtol (str, &end, 0);
866 if ((l > INT_MAX) || (l < INT_MIN))
875 /*****************************************************************************
876 * config_LoadConfigFile: loads the configuration file.
877 *****************************************************************************
878 * This function is called to load the config options stored in the config
880 *****************************************************************************/
881 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
886 file = config_OpenConfigFile (p_this, "rt");
890 /* Acquire config file lock */
891 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
893 /* Look for the selected module, if NULL then save everything */
894 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
896 /* Look for UTF-8 Byte Order Mark */
897 char * (*convert) (const char *) = strdupnull;
900 if ((fread (bom, 1, 3, file) != 3)
901 || memcmp (bom, "\xEF\xBB\xBF", 3))
903 convert = FromLocaleDup;
904 rewind (file); /* no BOM, rewind */
907 module_t *module = NULL;
908 char line[1024], section[1022];
911 while (fgets (line, 1024, file) != NULL)
913 /* Ignore comments and empty lines */
924 char *ptr = strchr (line, ']');
926 continue; /* syntax error; */
929 /* New section ( = a given module) */
930 strcpy (section, line + 1);
933 if ((psz_module_name == NULL)
934 || (strcmp (psz_module_name, section) == 0))
936 for (int i = 0; i < p_list->i_count; i++)
938 module_t *m = (module_t *)p_list->p_values[i].p_object;
940 if ((strcmp (section, m->psz_object_name) == 0)
941 && (m->i_config_items > 0)) /* ignore config-less modules */
944 if (psz_module_name != NULL)
946 "loading config for module \"%s\"",
957 continue; /* no need to parse if there is no matching module */
959 char *ptr = strchr (line, '\n');
963 /* look for option name */
964 const char *psz_option_name = line;
966 ptr = strchr (line, '=');
968 continue; /* syntax error */
971 const char *psz_option_value = ptr + 1;
973 /* try to match this option with one of the module's options */
974 for (size_t i = 0; i < module->confsize; i++)
976 module_config_t *p_item = module->p_config + i;
978 if ((p_item->i_type & CONFIG_HINT)
979 || strcmp (p_item->psz_name, psz_option_name))
985 switch( p_item->i_type )
987 case CONFIG_ITEM_BOOL:
988 case CONFIG_ITEM_INTEGER:
990 long l = strtoi (psz_option_value);
992 msg_Warn (p_this, "Integer value (%s) for %s: %m",
993 psz_option_value, psz_option_name);
995 p_item->saved.i = p_item->value.i = (int)l;
999 case CONFIG_ITEM_FLOAT:
1000 if( !*psz_option_value )
1001 break; /* ignore empty option */
1002 p_item->value.f = (float)i18n_atof( psz_option_value);
1003 p_item->saved.f = p_item->value.f;
1006 case CONFIG_ITEM_KEY:
1007 if( !*psz_option_value )
1008 break; /* ignore empty option */
1009 p_item->value.i = ConfigStringToKey(psz_option_value);
1010 p_item->saved.i = p_item->value.i;
1014 vlc_mutex_lock( p_item->p_lock );
1016 /* free old string */
1017 free( (char*) p_item->value.psz );
1018 free( (char*) p_item->saved.psz );
1020 p_item->value.psz = convert (psz_option_value);
1021 p_item->saved.psz = strdupnull (p_item->value.psz);
1023 vlc_mutex_unlock( p_item->p_lock );
1033 msg_Err (p_this, "error reading configuration: %m");
1038 vlc_list_release( p_list );
1040 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1044 /*****************************************************************************
1045 * config_CreateDir: Create configuration directory if it doesn't exist.
1046 *****************************************************************************/
1047 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
1049 if( !psz_dirname && !*psz_dirname ) return -1;
1051 if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
1061 /* Let's try to create the parent directory */
1062 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
1063 strcpy( psz_parent, psz_dirname );
1065 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
1066 if( psz_end && psz_end != psz_parent )
1069 if( config_CreateDir( p_this, psz_parent ) == 0 )
1071 if( !utf8_mkdir( psz_dirname, 0700 ) )
1078 msg_Err( p_this, "could not create %s: %m", psz_dirname );
1082 /*****************************************************************************
1083 * config_SaveConfigFile: Save a module's config options.
1084 *****************************************************************************
1085 * This will save the specified module's config options to the config file.
1086 * If psz_module_name is NULL then we save all the modules config options.
1087 * It's no use to save the config options that kept their default values, so
1088 * we'll try to be a bit clever here.
1090 * When we save we mustn't delete the config options of the modules that
1091 * haven't been loaded. So we cannot just create a new config file with the
1092 * config structures we've got in memory.
1093 * I don't really know how to deal with this nicely, so I will use a completly
1095 * I will load the config file in memory, but skipping all the sections of the
1096 * modules we want to save. Then I will create a brand new file, dump the file
1097 * loaded in memory and then append the sections of the modules we want to
1099 * Really stupid no ?
1100 *****************************************************************************/
1101 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
1102 vlc_bool_t b_autosave )
1107 char p_line[1024], *p_index2;
1109 char *p_bigbuffer, *p_index;
1110 vlc_bool_t b_backup;
1113 /* Acquire config file lock */
1114 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1116 if( p_this->p_libvlc->psz_configfile == NULL )
1118 const char *psz_configdir = p_this->p_libvlc->psz_configdir;
1119 if( !psz_configdir ) /* XXX: This should never happen */
1121 msg_Err( p_this, "no configuration directory defined" );
1122 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1126 config_CreateDir( p_this, psz_configdir );
1129 file = config_OpenConfigFile( p_this, "rt" );
1132 /* look for file size */
1133 fseek( file, 0L, SEEK_END );
1134 i_sizebuf = ftell( file );
1135 fseek( file, 0L, SEEK_SET );
1138 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1141 msg_Err( p_this, "out of memory" );
1142 if( file ) fclose( file );
1143 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1148 /* List all available modules */
1149 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1151 /* backup file into memory, we only need to backup the sections we won't
1154 while( file && fgets( p_line, 1024, file ) )
1156 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1159 /* we found a section, check if we need to do a backup */
1160 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1162 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1164 if( ((p_index2 - &p_line[1])
1165 == (int)strlen(p_parser->psz_object_name) )
1166 && !memcmp( &p_line[1], p_parser->psz_object_name,
1167 strlen(p_parser->psz_object_name) ) )
1169 if( !psz_module_name )
1171 else if( !strcmp( psz_module_name,
1172 p_parser->psz_object_name ) )
1177 if( i_index == p_list->i_count )
1179 /* we don't have this section in our list so we need to back
1183 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1196 /* save line if requested and line is valid (doesn't begin with a
1197 * space, tab, or eol) */
1198 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1199 && (p_line[0] != '\t') )
1201 strcpy( p_index, p_line );
1202 p_index += strlen( p_line );
1205 if( file ) fclose( file );
1209 * Save module config in file
1212 file = config_OpenConfigFile (p_this, "wt");
1215 vlc_list_release( p_list );
1216 free( p_bigbuffer );
1217 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1221 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
1222 "###\n### lines begining with a '#' character are comments\n###\n\n" );
1224 /* Look for the selected module, if NULL then save everything */
1225 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1227 module_config_t *p_item, *p_end;
1228 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1230 if( psz_module_name && strcmp( psz_module_name,
1231 p_parser->psz_object_name ) )
1234 if( !p_parser->i_config_items )
1237 if( psz_module_name )
1238 msg_Dbg( p_this, "saving config for module \"%s\"",
1239 p_parser->psz_object_name );
1241 fprintf( file, "[%s]", p_parser->psz_object_name );
1242 if( p_parser->psz_longname )
1243 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1245 fprintf( file, "\n\n" );
1247 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1252 int i_value = p_item->value.i;
1253 float f_value = p_item->value.f;
1254 const char *psz_value = p_item->value.psz;
1256 if( p_item->i_type & CONFIG_HINT )
1259 /* Ignore deprecated options */
1260 if( p_item->psz_current )
1262 if( p_item->b_unsaveable )
1266 if( b_autosave && !p_item->b_autosave )
1268 i_value = p_item->saved.i;
1269 f_value = p_item->saved.f;
1270 psz_value = p_item->saved.psz;
1271 if( !psz_value ) psz_value = p_item->orig.psz;
1275 p_item->b_dirty = VLC_FALSE;
1278 switch( p_item->i_type )
1280 case CONFIG_ITEM_BOOL:
1281 case CONFIG_ITEM_INTEGER:
1282 if( p_item->psz_text )
1283 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1284 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1285 _("boolean") : _("integer") );
1286 if( i_value == p_item->orig.i )
1288 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1290 p_item->saved.i = i_value;
1293 case CONFIG_ITEM_KEY:
1294 if( p_item->psz_text )
1295 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1297 if( i_value == p_item->orig.i )
1299 psz_key = ConfigKeyToString( i_value );
1300 fprintf( file, "%s=%s\n", p_item->psz_name,
1301 psz_key ? psz_key : "" );
1304 p_item->saved.i = i_value;
1307 case CONFIG_ITEM_FLOAT:
1308 if( p_item->psz_text )
1309 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1311 if( f_value == p_item->orig.f )
1313 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1315 p_item->saved.f = f_value;
1319 if( p_item->psz_text )
1320 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1322 if( (!psz_value && !p_item->orig.psz) ||
1323 (psz_value && p_item->orig.psz &&
1324 !strcmp( psz_value, p_item->orig.psz )) )
1326 fprintf( file, "%s=%s\n", p_item->psz_name,
1329 if( b_autosave && !p_item->b_autosave ) break;
1331 free ((char *)p_item->saved.psz);
1332 if( (psz_value && p_item->orig.psz &&
1333 strcmp( psz_value, p_item->orig.psz )) ||
1334 !psz_value || !p_item->orig.psz)
1335 p_item->saved.psz = strdupnull (psz_value);
1337 p_item->saved.psz = NULL;
1344 vlc_list_release( p_list );
1347 * Restore old settings from the config in file
1349 fputs( p_bigbuffer, file );
1350 free( p_bigbuffer );
1353 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1358 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1361 int i_index, i_count;
1363 if( !p_this ) return -1;
1365 /* Check if there's anything to save */
1366 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1367 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1368 i_count = p_list->i_count;
1369 for( i_index = 0; i_index < i_count; i_index++ )
1371 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1372 module_config_t *p_item, *p_end;
1374 if( !p_parser->i_config_items ) continue;
1376 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1380 if( p_item->b_autosave && p_item->b_dirty ) break;
1384 vlc_list_release( p_list );
1385 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1387 if( i_index == i_count ) return VLC_SUCCESS;
1388 return SaveConfigFile( p_this, 0, VLC_TRUE );
1391 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1393 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1396 /*****************************************************************************
1397 * config_LoadCmdLine: parse command line
1398 *****************************************************************************
1399 * Parse command line for configuration options.
1400 * Now that the module_bank has been initialized, we can dynamically
1401 * generate the longopts structure used by getops. We have to do it this way
1402 * because we don't know (and don't want to know) in advance the configuration
1403 * options used (ie. exported) by each module.
1404 *****************************************************************************/
1405 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc,
1406 const char *ppsz_argv[],
1407 vlc_bool_t b_ignore_errors )
1409 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1412 struct option *p_longopts;
1413 int i_modules_index;
1416 module_config_t *pp_shortopts[256];
1417 char *psz_shortopts;
1419 /* Set default configuration and copy arguments */
1420 p_this->p_libvlc->i_argc = *pi_argc;
1421 p_this->p_libvlc->ppsz_argv = ppsz_argv;
1424 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
1425 * is the PSN - process serial number (a unique PID-ish thingie)
1426 * still ok for real Darwin & when run from command line */
1427 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1428 /* for example -psn_0_9306113 */
1430 /* GDMF!... I can't do this or else the MacOSX window server will
1431 * not pick up the PSN and not register the app and we crash...
1432 * hence the following kludge otherwise we'll get confused w/ argv[1]
1433 * being an input file name */
1435 ppsz_argv[ 1 ] = NULL;
1437 *pi_argc = *pi_argc - 1;
1443 /* List all modules */
1444 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1447 * Generate the longopts and shortopts structures used by getopt_long
1451 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1454 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1456 /* count the number of exported configuration options (to allocate
1457 * longopts). We also need to allocate space for two options when
1458 * dealing with boolean to allow for --foo and --no-foo */
1459 i_opts += p_parser->i_config_items
1460 + 2 * p_parser->i_bool_items;
1463 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1464 if( p_longopts == NULL )
1466 msg_Err( p_this, "out of memory" );
1467 vlc_list_release( p_list );
1471 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1472 if( psz_shortopts == NULL )
1474 msg_Err( p_this, "out of memory" );
1476 vlc_list_release( p_list );
1480 /* If we are requested to ignore errors, then we must work on a copy
1481 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1482 * us, ignoring the arity of the options */
1483 if( b_ignore_errors )
1485 ppsz_argv = (const char**)malloc( *pi_argc * sizeof(char *) );
1486 if( ppsz_argv == NULL )
1488 msg_Err( p_this, "out of memory" );
1489 free( psz_shortopts );
1491 vlc_list_release( p_list );
1494 memcpy( ppsz_argv, p_this->p_libvlc->ppsz_argv,
1495 *pi_argc * sizeof(char *) );
1499 for( i_index = 0; i_index < 256; i_index++ )
1501 pp_shortopts[i_index] = NULL;
1504 /* Fill the p_longopts and psz_shortopts structures */
1506 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1509 module_config_t *p_item, *p_end;
1510 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1512 if( !p_parser->i_config_items )
1515 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1520 if( p_item->i_type & CONFIG_HINT )
1523 /* Add item to long options */
1524 p_longopts[i_index].name = strdup( p_item->psz_name );
1525 if( p_longopts[i_index].name == NULL ) continue;
1526 p_longopts[i_index].has_arg =
1527 (p_item->i_type == CONFIG_ITEM_BOOL)?
1528 no_argument : required_argument;
1529 p_longopts[i_index].flag = &flag;
1530 p_longopts[i_index].val = 0;
1533 /* When dealing with bools we also need to add the --no-foo
1535 if( p_item->i_type == CONFIG_ITEM_BOOL )
1537 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1538 if( psz_name == NULL ) continue;
1539 strcpy( psz_name, "no" );
1540 strcat( psz_name, p_item->psz_name );
1542 p_longopts[i_index].name = psz_name;
1543 p_longopts[i_index].has_arg = no_argument;
1544 p_longopts[i_index].flag = &flag;
1545 p_longopts[i_index].val = 1;
1548 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1549 if( psz_name == NULL ) continue;
1550 strcpy( psz_name, "no-" );
1551 strcat( psz_name, p_item->psz_name );
1553 p_longopts[i_index].name = psz_name;
1554 p_longopts[i_index].has_arg = no_argument;
1555 p_longopts[i_index].flag = &flag;
1556 p_longopts[i_index].val = 1;
1560 /* If item also has a short option, add it */
1561 if( p_item->i_short )
1563 pp_shortopts[(int)p_item->i_short] = p_item;
1564 psz_shortopts[i_shortopts] = p_item->i_short;
1566 if( p_item->i_type != CONFIG_ITEM_BOOL )
1568 psz_shortopts[i_shortopts] = ':';
1571 if( p_item->i_short == 'v' )
1573 psz_shortopts[i_shortopts] = ':';
1581 /* We don't need the module list anymore */
1582 vlc_list_release( p_list );
1584 /* Close the longopts and shortopts structures */
1585 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1586 psz_shortopts[i_shortopts] = '\0';
1589 * Parse the command line options
1592 optind = 0; /* set to 0 to tell GNU getopt to reinitialize */
1593 while( ( i_cmd = getopt_long( *pi_argc, (char **)ppsz_argv, psz_shortopts,
1594 p_longopts, &i_index ) ) != -1 )
1596 /* A long option has been recognized */
1599 module_config_t *p_conf;
1600 char *psz_name = (char *)p_longopts[i_index].name;
1602 /* Check if we deal with a --nofoo or --no-foo long option */
1603 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1605 /* Store the configuration option */
1606 p_conf = config_FindConfig( p_this, psz_name );
1609 /* Check if the option is deprecated */
1610 if( p_conf->psz_current )
1612 if( p_conf->b_strict )
1615 "Warning: option --%s no longer exists.\n",
1621 "%s: option --%s is deprecated. Use --%s instead.\n",
1622 b_ignore_errors ? "Warning" : "Error",
1623 p_conf->psz_name, p_conf->psz_current);
1624 if( !b_ignore_errors )
1627 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1628 free( (char *)p_longopts[i_index].name );
1631 free( psz_shortopts );
1635 psz_name = (char *)p_conf->psz_current;
1636 p_conf = config_FindConfig( p_this, psz_name );
1639 switch( p_conf->i_type )
1641 case CONFIG_ITEM_STRING:
1642 case CONFIG_ITEM_PASSWORD:
1643 case CONFIG_ITEM_FILE:
1644 case CONFIG_ITEM_DIRECTORY:
1645 case CONFIG_ITEM_MODULE:
1646 case CONFIG_ITEM_MODULE_LIST:
1647 case CONFIG_ITEM_MODULE_LIST_CAT:
1648 case CONFIG_ITEM_MODULE_CAT:
1649 config_PutPsz( p_this, psz_name, optarg );
1651 case CONFIG_ITEM_INTEGER:
1652 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1654 case CONFIG_ITEM_FLOAT:
1655 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1657 case CONFIG_ITEM_KEY:
1658 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1660 case CONFIG_ITEM_BOOL:
1661 config_PutInt( p_this, psz_name, !flag );
1668 /* A short option has been recognized */
1669 if( pp_shortopts[i_cmd] != NULL )
1671 switch( pp_shortopts[i_cmd]->i_type )
1673 case CONFIG_ITEM_STRING:
1674 case CONFIG_ITEM_PASSWORD:
1675 case CONFIG_ITEM_FILE:
1676 case CONFIG_ITEM_DIRECTORY:
1677 case CONFIG_ITEM_MODULE:
1678 case CONFIG_ITEM_MODULE_CAT:
1679 case CONFIG_ITEM_MODULE_LIST:
1680 case CONFIG_ITEM_MODULE_LIST_CAT:
1681 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1683 case CONFIG_ITEM_INTEGER:
1688 if( *optarg == 'v' ) /* eg. -vvv */
1691 while( *optarg == 'v' )
1699 i_verbose += atoi( optarg ); /* eg. -v2 */
1704 i_verbose++; /* -v */
1706 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1711 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1712 strtol(optarg, 0, 0) );
1715 case CONFIG_ITEM_BOOL:
1716 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1723 /* Internal error: unknown option */
1724 if( !b_ignore_errors )
1726 fprintf( stderr, "%s: unknown option"
1727 " or missing mandatory argument ",
1728 p_this->p_libvlc->psz_object_name );
1731 fprintf( stderr, "`-%c'\n", optopt );
1735 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1737 fprintf( stderr, "Try `%s --help' for more information.\n",
1738 p_this->p_libvlc->psz_object_name );
1740 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1741 free( (char *)p_longopts[i_index].name );
1743 free( psz_shortopts );
1748 /* Free allocated resources */
1749 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1750 free( (char *)p_longopts[i_index].name );
1752 free( psz_shortopts );
1753 if( b_ignore_errors ) free( ppsz_argv );
1759 * config_GetDataDir: find directory where shared data is installed
1761 * @return a string (always succeeds).
1763 const char *config_GetDataDir( void )
1765 #if defined (WIN32) || defined (UNDER_CE)
1766 return vlc_global()->psz_vlcpath;
1767 #elif defined(__APPLE__) || defined (SYS_BEOS)
1768 static char path[PATH_MAX] = "";
1772 snprintf( path, sizeof( path ), "%s/share",
1773 vlc_global()->psz_vlcpath );
1774 path[sizeof( path ) - 1] = '\0';
1782 /*****************************************************************************
1783 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1784 *****************************************************************************
1785 * This function will try by different ways to find the user's home path.
1786 * Note that this function is not reentrant, it should be called only once
1787 * at the beginning of main where the result will be stored for later use.
1788 *****************************************************************************/
1789 static char *GetDir( vlc_bool_t b_appdata )
1791 const char *psz_localhome = NULL;
1793 #if defined(WIN32) && !defined(UNDER_CE)
1794 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1796 #ifndef CSIDL_FLAG_CREATE
1797 # define CSIDL_FLAG_CREATE 0x8000
1799 #ifndef CSIDL_APPDATA
1800 # define CSIDL_APPDATA 0x1A
1802 #ifndef CSIDL_PROFILE
1803 # define CSIDL_PROFILE 0x28
1805 #ifndef SHGFP_TYPE_CURRENT
1806 # define SHGFP_TYPE_CURRENT 0
1809 HINSTANCE shfolder_dll;
1810 SHGETFOLDERPATH SHGetFolderPath ;
1812 /* load the shfolder dll to retrieve SHGetFolderPath */
1813 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1815 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1816 _T("SHGetFolderPathW") );
1817 if ( SHGetFolderPath != NULL )
1819 wchar_t whomedir[MAX_PATH];
1821 /* get the "Application Data" folder for the current user */
1822 if( S_OK == SHGetFolderPath( NULL,
1823 (b_appdata ? CSIDL_APPDATA :
1824 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1825 NULL, SHGFP_TYPE_CURRENT,
1828 FreeLibrary( shfolder_dll );
1829 return FromWide( whomedir );
1832 FreeLibrary( shfolder_dll );
1835 #elif defined(UNDER_CE)
1837 #ifndef CSIDL_APPDATA
1838 # define CSIDL_APPDATA 0x1A
1841 wchar_t whomedir[MAX_PATH];
1843 /* get the "Application Data" folder for the current user */
1844 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
1845 return FromWide( whomedir );
1848 psz_localhome = getenv( "HOME" );
1849 if( psz_localhome == NULL )
1851 #if defined(HAVE_GETPWUID)
1852 struct passwd *p_pw;
1855 if( ( p_pw = getpwuid( getuid() ) ) != NULL )
1856 psz_localhome = p_pw->pw_dir;
1860 psz_localhome = getenv( "TMP" );
1861 if( psz_localhome == NULL )
1862 psz_localhome = "/tmp";
1866 return FromLocaleDup( psz_localhome );
1870 * Get the user's home directory
1872 char *config_GetHomeDir( void )
1874 return GetDir( VLC_FALSE );
1878 * Get the user's main data and config directory:
1879 * - on windows that's the App Data directory;
1880 * - on other OSes it's the same as the home directory.
1882 char *config_GetUserDir( void );
1883 char *config_GetUserDir( void )
1885 return GetDir( VLC_TRUE );
1889 * Get the user's VLC configuration directory
1891 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
1894 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
1895 char *psz_parent = config_GetUserDir();
1896 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
1897 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
1901 /* XDG Base Directory Specification - Version 0.6 */
1902 char *psz_env = getenv( "XDG_CONFIG_HOME" );
1905 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
1909 psz_env = getenv( "HOME" );
1910 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
1911 if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
1918 * Get the user's VLC data directory
1919 * (used for stuff like the skins, custom lua modules, ...)
1921 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
1924 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
1925 char *psz_parent = config_GetUserDir();
1926 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
1927 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
1931 /* XDG Base Directory Specification - Version 0.6 */
1932 char *psz_env = getenv( "XDG_DATA_HOME" );
1935 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
1939 psz_env = getenv( "HOME" );
1940 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
1941 if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
1948 * Get the user's VLC cache directory
1949 * (used for stuff like the modules cache, the album art cache, ...)
1951 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
1954 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
1955 char *psz_parent = config_GetUserDir();
1956 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
1957 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
1961 /* XDG Base Directory Specification - Version 0.6 */
1962 char *psz_env = getenv( "XDG_CACHE_HOME" );
1965 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
1969 psz_env = getenv( "HOME" );
1970 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
1971 if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
1978 * Get the user's configuration file
1980 char *config_GetConfigFile( libvlc_int_t *p_libvlc )
1982 char *psz_configfile;
1983 if( asprintf( &psz_configfile, "%s" DIR_SEP CONFIG_FILE,
1984 p_libvlc->psz_configdir ) == -1 )
1986 return psz_configfile;
1990 * Get the user's configuration file when given with the --config option
1992 char *config_GetCustomConfigFile( libvlc_int_t *p_libvlc )
1994 char *psz_configfile = config_GetPsz( p_libvlc, "config" );
1995 if( psz_configfile != NULL )
1997 if( psz_configfile[0] == '~' && psz_configfile[1] == '/' )
1999 /* This is incomplete: we should also support the ~cmassiot/ syntax */
2001 if( asprintf( &psz_buf, "%s/%s", p_libvlc->psz_homedir,
2002 psz_configfile + 2 ) == -1 )
2004 free( psz_configfile );
2007 free( psz_configfile );
2008 psz_configfile = psz_buf;
2011 return psz_configfile;
2014 static int ConfigStringToKey( const char *psz_key )
2018 const char *psz_parser = strchr( psz_key, '-' );
2019 while( psz_parser && psz_parser != psz_key )
2021 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
2023 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
2024 strlen( vlc_modifiers[i].psz_key_string ) ) )
2026 i_key |= vlc_modifiers[i].i_key_code;
2029 psz_key = psz_parser + 1;
2030 psz_parser = strchr( psz_key, '-' );
2032 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
2034 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
2036 i_key |= vlc_keys[i].i_key_code;
2043 static char *ConfigKeyToString( int i_key )
2045 char *psz_key = malloc( 100 );
2055 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
2058 if( i_key & vlc_modifiers[index].i_key_code )
2060 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
2063 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
2066 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
2068 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );
2075 /* Adds an extra interface to the configuration */
2076 void __config_AddIntf( vlc_object_t *p_this, const char *psz_intf )
2080 char *psz_config, *psz_parser;
2081 size_t i_len = strlen( psz_intf );
2083 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
2086 if( !strncmp( psz_intf, psz_parser, i_len ) )
2091 psz_parser = strchr( psz_parser, ':' );
2092 if( psz_parser ) psz_parser++; /* skip the ':' */
2096 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
2099 if( !strncmp( psz_intf, psz_parser, i_len ) )
2104 psz_parser = strchr( psz_parser, ':' );
2105 if( psz_parser ) psz_parser++; /* skip the ':' */
2108 /* interface not found in the config, let's add it */
2109 if( psz_config && strlen( psz_config ) > 0 )
2111 char *psz_newconfig;
2112 if( asprintf( &psz_newconfig, "%s:%s", psz_config, psz_intf ) != -1 )
2114 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
2115 free( psz_newconfig );
2119 config_PutPsz( p_this->p_libvlc, "extraintf", psz_intf );
2124 /* Removes an extra interface from the configuration */
2125 void __config_RemoveIntf( vlc_object_t *p_this, const char *psz_intf )
2129 char *psz_config, *psz_parser;
2130 size_t i_len = strlen( psz_intf );
2132 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
2135 if( !strncmp( psz_intf, psz_parser, i_len ) )
2137 char *psz_newconfig;
2138 char *psz_end = psz_parser + i_len;
2139 if( *psz_end == ':' ) psz_end++;
2141 if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
2143 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
2144 free( psz_newconfig );
2148 psz_parser = strchr( psz_parser, ':' );
2149 if( psz_parser ) psz_parser++; /* skip the ':' */
2153 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
2156 if( !strncmp( psz_intf, psz_parser, i_len ) )
2158 char *psz_newconfig;
2159 char *psz_end = psz_parser + i_len;
2160 if( *psz_end == ':' ) psz_end++;
2162 if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
2164 config_PutPsz( p_this->p_libvlc, "control", psz_newconfig );
2165 free( psz_newconfig );
2169 psz_parser = strchr( psz_parser, ':' );
2170 if( psz_parser ) psz_parser++; /* skip the ':' */
2176 * Returns VLC_TRUE if the specified extra interface is present in the
2177 * configuration, VLC_FALSE if not
2179 vlc_bool_t __config_ExistIntf( vlc_object_t *p_this, const char *psz_intf )
2183 char *psz_config, *psz_parser;
2184 size_t i_len = strlen( psz_intf );
2186 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
2189 if( !strncmp( psz_parser, psz_intf, i_len ) )
2194 psz_parser = strchr( psz_parser, ':' );
2195 if( psz_parser ) psz_parser++; /* skip the ':' */
2199 psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
2202 if( !strncmp( psz_parser, psz_intf, i_len ) )
2207 psz_parser = strchr( psz_parser, ':' );
2208 if( psz_parser ) psz_parser++; /* skip the ':' */