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 i_type = VLC_VAR_INTEGER;
146 case CONFIG_ITEM_FLOAT:
147 i_type = VLC_VAR_FLOAT;
150 case CONFIG_ITEM_MODULE:
151 case CONFIG_ITEM_MODULE_CAT:
152 case CONFIG_ITEM_MODULE_LIST:
153 case CONFIG_ITEM_MODULE_LIST_CAT:
154 i_type = VLC_VAR_MODULE;
157 case CONFIG_ITEM_STRING:
158 i_type = VLC_VAR_STRING;
161 case CONFIG_ITEM_PASSWORD:
162 i_type = VLC_VAR_STRING;
165 case CONFIG_ITEM_FILE:
166 i_type = VLC_VAR_FILE;
169 case CONFIG_ITEM_DIRECTORY:
170 i_type = VLC_VAR_DIRECTORY;
181 /*****************************************************************************
182 * config_GetInt: get the value of an int variable
183 *****************************************************************************
184 * This function is used to get the value of variables which are internally
185 * represented by an integer (CONFIG_ITEM_INTEGER and
187 *****************************************************************************/
188 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
190 module_config_t *p_config;
192 p_config = config_FindConfig( p_this, psz_name );
197 msg_Err( p_this, "option %s does not exist", psz_name );
201 if (!IsConfigIntegerType (p_config->i_type))
203 msg_Err( p_this, "option %s does not refer to an int", psz_name );
207 return p_config->value.i;
210 /*****************************************************************************
211 * config_GetFloat: get the value of a float variable
212 *****************************************************************************
213 * This function is used to get the value of variables which are internally
214 * represented by a float (CONFIG_ITEM_FLOAT).
215 *****************************************************************************/
216 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
218 module_config_t *p_config;
220 p_config = config_FindConfig( p_this, psz_name );
225 msg_Err( p_this, "option %s does not exist", psz_name );
229 if (!IsConfigFloatType (p_config->i_type))
231 msg_Err( p_this, "option %s does not refer to a float", psz_name );
235 return p_config->value.f;
238 /*****************************************************************************
239 * config_GetPsz: get the string value of a string variable
240 *****************************************************************************
241 * This function is used to get the value of variables which are internally
242 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
243 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
245 * Important note: remember to free() the returned char* because it's a
246 * duplicate of the actual value. It isn't safe to return a pointer to the
247 * actual value as it can be modified at any time.
248 *****************************************************************************/
249 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
251 module_config_t *p_config;
253 p_config = config_FindConfig( p_this, psz_name );
258 msg_Err( p_this, "option %s does not exist", psz_name );
262 if (!IsConfigStringType (p_config->i_type))
264 msg_Err( p_this, "option %s does not refer to a string", psz_name );
268 /* return a copy of the string */
269 vlc_mutex_lock( p_config->p_lock );
270 char *psz_value = strdupnull (p_config->value.psz);
271 vlc_mutex_unlock( p_config->p_lock );
276 /*****************************************************************************
277 * config_PutPsz: set the string value of a string variable
278 *****************************************************************************
279 * This function is used to set the value of variables which are internally
280 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
281 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
282 *****************************************************************************/
283 void __config_PutPsz( vlc_object_t *p_this,
284 const char *psz_name, const char *psz_value )
286 module_config_t *p_config;
287 vlc_value_t oldval, val;
289 p_config = config_FindConfig( p_this, psz_name );
295 msg_Warn( p_this, "option %s does not exist", psz_name );
299 if (!IsConfigStringType (p_config->i_type))
301 msg_Err( p_this, "option %s does not refer to a string", psz_name );
305 vlc_mutex_lock( p_config->p_lock );
307 /* backup old value */
308 oldval.psz_string = (char *)p_config->value.psz;
310 if ((psz_value != NULL) && *psz_value)
311 p_config->value.psz = strdup (psz_value);
313 p_config->value.psz = NULL;
315 p_config->b_dirty = VLC_TRUE;
317 val.psz_string = (char *)p_config->value.psz;
319 vlc_mutex_unlock( p_config->p_lock );
321 if( p_config->pf_callback )
323 p_config->pf_callback( p_this, psz_name, oldval, val,
324 p_config->p_callback_data );
327 /* free old string */
328 if( oldval.psz_string ) free( oldval.psz_string );
331 /*****************************************************************************
332 * config_PutInt: set the integer value of an int variable
333 *****************************************************************************
334 * This function is used to set the value of variables which are internally
335 * represented by an integer (CONFIG_ITEM_INTEGER and
337 *****************************************************************************/
338 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
340 module_config_t *p_config;
341 vlc_value_t oldval, val;
343 p_config = config_FindConfig( p_this, psz_name );
348 msg_Warn( p_this, "option %s does not exist", psz_name );
352 if (!IsConfigIntegerType (p_config->i_type))
354 msg_Err( p_this, "option %s does not refer to an int", psz_name );
358 /* backup old value */
359 oldval.i_int = p_config->value.i;
361 /* if i_min == i_max == 0, then do not use them */
362 if ((p_config->min.i == 0) && (p_config->max.i == 0))
364 p_config->value.i = i_value;
366 else if (i_value < p_config->min.i)
368 p_config->value.i = p_config->min.i;
370 else if (i_value > p_config->max.i)
372 p_config->value.i = p_config->max.i;
376 p_config->value.i = i_value;
379 p_config->b_dirty = VLC_TRUE;
381 val.i_int = p_config->value.i;
383 if( p_config->pf_callback )
385 p_config->pf_callback( p_this, psz_name, oldval, val,
386 p_config->p_callback_data );
390 /*****************************************************************************
391 * config_PutFloat: set the value of a float variable
392 *****************************************************************************
393 * This function is used to set the value of variables which are internally
394 * represented by a float (CONFIG_ITEM_FLOAT).
395 *****************************************************************************/
396 void __config_PutFloat( vlc_object_t *p_this,
397 const char *psz_name, float f_value )
399 module_config_t *p_config;
400 vlc_value_t oldval, val;
402 p_config = config_FindConfig( p_this, psz_name );
407 msg_Warn( p_this, "option %s does not exist", psz_name );
411 if (!IsConfigFloatType (p_config->i_type))
413 msg_Err( p_this, "option %s does not refer to a float", psz_name );
417 /* backup old value */
418 oldval.f_float = p_config->value.f;
420 /* if f_min == f_max == 0, then do not use them */
421 if ((p_config->min.f == 0) && (p_config->max.f == 0))
423 p_config->value.f = f_value;
425 else if (f_value < p_config->min.f)
427 p_config->value.f = p_config->min.f;
429 else if (f_value > p_config->max.f)
431 p_config->value.f = p_config->max.f;
435 p_config->value.f = f_value;
438 p_config->b_dirty = VLC_TRUE;
440 val.f_float = p_config->value.f;
442 if( p_config->pf_callback )
444 p_config->pf_callback( p_this, psz_name, oldval, val,
445 p_config->p_callback_data );
449 /*****************************************************************************
450 * config_FindConfig: find the config structure associated with an option.
451 *****************************************************************************
452 * FIXME: This function really needs to be optimized.
453 * FIXME: And now even more.
454 *****************************************************************************/
455 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
460 if( !psz_name ) return NULL;
462 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
464 for( i_index = 0; i_index < p_list->i_count; i_index++ )
466 module_config_t *p_item, *p_end;
467 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
469 if( !p_parser->i_config_items )
472 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
476 if( p_item->i_type & CONFIG_HINT )
479 if( !strcmp( psz_name, p_item->psz_name ) )
481 vlc_list_release( p_list );
487 vlc_list_release( p_list );
492 /*****************************************************************************
493 * config_FindModule: find a specific module structure.
494 *****************************************************************************/
495 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
498 module_t *p_module, *p_result = NULL;
501 if( !psz_name ) return NULL;
503 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
505 for( i_index = 0; i_index < p_list->i_count; i_index++ )
507 p_module = (module_t *)p_list->p_values[i_index].p_object;
508 if( !strcmp( p_module->psz_object_name, psz_name ) )
515 vlc_list_release( p_list );
520 /*****************************************************************************
521 * config_Duplicate: creates a duplicate of a module's configuration data.
522 *****************************************************************************
523 * Unfortunatly we cannot work directly with the module's config data as
524 * this module might be unloaded from memory at any time (remember HideModule).
525 * This is why we need to create an exact copy of the config data.
526 *****************************************************************************/
527 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
531 const module_config_t *p_item, *p_end = p_orig + n;
533 /* Calculate the structure length */
534 p_module->i_config_items = 0;
535 p_module->i_bool_items = 0;
537 for( p_item = p_orig; p_item < p_end; p_item++ )
539 if( p_item->i_type & CONFIG_ITEM )
541 p_module->i_config_items++;
544 if( p_item->i_type == CONFIG_ITEM_BOOL )
546 p_module->i_bool_items++;
550 /* Allocate memory */
551 p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
552 if( p_module->p_config == NULL )
554 msg_Err( p_module, "config error: can't duplicate p_config" );
557 p_module->confsize = n;
559 /* Do the duplication job */
560 for( size_t i = 0; i < n ; i++ )
562 p_module->p_config[i] = p_orig[i];
564 if (IsConfigIntegerType (p_module->p_config[i].i_type))
566 p_module->p_config[i].orig.i = p_orig[i].value.i;
567 p_module->p_config[i].saved.i = p_orig[i].value.i;
570 if (IsConfigFloatType (p_module->p_config[i].i_type))
572 p_module->p_config[i].orig.f = p_orig[i].value.f;
573 p_module->p_config[i].saved.f = p_orig[i].value.f;
576 if (IsConfigStringType (p_module->p_config[i].i_type))
578 p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
579 p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
580 p_module->p_config[i].saved.psz = NULL;
583 p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
584 p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
585 p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
586 p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
587 p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
589 p_module->p_config[i].p_lock = &p_module->object_lock;
591 /* duplicate the string list */
592 if( p_orig[i].i_list )
594 if( p_orig[i].ppsz_list )
596 p_module->p_config[i].ppsz_list =
597 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
598 if( p_module->p_config[i].ppsz_list )
600 for( j = 0; j < p_orig[i].i_list; j++ )
601 p_module->p_config[i].ppsz_list[j] =
602 strdupnull (p_orig[i].ppsz_list[j]);
603 p_module->p_config[i].ppsz_list[j] = NULL;
606 if( p_orig[i].ppsz_list_text )
608 p_module->p_config[i].ppsz_list_text =
609 calloc( (p_orig[i].i_list + 1), sizeof(char *) );
610 if( p_module->p_config[i].ppsz_list_text )
612 for( j = 0; j < p_orig[i].i_list; j++ )
613 p_module->p_config[i].ppsz_list_text[j] =
614 strdupnull (_(p_orig[i].ppsz_list_text[j]));
615 p_module->p_config[i].ppsz_list_text[j] = NULL;
618 if( p_orig[i].pi_list )
620 p_module->p_config[i].pi_list =
621 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
622 if( p_module->p_config[i].pi_list )
624 for( j = 0; j < p_orig[i].i_list; j++ )
625 p_module->p_config[i].pi_list[j] =
626 p_orig[i].pi_list[j];
631 /* duplicate the actions list */
632 if( p_orig[i].i_action )
636 p_module->p_config[i].ppf_action =
637 malloc( p_orig[i].i_action * sizeof(void *) );
638 p_module->p_config[i].ppsz_action_text =
639 malloc( p_orig[i].i_action * sizeof(char *) );
641 for( j = 0; j < p_orig[i].i_action; j++ )
643 p_module->p_config[i].ppf_action[j] =
644 p_orig[i].ppf_action[j];
645 p_module->p_config[i].ppsz_action_text[j] =
646 strdupnull (p_orig[i].ppsz_action_text[j]);
650 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
656 /*****************************************************************************
657 * config_Free: frees a duplicated module's configuration data.
658 *****************************************************************************
659 * This function frees all the data duplicated by config_Duplicate.
660 *****************************************************************************/
661 void config_Free( module_t *p_module )
665 for (size_t j = 0; j < p_module->confsize; j++)
667 module_config_t *p_item = p_module->p_config + j;
669 free( (char*) p_item->psz_type );
670 free( (char*) p_item->psz_name );
671 free( (char*) p_item->psz_current );
672 free( (char*) p_item->psz_text );
673 free( (char*) p_item->psz_longtext );
675 if (IsConfigStringType (p_item->i_type))
677 free ((char *)p_item->value.psz);
678 free ((char *)p_item->orig.psz);
679 free ((char *)p_item->saved.psz);
684 for( i = 0; i < p_item->i_list; i++ )
686 if( p_item->ppsz_list && p_item->ppsz_list[i] )
687 free( (char*) p_item->ppsz_list[i] );
688 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
689 free( (char*) p_item->ppsz_list_text[i] );
691 if( p_item->ppsz_list ) free( p_item->ppsz_list );
692 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
693 if( p_item->pi_list ) free( p_item->pi_list );
696 if( p_item->i_action )
698 for( i = 0; i < p_item->i_action; i++ )
700 free( (char*) p_item->ppsz_action_text[i] );
702 if( p_item->ppf_action ) free( p_item->ppf_action );
703 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
707 if (p_module->p_config != NULL)
709 free (p_module->p_config);
710 p_module->p_config = NULL;
714 /*****************************************************************************
715 * config_SetCallbacks: sets callback functions in the duplicate p_config.
716 *****************************************************************************
717 * Unfortunatly we cannot work directly with the module's config data as
718 * this module might be unloaded from memory at any time (remember HideModule).
719 * This is why we need to duplicate callbacks each time we reload the module.
720 *****************************************************************************/
721 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
724 for (size_t i = 0; i < n; i++)
726 p_new->pf_callback = p_orig->pf_callback;
732 /*****************************************************************************
733 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
734 *****************************************************************************
735 * We simply undo what we did in config_SetCallbacks.
736 *****************************************************************************/
737 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
739 for (size_t i = 0; i < n; i++)
741 p_new->pf_callback = NULL;
746 /*****************************************************************************
747 * config_ResetAll: reset the configuration data for all the modules.
748 *****************************************************************************/
749 void __config_ResetAll( vlc_object_t *p_this )
755 /* Acquire config file lock */
756 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
758 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
760 for( i_index = 0; i_index < p_list->i_count; i_index++ )
762 p_module = (module_t *)p_list->p_values[i_index].p_object ;
763 if( p_module->b_submodule ) continue;
765 for (size_t i = 0; i < p_module->confsize; i++ )
767 if (IsConfigIntegerType (p_module->p_config[i].i_type))
768 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
770 if (IsConfigFloatType (p_module->p_config[i].i_type))
771 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
773 if (IsConfigStringType (p_module->p_config[i].i_type))
775 free ((char *)p_module->p_config[i].value.psz);
776 p_module->p_config[i].value.psz =
777 strdupnull (p_module->p_config[i].orig.psz);
782 vlc_list_release( p_list );
783 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
787 static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
789 char *psz_filename = p_obj->p_libvlc->psz_configfile;
794 psz_filename = config_GetConfigFile( p_obj->p_libvlc );
797 msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
799 p_stream = utf8_fopen( psz_filename, mode );
800 if( p_stream == NULL && errno != ENOENT )
802 msg_Err( p_obj, "cannot open config file (%s): %m",
806 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
807 else if( p_stream == NULL && errno == ENOENT && mode[0] == 'r' )
809 /* This is the fallback for pre XDG Base Directory
810 * Specification configs */
812 if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
813 p_obj->p_libvlc->psz_homedir ) != -1 )
815 p_stream = utf8_fopen( psz_old, mode );
818 /* Old config file found. We want to write it at the
819 * new location now. */
820 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
821 "VLC will now use %s.", psz_old, psz_filename );
823 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
824 p_obj->p_libvlc->psz_homedir ) != -1 )
826 FILE *p_readme = utf8_fopen( psz_readme, "wt" );
829 fputs( "The VLC media player configuration folder has "
830 "moved to comply with the XDG Base "
831 "Directory Specification version 0.6. Your "
832 "configuration has been copied to the new "
833 "location (", p_readme );
834 fputs( p_obj->p_libvlc->psz_configdir, p_readme );
835 fputs( "). You can delete this directory and "
836 "all its contents.", p_readme );
846 else if( p_stream != NULL )
848 p_obj->p_libvlc->psz_configfile = psz_filename;
855 static int strtoi (const char *str)
861 l = strtol (str, &end, 0);
865 if ((l > INT_MAX) || (l < INT_MIN))
874 /*****************************************************************************
875 * config_LoadConfigFile: loads the configuration file.
876 *****************************************************************************
877 * This function is called to load the config options stored in the config
879 *****************************************************************************/
880 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
885 file = config_OpenConfigFile (p_this, "rt");
889 /* Acquire config file lock */
890 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
892 /* Look for the selected module, if NULL then save everything */
893 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
895 /* Look for UTF-8 Byte Order Mark */
896 char * (*convert) (const char *) = strdupnull;
899 if ((fread (bom, 1, 3, file) != 3)
900 || memcmp (bom, "\xEF\xBB\xBF", 3))
902 convert = FromLocaleDup;
903 rewind (file); /* no BOM, rewind */
906 module_t *module = NULL;
907 char line[1024], section[1022];
910 while (fgets (line, 1024, file) != NULL)
912 /* Ignore comments and empty lines */
923 char *ptr = strchr (line, ']');
925 continue; /* syntax error; */
928 /* New section ( = a given module) */
929 strcpy (section, line + 1);
932 if ((psz_module_name == NULL)
933 || (strcmp (psz_module_name, section) == 0))
935 for (int i = 0; i < p_list->i_count; i++)
937 module_t *m = (module_t *)p_list->p_values[i].p_object;
939 if ((strcmp (section, m->psz_object_name) == 0)
940 && (m->i_config_items > 0)) /* ignore config-less modules */
943 if (psz_module_name != NULL)
945 "loading config for module \"%s\"",
956 continue; /* no need to parse if there is no matching module */
958 char *ptr = strchr (line, '\n');
962 /* look for option name */
963 const char *psz_option_name = line;
965 ptr = strchr (line, '=');
967 continue; /* syntax error */
970 const char *psz_option_value = ptr + 1;
972 /* try to match this option with one of the module's options */
973 for (size_t i = 0; i < module->confsize; i++)
975 module_config_t *p_item = module->p_config + i;
977 if ((p_item->i_type & CONFIG_HINT)
978 || strcmp (p_item->psz_name, psz_option_name))
984 switch( p_item->i_type )
986 case CONFIG_ITEM_BOOL:
987 case CONFIG_ITEM_INTEGER:
989 long l = strtoi (psz_option_value);
991 msg_Warn (p_this, "Integer value (%s) for %s: %m",
992 psz_option_value, psz_option_name);
994 p_item->saved.i = p_item->value.i = (int)l;
998 case CONFIG_ITEM_FLOAT:
999 if( !*psz_option_value )
1000 break; /* ignore empty option */
1001 p_item->value.f = (float)i18n_atof( psz_option_value);
1002 p_item->saved.f = p_item->value.f;
1005 case CONFIG_ITEM_KEY:
1006 if( !*psz_option_value )
1007 break; /* ignore empty option */
1008 p_item->value.i = ConfigStringToKey(psz_option_value);
1009 p_item->saved.i = p_item->value.i;
1013 vlc_mutex_lock( p_item->p_lock );
1015 /* free old string */
1016 free( (char*) p_item->value.psz );
1017 free( (char*) p_item->saved.psz );
1019 p_item->value.psz = convert (psz_option_value);
1020 p_item->saved.psz = strdupnull (p_item->value.psz);
1022 vlc_mutex_unlock( p_item->p_lock );
1032 msg_Err (p_this, "error reading configuration: %m");
1037 vlc_list_release( p_list );
1039 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1043 /*****************************************************************************
1044 * config_CreateDir: Create configuration directory if it doesn't exist.
1045 *****************************************************************************/
1046 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
1048 if( !psz_dirname && !*psz_dirname ) return -1;
1050 if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) )
1052 if( errno == ENOENT )
1054 /* Let's try to create the parent directory */
1055 char *psz_parent = strdup( psz_dirname );
1056 char *psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
1057 if( psz_end && psz_end != psz_parent )
1060 if( config_CreateDir( p_this, psz_parent ) == 0 )
1062 if( !utf8_mkdir( psz_dirname ) )
1071 msg_Err( p_this, "could not create %s: %m", psz_dirname );
1078 /*****************************************************************************
1079 * config_SaveConfigFile: Save a module's config options.
1080 *****************************************************************************
1081 * This will save the specified module's config options to the config file.
1082 * If psz_module_name is NULL then we save all the modules config options.
1083 * It's no use to save the config options that kept their default values, so
1084 * we'll try to be a bit clever here.
1086 * When we save we mustn't delete the config options of the modules that
1087 * haven't been loaded. So we cannot just create a new config file with the
1088 * config structures we've got in memory.
1089 * I don't really know how to deal with this nicely, so I will use a completly
1091 * I will load the config file in memory, but skipping all the sections of the
1092 * modules we want to save. Then I will create a brand new file, dump the file
1093 * loaded in memory and then append the sections of the modules we want to
1095 * Really stupid no ?
1096 *****************************************************************************/
1097 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
1098 vlc_bool_t b_autosave )
1103 char p_line[1024], *p_index2;
1105 char *p_bigbuffer, *p_index;
1106 vlc_bool_t b_backup;
1109 /* Acquire config file lock */
1110 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1112 if( p_this->p_libvlc->psz_configfile == NULL )
1114 const char *psz_configdir = p_this->p_libvlc->psz_configdir;
1115 if( !psz_configdir ) /* XXX: This should never happen */
1117 msg_Err( p_this, "no configuration directory defined" );
1118 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1122 config_CreateDir( p_this, psz_configdir );
1125 file = config_OpenConfigFile( p_this, "rt" );
1128 /* look for file size */
1129 fseek( file, 0L, SEEK_END );
1130 i_sizebuf = ftell( file );
1131 fseek( file, 0L, SEEK_SET );
1134 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1137 msg_Err( p_this, "out of memory" );
1138 if( file ) fclose( file );
1139 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1144 /* List all available modules */
1145 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1147 /* backup file into memory, we only need to backup the sections we won't
1150 while( file && fgets( p_line, 1024, file ) )
1152 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1155 /* we found a section, check if we need to do a backup */
1156 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1158 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1160 if( ((p_index2 - &p_line[1])
1161 == (int)strlen(p_parser->psz_object_name) )
1162 && !memcmp( &p_line[1], p_parser->psz_object_name,
1163 strlen(p_parser->psz_object_name) ) )
1165 if( !psz_module_name )
1167 else if( !strcmp( psz_module_name,
1168 p_parser->psz_object_name ) )
1173 if( i_index == p_list->i_count )
1175 /* we don't have this section in our list so we need to back
1179 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1192 /* save line if requested and line is valid (doesn't begin with a
1193 * space, tab, or eol) */
1194 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1195 && (p_line[0] != '\t') )
1197 strcpy( p_index, p_line );
1198 p_index += strlen( p_line );
1201 if( file ) fclose( file );
1205 * Save module config in file
1208 file = config_OpenConfigFile (p_this, "wt");
1211 vlc_list_release( p_list );
1212 free( p_bigbuffer );
1213 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1217 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
1218 "###\n### lines begining with a '#' character are comments\n###\n\n" );
1220 /* Look for the selected module, if NULL then save everything */
1221 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1223 module_config_t *p_item, *p_end;
1224 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1226 if( psz_module_name && strcmp( psz_module_name,
1227 p_parser->psz_object_name ) )
1230 if( !p_parser->i_config_items )
1233 if( psz_module_name )
1234 msg_Dbg( p_this, "saving config for module \"%s\"",
1235 p_parser->psz_object_name );
1237 fprintf( file, "[%s]", p_parser->psz_object_name );
1238 if( p_parser->psz_longname )
1239 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1241 fprintf( file, "\n\n" );
1243 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1248 int i_value = p_item->value.i;
1249 float f_value = p_item->value.f;
1250 const char *psz_value = p_item->value.psz;
1252 if( p_item->i_type & CONFIG_HINT )
1255 /* Ignore deprecated options */
1256 if( p_item->psz_current )
1258 if( p_item->b_unsaveable )
1262 if( b_autosave && !p_item->b_autosave )
1264 i_value = p_item->saved.i;
1265 f_value = p_item->saved.f;
1266 psz_value = p_item->saved.psz;
1267 if( !psz_value ) psz_value = p_item->orig.psz;
1271 p_item->b_dirty = VLC_FALSE;
1274 switch( p_item->i_type )
1276 case CONFIG_ITEM_BOOL:
1277 case CONFIG_ITEM_INTEGER:
1278 if( p_item->psz_text )
1279 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1280 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1281 _("boolean") : _("integer") );
1282 if( i_value == p_item->orig.i )
1284 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1286 p_item->saved.i = i_value;
1289 case CONFIG_ITEM_KEY:
1290 if( p_item->psz_text )
1291 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1293 if( i_value == p_item->orig.i )
1295 psz_key = ConfigKeyToString( i_value );
1296 fprintf( file, "%s=%s\n", p_item->psz_name,
1297 psz_key ? psz_key : "" );
1300 p_item->saved.i = i_value;
1303 case CONFIG_ITEM_FLOAT:
1304 if( p_item->psz_text )
1305 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1307 if( f_value == p_item->orig.f )
1309 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1311 p_item->saved.f = f_value;
1315 if( p_item->psz_text )
1316 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1318 if( (!psz_value && !p_item->orig.psz) ||
1319 (psz_value && p_item->orig.psz &&
1320 !strcmp( psz_value, p_item->orig.psz )) )
1322 fprintf( file, "%s=%s\n", p_item->psz_name,
1325 if( b_autosave && !p_item->b_autosave ) break;
1327 free ((char *)p_item->saved.psz);
1328 if( (psz_value && p_item->orig.psz &&
1329 strcmp( psz_value, p_item->orig.psz )) ||
1330 !psz_value || !p_item->orig.psz)
1331 p_item->saved.psz = strdupnull (psz_value);
1333 p_item->saved.psz = NULL;
1340 vlc_list_release( p_list );
1343 * Restore old settings from the config in file
1345 fputs( p_bigbuffer, file );
1346 free( p_bigbuffer );
1349 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1354 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1357 int i_index, i_count;
1359 if( !p_this ) return -1;
1361 /* Check if there's anything to save */
1362 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1363 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1364 i_count = p_list->i_count;
1365 for( i_index = 0; i_index < i_count; i_index++ )
1367 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1368 module_config_t *p_item, *p_end;
1370 if( !p_parser->i_config_items ) continue;
1372 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1376 if( p_item->b_autosave && p_item->b_dirty ) break;
1380 vlc_list_release( p_list );
1381 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1383 if( i_index == i_count ) return VLC_SUCCESS;
1384 return SaveConfigFile( p_this, 0, VLC_TRUE );
1387 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1389 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1392 /*****************************************************************************
1393 * config_LoadCmdLine: parse command line
1394 *****************************************************************************
1395 * Parse command line for configuration options.
1396 * Now that the module_bank has been initialized, we can dynamically
1397 * generate the longopts structure used by getops. We have to do it this way
1398 * because we don't know (and don't want to know) in advance the configuration
1399 * options used (ie. exported) by each module.
1400 *****************************************************************************/
1401 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1402 vlc_bool_t b_ignore_errors )
1404 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1407 struct option *p_longopts;
1408 int i_modules_index;
1411 module_config_t *pp_shortopts[256];
1412 char *psz_shortopts;
1414 /* Set default configuration and copy arguments */
1415 p_this->p_libvlc->i_argc = *pi_argc;
1416 p_this->p_libvlc->ppsz_argv = ppsz_argv;
1419 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
1420 * is the PSN - process serial number (a unique PID-ish thingie)
1421 * still ok for real Darwin & when run from command line */
1422 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1423 /* for example -psn_0_9306113 */
1425 /* GDMF!... I can't do this or else the MacOSX window server will
1426 * not pick up the PSN and not register the app and we crash...
1427 * hence the following kludge otherwise we'll get confused w/ argv[1]
1428 * being an input file name */
1430 ppsz_argv[ 1 ] = NULL;
1432 *pi_argc = *pi_argc - 1;
1438 /* List all modules */
1439 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1442 * Generate the longopts and shortopts structures used by getopt_long
1446 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1449 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1451 /* count the number of exported configuration options (to allocate
1452 * longopts). We also need to allocate space for two options when
1453 * dealing with boolean to allow for --foo and --no-foo */
1454 i_opts += p_parser->i_config_items
1455 + 2 * p_parser->i_bool_items;
1458 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1459 if( p_longopts == NULL )
1461 msg_Err( p_this, "out of memory" );
1462 vlc_list_release( p_list );
1466 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1467 if( psz_shortopts == NULL )
1469 msg_Err( p_this, "out of memory" );
1471 vlc_list_release( p_list );
1475 /* If we are requested to ignore errors, then we must work on a copy
1476 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1477 * us, ignoring the arity of the options */
1478 if( b_ignore_errors )
1480 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1481 if( ppsz_argv == NULL )
1483 msg_Err( p_this, "out of memory" );
1484 free( psz_shortopts );
1486 vlc_list_release( p_list );
1489 memcpy( ppsz_argv, p_this->p_libvlc->ppsz_argv,
1490 *pi_argc * sizeof(char *) );
1494 for( i_index = 0; i_index < 256; i_index++ )
1496 pp_shortopts[i_index] = NULL;
1499 /* Fill the p_longopts and psz_shortopts structures */
1501 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1504 module_config_t *p_item, *p_end;
1505 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1507 if( !p_parser->i_config_items )
1510 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1515 if( p_item->i_type & CONFIG_HINT )
1518 /* Add item to long options */
1519 p_longopts[i_index].name = strdup( p_item->psz_name );
1520 if( p_longopts[i_index].name == NULL ) continue;
1521 p_longopts[i_index].has_arg =
1522 (p_item->i_type == CONFIG_ITEM_BOOL)?
1523 no_argument : required_argument;
1524 p_longopts[i_index].flag = &flag;
1525 p_longopts[i_index].val = 0;
1528 /* When dealing with bools we also need to add the --no-foo
1530 if( p_item->i_type == CONFIG_ITEM_BOOL )
1532 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1533 if( psz_name == NULL ) continue;
1534 strcpy( psz_name, "no" );
1535 strcat( psz_name, p_item->psz_name );
1537 p_longopts[i_index].name = psz_name;
1538 p_longopts[i_index].has_arg = no_argument;
1539 p_longopts[i_index].flag = &flag;
1540 p_longopts[i_index].val = 1;
1543 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1544 if( psz_name == NULL ) continue;
1545 strcpy( psz_name, "no-" );
1546 strcat( psz_name, p_item->psz_name );
1548 p_longopts[i_index].name = psz_name;
1549 p_longopts[i_index].has_arg = no_argument;
1550 p_longopts[i_index].flag = &flag;
1551 p_longopts[i_index].val = 1;
1555 /* If item also has a short option, add it */
1556 if( p_item->i_short )
1558 pp_shortopts[(int)p_item->i_short] = p_item;
1559 psz_shortopts[i_shortopts] = p_item->i_short;
1561 if( p_item->i_type != CONFIG_ITEM_BOOL )
1563 psz_shortopts[i_shortopts] = ':';
1566 if( p_item->i_short == 'v' )
1568 psz_shortopts[i_shortopts] = ':';
1576 /* We don't need the module list anymore */
1577 vlc_list_release( p_list );
1579 /* Close the longopts and shortopts structures */
1580 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1581 psz_shortopts[i_shortopts] = '\0';
1584 * Parse the command line options
1587 optind = 0; /* set to 0 to tell GNU getopt to reinitialize */
1588 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1589 p_longopts, &i_index ) ) != -1 )
1591 /* A long option has been recognized */
1594 module_config_t *p_conf;
1595 char *psz_name = (char *)p_longopts[i_index].name;
1597 /* Check if we deal with a --nofoo or --no-foo long option */
1598 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1600 /* Store the configuration option */
1601 p_conf = config_FindConfig( p_this, psz_name );
1604 /* Check if the option is deprecated */
1605 if( p_conf->psz_current )
1607 if( p_conf->b_strict )
1610 "Warning: option --%s no longer exists.\n",
1616 "%s: option --%s is deprecated. Use --%s instead.\n",
1617 b_ignore_errors ? "Warning" : "Error",
1618 p_conf->psz_name, p_conf->psz_current);
1619 if( !b_ignore_errors )
1622 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1623 free( (char *)p_longopts[i_index].name );
1626 free( psz_shortopts );
1630 psz_name = (char *)p_conf->psz_current;
1631 p_conf = config_FindConfig( p_this, psz_name );
1634 switch( p_conf->i_type )
1636 case CONFIG_ITEM_STRING:
1637 case CONFIG_ITEM_PASSWORD:
1638 case CONFIG_ITEM_FILE:
1639 case CONFIG_ITEM_DIRECTORY:
1640 case CONFIG_ITEM_MODULE:
1641 case CONFIG_ITEM_MODULE_LIST:
1642 case CONFIG_ITEM_MODULE_LIST_CAT:
1643 case CONFIG_ITEM_MODULE_CAT:
1644 config_PutPsz( p_this, psz_name, optarg );
1646 case CONFIG_ITEM_INTEGER:
1647 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1649 case CONFIG_ITEM_FLOAT:
1650 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1652 case CONFIG_ITEM_KEY:
1653 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1655 case CONFIG_ITEM_BOOL:
1656 config_PutInt( p_this, psz_name, !flag );
1663 /* A short option has been recognized */
1664 if( pp_shortopts[i_cmd] != NULL )
1666 switch( pp_shortopts[i_cmd]->i_type )
1668 case CONFIG_ITEM_STRING:
1669 case CONFIG_ITEM_PASSWORD:
1670 case CONFIG_ITEM_FILE:
1671 case CONFIG_ITEM_DIRECTORY:
1672 case CONFIG_ITEM_MODULE:
1673 case CONFIG_ITEM_MODULE_CAT:
1674 case CONFIG_ITEM_MODULE_LIST:
1675 case CONFIG_ITEM_MODULE_LIST_CAT:
1676 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1678 case CONFIG_ITEM_INTEGER:
1683 if( *optarg == 'v' ) /* eg. -vvv */
1686 while( *optarg == 'v' )
1694 i_verbose += atoi( optarg ); /* eg. -v2 */
1699 i_verbose++; /* -v */
1701 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1706 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1707 strtol(optarg, 0, 0) );
1710 case CONFIG_ITEM_BOOL:
1711 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1718 /* Internal error: unknown option */
1719 if( !b_ignore_errors )
1721 fprintf( stderr, "%s: unknown option"
1722 " or missing mandatory argument ",
1723 p_this->p_libvlc->psz_object_name );
1726 fprintf( stderr, "`-%c'\n", optopt );
1730 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1732 fprintf( stderr, "Try `%s --help' for more information.\n",
1733 p_this->p_libvlc->psz_object_name );
1735 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1736 free( (char *)p_longopts[i_index].name );
1738 free( psz_shortopts );
1743 /* Free allocated resources */
1744 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1745 free( (char *)p_longopts[i_index].name );
1747 free( psz_shortopts );
1748 if( b_ignore_errors ) free( ppsz_argv );
1754 * config_GetDataDir: find directory where shared data is installed
1756 * @return a string (always succeeds).
1758 const char *config_GetDataDir( void )
1760 #if defined (WIN32) || defined (UNDER_CE)
1761 return vlc_global()->psz_vlcpath;
1762 #elif defined(__APPLE__) || defined (SYS_BEOS)
1763 static char path[PATH_MAX] = "";
1767 snprintf( path, sizeof( path ), "%s/share",
1768 vlc_global()->psz_vlcpath );
1769 path[sizeof( path ) - 1] = '\0';
1777 /*****************************************************************************
1778 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1779 *****************************************************************************
1780 * This function will try by different ways to find the user's home path.
1781 * Note that this function is not reentrant, it should be called only once
1782 * at the beginning of main where the result will be stored for later use.
1783 *****************************************************************************/
1784 static char *GetDir( vlc_bool_t b_appdata )
1786 const char *psz_localhome = NULL;
1788 #if defined(WIN32) && !defined(UNDER_CE)
1789 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1791 #ifndef CSIDL_FLAG_CREATE
1792 # define CSIDL_FLAG_CREATE 0x8000
1794 #ifndef CSIDL_APPDATA
1795 # define CSIDL_APPDATA 0x1A
1797 #ifndef CSIDL_PROFILE
1798 # define CSIDL_PROFILE 0x28
1800 #ifndef SHGFP_TYPE_CURRENT
1801 # define SHGFP_TYPE_CURRENT 0
1804 HINSTANCE shfolder_dll;
1805 SHGETFOLDERPATH SHGetFolderPath ;
1807 /* load the shfolder dll to retrieve SHGetFolderPath */
1808 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1810 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1811 _T("SHGetFolderPathW") );
1812 if ( SHGetFolderPath != NULL )
1814 wchar_t whomedir[MAX_PATH];
1816 /* get the "Application Data" folder for the current user */
1817 if( S_OK == SHGetFolderPath( NULL,
1818 (b_appdata ? CSIDL_APPDATA :
1819 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1820 NULL, SHGFP_TYPE_CURRENT,
1823 FreeLibrary( shfolder_dll );
1824 return FromWide( whomedir );
1827 FreeLibrary( shfolder_dll );
1830 #elif defined(UNDER_CE)
1832 #ifndef CSIDL_APPDATA
1833 # define CSIDL_APPDATA 0x1A
1836 wchar_t whomedir[MAX_PATH];
1838 /* get the "Application Data" folder for the current user */
1839 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
1840 return FromWide( whomedir );
1843 #if defined(HAVE_GETPWUID)
1844 struct passwd *p_pw;
1847 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1850 psz_localhome = getenv( "HOME" );
1851 if( psz_localhome == NULL )
1853 psz_localhome = getenv( "TMP" );
1854 if( psz_localhome == NULL )
1855 psz_localhome = "/tmp";
1858 #if defined(HAVE_GETPWUID)
1860 psz_localhome = p_pw->pw_dir;
1863 return FromLocaleDup( psz_localhome );
1867 * Get the user's home directory
1869 char *config_GetHomeDir( void )
1871 return GetDir( VLC_FALSE );
1875 * Get the user's main data and config directory:
1876 * - on windows that's the App Data directory;
1877 * - on other OSes it's the same as the home directory.
1879 char *config_GetUserDir( void );
1880 char *config_GetUserDir( void )
1882 return GetDir( VLC_TRUE );
1886 * Get the user's VLC configuration directory
1888 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
1891 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
1892 char *psz_parent = config_GetUserDir();
1893 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
1894 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
1898 /* XDG Base Directory Specification - Version 0.6 */
1899 char *psz_env = getenv( "XDG_CONFIG_HOME" );
1902 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
1906 psz_env = getenv( "HOME" );
1907 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
1908 if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
1915 * Get the user's VLC data directory
1916 * (used for stuff like the skins, custom lua modules, ...)
1918 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
1921 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
1922 char *psz_parent = config_GetUserDir();
1923 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
1924 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
1928 /* XDG Base Directory Specification - Version 0.6 */
1929 char *psz_env = getenv( "XDG_DATA_HOME" );
1932 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
1936 psz_env = getenv( "HOME" );
1937 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
1938 if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
1945 * Get the user's VLC cache directory
1946 * (used for stuff like the modules cache, the album art cache, ...)
1948 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
1951 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
1952 char *psz_parent = config_GetUserDir();
1953 if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
1954 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
1958 /* XDG Base Directory Specification - Version 0.6 */
1959 char *psz_env = getenv( "XDG_CACHE_HOME" );
1962 if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
1966 psz_env = getenv( "HOME" );
1967 if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
1968 if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
1975 * Get the user's configuration file
1977 char *config_GetConfigFile( libvlc_int_t *p_libvlc )
1979 char *psz_configfile;
1980 if( asprintf( &psz_configfile, "%s" DIR_SEP CONFIG_FILE,
1981 p_libvlc->psz_configdir ) == -1 )
1983 return psz_configfile;
1987 * Get the user's configuration file when given with the --config option
1989 char *config_GetCustomConfigFile( libvlc_int_t *p_libvlc )
1991 char *psz_configfile = config_GetPsz( p_libvlc, "config" );
1992 if( psz_configfile != NULL )
1994 if( psz_configfile[0] == '~' && psz_configfile[1] == '/' )
1996 /* This is incomplete: we should also support the ~cmassiot/ syntax */
1998 if( asprintf( &psz_buf, "%s/%s", p_libvlc->psz_homedir,
1999 psz_configfile + 2 ) == -1 )
2001 free( psz_configfile );
2004 free( psz_configfile );
2005 psz_configfile = psz_buf;
2008 return psz_configfile;
2011 static int ConfigStringToKey( const char *psz_key )
2015 const char *psz_parser = strchr( psz_key, '-' );
2016 while( psz_parser && psz_parser != psz_key )
2018 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
2020 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
2021 strlen( vlc_modifiers[i].psz_key_string ) ) )
2023 i_key |= vlc_modifiers[i].i_key_code;
2026 psz_key = psz_parser + 1;
2027 psz_parser = strchr( psz_key, '-' );
2029 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
2031 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
2033 i_key |= vlc_keys[i].i_key_code;
2040 static char *ConfigKeyToString( int i_key )
2042 char *psz_key = malloc( 100 );
2052 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
2055 if( i_key & vlc_modifiers[index].i_key_code )
2057 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
2060 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
2063 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
2065 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );