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 #if defined( WIN32 ) || defined( UNDER_CE )
72 static int ConfigStringToKey( char * );
73 static char *ConfigKeyToString( int );
75 /*****************************************************************************
76 * config_GetType: get the type of a variable (bool, int, float, string)
77 *****************************************************************************
78 * This function is used to get the type of a variable from its name.
79 * Beware, this is quite slow.
80 *****************************************************************************/
81 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
83 module_config_t *p_config;
86 p_config = config_FindConfig( p_this, psz_name );
94 switch( p_config->i_type )
96 case CONFIG_ITEM_BOOL:
97 i_type = VLC_VAR_BOOL;
100 case CONFIG_ITEM_INTEGER:
101 i_type = VLC_VAR_INTEGER;
104 case CONFIG_ITEM_FLOAT:
105 i_type = VLC_VAR_FLOAT;
108 case CONFIG_ITEM_MODULE:
109 case CONFIG_ITEM_MODULE_CAT:
110 case CONFIG_ITEM_MODULE_LIST:
111 case CONFIG_ITEM_MODULE_LIST_CAT:
112 i_type = VLC_VAR_MODULE;
115 case CONFIG_ITEM_STRING:
116 i_type = VLC_VAR_STRING;
119 case CONFIG_ITEM_FILE:
120 i_type = VLC_VAR_FILE;
123 case CONFIG_ITEM_DIRECTORY:
124 i_type = VLC_VAR_DIRECTORY;
135 /*****************************************************************************
136 * config_GetInt: get the value of an int variable
137 *****************************************************************************
138 * This function is used to get the value of variables which are internally
139 * represented by an integer (CONFIG_ITEM_INTEGER and
141 *****************************************************************************/
142 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
144 module_config_t *p_config;
146 p_config = config_FindConfig( p_this, psz_name );
151 msg_Err( p_this, "option %s does not exist", psz_name );
154 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
155 (p_config->i_type!=CONFIG_ITEM_KEY) &&
156 (p_config->i_type!=CONFIG_ITEM_BOOL) )
158 msg_Err( p_this, "option %s does not refer to an int", psz_name );
162 return p_config->i_value;
165 /*****************************************************************************
166 * config_GetFloat: get the value of a float variable
167 *****************************************************************************
168 * This function is used to get the value of variables which are internally
169 * represented by a float (CONFIG_ITEM_FLOAT).
170 *****************************************************************************/
171 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
173 module_config_t *p_config;
175 p_config = config_FindConfig( p_this, psz_name );
180 msg_Err( p_this, "option %s does not exist", psz_name );
183 if( p_config->i_type != CONFIG_ITEM_FLOAT )
185 msg_Err( p_this, "option %s does not refer to a float", psz_name );
189 return p_config->f_value;
192 /*****************************************************************************
193 * config_GetPsz: get the string value of a string variable
194 *****************************************************************************
195 * This function is used to get the value of variables which are internally
196 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
197 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
199 * Important note: remember to free() the returned char* because it's a
200 * duplicate of the actual value. It isn't safe to return a pointer to the
201 * actual value as it can be modified at any time.
202 *****************************************************************************/
203 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
205 module_config_t *p_config;
206 char *psz_value = NULL;
208 p_config = config_FindConfig( p_this, psz_name );
213 msg_Err( p_this, "option %s does not exist", psz_name );
216 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
217 (p_config->i_type!=CONFIG_ITEM_FILE) &&
218 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
219 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
220 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
221 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
222 (p_config->i_type!=CONFIG_ITEM_MODULE) )
224 msg_Err( p_this, "option %s does not refer to a string", psz_name );
229 /* return a copy of the string */
230 vlc_mutex_lock( p_config->p_lock );
231 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
232 vlc_mutex_unlock( p_config->p_lock );
237 /*****************************************************************************
238 * config_PutPsz: set the string value of a string variable
239 *****************************************************************************
240 * This function is used to set the value of variables which are internally
241 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
242 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
243 *****************************************************************************/
244 void __config_PutPsz( vlc_object_t *p_this,
245 const char *psz_name, const char *psz_value )
247 module_config_t *p_config;
248 vlc_value_t oldval, val;
250 p_config = config_FindConfig( p_this, psz_name );
256 msg_Warn( p_this, "option %s does not exist", psz_name );
259 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
260 (p_config->i_type!=CONFIG_ITEM_FILE) &&
261 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
262 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
263 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
264 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
265 (p_config->i_type!=CONFIG_ITEM_MODULE) )
267 msg_Err( p_this, "option %s does not refer to a string", psz_name );
271 vlc_mutex_lock( p_config->p_lock );
273 /* backup old value */
274 oldval.psz_string = p_config->psz_value;
276 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
277 else p_config->psz_value = NULL;
279 p_config->b_dirty = VLC_TRUE;
281 val.psz_string = p_config->psz_value;
283 vlc_mutex_unlock( p_config->p_lock );
285 if( p_config->pf_callback )
287 p_config->pf_callback( p_this, psz_name, oldval, val,
288 p_config->p_callback_data );
291 /* free old string */
292 if( oldval.psz_string ) free( oldval.psz_string );
295 /*****************************************************************************
296 * config_PutInt: set the integer value of an int variable
297 *****************************************************************************
298 * This function is used to set the value of variables which are internally
299 * represented by an integer (CONFIG_ITEM_INTEGER and
301 *****************************************************************************/
302 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
304 module_config_t *p_config;
305 vlc_value_t oldval, val;
307 p_config = config_FindConfig( p_this, psz_name );
312 msg_Warn( p_this, "option %s does not exist", psz_name );
315 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
316 (p_config->i_type!=CONFIG_ITEM_KEY) &&
317 (p_config->i_type!=CONFIG_ITEM_BOOL) )
319 msg_Err( p_this, "option %s does not refer to an int", psz_name );
323 /* backup old value */
324 oldval.i_int = p_config->i_value;
326 /* if i_min == i_max == 0, then do not use them */
327 if ((p_config->i_min == 0) && (p_config->i_max == 0))
329 p_config->i_value = i_value;
331 else if (i_value < p_config->i_min)
333 p_config->i_value = p_config->i_min;
335 else if (i_value > p_config->i_max)
337 p_config->i_value = p_config->i_max;
341 p_config->i_value = i_value;
344 p_config->b_dirty = VLC_TRUE;
346 val.i_int = p_config->i_value;
348 if( p_config->pf_callback )
350 p_config->pf_callback( p_this, psz_name, oldval, val,
351 p_config->p_callback_data );
355 /*****************************************************************************
356 * config_PutFloat: set the value of a float variable
357 *****************************************************************************
358 * This function is used to set the value of variables which are internally
359 * represented by a float (CONFIG_ITEM_FLOAT).
360 *****************************************************************************/
361 void __config_PutFloat( vlc_object_t *p_this,
362 const char *psz_name, float f_value )
364 module_config_t *p_config;
365 vlc_value_t oldval, val;
367 p_config = config_FindConfig( p_this, psz_name );
372 msg_Warn( p_this, "option %s does not exist", psz_name );
375 if( p_config->i_type != CONFIG_ITEM_FLOAT )
377 msg_Err( p_this, "option %s does not refer to a float", psz_name );
381 /* backup old value */
382 oldval.f_float = p_config->f_value;
384 /* if f_min == f_max == 0, then do not use them */
385 if ((p_config->f_min == 0) && (p_config->f_max == 0))
387 p_config->f_value = f_value;
389 else if (f_value < p_config->f_min)
391 p_config->f_value = p_config->f_min;
393 else if (f_value > p_config->f_max)
395 p_config->f_value = p_config->f_max;
399 p_config->f_value = f_value;
402 p_config->b_dirty = VLC_TRUE;
404 val.f_float = p_config->f_value;
406 if( p_config->pf_callback )
408 p_config->pf_callback( p_this, psz_name, oldval, val,
409 p_config->p_callback_data );
413 /*****************************************************************************
414 * config_FindConfig: find the config structure associated with an option.
415 *****************************************************************************
416 * FIXME: This function really needs to be optimized.
417 * FIXME: And now even more.
418 *****************************************************************************/
419 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
423 module_config_t *p_item;
426 if( !psz_name ) return NULL;
428 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
430 for( i_index = 0; i_index < p_list->i_count; i_index++ )
432 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
434 if( !p_parser->i_config_items )
437 for( p_item = p_parser->p_config;
438 p_item->i_type != CONFIG_HINT_END;
441 if( p_item->i_type & CONFIG_HINT )
444 if( !strcmp( psz_name, p_item->psz_name ) )
446 vlc_list_release( p_list );
452 vlc_list_release( p_list );
457 /*****************************************************************************
458 * config_FindModule: find a specific module structure.
459 *****************************************************************************/
460 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
463 module_t *p_module, *p_result = NULL;
466 if( !psz_name ) return NULL;
468 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
470 for( i_index = 0; i_index < p_list->i_count; i_index++ )
472 p_module = (module_t *)p_list->p_values[i_index].p_object;
473 if( !strcmp( p_module->psz_object_name, psz_name ) )
480 vlc_list_release( p_list );
485 /*****************************************************************************
486 * config_Duplicate: creates a duplicate of a module's configuration data.
487 *****************************************************************************
488 * Unfortunatly we cannot work directly with the module's config data as
489 * this module might be unloaded from memory at any time (remember HideModule).
490 * This is why we need to create an exact copy of the config data.
491 *****************************************************************************/
492 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
494 int i, j, i_lines = 1;
495 module_config_t *p_item;
497 /* Calculate the structure length */
498 p_module->i_config_items = 0;
499 p_module->i_bool_items = 0;
501 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
505 if( p_item->i_type & CONFIG_ITEM )
507 p_module->i_config_items++;
510 if( p_item->i_type == CONFIG_ITEM_BOOL )
512 p_module->i_bool_items++;
516 /* Allocate memory */
517 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
519 if( p_module->p_config == NULL )
521 msg_Err( p_module, "config error: can't duplicate p_config" );
525 /* Do the duplication job */
526 for( i = 0; i < i_lines ; i++ )
528 p_module->p_config[i] = p_orig[i];
530 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
531 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
532 p_module->p_config[i].i_value_saved = p_orig[i].i_value;
533 p_module->p_config[i].f_value_saved = p_orig[i].f_value;
534 p_module->p_config[i].psz_value_saved = 0;
536 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
537 strdup( p_orig[i].psz_type ) : NULL;
538 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
539 strdup( p_orig[i].psz_name ) : NULL;
540 p_module->p_config[i].psz_current = p_orig[i].psz_current?
541 strdup( p_orig[i].psz_current ) : NULL;
542 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
543 strdup( _(p_orig[i].psz_text) ) : NULL;
544 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
545 strdup( _(p_orig[i].psz_longtext) ) : NULL;
546 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
547 strdup( p_orig[i].psz_value ) : NULL;
548 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
549 strdup( p_orig[i].psz_value ) : NULL;
551 p_module->p_config[i].p_lock = &p_module->object_lock;
553 /* duplicate the string list */
554 if( p_orig[i].i_list )
556 if( p_orig[i].ppsz_list )
558 p_module->p_config[i].ppsz_list =
559 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
560 if( p_module->p_config[i].ppsz_list )
562 for( j = 0; j < p_orig[i].i_list; j++ )
563 p_module->p_config[i].ppsz_list[j] = p_orig[i].ppsz_list[j] ?
564 strdup( p_orig[i].ppsz_list[j] ) : NULL ;
565 p_module->p_config[i].ppsz_list[j] = NULL;
568 if( p_orig[i].ppsz_list_text )
570 p_module->p_config[i].ppsz_list_text =
571 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
572 if( p_module->p_config[i].ppsz_list_text )
574 for( j = 0; j < p_orig[i].i_list; j++ )
575 p_module->p_config[i].ppsz_list_text[j] = _(p_orig[i].ppsz_list_text[j]) ?
576 strdup( _(p_orig[i].ppsz_list_text[j] ) ) : NULL ;
577 p_module->p_config[i].ppsz_list_text[j] = NULL;
580 if( p_orig[i].pi_list )
582 p_module->p_config[i].pi_list =
583 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
584 if( p_module->p_config[i].pi_list )
586 for( j = 0; j < p_orig[i].i_list; j++ )
587 p_module->p_config[i].pi_list[j] =
588 p_orig[i].pi_list[j];
593 /* duplicate the actions list */
594 if( p_orig[i].i_action )
598 p_module->p_config[i].ppf_action =
599 malloc( p_orig[i].i_action * sizeof(void *) );
600 p_module->p_config[i].ppsz_action_text =
601 malloc( p_orig[i].i_action * sizeof(char *) );
603 for( j = 0; j < p_orig[i].i_action; j++ )
605 p_module->p_config[i].ppf_action[j] =
606 p_orig[i].ppf_action[j];
607 p_module->p_config[i].ppsz_action_text[j] =
608 p_orig[i].ppsz_action_text[j] ?
609 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
613 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
617 /*****************************************************************************
618 * config_Free: frees a duplicated module's configuration data.
619 *****************************************************************************
620 * This function frees all the data duplicated by config_Duplicate.
621 *****************************************************************************/
622 void config_Free( module_t *p_module )
624 module_config_t *p_item = p_module->p_config;
632 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
634 if( p_item->psz_type )
635 free( p_item->psz_type );
637 if( p_item->psz_name )
638 free( p_item->psz_name );
640 if( p_item->psz_current )
641 free( p_item->psz_current );
643 if( p_item->psz_text )
644 free( p_item->psz_text );
646 if( p_item->psz_longtext )
647 free( p_item->psz_longtext );
649 if( p_item->psz_value )
650 free( p_item->psz_value );
652 if( p_item->psz_value_orig )
653 free( p_item->psz_value_orig );
655 if( p_item->psz_value_saved )
656 free( p_item->psz_value_saved );
660 for( i = 0; i < p_item->i_list; i++ )
662 if( p_item->ppsz_list && p_item->ppsz_list[i] )
663 free( p_item->ppsz_list[i] );
664 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
665 free( p_item->ppsz_list_text[i] );
667 if( p_item->ppsz_list ) free( p_item->ppsz_list );
668 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
669 if( p_item->pi_list ) free( p_item->pi_list );
672 if( p_item->i_action )
674 for( i = 0; i < p_item->i_action; i++ )
676 if( p_item->ppsz_action_text[i] )
677 free( p_item->ppsz_action_text[i] );
679 if( p_item->ppf_action ) free( p_item->ppf_action );
680 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
684 free( p_module->p_config );
685 p_module->p_config = NULL;
688 /*****************************************************************************
689 * config_SetCallbacks: sets callback functions in the duplicate p_config.
690 *****************************************************************************
691 * Unfortunatly we cannot work directly with the module's config data as
692 * this module might be unloaded from memory at any time (remember HideModule).
693 * This is why we need to duplicate callbacks each time we reload the module.
694 *****************************************************************************/
695 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
697 while( p_new->i_type != CONFIG_HINT_END )
699 p_new->pf_callback = p_orig->pf_callback;
705 /*****************************************************************************
706 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
707 *****************************************************************************
708 * We simply undo what we did in config_SetCallbacks.
709 *****************************************************************************/
710 void config_UnsetCallbacks( module_config_t *p_new )
712 while( p_new->i_type != CONFIG_HINT_END )
714 p_new->pf_callback = NULL;
719 /*****************************************************************************
720 * config_ResetAll: reset the configuration data for all the modules.
721 *****************************************************************************/
722 void __config_ResetAll( vlc_object_t *p_this )
728 /* Acquire config file lock */
729 vlc_mutex_lock( &p_this->p_vlc->config_lock );
731 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
733 for( i_index = 0; i_index < p_list->i_count; i_index++ )
735 p_module = (module_t *)p_list->p_values[i_index].p_object ;
736 if( p_module->b_submodule ) continue;
738 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
740 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
741 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
742 if( p_module->p_config[i].psz_value )
743 free( p_module->p_config[i].psz_value );
744 p_module->p_config[i].psz_value =
745 p_module->p_config[i].psz_value_orig ?
746 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
750 vlc_list_release( p_list );
751 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
754 /*****************************************************************************
755 * config_LoadConfigFile: loads the configuration file.
756 *****************************************************************************
757 * This function is called to load the config options stored in the config
759 *****************************************************************************/
760 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
764 module_config_t *p_item;
767 char *p_index, *psz_option_name, *psz_option_value;
768 char *psz_filename, *psz_homedir, *psz_configfile;
771 psz_configfile = p_this->p_vlc->psz_configfile;
772 if( !psz_configfile || !psz_configfile )
774 psz_homedir = p_this->p_vlc->psz_homedir;
777 msg_Err( p_this, "psz_homedir is null" );
780 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
781 strlen(psz_homedir) );
783 sprintf( psz_filename,
784 "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
789 psz_filename = strdup( psz_configfile );
794 msg_Err( p_this, "out of memory" );
798 msg_Dbg( p_this, "opening config file %s", psz_filename );
800 /* Acquire config file lock */
801 vlc_mutex_lock( &p_this->p_vlc->config_lock );
803 file = utf8_fopen( psz_filename, "rt" );
806 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
807 free( psz_filename );
808 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
812 /* Look for the selected module, if NULL then save everything */
813 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
815 for( i_index = 0; i_index < p_list->i_count; i_index++ )
817 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
820 && strcmp( psz_module_name, p_parser->psz_object_name ) )
825 /* The config file is organized in sections, one per module. Look for
826 * the interesting section ( a section is of the form [foo] ) */
827 fseek( file, 0L, SEEK_SET );
828 while( fgets( line, 1024, file ) )
831 && (p_index = strchr(line,']'))
832 && (p_index - &line[1]
833 == (int)strlen(p_parser->psz_object_name))
834 && !memcmp( &line[1], p_parser->psz_object_name,
835 strlen(p_parser->psz_object_name) ) )
838 msg_Dbg( p_this, "loading config for module \"%s\"",
839 p_parser->psz_object_name );
845 /* either we found the section or we're at the EOF */
847 /* Now try to load the options in this section */
848 while( fgets( line, 1024, file ) )
850 if( line[0] == '[' ) break; /* end of section */
852 /* ignore comments or empty lines */
853 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
856 /* get rid of line feed */
857 if( line[strlen(line)-1] == '\n' )
858 line[strlen(line)-1] = (char)0;
860 /* look for option name */
861 psz_option_name = line;
862 psz_option_value = NULL;
863 p_index = strchr( line, '=' );
864 if( !p_index ) break; /* this ain't an option!!! */
867 psz_option_value = p_index + 1;
869 if( !p_parser->i_config_items )
874 /* try to match this option with one of the module's options */
875 for( p_item = p_parser->p_config;
876 p_item->i_type != CONFIG_HINT_END;
879 if( p_item->i_type & CONFIG_HINT )
883 if( !strcmp( p_item->psz_name, psz_option_name ) )
886 switch( p_item->i_type )
888 case CONFIG_ITEM_BOOL:
889 case CONFIG_ITEM_INTEGER:
890 if( !*psz_option_value )
891 break; /* ignore empty option */
892 p_item->i_value = strtol( psz_option_value, 0, 0 );
893 p_item->i_value_saved = p_item->i_value;
895 msg_Dbg( p_this, "option \"%s\", value %i",
896 p_item->psz_name, p_item->i_value );
900 case CONFIG_ITEM_FLOAT:
901 if( !*psz_option_value )
902 break; /* ignore empty option */
903 p_item->f_value = (float)i18n_atof( psz_option_value);
904 p_item->f_value_saved = p_item->f_value;
906 msg_Dbg( p_this, "option \"%s\", value %f",
907 p_item->psz_name, (double)p_item->f_value );
910 case CONFIG_ITEM_KEY:
911 if( !*psz_option_value )
912 break; /* ignore empty option */
913 p_item->i_value = ConfigStringToKey(psz_option_value);
914 p_item->i_value_saved = p_item->i_value;
918 vlc_mutex_lock( p_item->p_lock );
920 /* free old string */
921 if( p_item->psz_value )
922 free( p_item->psz_value );
924 p_item->psz_value = *psz_option_value ?
925 strdup( psz_option_value ) : NULL;
927 if( p_item->psz_value_saved )
928 free( p_item->psz_value_saved );
929 p_item->psz_value_saved = 0;
930 if( !p_item->psz_value || !p_item->psz_value_orig ||
931 (p_item->psz_value && p_item->psz_value_orig &&
932 strcmp(p_item->psz_value,p_item->psz_value_orig)))
933 p_item->psz_value_saved = p_item->psz_value ?
934 strdup( p_item->psz_value ) : 0;
936 vlc_mutex_unlock( p_item->p_lock );
939 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
941 p_item->psz_value ? p_item->psz_value : "" );
951 vlc_list_release( p_list );
954 free( psz_filename );
956 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
961 /*****************************************************************************
962 * config_CreateDir: Create configuration directory if it doesn't exist.
963 *****************************************************************************/
964 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
966 if( !psz_dirname && !*psz_dirname ) return -1;
968 if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) )
970 msg_Err( p_this, "could not create %s (%s)",
971 psz_dirname, strerror(errno) );
978 /*****************************************************************************
979 * config_SaveConfigFile: Save a module's config options.
980 *****************************************************************************
981 * This will save the specified module's config options to the config file.
982 * If psz_module_name is NULL then we save all the modules config options.
983 * It's no use to save the config options that kept their default values, so
984 * we'll try to be a bit clever here.
986 * When we save we mustn't delete the config options of the modules that
987 * haven't been loaded. So we cannot just create a new config file with the
988 * config structures we've got in memory.
989 * I don't really know how to deal with this nicely, so I will use a completly
991 * I will load the config file in memory, but skipping all the sections of the
992 * modules we want to save. Then I will create a brand new file, dump the file
993 * loaded in memory and then append the sections of the modules we want to
996 *****************************************************************************/
997 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
998 vlc_bool_t b_autosave )
1002 module_config_t *p_item;
1004 char p_line[1024], *p_index2;
1006 char *p_bigbuffer, *p_index;
1007 vlc_bool_t b_backup;
1008 char *psz_filename, *psz_homedir, *psz_configfile;
1011 /* Acquire config file lock */
1012 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1014 psz_configfile = p_this->p_vlc->psz_configfile;
1015 if( !psz_configfile || !psz_configfile )
1017 psz_homedir = p_this->p_vlc->psz_homedir;
1020 msg_Err( p_this, "psz_homedir is null" );
1021 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1024 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1025 strlen(psz_homedir) );
1028 sprintf( psz_filename, "%s" DIR_SEP CONFIG_DIR, psz_homedir );
1032 msg_Err( p_this, "out of memory" );
1033 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1037 config_CreateDir( p_this, psz_filename );
1039 strcat( psz_filename, DIR_SEP CONFIG_FILE );
1043 psz_filename = strdup( psz_configfile );
1046 msg_Err( p_this, "out of memory" );
1047 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1052 msg_Dbg( p_this, "opening config file %s", psz_filename );
1054 file = utf8_fopen( psz_filename, "rt" );
1057 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1061 /* look for file size */
1062 fseek( file, 0L, SEEK_END );
1063 i_sizebuf = ftell( file );
1064 fseek( file, 0L, SEEK_SET );
1067 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1070 msg_Err( p_this, "out of memory" );
1071 if( file ) fclose( file );
1072 free( psz_filename );
1073 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1078 /* List all available modules */
1079 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1081 /* backup file into memory, we only need to backup the sections we won't
1084 while( file && fgets( p_line, 1024, file ) )
1086 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1089 /* we found a section, check if we need to do a backup */
1090 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1092 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1094 if( ((p_index2 - &p_line[1])
1095 == (int)strlen(p_parser->psz_object_name) )
1096 && !memcmp( &p_line[1], p_parser->psz_object_name,
1097 strlen(p_parser->psz_object_name) ) )
1099 if( !psz_module_name )
1101 else if( !strcmp( psz_module_name,
1102 p_parser->psz_object_name ) )
1107 if( i_index == p_list->i_count )
1109 /* we don't have this section in our list so we need to back
1113 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1126 /* save line if requested and line is valid (doesn't begin with a
1127 * space, tab, or eol) */
1128 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1129 && (p_line[0] != '\t') )
1131 strcpy( p_index, p_line );
1132 p_index += strlen( p_line );
1135 if( file ) fclose( file );
1139 * Save module config in file
1142 file = utf8_fopen( psz_filename, "wt" );
1145 msg_Warn( p_this, "could not open config file %s for writing",
1147 free( psz_filename );
1148 vlc_list_release( p_list );
1149 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1153 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
1154 "###\n### lines begining with a '#' character are comments\n###\n\n" );
1156 /* Look for the selected module, if NULL then save everything */
1157 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1159 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1161 if( psz_module_name && strcmp( psz_module_name,
1162 p_parser->psz_object_name ) )
1165 if( !p_parser->i_config_items )
1168 if( psz_module_name )
1169 msg_Dbg( p_this, "saving config for module \"%s\"",
1170 p_parser->psz_object_name );
1172 fprintf( file, "[%s]", p_parser->psz_object_name );
1173 if( p_parser->psz_longname )
1174 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1176 fprintf( file, "\n\n" );
1178 for( p_item = p_parser->p_config;
1179 p_item->i_type != CONFIG_HINT_END;
1183 int i_value = p_item->i_value;
1184 float f_value = p_item->f_value;
1185 char *psz_value = p_item->psz_value;
1187 if( p_item->i_type & CONFIG_HINT )
1190 /* Ignore deprecated options */
1191 if( p_item->psz_current )
1193 if( b_autosave && !p_item->b_autosave )
1195 i_value = p_item->i_value_saved;
1196 f_value = p_item->f_value_saved;
1197 psz_value = p_item->psz_value_saved;
1198 if( !psz_value ) psz_value = p_item->psz_value_orig;
1202 p_item->b_dirty = VLC_FALSE;
1205 switch( p_item->i_type )
1207 case CONFIG_ITEM_BOOL:
1208 case CONFIG_ITEM_INTEGER:
1209 if( p_item->psz_text )
1210 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1211 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1212 _("boolean") : _("integer") );
1213 if( i_value == p_item->i_value_orig )
1214 fprintf( file, "#" );
1215 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1217 p_item->i_value_saved = i_value;
1220 case CONFIG_ITEM_KEY:
1221 if( p_item->psz_text )
1222 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1224 if( i_value == p_item->i_value_orig )
1225 fprintf( file, "#" );
1226 psz_key = ConfigKeyToString( i_value );
1227 fprintf( file, "%s=%s\n", p_item->psz_name,
1228 psz_key ? psz_key : "" );
1229 if ( psz_key ) free( psz_key );
1231 p_item->i_value_saved = i_value;
1234 case CONFIG_ITEM_FLOAT:
1235 if( p_item->psz_text )
1236 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1238 if( f_value == p_item->f_value_orig )
1239 fprintf( file, "#" );
1240 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1242 p_item->f_value_saved = f_value;
1246 if( p_item->psz_text )
1247 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1249 if( (!psz_value && !p_item->psz_value_orig) ||
1250 (psz_value && p_item->psz_value_orig &&
1251 !strcmp( psz_value, p_item->psz_value_orig )) )
1252 fprintf( file, "#" );
1253 fprintf( file, "%s=%s\n", p_item->psz_name,
1254 psz_value ? psz_value : "" );
1256 if( b_autosave && !p_item->b_autosave ) break;
1258 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1259 p_item->psz_value_saved = 0;
1260 if( (psz_value && p_item->psz_value_orig &&
1261 strcmp( psz_value, p_item->psz_value_orig )) ||
1262 !psz_value || !p_item->psz_value_orig)
1263 p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1267 fprintf( file, "\n" );
1270 vlc_list_release( p_list );
1273 * Restore old settings from the config in file
1275 fputs( p_bigbuffer, file );
1276 free( p_bigbuffer );
1279 free( psz_filename );
1280 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1285 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1289 module_config_t *p_item;
1290 int i_index, i_count;
1292 /* Check if there's anything to save */
1293 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1294 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1295 i_count = p_list->i_count;
1296 for( i_index = 0; i_index < i_count; i_index++ )
1298 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1300 if( !p_parser->i_config_items ) continue;
1302 for( p_item = p_parser->p_config;
1303 p_item->i_type != CONFIG_HINT_END;
1306 if( p_item->b_autosave && p_item->b_dirty ) break;
1308 if( p_item->i_type != CONFIG_HINT_END ) break;
1310 vlc_list_release( p_list );
1311 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1313 if( i_index == i_count ) return VLC_SUCCESS;
1314 return SaveConfigFile( p_this, 0, VLC_TRUE );
1317 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1319 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1322 /*****************************************************************************
1323 * config_LoadCmdLine: parse command line
1324 *****************************************************************************
1325 * Parse command line for configuration options.
1326 * Now that the module_bank has been initialized, we can dynamically
1327 * generate the longopts structure used by getops. We have to do it this way
1328 * because we don't know (and don't want to know) in advance the configuration
1329 * options used (ie. exported) by each module.
1330 *****************************************************************************/
1331 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1332 vlc_bool_t b_ignore_errors )
1334 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1337 module_config_t *p_item;
1338 struct option *p_longopts;
1339 int i_modules_index;
1342 module_config_t *pp_shortopts[256];
1343 char *psz_shortopts;
1345 /* Set default configuration and copy arguments */
1346 p_this->p_vlc->i_argc = *pi_argc;
1347 p_this->p_vlc->ppsz_argv = ppsz_argv;
1350 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
1351 * is the PSN - process serial number (a unique PID-ish thingie)
1352 * still ok for real Darwin & when run from command line */
1353 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1354 /* for example -psn_0_9306113 */
1356 /* GDMF!... I can't do this or else the MacOSX window server will
1357 * not pick up the PSN and not register the app and we crash...
1358 * hence the following kludge otherwise we'll get confused w/ argv[1]
1359 * being an input file name */
1361 ppsz_argv[ 1 ] = NULL;
1363 *pi_argc = *pi_argc - 1;
1369 /* List all modules */
1370 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1373 * Generate the longopts and shortopts structures used by getopt_long
1377 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1380 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1382 /* count the number of exported configuration options (to allocate
1383 * longopts). We also need to allocate space for too options when
1384 * dealing with boolean to allow for --foo and --no-foo */
1385 i_opts += p_parser->i_config_items
1386 + 2 * p_parser->i_bool_items;
1389 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1390 if( p_longopts == NULL )
1392 msg_Err( p_this, "out of memory" );
1393 vlc_list_release( p_list );
1397 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1398 if( psz_shortopts == NULL )
1400 msg_Err( p_this, "out of memory" );
1402 vlc_list_release( p_list );
1406 /* If we are requested to ignore errors, then we must work on a copy
1407 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1408 * us, ignoring the arity of the options */
1409 if( b_ignore_errors )
1411 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1412 if( ppsz_argv == NULL )
1414 msg_Err( p_this, "out of memory" );
1415 free( psz_shortopts );
1417 vlc_list_release( p_list );
1420 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1421 *pi_argc * sizeof(char *) );
1425 for( i_index = 0; i_index < 256; i_index++ )
1427 pp_shortopts[i_index] = NULL;
1430 /* Fill the p_longopts and psz_shortopts structures */
1432 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1435 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1437 if( !p_parser->i_config_items )
1440 for( p_item = p_parser->p_config;
1441 p_item->i_type != CONFIG_HINT_END;
1445 if( p_item->i_type & CONFIG_HINT )
1448 /* Add item to long options */
1449 p_longopts[i_index].name = strdup( p_item->psz_name );
1450 if( p_longopts[i_index].name == NULL ) continue;
1451 p_longopts[i_index].has_arg =
1452 (p_item->i_type == CONFIG_ITEM_BOOL)?
1453 no_argument : required_argument;
1454 p_longopts[i_index].flag = &flag;
1455 p_longopts[i_index].val = 0;
1458 /* When dealing with bools we also need to add the --no-foo
1460 if( p_item->i_type == CONFIG_ITEM_BOOL )
1462 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1463 if( psz_name == NULL ) continue;
1464 strcpy( psz_name, "no" );
1465 strcat( psz_name, p_item->psz_name );
1467 p_longopts[i_index].name = psz_name;
1468 p_longopts[i_index].has_arg = no_argument;
1469 p_longopts[i_index].flag = &flag;
1470 p_longopts[i_index].val = 1;
1473 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1474 if( psz_name == NULL ) continue;
1475 strcpy( psz_name, "no-" );
1476 strcat( psz_name, p_item->psz_name );
1478 p_longopts[i_index].name = psz_name;
1479 p_longopts[i_index].has_arg = no_argument;
1480 p_longopts[i_index].flag = &flag;
1481 p_longopts[i_index].val = 1;
1485 /* If item also has a short option, add it */
1486 if( p_item->i_short )
1488 pp_shortopts[(int)p_item->i_short] = p_item;
1489 psz_shortopts[i_shortopts] = p_item->i_short;
1491 if( p_item->i_type != CONFIG_ITEM_BOOL )
1493 psz_shortopts[i_shortopts] = ':';
1496 if( p_item->i_short == 'v' )
1498 psz_shortopts[i_shortopts] = ':';
1506 /* We don't need the module list anymore */
1507 vlc_list_release( p_list );
1509 /* Close the longopts and shortopts structures */
1510 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1511 psz_shortopts[i_shortopts] = '\0';
1514 * Parse the command line options
1518 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1519 p_longopts, &i_index ) ) != EOF )
1521 /* A long option has been recognized */
1524 module_config_t *p_conf;
1525 char *psz_name = (char *)p_longopts[i_index].name;
1527 /* Check if we deal with a --nofoo or --no-foo long option */
1528 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1530 /* Store the configuration option */
1531 p_conf = config_FindConfig( p_this, psz_name );
1534 /* Check if the option is deprecated */
1535 if( p_conf->psz_current )
1537 if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1539 if( !b_ignore_errors )
1542 "Warning: option --%s is no longer used.\n",
1547 if( !b_ignore_errors )
1549 if( p_conf->b_strict )
1552 "Error: option --%s is deprecated. "
1553 "Use --%s instead.\n",
1554 p_conf->psz_name, p_conf->psz_current);
1556 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1557 free( (char *)p_longopts[i_index].name );
1560 free( psz_shortopts );
1564 "Warning: option --%s is deprecated. "
1565 "You should use --%s instead.\n",
1566 p_conf->psz_name, p_conf->psz_current);
1568 psz_name=p_conf->psz_current;
1569 p_conf = config_FindConfig( p_this, psz_name );
1572 switch( p_conf->i_type )
1574 case CONFIG_ITEM_STRING:
1575 case CONFIG_ITEM_FILE:
1576 case CONFIG_ITEM_DIRECTORY:
1577 case CONFIG_ITEM_MODULE:
1578 case CONFIG_ITEM_MODULE_LIST:
1579 case CONFIG_ITEM_MODULE_LIST_CAT:
1580 case CONFIG_ITEM_MODULE_CAT:
1581 config_PutPsz( p_this, psz_name, optarg );
1583 case CONFIG_ITEM_INTEGER:
1584 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1586 case CONFIG_ITEM_FLOAT:
1587 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1589 case CONFIG_ITEM_KEY:
1590 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1592 case CONFIG_ITEM_BOOL:
1593 config_PutInt( p_this, psz_name, !flag );
1600 /* A short option has been recognized */
1601 if( pp_shortopts[i_cmd] != NULL )
1603 switch( pp_shortopts[i_cmd]->i_type )
1605 case CONFIG_ITEM_STRING:
1606 case CONFIG_ITEM_FILE:
1607 case CONFIG_ITEM_DIRECTORY:
1608 case CONFIG_ITEM_MODULE:
1609 case CONFIG_ITEM_MODULE_CAT:
1610 case CONFIG_ITEM_MODULE_LIST:
1611 case CONFIG_ITEM_MODULE_LIST_CAT:
1612 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1614 case CONFIG_ITEM_INTEGER:
1619 if( *optarg == 'v' ) /* eg. -vvv */
1622 while( *optarg == 'v' )
1630 i_verbose += atoi( optarg ); /* eg. -v2 */
1635 i_verbose++; /* -v */
1637 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1642 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1643 strtol(optarg, 0, 0) );
1646 case CONFIG_ITEM_BOOL:
1647 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1654 /* Internal error: unknown option */
1655 if( !b_ignore_errors )
1657 fprintf( stderr, "%s: unknown option"
1658 " or missing mandatory argument ",
1659 p_this->p_vlc->psz_object_name );
1662 fprintf( stderr, "`-%c'\n", optopt );
1666 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1668 fprintf( stderr, "Try `%s --help' for more information.\n",
1669 p_this->p_vlc->psz_object_name );
1671 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1672 free( (char *)p_longopts[i_index].name );
1674 free( psz_shortopts );
1679 /* Free allocated resources */
1680 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1681 free( (char *)p_longopts[i_index].name );
1683 free( psz_shortopts );
1684 if( b_ignore_errors ) free( ppsz_argv );
1690 * config_GetDataDir: find directory where shared data is installed
1692 * @return a string (always succeeds).
1694 const char *config_GetDataDir( const vlc_object_t *p_this )
1696 #if defined (WIN32) || defined (UNDER_CE)
1697 return p_this->p_libvlc->psz_vlcpath;
1698 #elif defined(__APPLE__) || defined (SYS_BEOS)
1699 static char path[PATH_MAX] = "";
1703 snprintf( path, sizeof( path ), "%s/share",
1704 p_this->p_libvlc->psz_vlcpath );
1705 path[sizeof( path ) - 1] = '\0';
1713 /*****************************************************************************
1714 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1715 *****************************************************************************
1716 * This function will try by different ways to find the user's home path.
1717 * Note that this function is not reentrant, it should be called only once
1718 * at the beginning of main where the result will be stored for later use.
1719 *****************************************************************************/
1720 static char *GetDir( vlc_bool_t b_appdata )
1722 char *psz_localhome = NULL;
1724 #if defined(HAVE_GETPWUID)
1725 struct passwd *p_pw = NULL;
1728 #if defined(WIN32) && !defined(UNDER_CE)
1729 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1731 #ifndef CSIDL_FLAG_CREATE
1732 # define CSIDL_FLAG_CREATE 0x8000
1734 #ifndef CSIDL_APPDATA
1735 # define CSIDL_APPDATA 0x1A
1737 #ifndef CSIDL_PROFILE
1738 # define CSIDL_PROFILE 0x28
1740 #ifndef SHGFP_TYPE_CURRENT
1741 # define SHGFP_TYPE_CURRENT 0
1744 HINSTANCE shfolder_dll;
1745 SHGETFOLDERPATH SHGetFolderPath ;
1747 /* load the shfolder dll to retrieve SHGetFolderPath */
1748 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1750 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1751 _T("SHGetFolderPathA") );
1752 if ( SHGetFolderPath != NULL )
1754 char psz_ACPhome[MAX_PATH];
1756 /* get the "Application Data" folder for the current user */
1757 if( S_OK == SHGetFolderPath( NULL,
1758 (b_appdata ? CSIDL_APPDATA :
1759 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1760 NULL, SHGFP_TYPE_CURRENT,
1763 FreeLibrary( shfolder_dll );
1764 return FromLocaleDup( psz_ACPhome );
1767 FreeLibrary( shfolder_dll );
1770 #elif defined(UNDER_CE)
1772 #ifndef CSIDL_APPDATA
1773 # define CSIDL_APPDATA 0x1A
1776 wchar_t p_whomedir[MAX_PATH];
1778 /* get the "Application Data" folder for the current user */
1779 if( SHGetSpecialFolderPath( NULL, p_whomedir, CSIDL_APPDATA, 1 ) )
1781 char psz_ACPhome[2 * MAX_PATH];
1783 sprintf( psz_ACPhome, "%ls", p_whomedir );
1784 return FromLocaleDup( psz_ACPhome );
1788 #if defined(HAVE_GETPWUID)
1789 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1792 psz_localhome = getenv( "HOME" );
1793 if( psz_localhome == NULL )
1795 psz_localhome = getenv( "TMP" );
1796 if( psz_localhome == NULL )
1797 psz_localhome = "/tmp";
1800 #if defined(HAVE_GETPWUID)
1802 psz_localhome = p_pw->pw_dir;
1805 return FromLocaleDup( psz_localhome );
1808 char *config_GetHomeDir( void )
1810 return GetDir( VLC_TRUE );
1813 char *config_GetUserDir( void )
1815 return GetDir( VLC_FALSE );
1819 static int ConfigStringToKey( char *psz_key )
1823 char *psz_parser = strchr( psz_key, '-' );
1824 while( psz_parser && psz_parser != psz_key )
1826 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1828 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1829 strlen( vlc_modifiers[i].psz_key_string ) ) )
1831 i_key |= vlc_modifiers[i].i_key_code;
1834 psz_key = psz_parser + 1;
1835 psz_parser = strchr( psz_key, '-' );
1837 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1839 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1841 i_key |= vlc_keys[i].i_key_code;
1848 static char *ConfigKeyToString( int i_key )
1850 char *psz_key = malloc( 100 );
1860 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1863 if( i_key & vlc_modifiers[index].i_key_code )
1865 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1868 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1871 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1873 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );