1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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 *****************************************************************************/
28 #include <stdio.h> /* sprintf() */
29 #include <stdlib.h> /* free(), strtol() */
30 #include <string.h> /* strdup() */
31 #include <errno.h> /* errno */
38 # include <unistd.h> /* getuid() */
41 #ifdef HAVE_GETOPT_LONG
43 # include <getopt.h> /* getopt() */
46 # include "../extras/getopt.h"
49 #if defined(HAVE_GETPWUID)
50 # include <pwd.h> /* getpwuid() */
53 #if defined( HAVE_SYS_STAT_H )
54 # include <sys/stat.h>
56 #if defined( HAVE_SYS_TYPES_H )
57 # include <sys/types.h>
60 # if !defined( UNDER_CE )
66 static int ConfigStringToKey( const char * );
67 static char *ConfigKeyToString( int );
69 /*****************************************************************************
70 * config_GetType: get the type of a variable (bool, int, float, string)
71 *****************************************************************************
72 * This function is used to get the type of a variable from its name.
73 * Beware, this is quite slow.
74 *****************************************************************************/
75 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
77 module_config_t *p_config;
80 p_config = config_FindConfig( p_this, psz_name );
88 switch( p_config->i_type )
90 case CONFIG_ITEM_BOOL:
91 i_type = VLC_VAR_BOOL;
94 case CONFIG_ITEM_INTEGER:
95 i_type = VLC_VAR_INTEGER;
98 case CONFIG_ITEM_FLOAT:
99 i_type = VLC_VAR_FLOAT;
102 case CONFIG_ITEM_MODULE:
103 case CONFIG_ITEM_MODULE_CAT:
104 case CONFIG_ITEM_MODULE_LIST:
105 case CONFIG_ITEM_MODULE_LIST_CAT:
106 i_type = VLC_VAR_MODULE;
109 case CONFIG_ITEM_STRING:
110 i_type = VLC_VAR_STRING;
113 case CONFIG_ITEM_FILE:
114 i_type = VLC_VAR_FILE;
117 case CONFIG_ITEM_DIRECTORY:
118 i_type = VLC_VAR_DIRECTORY;
129 /*****************************************************************************
130 * config_GetInt: get the value of an int variable
131 *****************************************************************************
132 * This function is used to get the value of variables which are internally
133 * represented by an integer (CONFIG_ITEM_INTEGER and
135 *****************************************************************************/
136 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
138 module_config_t *p_config;
140 p_config = config_FindConfig( p_this, psz_name );
145 msg_Err( p_this, "option %s does not exist", psz_name );
148 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
149 (p_config->i_type!=CONFIG_ITEM_KEY) &&
150 (p_config->i_type!=CONFIG_ITEM_BOOL) )
152 msg_Err( p_this, "option %s does not refer to an int", psz_name );
156 return p_config->i_value;
159 /*****************************************************************************
160 * config_GetFloat: get the value of a float variable
161 *****************************************************************************
162 * This function is used to get the value of variables which are internally
163 * represented by a float (CONFIG_ITEM_FLOAT).
164 *****************************************************************************/
165 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
167 module_config_t *p_config;
169 p_config = config_FindConfig( p_this, psz_name );
174 msg_Err( p_this, "option %s does not exist", psz_name );
177 if( p_config->i_type != CONFIG_ITEM_FLOAT )
179 msg_Err( p_this, "option %s does not refer to a float", psz_name );
183 return p_config->f_value;
186 /*****************************************************************************
187 * config_GetPsz: get the string value of a string variable
188 *****************************************************************************
189 * This function is used to get the value of variables which are internally
190 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
191 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
193 * Important note: remember to free() the returned char* because it's a
194 * duplicate of the actual value. It isn't safe to return a pointer to the
195 * actual value as it can be modified at any time.
196 *****************************************************************************/
197 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
199 module_config_t *p_config;
200 char *psz_value = NULL;
202 p_config = config_FindConfig( p_this, psz_name );
207 msg_Err( p_this, "option %s does not exist", psz_name );
210 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
211 (p_config->i_type!=CONFIG_ITEM_FILE) &&
212 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
213 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
214 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
215 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
216 (p_config->i_type!=CONFIG_ITEM_MODULE) )
218 msg_Err( p_this, "option %s does not refer to a string", psz_name );
223 /* return a copy of the string */
224 vlc_mutex_lock( p_config->p_lock );
225 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
226 vlc_mutex_unlock( p_config->p_lock );
231 /*****************************************************************************
232 * config_PutPsz: set the string value of a string variable
233 *****************************************************************************
234 * This function is used to set the value of variables which are internally
235 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
236 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
237 *****************************************************************************/
238 void __config_PutPsz( vlc_object_t *p_this,
239 const char *psz_name, const char *psz_value )
241 module_config_t *p_config;
242 vlc_value_t oldval, val;
244 p_config = config_FindConfig( p_this, psz_name );
250 msg_Warn( p_this, "option %s does not exist", psz_name );
253 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
254 (p_config->i_type!=CONFIG_ITEM_FILE) &&
255 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
256 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
257 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
258 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
259 (p_config->i_type!=CONFIG_ITEM_MODULE) )
261 msg_Err( p_this, "option %s does not refer to a string", psz_name );
265 vlc_mutex_lock( p_config->p_lock );
267 /* backup old value */
268 oldval.psz_string = p_config->psz_value;
270 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
271 else p_config->psz_value = NULL;
273 p_config->b_dirty = VLC_TRUE;
275 val.psz_string = p_config->psz_value;
277 vlc_mutex_unlock( p_config->p_lock );
279 if( p_config->pf_callback )
281 p_config->pf_callback( p_this, psz_name, oldval, val,
282 p_config->p_callback_data );
285 /* free old string */
286 if( oldval.psz_string ) free( oldval.psz_string );
289 /*****************************************************************************
290 * config_PutInt: set the integer value of an int variable
291 *****************************************************************************
292 * This function is used to set the value of variables which are internally
293 * represented by an integer (CONFIG_ITEM_INTEGER and
295 *****************************************************************************/
296 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
298 module_config_t *p_config;
299 vlc_value_t oldval, val;
301 p_config = config_FindConfig( p_this, psz_name );
306 msg_Warn( p_this, "option %s does not exist", psz_name );
309 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
310 (p_config->i_type!=CONFIG_ITEM_KEY) &&
311 (p_config->i_type!=CONFIG_ITEM_BOOL) )
313 msg_Err( p_this, "option %s does not refer to an int", psz_name );
317 /* backup old value */
318 oldval.i_int = p_config->i_value;
320 /* if i_min == i_max == 0, then do not use them */
321 if ((p_config->i_min == 0) && (p_config->i_max == 0))
323 p_config->i_value = i_value;
325 else if (i_value < p_config->i_min)
327 p_config->i_value = p_config->i_min;
329 else if (i_value > p_config->i_max)
331 p_config->i_value = p_config->i_max;
335 p_config->i_value = i_value;
338 p_config->b_dirty = VLC_TRUE;
340 val.i_int = p_config->i_value;
342 if( p_config->pf_callback )
344 p_config->pf_callback( p_this, psz_name, oldval, val,
345 p_config->p_callback_data );
349 /*****************************************************************************
350 * config_PutFloat: set the value of a float variable
351 *****************************************************************************
352 * This function is used to set the value of variables which are internally
353 * represented by a float (CONFIG_ITEM_FLOAT).
354 *****************************************************************************/
355 void __config_PutFloat( vlc_object_t *p_this,
356 const char *psz_name, float f_value )
358 module_config_t *p_config;
359 vlc_value_t oldval, val;
361 p_config = config_FindConfig( p_this, psz_name );
366 msg_Warn( p_this, "option %s does not exist", psz_name );
369 if( p_config->i_type != CONFIG_ITEM_FLOAT )
371 msg_Err( p_this, "option %s does not refer to a float", psz_name );
375 /* backup old value */
376 oldval.f_float = p_config->f_value;
378 /* if f_min == f_max == 0, then do not use them */
379 if ((p_config->f_min == 0) && (p_config->f_max == 0))
381 p_config->f_value = f_value;
383 else if (f_value < p_config->f_min)
385 p_config->f_value = p_config->f_min;
387 else if (f_value > p_config->f_max)
389 p_config->f_value = p_config->f_max;
393 p_config->f_value = f_value;
396 p_config->b_dirty = VLC_TRUE;
398 val.f_float = p_config->f_value;
400 if( p_config->pf_callback )
402 p_config->pf_callback( p_this, psz_name, oldval, val,
403 p_config->p_callback_data );
407 /*****************************************************************************
408 * config_FindConfig: find the config structure associated with an option.
409 *****************************************************************************
410 * FIXME: This function really needs to be optimized.
411 * FIXME: And now even more.
412 *****************************************************************************/
413 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
417 module_config_t *p_item;
420 if( !psz_name ) return NULL;
422 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
424 for( i_index = 0; i_index < p_list->i_count; i_index++ )
426 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
428 if( !p_parser->i_config_items )
431 for( p_item = p_parser->p_config;
432 p_item->i_type != CONFIG_HINT_END;
435 if( p_item->i_type & CONFIG_HINT )
438 if( !strcmp( psz_name, p_item->psz_name ) )
440 vlc_list_release( p_list );
446 vlc_list_release( p_list );
451 /*****************************************************************************
452 * config_FindModule: find a specific module structure.
453 *****************************************************************************/
454 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
457 module_t *p_module, *p_result = NULL;
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 p_module = (module_t *)p_list->p_values[i_index].p_object;
467 if( !strcmp( p_module->psz_object_name, psz_name ) )
474 vlc_list_release( p_list );
479 /*****************************************************************************
480 * config_Duplicate: creates a duplicate of a module's configuration data.
481 *****************************************************************************
482 * Unfortunatly we cannot work directly with the module's config data as
483 * this module might be unloaded from memory at any time (remember HideModule).
484 * This is why we need to create an exact copy of the config data.
485 *****************************************************************************/
486 void config_Duplicate( module_t *p_module, const module_config_t *p_orig )
488 int i, j, i_lines = 1;
489 module_config_t *p_item;
491 /* Calculate the structure length */
492 p_module->i_config_items = 0;
493 p_module->i_bool_items = 0;
495 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
499 if( p_item->i_type & CONFIG_ITEM )
501 p_module->i_config_items++;
504 if( p_item->i_type == CONFIG_ITEM_BOOL )
506 p_module->i_bool_items++;
510 /* Allocate memory */
511 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
513 if( p_module->p_config == NULL )
515 msg_Err( p_module, "config error: can't duplicate p_config" );
519 /* Do the duplication job */
520 for( i = 0; i < i_lines ; i++ )
522 p_module->p_config[i] = p_orig[i];
524 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
525 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
526 p_module->p_config[i].i_value_saved = p_orig[i].i_value;
527 p_module->p_config[i].f_value_saved = p_orig[i].f_value;
528 p_module->p_config[i].psz_value_saved = 0;
530 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
531 strdup( p_orig[i].psz_type ) : NULL;
532 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
533 strdup( p_orig[i].psz_name ) : NULL;
534 p_module->p_config[i].psz_current = p_orig[i].psz_current?
535 strdup( p_orig[i].psz_current ) : NULL;
536 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
537 strdup( _(p_orig[i].psz_text) ) : NULL;
538 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
539 strdup( _(p_orig[i].psz_longtext) ) : NULL;
540 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
541 strdup( p_orig[i].psz_value ) : NULL;
542 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
543 strdup( p_orig[i].psz_value ) : NULL;
545 p_module->p_config[i].p_lock = &p_module->object_lock;
547 /* duplicate the string list */
548 if( p_orig[i].i_list )
550 if( p_orig[i].ppsz_list )
552 p_module->p_config[i].ppsz_list =
553 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
554 if( p_module->p_config[i].ppsz_list )
556 for( j = 0; j < p_orig[i].i_list; j++ )
557 p_module->p_config[i].ppsz_list[j] = p_orig[i].ppsz_list[j] ?
558 strdup( p_orig[i].ppsz_list[j] ) : NULL ;
559 p_module->p_config[i].ppsz_list[j] = NULL;
562 if( p_orig[i].ppsz_list_text )
564 p_module->p_config[i].ppsz_list_text =
565 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
566 if( p_module->p_config[i].ppsz_list_text )
568 for( j = 0; j < p_orig[i].i_list; j++ )
569 p_module->p_config[i].ppsz_list_text[j] = _(p_orig[i].ppsz_list_text[j]) ?
570 strdup( _(p_orig[i].ppsz_list_text[j] ) ) : NULL ;
571 p_module->p_config[i].ppsz_list_text[j] = NULL;
574 if( p_orig[i].pi_list )
576 p_module->p_config[i].pi_list =
577 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
578 if( p_module->p_config[i].pi_list )
580 for( j = 0; j < p_orig[i].i_list; j++ )
581 p_module->p_config[i].pi_list[j] =
582 p_orig[i].pi_list[j];
587 /* duplicate the actions list */
588 if( p_orig[i].i_action )
592 p_module->p_config[i].ppf_action =
593 malloc( p_orig[i].i_action * sizeof(void *) );
594 p_module->p_config[i].ppsz_action_text =
595 malloc( p_orig[i].i_action * sizeof(char *) );
597 for( j = 0; j < p_orig[i].i_action; j++ )
599 p_module->p_config[i].ppf_action[j] =
600 p_orig[i].ppf_action[j];
601 p_module->p_config[i].ppsz_action_text[j] =
602 p_orig[i].ppsz_action_text[j] ?
603 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
607 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
611 /*****************************************************************************
612 * config_Free: frees a duplicated module's configuration data.
613 *****************************************************************************
614 * This function frees all the data duplicated by config_Duplicate.
615 *****************************************************************************/
616 void config_Free( module_t *p_module )
618 module_config_t *p_item = p_module->p_config;
626 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
628 if( p_item->psz_type )
629 free( p_item->psz_type );
631 if( p_item->psz_name )
632 free( p_item->psz_name );
634 if( p_item->psz_current )
635 free( p_item->psz_current );
637 if( p_item->psz_text )
638 free( p_item->psz_text );
640 if( p_item->psz_longtext )
641 free( p_item->psz_longtext );
643 if( p_item->psz_value )
644 free( p_item->psz_value );
646 if( p_item->psz_value_orig )
647 free( p_item->psz_value_orig );
649 if( p_item->psz_value_saved )
650 free( p_item->psz_value_saved );
654 for( i = 0; i < p_item->i_list; i++ )
656 if( p_item->ppsz_list && p_item->ppsz_list[i] )
657 free( p_item->ppsz_list[i] );
658 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
659 free( p_item->ppsz_list_text[i] );
661 if( p_item->ppsz_list ) free( p_item->ppsz_list );
662 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
663 if( p_item->pi_list ) free( p_item->pi_list );
666 if( p_item->i_action )
668 for( i = 0; i < p_item->i_action; i++ )
670 if( p_item->ppsz_action_text[i] )
671 free( p_item->ppsz_action_text[i] );
673 if( p_item->ppf_action ) free( p_item->ppf_action );
674 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
678 free( p_module->p_config );
679 p_module->p_config = NULL;
682 /*****************************************************************************
683 * config_SetCallbacks: sets callback functions in the duplicate p_config.
684 *****************************************************************************
685 * Unfortunatly we cannot work directly with the module's config data as
686 * this module might be unloaded from memory at any time (remember HideModule).
687 * This is why we need to duplicate callbacks each time we reload the module.
688 *****************************************************************************/
689 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
691 while( p_new->i_type != CONFIG_HINT_END )
693 p_new->pf_callback = p_orig->pf_callback;
699 /*****************************************************************************
700 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
701 *****************************************************************************
702 * We simply undo what we did in config_SetCallbacks.
703 *****************************************************************************/
704 void config_UnsetCallbacks( module_config_t *p_new )
706 while( p_new->i_type != CONFIG_HINT_END )
708 p_new->pf_callback = NULL;
713 /*****************************************************************************
714 * config_ResetAll: reset the configuration data for all the modules.
715 *****************************************************************************/
716 void __config_ResetAll( vlc_object_t *p_this )
722 /* Acquire config file lock */
723 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
725 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
727 for( i_index = 0; i_index < p_list->i_count; i_index++ )
729 p_module = (module_t *)p_list->p_values[i_index].p_object ;
730 if( p_module->b_submodule ) continue;
732 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
734 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
735 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
736 if( p_module->p_config[i].psz_value )
737 free( p_module->p_config[i].psz_value );
738 p_module->p_config[i].psz_value =
739 p_module->p_config[i].psz_value_orig ?
740 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
744 vlc_list_release( p_list );
745 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
748 /*****************************************************************************
749 * config_LoadConfigFile: loads the configuration file.
750 *****************************************************************************
751 * This function is called to load the config options stored in the config
753 *****************************************************************************/
754 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
762 const char *psz_configfile = p_this->p_libvlc->psz_configfile;
763 if( !psz_configfile || !psz_configfile )
765 const char *psz_homedir = p_this->p_libvlc->psz_homedir;
768 msg_Err( p_this, "psz_homedir is null" );
772 if( asprintf( &psz_filename, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
773 psz_homedir ) == -1 )
778 psz_filename = strdup( psz_configfile );
783 msg_Err( p_this, "out of memory" );
787 msg_Dbg( p_this, "opening config file %s", psz_filename );
789 /* Acquire config file lock */
790 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
792 file = utf8_fopen( psz_filename, "rt" );
795 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
796 free( psz_filename );
797 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
800 free( psz_filename );
802 /* Look for the selected module, if NULL then save everything */
803 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
805 for( i_index = 0; i_index < p_list->i_count; i_index++ )
807 module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
811 && strcmp( psz_module_name, p_parser->psz_object_name ) )
816 /* The config file is organized in sections, one per module. Look for
817 * the interesting section ( a section is of the form [foo] ) */
818 fseek( file, 0L, SEEK_SET );
819 while( fgets( line, 1024, file ) )
822 && (p_index = strchr(line,']'))
823 && (p_index - &line[1]
824 == (int)strlen(p_parser->psz_object_name))
825 && !memcmp( &line[1], p_parser->psz_object_name,
826 strlen(p_parser->psz_object_name) ) )
829 msg_Dbg( p_this, "loading config for module \"%s\"",
830 p_parser->psz_object_name );
836 /* either we found the section or we're at the EOF */
838 /* Now try to load the options in this section */
839 while( fgets( line, 1024, file ) )
841 const char *psz_option_name, *psz_option_value;
842 module_config_t *p_item;
844 if( line[0] == '[' ) break; /* end of section */
846 /* ignore comments or empty lines */
847 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
850 /* get rid of line feed */
851 if( line[strlen(line)-1] == '\n' )
852 line[strlen(line)-1] = (char)0;
854 /* look for option name */
855 psz_option_name = line;
856 psz_option_value = NULL;
857 p_index = strchr( line, '=' );
858 if( !p_index ) break; /* this ain't an option!!! */
861 psz_option_value = p_index + 1;
863 if( !p_parser->i_config_items )
868 /* try to match this option with one of the module's options */
869 for( p_item = p_parser->p_config;
870 p_item->i_type != CONFIG_HINT_END;
873 if( p_item->i_type & CONFIG_HINT )
877 if( !strcmp( p_item->psz_name, psz_option_name ) )
880 switch( p_item->i_type )
882 case CONFIG_ITEM_BOOL:
883 case CONFIG_ITEM_INTEGER:
884 if( !*psz_option_value )
885 break; /* ignore empty option */
886 p_item->i_value = strtol( psz_option_value, 0, 0 );
887 p_item->i_value_saved = p_item->i_value;
889 msg_Dbg( p_this, "option \"%s\", value %i",
890 p_item->psz_name, p_item->i_value );
894 case CONFIG_ITEM_FLOAT:
895 if( !*psz_option_value )
896 break; /* ignore empty option */
897 p_item->f_value = (float)i18n_atof( psz_option_value);
898 p_item->f_value_saved = p_item->f_value;
900 msg_Dbg( p_this, "option \"%s\", value %f",
901 p_item->psz_name, (double)p_item->f_value );
904 case CONFIG_ITEM_KEY:
905 if( !*psz_option_value )
906 break; /* ignore empty option */
907 p_item->i_value = ConfigStringToKey(psz_option_value);
908 p_item->i_value_saved = p_item->i_value;
912 vlc_mutex_lock( p_item->p_lock );
914 /* free old string */
915 if( p_item->psz_value )
916 free( p_item->psz_value );
918 p_item->psz_value = *psz_option_value ?
919 strdup( psz_option_value ) : NULL;
921 if( p_item->psz_value_saved )
922 free( p_item->psz_value_saved );
923 p_item->psz_value_saved = NULL;
924 if( !p_item->psz_value || !p_item->psz_value_orig ||
925 (p_item->psz_value && p_item->psz_value_orig &&
926 strcmp(p_item->psz_value,p_item->psz_value_orig)))
927 p_item->psz_value_saved = p_item->psz_value ?
928 strdup( p_item->psz_value ) : NULL;
930 vlc_mutex_unlock( p_item->p_lock );
933 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
935 p_item->psz_value ? p_item->psz_value : "" );
945 vlc_list_release( p_list );
949 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
954 /*****************************************************************************
955 * config_CreateDir: Create configuration directory if it doesn't exist.
956 *****************************************************************************/
957 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
959 if( !psz_dirname && !*psz_dirname ) return -1;
961 if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) )
963 msg_Err( p_this, "could not create %s (%s)",
964 psz_dirname, strerror(errno) );
971 /*****************************************************************************
972 * config_SaveConfigFile: Save a module's config options.
973 *****************************************************************************
974 * This will save the specified module's config options to the config file.
975 * If psz_module_name is NULL then we save all the modules config options.
976 * It's no use to save the config options that kept their default values, so
977 * we'll try to be a bit clever here.
979 * When we save we mustn't delete the config options of the modules that
980 * haven't been loaded. So we cannot just create a new config file with the
981 * config structures we've got in memory.
982 * I don't really know how to deal with this nicely, so I will use a completly
984 * I will load the config file in memory, but skipping all the sections of the
985 * modules we want to save. Then I will create a brand new file, dump the file
986 * loaded in memory and then append the sections of the modules we want to
989 *****************************************************************************/
990 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
991 vlc_bool_t b_autosave )
995 module_config_t *p_item;
997 char p_line[1024], *p_index2;
999 char *p_bigbuffer, *p_index;
1000 vlc_bool_t b_backup;
1001 char *psz_filename, *psz_homedir, *psz_configfile;
1004 /* Acquire config file lock */
1005 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1007 psz_configfile = p_this->p_libvlc->psz_configfile;
1008 if( !psz_configfile || !psz_configfile )
1010 psz_homedir = p_this->p_libvlc->psz_homedir;
1013 msg_Err( p_this, "psz_homedir is null" );
1014 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1017 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1018 strlen(psz_homedir) );
1021 sprintf( psz_filename, "%s" DIR_SEP CONFIG_DIR, psz_homedir );
1025 msg_Err( p_this, "out of memory" );
1026 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1030 config_CreateDir( p_this, psz_filename );
1032 strcat( psz_filename, DIR_SEP CONFIG_FILE );
1036 psz_filename = strdup( psz_configfile );
1039 msg_Err( p_this, "out of memory" );
1040 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1045 msg_Dbg( p_this, "opening config file %s", psz_filename );
1047 file = utf8_fopen( psz_filename, "rt" );
1050 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1054 /* look for file size */
1055 fseek( file, 0L, SEEK_END );
1056 i_sizebuf = ftell( file );
1057 fseek( file, 0L, SEEK_SET );
1060 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1063 msg_Err( p_this, "out of memory" );
1064 if( file ) fclose( file );
1065 free( psz_filename );
1066 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1071 /* List all available modules */
1072 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1074 /* backup file into memory, we only need to backup the sections we won't
1077 while( file && fgets( p_line, 1024, file ) )
1079 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1082 /* we found a section, check if we need to do a backup */
1083 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1085 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1087 if( ((p_index2 - &p_line[1])
1088 == (int)strlen(p_parser->psz_object_name) )
1089 && !memcmp( &p_line[1], p_parser->psz_object_name,
1090 strlen(p_parser->psz_object_name) ) )
1092 if( !psz_module_name )
1094 else if( !strcmp( psz_module_name,
1095 p_parser->psz_object_name ) )
1100 if( i_index == p_list->i_count )
1102 /* we don't have this section in our list so we need to back
1106 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1119 /* save line if requested and line is valid (doesn't begin with a
1120 * space, tab, or eol) */
1121 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1122 && (p_line[0] != '\t') )
1124 strcpy( p_index, p_line );
1125 p_index += strlen( p_line );
1128 if( file ) fclose( file );
1132 * Save module config in file
1135 file = utf8_fopen( psz_filename, "wt" );
1138 msg_Warn( p_this, "could not open config file %s for writing",
1140 free( psz_filename );
1141 vlc_list_release( p_list );
1142 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1146 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
1147 "###\n### lines begining with a '#' character are comments\n###\n\n" );
1149 /* Look for the selected module, if NULL then save everything */
1150 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1152 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1154 if( psz_module_name && strcmp( psz_module_name,
1155 p_parser->psz_object_name ) )
1158 if( !p_parser->i_config_items )
1161 if( psz_module_name )
1162 msg_Dbg( p_this, "saving config for module \"%s\"",
1163 p_parser->psz_object_name );
1165 fprintf( file, "[%s]", p_parser->psz_object_name );
1166 if( p_parser->psz_longname )
1167 utf8_fprintf( file, " # %s\n\n", p_parser->psz_longname );
1169 fprintf( file, "\n\n" );
1171 for( p_item = p_parser->p_config;
1172 p_item->i_type != CONFIG_HINT_END;
1176 int i_value = p_item->i_value;
1177 float f_value = p_item->f_value;
1178 char *psz_value = p_item->psz_value;
1180 if( p_item->i_type & CONFIG_HINT )
1183 /* Ignore deprecated options */
1184 if( p_item->psz_current )
1186 if( p_item->b_unsaveable )
1190 if( b_autosave && !p_item->b_autosave )
1192 i_value = p_item->i_value_saved;
1193 f_value = p_item->f_value_saved;
1194 psz_value = p_item->psz_value_saved;
1195 if( !psz_value ) psz_value = p_item->psz_value_orig;
1199 p_item->b_dirty = VLC_FALSE;
1202 switch( p_item->i_type )
1204 case CONFIG_ITEM_BOOL:
1205 case CONFIG_ITEM_INTEGER:
1206 if( p_item->psz_text )
1207 utf8_fprintf( file, "# %s (%s)\n", p_item->psz_text,
1208 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1209 _("boolean") : _("integer") );
1210 if( i_value == p_item->i_value_orig )
1211 fprintf( file, "#" );
1212 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1214 p_item->i_value_saved = i_value;
1217 case CONFIG_ITEM_KEY:
1218 if( p_item->psz_text )
1219 utf8_fprintf( file, "# %s (%s)\n", p_item->psz_text,
1221 if( i_value == p_item->i_value_orig )
1222 fprintf( file, "#" );
1223 psz_key = ConfigKeyToString( i_value );
1224 fprintf( file, "%s=%s\n", p_item->psz_name,
1225 psz_key ? psz_key : "" );
1226 if ( psz_key ) free( psz_key );
1228 p_item->i_value_saved = i_value;
1231 case CONFIG_ITEM_FLOAT:
1232 if( p_item->psz_text )
1233 utf8_fprintf( file, "# %s (%s)\n", p_item->psz_text,
1235 if( f_value == p_item->f_value_orig )
1236 fprintf( file, "#" );
1237 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1239 p_item->f_value_saved = f_value;
1243 if( p_item->psz_text )
1244 utf8_fprintf( file, "# %s (%s)\n", p_item->psz_text,
1246 if( (!psz_value && !p_item->psz_value_orig) ||
1247 (psz_value && p_item->psz_value_orig &&
1248 !strcmp( psz_value, p_item->psz_value_orig )) )
1249 fprintf( file, "#" );
1250 fprintf( file, "%s=%s\n", p_item->psz_name,
1251 psz_value ? psz_value : "" );
1253 if( b_autosave && !p_item->b_autosave ) break;
1255 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1256 p_item->psz_value_saved = 0;
1257 if( (psz_value && p_item->psz_value_orig &&
1258 strcmp( psz_value, p_item->psz_value_orig )) ||
1259 !psz_value || !p_item->psz_value_orig)
1260 p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1264 fprintf( file, "\n" );
1267 vlc_list_release( p_list );
1270 * Restore old settings from the config in file
1272 fputs( p_bigbuffer, file );
1273 free( p_bigbuffer );
1276 free( psz_filename );
1277 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1282 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1286 module_config_t *p_item;
1287 int i_index, i_count;
1289 /* Check if there's anything to save */
1290 vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1291 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1292 i_count = p_list->i_count;
1293 for( i_index = 0; i_index < i_count; i_index++ )
1295 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1297 if( !p_parser->i_config_items ) continue;
1299 for( p_item = p_parser->p_config;
1300 p_item->i_type != CONFIG_HINT_END;
1303 if( p_item->b_autosave && p_item->b_dirty ) break;
1305 if( p_item->i_type != CONFIG_HINT_END ) break;
1307 vlc_list_release( p_list );
1308 vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1310 if( i_index == i_count ) return VLC_SUCCESS;
1311 return SaveConfigFile( p_this, 0, VLC_TRUE );
1314 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1316 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1319 /*****************************************************************************
1320 * config_LoadCmdLine: parse command line
1321 *****************************************************************************
1322 * Parse command line for configuration options.
1323 * Now that the module_bank has been initialized, we can dynamically
1324 * generate the longopts structure used by getops. We have to do it this way
1325 * because we don't know (and don't want to know) in advance the configuration
1326 * options used (ie. exported) by each module.
1327 *****************************************************************************/
1328 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1329 vlc_bool_t b_ignore_errors )
1331 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1334 module_config_t *p_item;
1335 struct option *p_longopts;
1336 int i_modules_index;
1339 module_config_t *pp_shortopts[256];
1340 char *psz_shortopts;
1342 /* Set default configuration and copy arguments */
1343 p_this->p_libvlc->i_argc = *pi_argc;
1344 p_this->p_libvlc->ppsz_argv = ppsz_argv;
1347 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
1348 * is the PSN - process serial number (a unique PID-ish thingie)
1349 * still ok for real Darwin & when run from command line */
1350 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1351 /* for example -psn_0_9306113 */
1353 /* GDMF!... I can't do this or else the MacOSX window server will
1354 * not pick up the PSN and not register the app and we crash...
1355 * hence the following kludge otherwise we'll get confused w/ argv[1]
1356 * being an input file name */
1358 ppsz_argv[ 1 ] = NULL;
1360 *pi_argc = *pi_argc - 1;
1366 /* List all modules */
1367 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1370 * Generate the longopts and shortopts structures used by getopt_long
1374 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1377 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1379 /* count the number of exported configuration options (to allocate
1380 * longopts). We also need to allocate space for two options when
1381 * dealing with boolean to allow for --foo and --no-foo */
1382 i_opts += p_parser->i_config_items
1383 + 2 * p_parser->i_bool_items;
1386 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1387 if( p_longopts == NULL )
1389 msg_Err( p_this, "out of memory" );
1390 vlc_list_release( p_list );
1394 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1395 if( psz_shortopts == NULL )
1397 msg_Err( p_this, "out of memory" );
1399 vlc_list_release( p_list );
1403 /* If we are requested to ignore errors, then we must work on a copy
1404 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1405 * us, ignoring the arity of the options */
1406 if( b_ignore_errors )
1408 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1409 if( ppsz_argv == NULL )
1411 msg_Err( p_this, "out of memory" );
1412 free( psz_shortopts );
1414 vlc_list_release( p_list );
1417 memcpy( ppsz_argv, p_this->p_libvlc->ppsz_argv,
1418 *pi_argc * sizeof(char *) );
1422 for( i_index = 0; i_index < 256; i_index++ )
1424 pp_shortopts[i_index] = NULL;
1427 /* Fill the p_longopts and psz_shortopts structures */
1429 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1432 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1434 if( !p_parser->i_config_items )
1437 for( p_item = p_parser->p_config;
1438 p_item->i_type != CONFIG_HINT_END;
1442 if( p_item->i_type & CONFIG_HINT )
1445 /* Add item to long options */
1446 p_longopts[i_index].name = strdup( p_item->psz_name );
1447 if( p_longopts[i_index].name == NULL ) continue;
1448 p_longopts[i_index].has_arg =
1449 (p_item->i_type == CONFIG_ITEM_BOOL)?
1450 no_argument : required_argument;
1451 p_longopts[i_index].flag = &flag;
1452 p_longopts[i_index].val = 0;
1455 /* When dealing with bools we also need to add the --no-foo
1457 if( p_item->i_type == CONFIG_ITEM_BOOL )
1459 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1460 if( psz_name == NULL ) continue;
1461 strcpy( psz_name, "no" );
1462 strcat( psz_name, p_item->psz_name );
1464 p_longopts[i_index].name = psz_name;
1465 p_longopts[i_index].has_arg = no_argument;
1466 p_longopts[i_index].flag = &flag;
1467 p_longopts[i_index].val = 1;
1470 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1471 if( psz_name == NULL ) continue;
1472 strcpy( psz_name, "no-" );
1473 strcat( psz_name, p_item->psz_name );
1475 p_longopts[i_index].name = psz_name;
1476 p_longopts[i_index].has_arg = no_argument;
1477 p_longopts[i_index].flag = &flag;
1478 p_longopts[i_index].val = 1;
1482 /* If item also has a short option, add it */
1483 if( p_item->i_short )
1485 pp_shortopts[(int)p_item->i_short] = p_item;
1486 psz_shortopts[i_shortopts] = p_item->i_short;
1488 if( p_item->i_type != CONFIG_ITEM_BOOL )
1490 psz_shortopts[i_shortopts] = ':';
1493 if( p_item->i_short == 'v' )
1495 psz_shortopts[i_shortopts] = ':';
1503 /* We don't need the module list anymore */
1504 vlc_list_release( p_list );
1506 /* Close the longopts and shortopts structures */
1507 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1508 psz_shortopts[i_shortopts] = '\0';
1511 * Parse the command line options
1515 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1516 p_longopts, &i_index ) ) != EOF )
1518 /* A long option has been recognized */
1521 module_config_t *p_conf;
1522 char *psz_name = (char *)p_longopts[i_index].name;
1524 /* Check if we deal with a --nofoo or --no-foo long option */
1525 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1527 /* Store the configuration option */
1528 p_conf = config_FindConfig( p_this, psz_name );
1531 /* Check if the option is deprecated */
1532 if( p_conf->psz_current )
1534 if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1536 if( !b_ignore_errors )
1539 "Warning: option --%s is no longer used.\n",
1544 if( !b_ignore_errors )
1546 if( p_conf->b_strict )
1549 "Error: option --%s is deprecated. "
1550 "Use --%s instead.\n",
1551 p_conf->psz_name, p_conf->psz_current);
1553 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1554 free( (char *)p_longopts[i_index].name );
1557 free( psz_shortopts );
1561 "Warning: option --%s is deprecated. "
1562 "You should use --%s instead.\n",
1563 p_conf->psz_name, p_conf->psz_current);
1565 psz_name=p_conf->psz_current;
1566 p_conf = config_FindConfig( p_this, psz_name );
1569 switch( p_conf->i_type )
1571 case CONFIG_ITEM_STRING:
1572 case CONFIG_ITEM_FILE:
1573 case CONFIG_ITEM_DIRECTORY:
1574 case CONFIG_ITEM_MODULE:
1575 case CONFIG_ITEM_MODULE_LIST:
1576 case CONFIG_ITEM_MODULE_LIST_CAT:
1577 case CONFIG_ITEM_MODULE_CAT:
1578 config_PutPsz( p_this, psz_name, optarg );
1580 case CONFIG_ITEM_INTEGER:
1581 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1583 case CONFIG_ITEM_FLOAT:
1584 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1586 case CONFIG_ITEM_KEY:
1587 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1589 case CONFIG_ITEM_BOOL:
1590 config_PutInt( p_this, psz_name, !flag );
1597 /* A short option has been recognized */
1598 if( pp_shortopts[i_cmd] != NULL )
1600 switch( pp_shortopts[i_cmd]->i_type )
1602 case CONFIG_ITEM_STRING:
1603 case CONFIG_ITEM_FILE:
1604 case CONFIG_ITEM_DIRECTORY:
1605 case CONFIG_ITEM_MODULE:
1606 case CONFIG_ITEM_MODULE_CAT:
1607 case CONFIG_ITEM_MODULE_LIST:
1608 case CONFIG_ITEM_MODULE_LIST_CAT:
1609 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1611 case CONFIG_ITEM_INTEGER:
1616 if( *optarg == 'v' ) /* eg. -vvv */
1619 while( *optarg == 'v' )
1627 i_verbose += atoi( optarg ); /* eg. -v2 */
1632 i_verbose++; /* -v */
1634 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1639 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1640 strtol(optarg, 0, 0) );
1643 case CONFIG_ITEM_BOOL:
1644 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1651 /* Internal error: unknown option */
1652 if( !b_ignore_errors )
1654 fprintf( stderr, "%s: unknown option"
1655 " or missing mandatory argument ",
1656 p_this->p_libvlc->psz_object_name );
1659 fprintf( stderr, "`-%c'\n", optopt );
1663 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1665 fprintf( stderr, "Try `%s --help' for more information.\n",
1666 p_this->p_libvlc->psz_object_name );
1668 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1669 free( (char *)p_longopts[i_index].name );
1671 free( psz_shortopts );
1676 /* Free allocated resources */
1677 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1678 free( (char *)p_longopts[i_index].name );
1680 free( psz_shortopts );
1681 if( b_ignore_errors ) free( ppsz_argv );
1687 * config_GetDataDir: find directory where shared data is installed
1689 * @return a string (always succeeds).
1691 const char *config_GetDataDir( const vlc_object_t *p_this )
1693 #if defined (WIN32) || defined (UNDER_CE)
1694 return p_this->p_libvlc_global->psz_vlcpath;
1695 #elif defined(__APPLE__) || defined (SYS_BEOS)
1696 static char path[PATH_MAX] = "";
1700 snprintf( path, sizeof( path ), "%s/share",
1701 p_this->p_libvlc_global->psz_vlcpath );
1702 path[sizeof( path ) - 1] = '\0';
1710 /*****************************************************************************
1711 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1712 *****************************************************************************
1713 * This function will try by different ways to find the user's home path.
1714 * Note that this function is not reentrant, it should be called only once
1715 * at the beginning of main where the result will be stored for later use.
1716 *****************************************************************************/
1717 static char *GetDir( vlc_bool_t b_appdata )
1719 const char *psz_localhome = NULL;
1721 #if defined(HAVE_GETPWUID)
1722 struct passwd *p_pw = NULL;
1725 #if defined(WIN32) && !defined(UNDER_CE)
1726 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1728 #ifndef CSIDL_FLAG_CREATE
1729 # define CSIDL_FLAG_CREATE 0x8000
1731 #ifndef CSIDL_APPDATA
1732 # define CSIDL_APPDATA 0x1A
1734 #ifndef CSIDL_PROFILE
1735 # define CSIDL_PROFILE 0x28
1737 #ifndef SHGFP_TYPE_CURRENT
1738 # define SHGFP_TYPE_CURRENT 0
1741 HINSTANCE shfolder_dll;
1742 SHGETFOLDERPATH SHGetFolderPath ;
1744 /* load the shfolder dll to retrieve SHGetFolderPath */
1745 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1747 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1748 _T("SHGetFolderPathW") );
1749 if ( SHGetFolderPath != NULL )
1751 wchar_t whomedir[MAX_PATH];
1753 /* get the "Application Data" folder for the current user */
1754 if( S_OK == SHGetFolderPath( NULL,
1755 (b_appdata ? CSIDL_APPDATA :
1756 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1757 NULL, SHGFP_TYPE_CURRENT,
1760 FreeLibrary( shfolder_dll );
1761 return FromWide( whomedir );
1764 FreeLibrary( shfolder_dll );
1767 #elif defined(UNDER_CE)
1769 #ifndef CSIDL_APPDATA
1770 # define CSIDL_APPDATA 0x1A
1773 wchar_t whomedir[MAX_PATH];
1775 /* get the "Application Data" folder for the current user */
1776 if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
1777 return FromWide( whomedir );
1780 #if defined(HAVE_GETPWUID)
1781 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1784 psz_localhome = getenv( "HOME" );
1785 if( psz_localhome == NULL )
1787 psz_localhome = getenv( "TMP" );
1788 if( psz_localhome == NULL )
1789 psz_localhome = "/tmp";
1792 #if defined(HAVE_GETPWUID)
1794 psz_localhome = p_pw->pw_dir;
1797 return FromLocaleDup( psz_localhome );
1800 char *config_GetHomeDir( void )
1802 return GetDir( VLC_TRUE );
1805 char *config_GetUserDir( void )
1807 return GetDir( VLC_FALSE );
1811 static int ConfigStringToKey( const char *psz_key )
1815 const char *psz_parser = strchr( psz_key, '-' );
1816 while( psz_parser && psz_parser != psz_key )
1818 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1820 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1821 strlen( vlc_modifiers[i].psz_key_string ) ) )
1823 i_key |= vlc_modifiers[i].i_key_code;
1826 psz_key = psz_parser + 1;
1827 psz_parser = strchr( psz_key, '-' );
1829 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1831 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1833 i_key |= vlc_keys[i].i_key_code;
1840 static char *ConfigKeyToString( int i_key )
1842 char *psz_key = malloc( 100 );
1852 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1855 if( i_key & vlc_modifiers[index].i_key_code )
1857 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1860 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1863 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1865 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );