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 */
34 # include <unistd.h> /* getuid() */
37 #ifdef HAVE_GETOPT_LONG
39 # include <getopt.h> /* getopt() */
42 # include "../extras/getopt.h"
45 #if defined(HAVE_GETPWUID)
46 # include <pwd.h> /* getpwuid() */
49 #if defined( HAVE_SYS_STAT_H )
50 # include <sys/stat.h>
52 #if defined( HAVE_SYS_TYPES_H )
53 # include <sys/types.h>
56 # if !defined( UNDER_CE )
63 static int ConfigStringToKey( char * );
64 static char *ConfigKeyToString( int );
66 /*****************************************************************************
67 * config_GetType: get the type of a variable (bool, int, float, string)
68 *****************************************************************************
69 * This function is used to get the type of a variable from its name.
70 * Beware, this is quite slow.
71 *****************************************************************************/
72 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
74 module_config_t *p_config;
77 p_config = config_FindConfig( p_this, psz_name );
85 switch( p_config->i_type )
87 case CONFIG_ITEM_BOOL:
88 i_type = VLC_VAR_BOOL;
91 case CONFIG_ITEM_INTEGER:
92 i_type = VLC_VAR_INTEGER;
95 case CONFIG_ITEM_FLOAT:
96 i_type = VLC_VAR_FLOAT;
99 case CONFIG_ITEM_MODULE:
100 case CONFIG_ITEM_MODULE_CAT:
101 case CONFIG_ITEM_MODULE_LIST:
102 case CONFIG_ITEM_MODULE_LIST_CAT:
103 i_type = VLC_VAR_MODULE;
106 case CONFIG_ITEM_STRING:
107 i_type = VLC_VAR_STRING;
110 case CONFIG_ITEM_FILE:
111 i_type = VLC_VAR_FILE;
114 case CONFIG_ITEM_DIRECTORY:
115 i_type = VLC_VAR_DIRECTORY;
126 /*****************************************************************************
127 * config_GetInt: get the value of an int variable
128 *****************************************************************************
129 * This function is used to get the value of variables which are internally
130 * represented by an integer (CONFIG_ITEM_INTEGER and
132 *****************************************************************************/
133 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
135 module_config_t *p_config;
137 p_config = config_FindConfig( p_this, psz_name );
142 msg_Err( p_this, "option %s does not exist", psz_name );
145 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
146 (p_config->i_type!=CONFIG_ITEM_KEY) &&
147 (p_config->i_type!=CONFIG_ITEM_BOOL) )
149 msg_Err( p_this, "option %s does not refer to an int", psz_name );
153 return p_config->i_value;
156 /*****************************************************************************
157 * config_GetFloat: get the value of a float variable
158 *****************************************************************************
159 * This function is used to get the value of variables which are internally
160 * represented by a float (CONFIG_ITEM_FLOAT).
161 *****************************************************************************/
162 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
164 module_config_t *p_config;
166 p_config = config_FindConfig( p_this, psz_name );
171 msg_Err( p_this, "option %s does not exist", psz_name );
174 if( p_config->i_type != CONFIG_ITEM_FLOAT )
176 msg_Err( p_this, "option %s does not refer to a float", psz_name );
180 return p_config->f_value;
183 /*****************************************************************************
184 * config_GetPsz: get the string value of a string variable
185 *****************************************************************************
186 * This function is used to get the value of variables which are internally
187 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
188 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
190 * Important note: remember to free() the returned char* because it's a
191 * duplicate of the actual value. It isn't safe to return a pointer to the
192 * actual value as it can be modified at any time.
193 *****************************************************************************/
194 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
196 module_config_t *p_config;
197 char *psz_value = NULL;
199 p_config = config_FindConfig( p_this, psz_name );
204 msg_Err( p_this, "option %s does not exist", psz_name );
207 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
208 (p_config->i_type!=CONFIG_ITEM_FILE) &&
209 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
210 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
211 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
212 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
213 (p_config->i_type!=CONFIG_ITEM_MODULE) )
215 msg_Err( p_this, "option %s does not refer to a string", psz_name );
220 /* return a copy of the string */
221 vlc_mutex_lock( p_config->p_lock );
222 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
223 vlc_mutex_unlock( p_config->p_lock );
228 /*****************************************************************************
229 * config_PutPsz: set the string value of a string variable
230 *****************************************************************************
231 * This function is used to set the value of variables which are internally
232 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
233 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
234 *****************************************************************************/
235 void __config_PutPsz( vlc_object_t *p_this,
236 const char *psz_name, const char *psz_value )
238 module_config_t *p_config;
239 vlc_value_t oldval, val;
241 p_config = config_FindConfig( p_this, psz_name );
247 msg_Warn( p_this, "option %s does not exist", psz_name );
250 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
251 (p_config->i_type!=CONFIG_ITEM_FILE) &&
252 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
253 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
254 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
255 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
256 (p_config->i_type!=CONFIG_ITEM_MODULE) )
258 msg_Err( p_this, "option %s does not refer to a string", psz_name );
262 vlc_mutex_lock( p_config->p_lock );
264 /* backup old value */
265 oldval.psz_string = p_config->psz_value;
267 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
268 else p_config->psz_value = NULL;
270 p_config->b_dirty = VLC_TRUE;
272 val.psz_string = p_config->psz_value;
274 vlc_mutex_unlock( p_config->p_lock );
276 if( p_config->pf_callback )
278 p_config->pf_callback( p_this, psz_name, oldval, val,
279 p_config->p_callback_data );
282 /* free old string */
283 if( oldval.psz_string ) free( oldval.psz_string );
286 /*****************************************************************************
287 * config_PutInt: set the integer value of an int variable
288 *****************************************************************************
289 * This function is used to set the value of variables which are internally
290 * represented by an integer (CONFIG_ITEM_INTEGER and
292 *****************************************************************************/
293 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
295 module_config_t *p_config;
296 vlc_value_t oldval, val;
298 p_config = config_FindConfig( p_this, psz_name );
303 msg_Warn( p_this, "option %s does not exist", psz_name );
306 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
307 (p_config->i_type!=CONFIG_ITEM_KEY) &&
308 (p_config->i_type!=CONFIG_ITEM_BOOL) )
310 msg_Err( p_this, "option %s does not refer to an int", psz_name );
314 /* backup old value */
315 oldval.i_int = p_config->i_value;
317 /* if i_min == i_max == 0, then do not use them */
318 if ((p_config->i_min == 0) && (p_config->i_max == 0))
320 p_config->i_value = i_value;
322 else if (i_value < p_config->i_min)
324 p_config->i_value = p_config->i_min;
326 else if (i_value > p_config->i_max)
328 p_config->i_value = p_config->i_max;
332 p_config->i_value = i_value;
335 p_config->b_dirty = VLC_TRUE;
337 val.i_int = p_config->i_value;
339 if( p_config->pf_callback )
341 p_config->pf_callback( p_this, psz_name, oldval, val,
342 p_config->p_callback_data );
346 /*****************************************************************************
347 * config_PutFloat: set the value of a float variable
348 *****************************************************************************
349 * This function is used to set the value of variables which are internally
350 * represented by a float (CONFIG_ITEM_FLOAT).
351 *****************************************************************************/
352 void __config_PutFloat( vlc_object_t *p_this,
353 const char *psz_name, float f_value )
355 module_config_t *p_config;
356 vlc_value_t oldval, val;
358 p_config = config_FindConfig( p_this, psz_name );
363 msg_Warn( p_this, "option %s does not exist", psz_name );
366 if( p_config->i_type != CONFIG_ITEM_FLOAT )
368 msg_Err( p_this, "option %s does not refer to a float", psz_name );
372 /* backup old value */
373 oldval.f_float = p_config->f_value;
375 /* if f_min == f_max == 0, then do not use them */
376 if ((p_config->f_min == 0) && (p_config->f_max == 0))
378 p_config->f_value = f_value;
380 else if (f_value < p_config->f_min)
382 p_config->f_value = p_config->f_min;
384 else if (f_value > p_config->f_max)
386 p_config->f_value = p_config->f_max;
390 p_config->f_value = f_value;
393 p_config->b_dirty = VLC_TRUE;
395 val.f_float = p_config->f_value;
397 if( p_config->pf_callback )
399 p_config->pf_callback( p_this, psz_name, oldval, val,
400 p_config->p_callback_data );
404 /*****************************************************************************
405 * config_FindConfig: find the config structure associated with an option.
406 *****************************************************************************
407 * FIXME: This function really needs to be optimized.
408 * FIXME: And now even more.
409 *****************************************************************************/
410 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
414 module_config_t *p_item;
417 if( !psz_name ) return NULL;
419 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
421 for( i_index = 0; i_index < p_list->i_count; i_index++ )
423 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
425 if( !p_parser->i_config_items )
428 for( p_item = p_parser->p_config;
429 p_item->i_type != CONFIG_HINT_END;
432 if( p_item->i_type & CONFIG_HINT )
435 if( !strcmp( psz_name, p_item->psz_name ) )
437 vlc_list_release( p_list );
443 vlc_list_release( p_list );
448 /*****************************************************************************
449 * config_FindModule: find a specific module structure.
450 *****************************************************************************/
451 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
454 module_t *p_module, *p_result = NULL;
457 if( !psz_name ) return NULL;
459 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
461 for( i_index = 0; i_index < p_list->i_count; i_index++ )
463 p_module = (module_t *)p_list->p_values[i_index].p_object;
464 if( !strcmp( p_module->psz_object_name, psz_name ) )
471 vlc_list_release( p_list );
476 /*****************************************************************************
477 * config_Duplicate: creates a duplicate of a module's configuration data.
478 *****************************************************************************
479 * Unfortunatly we cannot work directly with the module's config data as
480 * this module might be unloaded from memory at any time (remember HideModule).
481 * This is why we need to create an exact copy of the config data.
482 *****************************************************************************/
483 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
485 int i, j, i_lines = 1;
486 module_config_t *p_item;
488 /* Calculate the structure length */
489 p_module->i_config_items = 0;
490 p_module->i_bool_items = 0;
492 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
496 if( p_item->i_type & CONFIG_ITEM )
498 p_module->i_config_items++;
501 if( p_item->i_type == CONFIG_ITEM_BOOL )
503 p_module->i_bool_items++;
507 /* Allocate memory */
508 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
510 if( p_module->p_config == NULL )
512 msg_Err( p_module, "config error: can't duplicate p_config" );
516 /* Do the duplication job */
517 for( i = 0; i < i_lines ; i++ )
519 p_module->p_config[i] = p_orig[i];
521 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
522 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
523 p_module->p_config[i].i_value_saved = p_orig[i].i_value;
524 p_module->p_config[i].f_value_saved = p_orig[i].f_value;
525 p_module->p_config[i].psz_value_saved = 0;
527 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
528 strdup( p_orig[i].psz_type ) : NULL;
529 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
530 strdup( p_orig[i].psz_name ) : NULL;
531 p_module->p_config[i].psz_current = p_orig[i].psz_current?
532 strdup( p_orig[i].psz_current ) : NULL;
533 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
534 strdup( _(p_orig[i].psz_text) ) : NULL;
535 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
536 strdup( _(p_orig[i].psz_longtext) ) : NULL;
537 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
538 strdup( p_orig[i].psz_value ) : NULL;
539 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
540 strdup( p_orig[i].psz_value ) : NULL;
542 p_module->p_config[i].p_lock = &p_module->object_lock;
544 /* duplicate the string list */
545 if( p_orig[i].i_list )
547 if( p_orig[i].ppsz_list )
549 p_module->p_config[i].ppsz_list =
550 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
551 if( p_module->p_config[i].ppsz_list )
553 for( j = 0; j < p_orig[i].i_list; j++ )
554 p_module->p_config[i].ppsz_list[j] = p_orig[i].ppsz_list[j] ?
555 strdup( p_orig[i].ppsz_list[j] ) : NULL ;
556 p_module->p_config[i].ppsz_list[j] = NULL;
559 if( p_orig[i].ppsz_list_text )
561 p_module->p_config[i].ppsz_list_text =
562 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
563 if( p_module->p_config[i].ppsz_list_text )
565 for( j = 0; j < p_orig[i].i_list; j++ )
566 p_module->p_config[i].ppsz_list_text[j] = _(p_orig[i].ppsz_list_text[j]) ?
567 strdup( _(p_orig[i].ppsz_list_text[j] ) ) : NULL ;
568 p_module->p_config[i].ppsz_list_text[j] = NULL;
571 if( p_orig[i].pi_list )
573 p_module->p_config[i].pi_list =
574 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
575 if( p_module->p_config[i].pi_list )
577 for( j = 0; j < p_orig[i].i_list; j++ )
578 p_module->p_config[i].pi_list[j] =
579 p_orig[i].pi_list[j];
584 /* duplicate the actions list */
585 if( p_orig[i].i_action )
589 p_module->p_config[i].ppf_action =
590 malloc( p_orig[i].i_action * sizeof(void *) );
591 p_module->p_config[i].ppsz_action_text =
592 malloc( p_orig[i].i_action * sizeof(char *) );
594 for( j = 0; j < p_orig[i].i_action; j++ )
596 p_module->p_config[i].ppf_action[j] =
597 p_orig[i].ppf_action[j];
598 p_module->p_config[i].ppsz_action_text[j] =
599 p_orig[i].ppsz_action_text[j] ?
600 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
604 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
608 /*****************************************************************************
609 * config_Free: frees a duplicated module's configuration data.
610 *****************************************************************************
611 * This function frees all the data duplicated by config_Duplicate.
612 *****************************************************************************/
613 void config_Free( module_t *p_module )
615 module_config_t *p_item = p_module->p_config;
623 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
625 if( p_item->psz_type )
626 free( p_item->psz_type );
628 if( p_item->psz_name )
629 free( p_item->psz_name );
631 if( p_item->psz_current )
632 free( p_item->psz_current );
634 if( p_item->psz_text )
635 free( p_item->psz_text );
637 if( p_item->psz_longtext )
638 free( p_item->psz_longtext );
640 if( p_item->psz_value )
641 free( p_item->psz_value );
643 if( p_item->psz_value_orig )
644 free( p_item->psz_value_orig );
646 if( p_item->psz_value_saved )
647 free( p_item->psz_value_saved );
651 for( i = 0; i < p_item->i_list; i++ )
653 if( p_item->ppsz_list && p_item->ppsz_list[i] )
654 free( p_item->ppsz_list[i] );
655 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
656 free( p_item->ppsz_list_text[i] );
658 if( p_item->ppsz_list ) free( p_item->ppsz_list );
659 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
660 if( p_item->pi_list ) free( p_item->pi_list );
663 if( p_item->i_action )
665 for( i = 0; i < p_item->i_action; i++ )
667 if( p_item->ppsz_action_text[i] )
668 free( p_item->ppsz_action_text[i] );
670 if( p_item->ppf_action ) free( p_item->ppf_action );
671 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
675 free( p_module->p_config );
676 p_module->p_config = NULL;
679 /*****************************************************************************
680 * config_SetCallbacks: sets callback functions in the duplicate p_config.
681 *****************************************************************************
682 * Unfortunatly we cannot work directly with the module's config data as
683 * this module might be unloaded from memory at any time (remember HideModule).
684 * This is why we need to duplicate callbacks each time we reload the module.
685 *****************************************************************************/
686 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
688 while( p_new->i_type != CONFIG_HINT_END )
690 p_new->pf_callback = p_orig->pf_callback;
696 /*****************************************************************************
697 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
698 *****************************************************************************
699 * We simply undo what we did in config_SetCallbacks.
700 *****************************************************************************/
701 void config_UnsetCallbacks( module_config_t *p_new )
703 while( p_new->i_type != CONFIG_HINT_END )
705 p_new->pf_callback = NULL;
710 /*****************************************************************************
711 * config_ResetAll: reset the configuration data for all the modules.
712 *****************************************************************************/
713 void __config_ResetAll( vlc_object_t *p_this )
719 /* Acquire config file lock */
720 vlc_mutex_lock( &p_this->p_vlc->config_lock );
722 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
724 for( i_index = 0; i_index < p_list->i_count; i_index++ )
726 p_module = (module_t *)p_list->p_values[i_index].p_object ;
727 if( p_module->b_submodule ) continue;
729 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
731 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
732 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
733 if( p_module->p_config[i].psz_value )
734 free( p_module->p_config[i].psz_value );
735 p_module->p_config[i].psz_value =
736 p_module->p_config[i].psz_value_orig ?
737 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
741 vlc_list_release( p_list );
742 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
745 /*****************************************************************************
746 * config_LoadConfigFile: loads the configuration file.
747 *****************************************************************************
748 * This function is called to load the config options stored in the config
750 *****************************************************************************/
751 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
755 module_config_t *p_item;
758 char *p_index, *psz_option_name, *psz_option_value;
759 char *psz_filename, *psz_homedir, *psz_configfile;
762 psz_configfile = p_this->p_vlc->psz_configfile;
763 if( !psz_configfile || !psz_configfile )
765 psz_homedir = p_this->p_vlc->psz_homedir;
768 msg_Err( p_this, "psz_homedir is null" );
771 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
772 strlen(psz_homedir) );
774 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
779 psz_filename = strdup( psz_configfile );
784 msg_Err( p_this, "out of memory" );
788 msg_Dbg( p_this, "opening config file %s", psz_filename );
790 /* Acquire config file lock */
791 vlc_mutex_lock( &p_this->p_vlc->config_lock );
793 file = utf8_fopen( psz_filename, "rt" );
796 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
797 free( psz_filename );
798 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
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 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
810 && strcmp( psz_module_name, p_parser->psz_object_name ) )
815 /* The config file is organized in sections, one per module. Look for
816 * the interesting section ( a section is of the form [foo] ) */
817 fseek( file, 0L, SEEK_SET );
818 while( fgets( line, 1024, file ) )
821 && (p_index = strchr(line,']'))
822 && (p_index - &line[1]
823 == (int)strlen(p_parser->psz_object_name))
824 && !memcmp( &line[1], p_parser->psz_object_name,
825 strlen(p_parser->psz_object_name) ) )
828 msg_Dbg( p_this, "loading config for module \"%s\"",
829 p_parser->psz_object_name );
835 /* either we found the section or we're at the EOF */
837 /* Now try to load the options in this section */
838 while( fgets( line, 1024, file ) )
840 if( line[0] == '[' ) break; /* end of section */
842 /* ignore comments or empty lines */
843 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
846 /* get rid of line feed */
847 if( line[strlen(line)-1] == '\n' )
848 line[strlen(line)-1] = (char)0;
850 /* look for option name */
851 psz_option_name = line;
852 psz_option_value = NULL;
853 p_index = strchr( line, '=' );
854 if( !p_index ) break; /* this ain't an option!!! */
857 psz_option_value = p_index + 1;
859 if( !p_parser->i_config_items )
864 /* try to match this option with one of the module's options */
865 for( p_item = p_parser->p_config;
866 p_item->i_type != CONFIG_HINT_END;
869 if( p_item->i_type & CONFIG_HINT )
873 if( !strcmp( p_item->psz_name, psz_option_name ) )
876 switch( p_item->i_type )
878 case CONFIG_ITEM_BOOL:
879 case CONFIG_ITEM_INTEGER:
880 if( !*psz_option_value )
881 break; /* ignore empty option */
882 p_item->i_value = strtol( psz_option_value, 0, 0 );
883 p_item->i_value_saved = p_item->i_value;
885 msg_Dbg( p_this, "option \"%s\", value %i",
886 p_item->psz_name, p_item->i_value );
890 case CONFIG_ITEM_FLOAT:
891 if( !*psz_option_value )
892 break; /* ignore empty option */
893 p_item->f_value = (float)atof( psz_option_value);
894 p_item->f_value_saved = p_item->f_value;
896 msg_Dbg( p_this, "option \"%s\", value %f",
897 p_item->psz_name, (double)p_item->f_value );
900 case CONFIG_ITEM_KEY:
901 if( !*psz_option_value )
902 break; /* ignore empty option */
903 p_item->i_value = ConfigStringToKey(psz_option_value);
904 p_item->i_value_saved = p_item->i_value;
908 vlc_mutex_lock( p_item->p_lock );
910 /* free old string */
911 if( p_item->psz_value )
912 free( p_item->psz_value );
914 p_item->psz_value = *psz_option_value ?
915 strdup( psz_option_value ) : NULL;
917 if( p_item->psz_value_saved )
918 free( p_item->psz_value_saved );
919 p_item->psz_value_saved = 0;
920 if( !p_item->psz_value || !p_item->psz_value_orig ||
921 (p_item->psz_value && p_item->psz_value_orig &&
922 strcmp(p_item->psz_value,p_item->psz_value_orig)))
923 p_item->psz_value_saved = p_item->psz_value ?
924 strdup( p_item->psz_value ) : 0;
926 vlc_mutex_unlock( p_item->p_lock );
929 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
931 p_item->psz_value ? p_item->psz_value : "" );
941 vlc_list_release( p_list );
944 free( psz_filename );
946 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
951 /*****************************************************************************
952 * config_CreateDir: Create configuration directory if it doesn't exist.
953 *****************************************************************************/
954 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
956 if( !psz_dirname && !*psz_dirname ) return -1;
958 if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) )
960 msg_Err( p_this, "could not create %s (%s)",
961 psz_dirname, strerror(errno) );
968 /*****************************************************************************
969 * config_SaveConfigFile: Save a module's config options.
970 *****************************************************************************
971 * This will save the specified module's config options to the config file.
972 * If psz_module_name is NULL then we save all the modules config options.
973 * It's no use to save the config options that kept their default values, so
974 * we'll try to be a bit clever here.
976 * When we save we mustn't delete the config options of the modules that
977 * haven't been loaded. So we cannot just create a new config file with the
978 * config structures we've got in memory.
979 * I don't really know how to deal with this nicely, so I will use a completly
981 * I will load the config file in memory, but skipping all the sections of the
982 * modules we want to save. Then I will create a brand new file, dump the file
983 * loaded in memory and then append the sections of the modules we want to
986 *****************************************************************************/
987 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
988 vlc_bool_t b_autosave )
992 module_config_t *p_item;
994 char p_line[1024], *p_index2;
996 char *p_bigbuffer, *p_index;
998 char *psz_filename, *psz_homedir, *psz_configfile;
1001 /* Acquire config file lock */
1002 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1004 psz_configfile = p_this->p_vlc->psz_configfile;
1005 if( !psz_configfile || !psz_configfile )
1007 psz_homedir = p_this->p_vlc->psz_homedir;
1010 msg_Err( p_this, "psz_homedir is null" );
1011 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1014 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1015 strlen(psz_homedir) );
1018 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1022 msg_Err( p_this, "out of memory" );
1023 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1027 config_CreateDir( p_this, psz_filename );
1029 strcat( psz_filename, "/" CONFIG_FILE );
1033 psz_filename = strdup( psz_configfile );
1036 msg_Err( p_this, "out of memory" );
1037 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1042 msg_Dbg( p_this, "opening config file %s", psz_filename );
1044 file = utf8_fopen( psz_filename, "rt" );
1047 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1051 /* look for file size */
1052 fseek( file, 0L, SEEK_END );
1053 i_sizebuf = ftell( file );
1054 fseek( file, 0L, SEEK_SET );
1057 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1060 msg_Err( p_this, "out of memory" );
1061 if( file ) fclose( file );
1062 free( psz_filename );
1063 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1068 /* List all available modules */
1069 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1071 /* backup file into memory, we only need to backup the sections we won't
1074 while( file && fgets( p_line, 1024, file ) )
1076 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1079 /* we found a section, check if we need to do a backup */
1080 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1082 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1084 if( ((p_index2 - &p_line[1])
1085 == (int)strlen(p_parser->psz_object_name) )
1086 && !memcmp( &p_line[1], p_parser->psz_object_name,
1087 strlen(p_parser->psz_object_name) ) )
1089 if( !psz_module_name )
1091 else if( !strcmp( psz_module_name,
1092 p_parser->psz_object_name ) )
1097 if( i_index == p_list->i_count )
1099 /* we don't have this section in our list so we need to back
1103 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1116 /* save line if requested and line is valid (doesn't begin with a
1117 * space, tab, or eol) */
1118 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1119 && (p_line[0] != '\t') )
1121 strcpy( p_index, p_line );
1122 p_index += strlen( p_line );
1125 if( file ) fclose( file );
1129 * Save module config in file
1132 file = utf8_fopen( psz_filename, "wt" );
1135 msg_Warn( p_this, "could not open config file %s for writing",
1137 free( psz_filename );
1138 vlc_list_release( p_list );
1139 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1143 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
1144 "###\n### lines begining with a '#' character are comments\n###\n\n" );
1146 /* Look for the selected module, if NULL then save everything */
1147 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1149 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1151 if( psz_module_name && strcmp( psz_module_name,
1152 p_parser->psz_object_name ) )
1155 if( !p_parser->i_config_items )
1158 if( psz_module_name )
1159 msg_Dbg( p_this, "saving config for module \"%s\"",
1160 p_parser->psz_object_name );
1162 fprintf( file, "[%s]", p_parser->psz_object_name );
1163 if( p_parser->psz_longname )
1164 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1166 fprintf( file, "\n\n" );
1168 for( p_item = p_parser->p_config;
1169 p_item->i_type != CONFIG_HINT_END;
1173 int i_value = p_item->i_value;
1174 float f_value = p_item->f_value;
1175 char *psz_value = p_item->psz_value;
1177 if( p_item->i_type & CONFIG_HINT )
1180 /* Ignore deprecated options */
1181 if( p_item->psz_current )
1183 if( b_autosave && !p_item->b_autosave )
1185 i_value = p_item->i_value_saved;
1186 f_value = p_item->f_value_saved;
1187 psz_value = p_item->psz_value_saved;
1188 if( !psz_value ) psz_value = p_item->psz_value_orig;
1192 p_item->b_dirty = VLC_FALSE;
1195 switch( p_item->i_type )
1197 case CONFIG_ITEM_BOOL:
1198 case CONFIG_ITEM_INTEGER:
1199 if( p_item->psz_text )
1200 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1201 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1202 _("boolean") : _("integer") );
1203 if( i_value == p_item->i_value_orig )
1204 fprintf( file, "#" );
1205 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1207 p_item->i_value_saved = i_value;
1210 case CONFIG_ITEM_KEY:
1211 if( p_item->psz_text )
1212 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1214 if( i_value == p_item->i_value_orig )
1215 fprintf( file, "#" );
1216 psz_key = ConfigKeyToString( i_value );
1217 fprintf( file, "%s=%s\n", p_item->psz_name,
1218 psz_key ? psz_key : "" );
1219 if ( psz_key ) free( psz_key );
1221 p_item->i_value_saved = i_value;
1224 case CONFIG_ITEM_FLOAT:
1225 if( p_item->psz_text )
1226 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1228 if( f_value == p_item->f_value_orig )
1229 fprintf( file, "#" );
1230 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1232 p_item->f_value_saved = f_value;
1236 if( p_item->psz_text )
1237 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1239 if( (!psz_value && !p_item->psz_value_orig) ||
1240 (psz_value && p_item->psz_value_orig &&
1241 !strcmp( psz_value, p_item->psz_value_orig )) )
1242 fprintf( file, "#" );
1243 fprintf( file, "%s=%s\n", p_item->psz_name,
1244 psz_value ? psz_value : "" );
1246 if( b_autosave && !p_item->b_autosave ) break;
1248 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1249 p_item->psz_value_saved = 0;
1250 if( (psz_value && p_item->psz_value_orig &&
1251 strcmp( psz_value, p_item->psz_value_orig )) ||
1252 !psz_value || !p_item->psz_value_orig)
1253 p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1257 fprintf( file, "\n" );
1260 vlc_list_release( p_list );
1263 * Restore old settings from the config in file
1265 fputs( p_bigbuffer, file );
1266 free( p_bigbuffer );
1269 free( psz_filename );
1270 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1275 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1279 module_config_t *p_item;
1280 int i_index, i_count;
1282 /* Check if there's anything to save */
1283 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1284 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1285 i_count = p_list->i_count;
1286 for( i_index = 0; i_index < i_count; i_index++ )
1288 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1290 if( !p_parser->i_config_items ) continue;
1292 for( p_item = p_parser->p_config;
1293 p_item->i_type != CONFIG_HINT_END;
1296 if( p_item->b_autosave && p_item->b_dirty ) break;
1298 if( p_item->i_type != CONFIG_HINT_END ) break;
1300 vlc_list_release( p_list );
1301 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1303 if( i_index == i_count ) return VLC_SUCCESS;
1304 return SaveConfigFile( p_this, 0, VLC_TRUE );
1307 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1309 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1312 /*****************************************************************************
1313 * config_LoadCmdLine: parse command line
1314 *****************************************************************************
1315 * Parse command line for configuration options.
1316 * Now that the module_bank has been initialized, we can dynamically
1317 * generate the longopts structure used by getops. We have to do it this way
1318 * because we don't know (and don't want to know) in advance the configuration
1319 * options used (ie. exported) by each module.
1320 *****************************************************************************/
1321 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1322 vlc_bool_t b_ignore_errors )
1324 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1327 module_config_t *p_item;
1328 struct option *p_longopts;
1329 int i_modules_index;
1332 module_config_t *pp_shortopts[256];
1333 char *psz_shortopts;
1335 /* Set default configuration and copy arguments */
1336 p_this->p_vlc->i_argc = *pi_argc;
1337 p_this->p_vlc->ppsz_argv = ppsz_argv;
1340 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1341 * is the PSN - process serial number (a unique PID-ish thingie)
1342 * still ok for real Darwin & when run from command line */
1343 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1344 /* for example -psn_0_9306113 */
1346 /* GDMF!... I can't do this or else the MacOSX window server will
1347 * not pick up the PSN and not register the app and we crash...
1348 * hence the following kludge otherwise we'll get confused w/ argv[1]
1349 * being an input file name */
1351 ppsz_argv[ 1 ] = NULL;
1353 *pi_argc = *pi_argc - 1;
1359 /* List all modules */
1360 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1363 * Generate the longopts and shortopts structures used by getopt_long
1367 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1370 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1372 /* count the number of exported configuration options (to allocate
1373 * longopts). We also need to allocate space for too options when
1374 * dealing with boolean to allow for --foo and --no-foo */
1375 i_opts += p_parser->i_config_items
1376 + 2 * p_parser->i_bool_items;
1379 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1380 if( p_longopts == NULL )
1382 msg_Err( p_this, "out of memory" );
1383 vlc_list_release( p_list );
1387 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1388 if( psz_shortopts == NULL )
1390 msg_Err( p_this, "out of memory" );
1392 vlc_list_release( p_list );
1396 /* If we are requested to ignore errors, then we must work on a copy
1397 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1398 * us, ignoring the arity of the options */
1399 if( b_ignore_errors )
1401 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1402 if( ppsz_argv == NULL )
1404 msg_Err( p_this, "out of memory" );
1405 free( psz_shortopts );
1407 vlc_list_release( p_list );
1410 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1411 *pi_argc * sizeof(char *) );
1415 for( i_index = 0; i_index < 256; i_index++ )
1417 pp_shortopts[i_index] = NULL;
1420 /* Fill the p_longopts and psz_shortopts structures */
1422 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1425 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1427 if( !p_parser->i_config_items )
1430 for( p_item = p_parser->p_config;
1431 p_item->i_type != CONFIG_HINT_END;
1435 if( p_item->i_type & CONFIG_HINT )
1438 /* Add item to long options */
1439 p_longopts[i_index].name = strdup( p_item->psz_name );
1440 if( p_longopts[i_index].name == NULL ) continue;
1441 p_longopts[i_index].has_arg =
1442 (p_item->i_type == CONFIG_ITEM_BOOL)?
1443 no_argument : required_argument;
1444 p_longopts[i_index].flag = &flag;
1445 p_longopts[i_index].val = 0;
1448 /* When dealing with bools we also need to add the --no-foo
1450 if( p_item->i_type == CONFIG_ITEM_BOOL )
1452 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1453 if( psz_name == NULL ) continue;
1454 strcpy( psz_name, "no" );
1455 strcat( psz_name, p_item->psz_name );
1457 p_longopts[i_index].name = psz_name;
1458 p_longopts[i_index].has_arg = no_argument;
1459 p_longopts[i_index].flag = &flag;
1460 p_longopts[i_index].val = 1;
1463 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1464 if( psz_name == NULL ) continue;
1465 strcpy( psz_name, "no-" );
1466 strcat( psz_name, p_item->psz_name );
1468 p_longopts[i_index].name = psz_name;
1469 p_longopts[i_index].has_arg = no_argument;
1470 p_longopts[i_index].flag = &flag;
1471 p_longopts[i_index].val = 1;
1475 /* If item also has a short option, add it */
1476 if( p_item->i_short )
1478 pp_shortopts[(int)p_item->i_short] = p_item;
1479 psz_shortopts[i_shortopts] = p_item->i_short;
1481 if( p_item->i_type != CONFIG_ITEM_BOOL )
1483 psz_shortopts[i_shortopts] = ':';
1486 if( p_item->i_short == 'v' )
1488 psz_shortopts[i_shortopts] = ':';
1496 /* We don't need the module list anymore */
1497 vlc_list_release( p_list );
1499 /* Close the longopts and shortopts structures */
1500 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1501 psz_shortopts[i_shortopts] = '\0';
1504 * Parse the command line options
1508 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1509 p_longopts, &i_index ) ) != EOF )
1511 /* A long option has been recognized */
1514 module_config_t *p_conf;
1515 char *psz_name = (char *)p_longopts[i_index].name;
1517 /* Check if we deal with a --nofoo or --no-foo long option */
1518 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1520 /* Store the configuration option */
1521 p_conf = config_FindConfig( p_this, psz_name );
1524 /* Check if the option is deprecated */
1525 if( p_conf->psz_current )
1527 if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1529 if( !b_ignore_errors )
1532 "Warning: option --%s is no longer used.\n",
1537 if( !b_ignore_errors )
1539 if( p_conf->b_strict )
1542 "Error: option --%s is deprecated. "
1543 "Use --%s instead.\n",
1544 p_conf->psz_name, p_conf->psz_current);
1546 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1547 free( (char *)p_longopts[i_index].name );
1550 free( psz_shortopts );
1554 "Warning: option --%s is deprecated. "
1555 "You should use --%s instead.\n",
1556 p_conf->psz_name, p_conf->psz_current);
1558 psz_name=p_conf->psz_current;
1559 p_conf = config_FindConfig( p_this, psz_name );
1562 switch( p_conf->i_type )
1564 case CONFIG_ITEM_STRING:
1565 case CONFIG_ITEM_FILE:
1566 case CONFIG_ITEM_DIRECTORY:
1567 case CONFIG_ITEM_MODULE:
1568 case CONFIG_ITEM_MODULE_LIST:
1569 case CONFIG_ITEM_MODULE_LIST_CAT:
1570 case CONFIG_ITEM_MODULE_CAT:
1571 config_PutPsz( p_this, psz_name, optarg );
1573 case CONFIG_ITEM_INTEGER:
1574 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1576 case CONFIG_ITEM_FLOAT:
1577 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1579 case CONFIG_ITEM_KEY:
1580 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1582 case CONFIG_ITEM_BOOL:
1583 config_PutInt( p_this, psz_name, !flag );
1590 /* A short option has been recognized */
1591 if( pp_shortopts[i_cmd] != NULL )
1593 switch( pp_shortopts[i_cmd]->i_type )
1595 case CONFIG_ITEM_STRING:
1596 case CONFIG_ITEM_FILE:
1597 case CONFIG_ITEM_DIRECTORY:
1598 case CONFIG_ITEM_MODULE:
1599 case CONFIG_ITEM_MODULE_CAT:
1600 case CONFIG_ITEM_MODULE_LIST:
1601 case CONFIG_ITEM_MODULE_LIST_CAT:
1602 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1604 case CONFIG_ITEM_INTEGER:
1609 if( *optarg == 'v' ) /* eg. -vvv */
1612 while( *optarg == 'v' )
1620 i_verbose += atoi( optarg ); /* eg. -v2 */
1625 i_verbose++; /* -v */
1627 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1632 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1633 strtol(optarg, 0, 0) );
1636 case CONFIG_ITEM_BOOL:
1637 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1644 /* Internal error: unknown option */
1645 if( !b_ignore_errors )
1647 fprintf( stderr, "%s: unknown option"
1648 " or missing mandatory argument ",
1649 p_this->p_vlc->psz_object_name );
1652 fprintf( stderr, "`-%c'\n", optopt );
1656 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1658 fprintf( stderr, "Try `%s --help' for more information.\n",
1659 p_this->p_vlc->psz_object_name );
1661 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1662 free( (char *)p_longopts[i_index].name );
1664 free( psz_shortopts );
1669 /* Free allocated resources */
1670 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1671 free( (char *)p_longopts[i_index].name );
1673 free( psz_shortopts );
1674 if( b_ignore_errors ) free( ppsz_argv );
1679 /*****************************************************************************
1680 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1681 *****************************************************************************
1682 * This function will try by different ways to find the user's home path.
1683 * Note that this function is not reentrant, it should be called only once
1684 * at the beginning of main where the result will be stored for later use.
1685 *****************************************************************************/
1686 static char *GetDir( vlc_bool_t b_appdata )
1688 char *psz_localhome = NULL;
1690 #if defined(HAVE_GETPWUID)
1691 struct passwd *p_pw = NULL;
1694 #if defined(WIN32) && !defined(UNDER_CE)
1695 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1697 #ifndef CSIDL_FLAG_CREATE
1698 # define CSIDL_FLAG_CREATE 0x8000
1700 #ifndef CSIDL_APPDATA
1701 # define CSIDL_APPDATA 0x1A
1703 #ifndef CSIDL_PROFILE
1704 # define CSIDL_PROFILE 0x28
1706 #ifndef SHGFP_TYPE_CURRENT
1707 # define SHGFP_TYPE_CURRENT 0
1710 HINSTANCE shfolder_dll;
1711 SHGETFOLDERPATH SHGetFolderPath ;
1713 /* load the shfolder dll to retrieve SHGetFolderPath */
1714 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1716 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1717 _T("SHGetFolderPathA") );
1718 if ( SHGetFolderPath != NULL )
1720 char psz_ACPhome[MAX_PATH];
1722 /* get the "Application Data" folder for the current user */
1723 if( S_OK == SHGetFolderPath( NULL,
1724 (b_appdata ? CSIDL_APPDATA :
1725 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1726 NULL, SHGFP_TYPE_CURRENT,
1729 FreeLibrary( shfolder_dll );
1730 return FromLocaleDup( psz_ACPhome );
1733 FreeLibrary( shfolder_dll );
1736 #elif defined(UNDER_CE)
1738 #ifndef CSIDL_APPDATA
1739 # define CSIDL_APPDATA 0x1A
1742 wchar_t p_whomedir[MAX_PATH];
1744 /* get the "Application Data" folder for the current user */
1745 if( SHGetSpecialFolderPath( NULL, p_whomedir, CSIDL_APPDATA, 1 ) )
1747 char psz_ACPhome[2 * MAX_PATH];
1749 sprintf( psz_ACPhome, "%ls", p_whomedir );
1750 return FromLocaleDup( p_ACPhome );
1754 #if defined(HAVE_GETPWUID)
1755 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1758 psz_localhome = getenv( "HOME" );
1759 if( psz_localhome == NULL )
1761 psz_localhome = getenv( "TMP" );
1762 if( psz_localhome == NULL )
1763 psz_localhome = "/tmp";
1766 #if defined(HAVE_GETPWUID)
1768 psz_localhome = p_pw->pw_dir;
1771 return FromLocaleDup( psz_localhome );
1774 char *config_GetHomeDir( void )
1776 return GetDir( VLC_TRUE );
1779 char *config_GetUserDir( void )
1781 return GetDir( VLC_FALSE );
1785 static int ConfigStringToKey( char *psz_key )
1789 char *psz_parser = strchr( psz_key, '-' );
1790 while( psz_parser && psz_parser != psz_key )
1792 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1794 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1795 strlen( vlc_modifiers[i].psz_key_string ) ) )
1797 i_key |= vlc_modifiers[i].i_key_code;
1800 psz_key = psz_parser + 1;
1801 psz_parser = strchr( psz_key, '-' );
1803 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1805 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1807 i_key |= vlc_keys[i].i_key_code;
1814 static char *ConfigKeyToString( int i_key )
1816 char *psz_key = malloc( 100 );
1826 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1829 if( i_key & vlc_modifiers[index].i_key_code )
1831 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1834 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1837 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1839 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );