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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #include <stdio.h> /* sprintf() */
28 #include <stdlib.h> /* free(), strtol() */
29 #include <string.h> /* strdup() */
30 #include <errno.h> /* errno */
33 # include <unistd.h> /* getuid() */
36 #ifdef HAVE_GETOPT_LONG
38 # include <getopt.h> /* getopt() */
41 # include "../extras/getopt.h"
44 #if defined(HAVE_GETPWUID)
45 # include <pwd.h> /* getpwuid() */
48 #if defined( HAVE_SYS_STAT_H )
49 # include <sys/stat.h>
51 #if defined( HAVE_SYS_TYPES_H )
52 # include <sys/types.h>
55 # if !defined( UNDER_CE )
62 static int ConfigStringToKey( char * );
63 static char *ConfigKeyToString( int );
65 /*****************************************************************************
66 * config_GetType: get the type of a variable (bool, int, float, string)
67 *****************************************************************************
68 * This function is used to get the type of a variable from its name.
69 * Beware, this is quite slow.
70 *****************************************************************************/
71 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
73 module_config_t *p_config;
76 p_config = config_FindConfig( p_this, psz_name );
84 switch( p_config->i_type )
86 case CONFIG_ITEM_BOOL:
87 i_type = VLC_VAR_BOOL;
90 case CONFIG_ITEM_INTEGER:
91 i_type = VLC_VAR_INTEGER;
94 case CONFIG_ITEM_FLOAT:
95 i_type = VLC_VAR_FLOAT;
98 case CONFIG_ITEM_MODULE:
99 case CONFIG_ITEM_MODULE_CAT:
100 case CONFIG_ITEM_MODULE_LIST:
101 case CONFIG_ITEM_MODULE_LIST_CAT:
102 i_type = VLC_VAR_MODULE;
105 case CONFIG_ITEM_STRING:
106 i_type = VLC_VAR_STRING;
109 case CONFIG_ITEM_FILE:
110 i_type = VLC_VAR_FILE;
113 case CONFIG_ITEM_DIRECTORY:
114 i_type = VLC_VAR_DIRECTORY;
125 /*****************************************************************************
126 * config_GetInt: get the value of an int variable
127 *****************************************************************************
128 * This function is used to get the value of variables which are internally
129 * represented by an integer (CONFIG_ITEM_INTEGER and
131 *****************************************************************************/
132 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
134 module_config_t *p_config;
136 p_config = config_FindConfig( p_this, psz_name );
141 msg_Err( p_this, "option %s does not exist", psz_name );
144 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
145 (p_config->i_type!=CONFIG_ITEM_KEY) &&
146 (p_config->i_type!=CONFIG_ITEM_BOOL) )
148 msg_Err( p_this, "option %s does not refer to an int", psz_name );
152 return p_config->i_value;
155 /*****************************************************************************
156 * config_GetFloat: get the value of a float variable
157 *****************************************************************************
158 * This function is used to get the value of variables which are internally
159 * represented by a float (CONFIG_ITEM_FLOAT).
160 *****************************************************************************/
161 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
163 module_config_t *p_config;
165 p_config = config_FindConfig( p_this, psz_name );
170 msg_Err( p_this, "option %s does not exist", psz_name );
173 if( p_config->i_type != CONFIG_ITEM_FLOAT )
175 msg_Err( p_this, "option %s does not refer to a float", psz_name );
179 return p_config->f_value;
182 /*****************************************************************************
183 * config_GetPsz: get the string value of a string variable
184 *****************************************************************************
185 * This function is used to get the value of variables which are internally
186 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
187 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
189 * Important note: remember to free() the returned char* because it's a
190 * duplicate of the actual value. It isn't safe to return a pointer to the
191 * actual value as it can be modified at any time.
192 *****************************************************************************/
193 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
195 module_config_t *p_config;
196 char *psz_value = NULL;
198 p_config = config_FindConfig( p_this, psz_name );
203 msg_Err( p_this, "option %s does not exist", psz_name );
206 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
207 (p_config->i_type!=CONFIG_ITEM_FILE) &&
208 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
209 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
210 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
211 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
212 (p_config->i_type!=CONFIG_ITEM_MODULE) )
214 msg_Err( p_this, "option %s does not refer to a string", psz_name );
219 /* return a copy of the string */
220 vlc_mutex_lock( p_config->p_lock );
221 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
222 vlc_mutex_unlock( p_config->p_lock );
227 /*****************************************************************************
228 * config_PutPsz: set the string value of a string variable
229 *****************************************************************************
230 * This function is used to set the value of variables which are internally
231 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
232 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
233 *****************************************************************************/
234 void __config_PutPsz( vlc_object_t *p_this,
235 const char *psz_name, const char *psz_value )
237 module_config_t *p_config;
238 vlc_value_t oldval, val;
240 p_config = config_FindConfig( p_this, psz_name );
246 msg_Warn( p_this, "option %s does not exist", psz_name );
249 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
250 (p_config->i_type!=CONFIG_ITEM_FILE) &&
251 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
252 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
253 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
254 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
255 (p_config->i_type!=CONFIG_ITEM_MODULE) )
257 msg_Err( p_this, "option %s does not refer to a string", psz_name );
261 vlc_mutex_lock( p_config->p_lock );
263 /* backup old value */
264 oldval.psz_string = p_config->psz_value;
266 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
267 else p_config->psz_value = NULL;
269 p_config->b_dirty = VLC_TRUE;
271 val.psz_string = p_config->psz_value;
273 vlc_mutex_unlock( p_config->p_lock );
275 if( p_config->pf_callback )
277 p_config->pf_callback( p_this, psz_name, oldval, val,
278 p_config->p_callback_data );
281 /* free old string */
282 if( oldval.psz_string ) free( oldval.psz_string );
285 /*****************************************************************************
286 * config_PutInt: set the integer value of an int variable
287 *****************************************************************************
288 * This function is used to set the value of variables which are internally
289 * represented by an integer (CONFIG_ITEM_INTEGER and
291 *****************************************************************************/
292 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
294 module_config_t *p_config;
295 vlc_value_t oldval, val;
297 p_config = config_FindConfig( p_this, psz_name );
302 msg_Warn( p_this, "option %s does not exist", psz_name );
305 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
306 (p_config->i_type!=CONFIG_ITEM_KEY) &&
307 (p_config->i_type!=CONFIG_ITEM_BOOL) )
309 msg_Err( p_this, "option %s does not refer to an int", psz_name );
313 /* backup old value */
314 oldval.i_int = p_config->i_value;
316 /* if i_min == i_max == 0, then do not use them */
317 if ((p_config->i_min == 0) && (p_config->i_max == 0))
319 p_config->i_value = i_value;
321 else if (i_value < p_config->i_min)
323 p_config->i_value = p_config->i_min;
325 else if (i_value > p_config->i_max)
327 p_config->i_value = p_config->i_max;
331 p_config->i_value = i_value;
334 p_config->b_dirty = VLC_TRUE;
336 val.i_int = p_config->i_value;
338 if( p_config->pf_callback )
340 p_config->pf_callback( p_this, psz_name, oldval, val,
341 p_config->p_callback_data );
345 /*****************************************************************************
346 * config_PutFloat: set the value of a float variable
347 *****************************************************************************
348 * This function is used to set the value of variables which are internally
349 * represented by a float (CONFIG_ITEM_FLOAT).
350 *****************************************************************************/
351 void __config_PutFloat( vlc_object_t *p_this,
352 const char *psz_name, float f_value )
354 module_config_t *p_config;
355 vlc_value_t oldval, val;
357 p_config = config_FindConfig( p_this, psz_name );
362 msg_Warn( p_this, "option %s does not exist", psz_name );
365 if( p_config->i_type != CONFIG_ITEM_FLOAT )
367 msg_Err( p_this, "option %s does not refer to a float", psz_name );
371 /* backup old value */
372 oldval.f_float = p_config->f_value;
374 /* if f_min == f_max == 0, then do not use them */
375 if ((p_config->f_min == 0) && (p_config->f_max == 0))
377 p_config->f_value = f_value;
379 else if (f_value < p_config->f_min)
381 p_config->f_value = p_config->f_min;
383 else if (f_value > p_config->f_max)
385 p_config->f_value = p_config->f_max;
389 p_config->f_value = f_value;
392 p_config->b_dirty = VLC_TRUE;
394 val.f_float = p_config->f_value;
396 if( p_config->pf_callback )
398 p_config->pf_callback( p_this, psz_name, oldval, val,
399 p_config->p_callback_data );
403 /*****************************************************************************
404 * config_FindConfig: find the config structure associated with an option.
405 *****************************************************************************
406 * FIXME: This function really needs to be optimized.
407 * FIXME: And now even more.
408 *****************************************************************************/
409 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
413 module_config_t *p_item;
416 if( !psz_name ) return NULL;
418 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
420 for( i_index = 0; i_index < p_list->i_count; i_index++ )
422 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
424 if( !p_parser->i_config_items )
427 for( p_item = p_parser->p_config;
428 p_item->i_type != CONFIG_HINT_END;
431 if( p_item->i_type & CONFIG_HINT )
434 if( !strcmp( psz_name, p_item->psz_name ) )
436 vlc_list_release( p_list );
442 vlc_list_release( p_list );
447 /*****************************************************************************
448 * config_FindModule: find a specific module structure.
449 *****************************************************************************/
450 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
453 module_t *p_module, *p_result = NULL;
456 if( !psz_name ) return NULL;
458 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
460 for( i_index = 0; i_index < p_list->i_count; i_index++ )
462 p_module = (module_t *)p_list->p_values[i_index].p_object;
463 if( !strcmp( p_module->psz_object_name, psz_name ) )
470 vlc_list_release( p_list );
475 /*****************************************************************************
476 * config_Duplicate: creates a duplicate of a module's configuration data.
477 *****************************************************************************
478 * Unfortunatly we cannot work directly with the module's config data as
479 * this module might be unloaded from memory at any time (remember HideModule).
480 * This is why we need to create an exact copy of the config data.
481 *****************************************************************************/
482 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
484 int i, j, i_lines = 1;
485 module_config_t *p_item;
487 /* Calculate the structure length */
488 p_module->i_config_items = 0;
489 p_module->i_bool_items = 0;
491 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
495 if( p_item->i_type & CONFIG_ITEM )
497 p_module->i_config_items++;
500 if( p_item->i_type == CONFIG_ITEM_BOOL )
502 p_module->i_bool_items++;
506 /* Allocate memory */
507 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
509 if( p_module->p_config == NULL )
511 msg_Err( p_module, "config error: can't duplicate p_config" );
515 /* Do the duplication job */
516 for( i = 0; i < i_lines ; i++ )
518 p_module->p_config[i] = p_orig[i];
520 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
521 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
522 p_module->p_config[i].i_value_saved = p_orig[i].i_value;
523 p_module->p_config[i].f_value_saved = p_orig[i].f_value;
524 p_module->p_config[i].psz_value_saved = 0;
526 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
527 strdup( p_orig[i].psz_type ) : NULL;
528 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
529 strdup( p_orig[i].psz_name ) : NULL;
530 p_module->p_config[i].psz_current = p_orig[i].psz_current?
531 strdup( p_orig[i].psz_current ) : NULL;
532 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
533 strdup( _(p_orig[i].psz_text) ) : NULL;
534 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
535 strdup( _(p_orig[i].psz_longtext) ) : NULL;
536 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
537 strdup( p_orig[i].psz_value ) : NULL;
538 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
539 strdup( p_orig[i].psz_value ) : NULL;
541 p_module->p_config[i].p_lock = &p_module->object_lock;
543 /* duplicate the string list */
544 if( p_orig[i].i_list )
546 if( p_orig[i].ppsz_list )
548 p_module->p_config[i].ppsz_list =
549 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
550 if( p_module->p_config[i].ppsz_list )
552 for( j = 0; j < p_orig[i].i_list; j++ )
553 p_module->p_config[i].ppsz_list[j] = p_orig[i].ppsz_list[j] ?
554 strdup( p_orig[i].ppsz_list[j] ) : NULL ;
555 p_module->p_config[i].ppsz_list[j] = NULL;
558 if( p_orig[i].ppsz_list_text )
560 p_module->p_config[i].ppsz_list_text =
561 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
562 if( p_module->p_config[i].ppsz_list_text )
564 for( j = 0; j < p_orig[i].i_list; j++ )
565 p_module->p_config[i].ppsz_list_text[j] = _(p_orig[i].ppsz_list_text[j]) ?
566 strdup( _(p_orig[i].ppsz_list_text[j] ) ) : NULL ;
567 p_module->p_config[i].ppsz_list_text[j] = NULL;
570 if( p_orig[i].pi_list )
572 p_module->p_config[i].pi_list =
573 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
574 if( p_module->p_config[i].pi_list )
576 for( j = 0; j < p_orig[i].i_list; j++ )
577 p_module->p_config[i].pi_list[j] =
578 p_orig[i].pi_list[j];
583 /* duplicate the actions list */
584 if( p_orig[i].i_action )
588 p_module->p_config[i].ppf_action =
589 malloc( p_orig[i].i_action * sizeof(void *) );
590 p_module->p_config[i].ppsz_action_text =
591 malloc( p_orig[i].i_action * sizeof(char *) );
593 for( j = 0; j < p_orig[i].i_action; j++ )
595 p_module->p_config[i].ppf_action[j] =
596 p_orig[i].ppf_action[j];
597 p_module->p_config[i].ppsz_action_text[j] =
598 p_orig[i].ppsz_action_text[j] ?
599 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
603 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
607 /*****************************************************************************
608 * config_Free: frees a duplicated module's configuration data.
609 *****************************************************************************
610 * This function frees all the data duplicated by config_Duplicate.
611 *****************************************************************************/
612 void config_Free( module_t *p_module )
614 module_config_t *p_item = p_module->p_config;
622 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
624 if( p_item->psz_type )
625 free( p_item->psz_type );
627 if( p_item->psz_name )
628 free( p_item->psz_name );
630 if( p_item->psz_current )
631 free( p_item->psz_current );
633 if( p_item->psz_text )
634 free( p_item->psz_text );
636 if( p_item->psz_longtext )
637 free( p_item->psz_longtext );
639 if( p_item->psz_value )
640 free( p_item->psz_value );
642 if( p_item->psz_value_orig )
643 free( p_item->psz_value_orig );
645 if( p_item->psz_value_saved )
646 free( p_item->psz_value_saved );
650 for( i = 0; i < p_item->i_list; i++ )
652 if( p_item->ppsz_list && p_item->ppsz_list[i] )
653 free( p_item->ppsz_list[i] );
654 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
655 free( p_item->ppsz_list_text[i] );
657 if( p_item->ppsz_list ) free( p_item->ppsz_list );
658 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
659 if( p_item->pi_list ) free( p_item->pi_list );
662 if( p_item->i_action )
664 for( i = 0; i < p_item->i_action; i++ )
666 if( p_item->ppsz_action_text[i] )
667 free( p_item->ppsz_action_text[i] );
669 if( p_item->ppf_action ) free( p_item->ppf_action );
670 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
674 free( p_module->p_config );
675 p_module->p_config = NULL;
678 /*****************************************************************************
679 * config_SetCallbacks: sets callback functions in the duplicate p_config.
680 *****************************************************************************
681 * Unfortunatly we cannot work directly with the module's config data as
682 * this module might be unloaded from memory at any time (remember HideModule).
683 * This is why we need to duplicate callbacks each time we reload the module.
684 *****************************************************************************/
685 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
687 while( p_new->i_type != CONFIG_HINT_END )
689 p_new->pf_callback = p_orig->pf_callback;
695 /*****************************************************************************
696 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
697 *****************************************************************************
698 * We simply undo what we did in config_SetCallbacks.
699 *****************************************************************************/
700 void config_UnsetCallbacks( module_config_t *p_new )
702 while( p_new->i_type != CONFIG_HINT_END )
704 p_new->pf_callback = NULL;
709 /*****************************************************************************
710 * config_ResetAll: reset the configuration data for all the modules.
711 *****************************************************************************/
712 void __config_ResetAll( vlc_object_t *p_this )
718 /* Acquire config file lock */
719 vlc_mutex_lock( &p_this->p_vlc->config_lock );
721 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
723 for( i_index = 0; i_index < p_list->i_count; i_index++ )
725 p_module = (module_t *)p_list->p_values[i_index].p_object ;
726 if( p_module->b_submodule ) continue;
728 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
730 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
731 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
732 if( p_module->p_config[i].psz_value )
733 free( p_module->p_config[i].psz_value );
734 p_module->p_config[i].psz_value =
735 p_module->p_config[i].psz_value_orig ?
736 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
740 vlc_list_release( p_list );
741 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
744 /*****************************************************************************
745 * config_LoadConfigFile: loads the configuration file.
746 *****************************************************************************
747 * This function is called to load the config options stored in the config
749 *****************************************************************************/
750 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
754 module_config_t *p_item;
757 char *p_index, *psz_option_name, *psz_option_value;
758 char *psz_filename, *psz_homedir, *psz_configfile;
761 setlocale( LC_NUMERIC, "C" );
763 psz_configfile = p_this->p_vlc->psz_configfile;
764 if( !psz_configfile || !psz_configfile )
766 psz_homedir = p_this->p_vlc->psz_homedir;
769 msg_Err( p_this, "psz_homedir is null" );
772 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
773 strlen(psz_homedir) );
775 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
780 psz_filename = strdup( psz_configfile );
785 msg_Err( p_this, "out of memory" );
789 msg_Dbg( p_this, "opening config file %s", psz_filename );
791 /* Acquire config file lock */
792 vlc_mutex_lock( &p_this->p_vlc->config_lock );
794 file = fopen( psz_filename, "rt" );
797 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
798 free( psz_filename );
799 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
803 /* Look for the selected module, if NULL then save everything */
804 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
806 for( i_index = 0; i_index < p_list->i_count; i_index++ )
808 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
811 && strcmp( psz_module_name, p_parser->psz_object_name ) )
816 /* The config file is organized in sections, one per module. Look for
817 * the interesting section ( a section is of the form [foo] ) */
818 fseek( file, 0L, SEEK_SET );
819 while( fgets( line, 1024, file ) )
822 && (p_index = strchr(line,']'))
823 && (p_index - &line[1]
824 == (int)strlen(p_parser->psz_object_name))
825 && !memcmp( &line[1], p_parser->psz_object_name,
826 strlen(p_parser->psz_object_name) ) )
829 msg_Dbg( p_this, "loading config for module \"%s\"",
830 p_parser->psz_object_name );
836 /* either we found the section or we're at the EOF */
838 /* Now try to load the options in this section */
839 while( fgets( line, 1024, file ) )
841 if( line[0] == '[' ) break; /* end of section */
843 /* ignore comments or empty lines */
844 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
847 /* get rid of line feed */
848 if( line[strlen(line)-1] == '\n' )
849 line[strlen(line)-1] = (char)0;
851 /* look for option name */
852 psz_option_name = line;
853 psz_option_value = NULL;
854 p_index = strchr( line, '=' );
855 if( !p_index ) break; /* this ain't an option!!! */
858 psz_option_value = p_index + 1;
860 if( !p_parser->i_config_items )
865 /* try to match this option with one of the module's options */
866 for( p_item = p_parser->p_config;
867 p_item->i_type != CONFIG_HINT_END;
870 if( p_item->i_type & CONFIG_HINT )
874 if( !strcmp( p_item->psz_name, psz_option_name ) )
877 switch( p_item->i_type )
879 case CONFIG_ITEM_BOOL:
880 case CONFIG_ITEM_INTEGER:
881 if( !*psz_option_value )
882 break; /* ignore empty option */
883 p_item->i_value = strtol( psz_option_value, 0, 0 );
884 p_item->i_value_saved = p_item->i_value;
886 msg_Dbg( p_this, "option \"%s\", value %i",
887 p_item->psz_name, p_item->i_value );
891 case CONFIG_ITEM_FLOAT:
892 if( !*psz_option_value )
893 break; /* ignore empty option */
894 p_item->f_value = (float)atof( psz_option_value);
895 p_item->f_value_saved = p_item->f_value;
897 msg_Dbg( p_this, "option \"%s\", value %f",
898 p_item->psz_name, (double)p_item->f_value );
901 case CONFIG_ITEM_KEY:
902 if( !*psz_option_value )
903 break; /* ignore empty option */
904 p_item->i_value = ConfigStringToKey(psz_option_value);
905 p_item->i_value_saved = p_item->i_value;
909 vlc_mutex_lock( p_item->p_lock );
911 /* free old string */
912 if( p_item->psz_value )
913 free( p_item->psz_value );
915 p_item->psz_value = *psz_option_value ?
916 strdup( psz_option_value ) : NULL;
918 if( p_item->psz_value_saved )
919 free( p_item->psz_value_saved );
920 p_item->psz_value_saved = 0;
921 if( !p_item->psz_value || !p_item->psz_value_orig ||
922 (p_item->psz_value && p_item->psz_value_orig &&
923 strcmp(p_item->psz_value,p_item->psz_value_orig)))
924 p_item->psz_value_saved = p_item->psz_value ?
925 strdup( p_item->psz_value ) : 0;
927 vlc_mutex_unlock( p_item->p_lock );
930 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
932 p_item->psz_value ? p_item->psz_value : "" );
942 vlc_list_release( p_list );
945 free( psz_filename );
947 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
952 /*****************************************************************************
953 * config_CreateDir: Create configuration directory if it doesn't exist.
954 *****************************************************************************/
955 int config_CreateDir( vlc_object_t *p_this, char *psz_dirname )
957 if( !psz_dirname && !*psz_dirname ) return -1;
959 #if defined( UNDER_CE )
961 wchar_t psz_new[ MAX_PATH ];
962 char psz_mod[MAX_PATH];
965 /* Convert '/' into '\' */
966 while( *psz_dirname )
968 if( *psz_dirname == '/' ) psz_mod[i] = '\\';
969 else psz_mod[i] = *psz_dirname;
975 MultiByteToWideChar( CP_ACP, 0, psz_mod, -1, psz_new, MAX_PATH );
976 if( CreateDirectory( psz_new, NULL ) )
978 msg_Err( p_this, "could not create %s", psz_mod );
983 # if defined( WIN32 )
984 if( mkdir( psz_dirname ) && errno != EEXIST )
986 if( mkdir( psz_dirname, 0755 ) && errno != EEXIST )
989 msg_Err( p_this, "could not create %s (%s)",
990 psz_dirname, strerror(errno) );
998 /*****************************************************************************
999 * config_SaveConfigFile: Save a module's config options.
1000 *****************************************************************************
1001 * This will save the specified module's config options to the config file.
1002 * If psz_module_name is NULL then we save all the modules config options.
1003 * It's no use to save the config options that kept their default values, so
1004 * we'll try to be a bit clever here.
1006 * When we save we mustn't delete the config options of the modules that
1007 * haven't been loaded. So we cannot just create a new config file with the
1008 * config structures we've got in memory.
1009 * I don't really know how to deal with this nicely, so I will use a completly
1011 * I will load the config file in memory, but skipping all the sections of the
1012 * modules we want to save. Then I will create a brand new file, dump the file
1013 * loaded in memory and then append the sections of the modules we want to
1015 * Really stupid no ?
1016 *****************************************************************************/
1017 int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
1018 vlc_bool_t b_autosave )
1022 module_config_t *p_item;
1024 char p_line[1024], *p_index2;
1026 char *p_bigbuffer, *p_index;
1027 vlc_bool_t b_backup;
1028 char *psz_filename, *psz_homedir, *psz_configfile;
1031 setlocale( LC_NUMERIC, "C" );
1033 /* Acquire config file lock */
1034 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1036 psz_configfile = p_this->p_vlc->psz_configfile;
1037 if( !psz_configfile || !psz_configfile )
1039 psz_homedir = p_this->p_vlc->psz_homedir;
1042 msg_Err( p_this, "psz_homedir is null" );
1043 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1046 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1047 strlen(psz_homedir) );
1050 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1054 msg_Err( p_this, "out of memory" );
1055 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1059 config_CreateDir( p_this, psz_filename );
1061 strcat( psz_filename, "/" CONFIG_FILE );
1065 psz_filename = strdup( psz_configfile );
1068 msg_Err( p_this, "out of memory" );
1069 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1074 msg_Dbg( p_this, "opening config file %s", psz_filename );
1076 file = fopen( psz_filename, "rt" );
1079 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1083 /* look for file size */
1084 fseek( file, 0L, SEEK_END );
1085 i_sizebuf = ftell( file );
1086 fseek( file, 0L, SEEK_SET );
1089 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1092 msg_Err( p_this, "out of memory" );
1093 if( file ) fclose( file );
1094 free( psz_filename );
1095 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1100 /* List all available modules */
1101 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1103 /* backup file into memory, we only need to backup the sections we won't
1106 while( file && fgets( p_line, 1024, file ) )
1108 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1111 /* we found a section, check if we need to do a backup */
1112 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1114 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1116 if( ((p_index2 - &p_line[1])
1117 == (int)strlen(p_parser->psz_object_name) )
1118 && !memcmp( &p_line[1], p_parser->psz_object_name,
1119 strlen(p_parser->psz_object_name) ) )
1121 if( !psz_module_name )
1123 else if( !strcmp( psz_module_name,
1124 p_parser->psz_object_name ) )
1129 if( i_index == p_list->i_count )
1131 /* we don't have this section in our list so we need to back
1135 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1148 /* save line if requested and line is valid (doesn't begin with a
1149 * space, tab, or eol) */
1150 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1151 && (p_line[0] != '\t') )
1153 strcpy( p_index, p_line );
1154 p_index += strlen( p_line );
1157 if( file ) fclose( file );
1161 * Save module config in file
1164 file = fopen( psz_filename, "wt" );
1167 msg_Warn( p_this, "could not open config file %s for writing",
1169 free( psz_filename );
1170 vlc_list_release( p_list );
1171 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1175 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
1177 /* Look for the selected module, if NULL then save everything */
1178 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1180 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1182 if( psz_module_name && strcmp( psz_module_name,
1183 p_parser->psz_object_name ) )
1186 if( !p_parser->i_config_items )
1189 if( psz_module_name )
1190 msg_Dbg( p_this, "saving config for module \"%s\"",
1191 p_parser->psz_object_name );
1193 fprintf( file, "[%s]", p_parser->psz_object_name );
1194 if( p_parser->psz_longname )
1195 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1197 fprintf( file, "\n\n" );
1199 for( p_item = p_parser->p_config;
1200 p_item->i_type != CONFIG_HINT_END;
1204 int i_value = p_item->i_value;
1205 float f_value = p_item->f_value;
1206 char *psz_value = p_item->psz_value;
1208 if( p_item->i_type & CONFIG_HINT )
1211 /* Ignore deprecated options */
1212 if( p_item->psz_current )
1214 if( b_autosave && !p_item->b_autosave )
1216 i_value = p_item->i_value_saved;
1217 f_value = p_item->f_value_saved;
1218 psz_value = p_item->psz_value_saved;
1219 if( !psz_value ) psz_value = p_item->psz_value_orig;
1223 p_item->b_dirty = VLC_FALSE;
1226 switch( p_item->i_type )
1228 case CONFIG_ITEM_BOOL:
1229 case CONFIG_ITEM_INTEGER:
1230 if( p_item->psz_text )
1231 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1232 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1233 _("boolean") : _("integer") );
1234 if( i_value == p_item->i_value_orig )
1235 fprintf( file, "#" );
1236 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1238 p_item->i_value_saved = i_value;
1241 case CONFIG_ITEM_KEY:
1242 if( p_item->psz_text )
1243 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1245 if( i_value == p_item->i_value_orig )
1246 fprintf( file, "#" );
1247 psz_key = ConfigKeyToString( i_value );
1248 fprintf( file, "%s=%s\n", p_item->psz_name,
1249 psz_key ? psz_key : "" );
1250 if ( psz_key ) free( psz_key );
1252 p_item->i_value_saved = i_value;
1255 case CONFIG_ITEM_FLOAT:
1256 if( p_item->psz_text )
1257 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1259 if( f_value == p_item->f_value_orig )
1260 fprintf( file, "#" );
1261 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1263 p_item->f_value_saved = f_value;
1267 if( p_item->psz_text )
1268 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1270 if( (!psz_value && !p_item->psz_value_orig) ||
1271 (psz_value && p_item->psz_value_orig &&
1272 !strcmp( psz_value, p_item->psz_value_orig )) )
1273 fprintf( file, "#" );
1274 fprintf( file, "%s=%s\n", p_item->psz_name,
1275 psz_value ? psz_value : "" );
1277 if( b_autosave && !p_item->b_autosave ) break;
1279 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1280 p_item->psz_value_saved = 0;
1281 if( (psz_value && p_item->psz_value_orig &&
1282 strcmp( psz_value, p_item->psz_value_orig )) ||
1283 !psz_value || !p_item->psz_value_orig)
1284 p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1288 fprintf( file, "\n" );
1291 vlc_list_release( p_list );
1294 * Restore old settings from the config in file
1296 fputs( p_bigbuffer, file );
1297 free( p_bigbuffer );
1300 free( psz_filename );
1301 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1306 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1310 module_config_t *p_item;
1311 int i_index, i_count;
1313 /* Check if there's anything to save */
1314 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1315 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1316 i_count = p_list->i_count;
1317 for( i_index = 0; i_index < i_count; i_index++ )
1319 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1321 if( !p_parser->i_config_items ) continue;
1323 for( p_item = p_parser->p_config;
1324 p_item->i_type != CONFIG_HINT_END;
1327 if( p_item->b_autosave && p_item->b_dirty ) break;
1329 if( p_item->i_type != CONFIG_HINT_END ) break;
1331 vlc_list_release( p_list );
1332 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1334 if( i_index == i_count ) return VLC_SUCCESS;
1335 return SaveConfigFile( p_this, 0, VLC_TRUE );
1338 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1340 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1343 /*****************************************************************************
1344 * config_LoadCmdLine: parse command line
1345 *****************************************************************************
1346 * Parse command line for configuration options.
1347 * Now that the module_bank has been initialized, we can dynamically
1348 * generate the longopts structure used by getops. We have to do it this way
1349 * because we don't know (and don't want to know) in advance the configuration
1350 * options used (ie. exported) by each module.
1351 *****************************************************************************/
1352 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1353 vlc_bool_t b_ignore_errors )
1355 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1358 module_config_t *p_item;
1359 struct option *p_longopts;
1360 int i_modules_index;
1363 module_config_t *pp_shortopts[256];
1364 char *psz_shortopts;
1366 /* Set default configuration and copy arguments */
1367 p_this->p_vlc->i_argc = *pi_argc;
1368 p_this->p_vlc->ppsz_argv = ppsz_argv;
1371 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1372 * is the PSN - process serial number (a unique PID-ish thingie)
1373 * still ok for real Darwin & when run from command line */
1374 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1375 /* for example -psn_0_9306113 */
1377 /* GDMF!... I can't do this or else the MacOSX window server will
1378 * not pick up the PSN and not register the app and we crash...
1379 * hence the following kludge otherwise we'll get confused w/ argv[1]
1380 * being an input file name */
1382 ppsz_argv[ 1 ] = NULL;
1384 *pi_argc = *pi_argc - 1;
1390 /* List all modules */
1391 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1394 * Generate the longopts and shortopts structures used by getopt_long
1398 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1401 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1403 /* count the number of exported configuration options (to allocate
1404 * longopts). We also need to allocate space for too options when
1405 * dealing with boolean to allow for --foo and --no-foo */
1406 i_opts += p_parser->i_config_items
1407 + 2 * p_parser->i_bool_items;
1410 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1411 if( p_longopts == NULL )
1413 msg_Err( p_this, "out of memory" );
1414 vlc_list_release( p_list );
1418 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1419 if( psz_shortopts == NULL )
1421 msg_Err( p_this, "out of memory" );
1423 vlc_list_release( p_list );
1427 /* If we are requested to ignore errors, then we must work on a copy
1428 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1429 * us, ignoring the arity of the options */
1430 if( b_ignore_errors )
1432 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1433 if( ppsz_argv == NULL )
1435 msg_Err( p_this, "out of memory" );
1436 free( psz_shortopts );
1438 vlc_list_release( p_list );
1441 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1442 *pi_argc * sizeof(char *) );
1446 for( i_index = 0; i_index < 256; i_index++ )
1448 pp_shortopts[i_index] = NULL;
1451 /* Fill the p_longopts and psz_shortopts structures */
1453 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1456 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1458 if( !p_parser->i_config_items )
1461 for( p_item = p_parser->p_config;
1462 p_item->i_type != CONFIG_HINT_END;
1466 if( p_item->i_type & CONFIG_HINT )
1469 /* Add item to long options */
1470 p_longopts[i_index].name = strdup( p_item->psz_name );
1471 if( p_longopts[i_index].name == NULL ) continue;
1472 p_longopts[i_index].has_arg =
1473 (p_item->i_type == CONFIG_ITEM_BOOL)?
1474 no_argument : required_argument;
1475 p_longopts[i_index].flag = &flag;
1476 p_longopts[i_index].val = 0;
1479 /* When dealing with bools we also need to add the --no-foo
1481 if( p_item->i_type == CONFIG_ITEM_BOOL )
1483 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1484 if( psz_name == NULL ) continue;
1485 strcpy( psz_name, "no" );
1486 strcat( psz_name, p_item->psz_name );
1488 p_longopts[i_index].name = psz_name;
1489 p_longopts[i_index].has_arg = no_argument;
1490 p_longopts[i_index].flag = &flag;
1491 p_longopts[i_index].val = 1;
1494 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1495 if( psz_name == NULL ) continue;
1496 strcpy( psz_name, "no-" );
1497 strcat( psz_name, p_item->psz_name );
1499 p_longopts[i_index].name = psz_name;
1500 p_longopts[i_index].has_arg = no_argument;
1501 p_longopts[i_index].flag = &flag;
1502 p_longopts[i_index].val = 1;
1506 /* If item also has a short option, add it */
1507 if( p_item->i_short )
1509 pp_shortopts[(int)p_item->i_short] = p_item;
1510 psz_shortopts[i_shortopts] = p_item->i_short;
1512 if( p_item->i_type != CONFIG_ITEM_BOOL )
1514 psz_shortopts[i_shortopts] = ':';
1517 if( p_item->i_short == 'v' )
1519 psz_shortopts[i_shortopts] = ':';
1527 /* We don't need the module list anymore */
1528 vlc_list_release( p_list );
1530 /* Close the longopts and shortopts structures */
1531 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1532 psz_shortopts[i_shortopts] = '\0';
1535 * Parse the command line options
1539 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1540 p_longopts, &i_index ) ) != EOF )
1542 /* A long option has been recognized */
1545 module_config_t *p_conf;
1546 char *psz_name = (char *)p_longopts[i_index].name;
1548 /* Check if we deal with a --nofoo or --no-foo long option */
1549 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1551 /* Store the configuration option */
1552 p_conf = config_FindConfig( p_this, psz_name );
1555 /* Check if the option is deprecated */
1556 if( p_conf->psz_current )
1558 if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1560 if( !b_ignore_errors )
1563 "Warning: option --%s is no longer used.\n",
1568 if( !b_ignore_errors )
1570 if( p_conf->b_strict )
1573 "Error: option --%s is deprecated. "
1574 "Use --%s instead.\n",
1575 p_conf->psz_name, p_conf->psz_current);
1577 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1578 free( (char *)p_longopts[i_index].name );
1581 free( psz_shortopts );
1585 "Warning: option --%s is deprecated. "
1586 "You should use --%s instead.\n",
1587 p_conf->psz_name, p_conf->psz_current);
1589 psz_name=p_conf->psz_current;
1590 p_conf = config_FindConfig( p_this, psz_name );
1593 switch( p_conf->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_LIST:
1600 case CONFIG_ITEM_MODULE_LIST_CAT:
1601 case CONFIG_ITEM_MODULE_CAT:
1602 config_PutPsz( p_this, psz_name, optarg );
1604 case CONFIG_ITEM_INTEGER:
1605 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1607 case CONFIG_ITEM_FLOAT:
1608 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1610 case CONFIG_ITEM_KEY:
1611 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1613 case CONFIG_ITEM_BOOL:
1614 config_PutInt( p_this, psz_name, !flag );
1621 /* A short option has been recognized */
1622 if( pp_shortopts[i_cmd] != NULL )
1624 switch( pp_shortopts[i_cmd]->i_type )
1626 case CONFIG_ITEM_STRING:
1627 case CONFIG_ITEM_FILE:
1628 case CONFIG_ITEM_DIRECTORY:
1629 case CONFIG_ITEM_MODULE:
1630 case CONFIG_ITEM_MODULE_CAT:
1631 case CONFIG_ITEM_MODULE_LIST:
1632 case CONFIG_ITEM_MODULE_LIST_CAT:
1633 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1635 case CONFIG_ITEM_INTEGER:
1640 if( *optarg == 'v' ) /* eg. -vvv */
1643 while( *optarg == 'v' )
1651 i_verbose += atoi( optarg ); /* eg. -v2 */
1656 i_verbose++; /* -v */
1658 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1663 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1664 strtol(optarg, 0, 0) );
1667 case CONFIG_ITEM_BOOL:
1668 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1675 /* Internal error: unknown option */
1676 if( !b_ignore_errors )
1678 fprintf( stderr, "%s: unknown option"
1679 " or missing mandatory argument ",
1680 p_this->p_vlc->psz_object_name );
1683 fprintf( stderr, "`-%c'\n", optopt );
1687 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1689 fprintf( stderr, "Try `%s --help' for more information.\n",
1690 p_this->p_vlc->psz_object_name );
1692 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1693 free( (char *)p_longopts[i_index].name );
1695 free( psz_shortopts );
1700 /* Free allocated resources */
1701 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1702 free( (char *)p_longopts[i_index].name );
1704 free( psz_shortopts );
1705 if( b_ignore_errors ) free( ppsz_argv );
1710 /*****************************************************************************
1711 * config_GetHomeDir: find the user's home directory.
1712 *****************************************************************************
1713 * This function will try by different ways to find the user's home path.
1714 * Note that this function is not reentrant, it should be called only once
1715 * at the beginning of main where the result will be stored for later use.
1716 *****************************************************************************/
1717 char *config_GetHomeDir( void )
1719 char *p_tmp, *p_homedir = NULL;
1721 #if defined(HAVE_GETPWUID)
1722 struct passwd *p_pw = NULL;
1725 #if defined(WIN32) && !defined(UNDER_CE)
1726 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1728 #ifndef CSIDL_FLAG_CREATE
1729 # define CSIDL_FLAG_CREATE 0x8000
1731 #ifndef CSIDL_APPDATA
1732 # define CSIDL_APPDATA 0x1A
1734 #ifndef SHGFP_TYPE_CURRENT
1735 # define SHGFP_TYPE_CURRENT 0
1738 HINSTANCE shfolder_dll;
1739 SHGETFOLDERPATH SHGetFolderPath ;
1741 /* load the shfolder dll to retrieve SHGetFolderPath */
1742 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1744 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1745 _T("SHGetFolderPathA") );
1746 if ( SHGetFolderPath != NULL )
1748 p_homedir = (char *)malloc( MAX_PATH );
1749 if( !p_homedir ) return NULL;
1751 /* get the "Application Data" folder for the current user */
1752 if( S_OK == SHGetFolderPath( NULL,
1753 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1754 NULL, SHGFP_TYPE_CURRENT,
1757 FreeLibrary( shfolder_dll );
1763 FreeLibrary( shfolder_dll );
1766 #elif defined(UNDER_CE)
1768 #ifndef CSIDL_APPDATA
1769 # define CSIDL_APPDATA 0x1A
1772 wchar_t p_whomedir[MAX_PATH];
1774 /* get the "Application Data" folder for the current user */
1775 if( SHGetSpecialFolderPath( NULL, p_whomedir, CSIDL_APPDATA, 1 ) )
1777 p_homedir = (char *)malloc( MAX_PATH );
1778 if( !p_homedir ) return NULL;
1780 sprintf( p_homedir, "%ls", p_whomedir );
1785 #if defined(HAVE_GETPWUID)
1786 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1789 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1791 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1797 p_homedir = strdup( p_tmp );
1799 #if defined(HAVE_GETPWUID)
1802 p_homedir = strdup( p_pw->pw_dir );
1810 static int ConfigStringToKey( char *psz_key )
1814 char *psz_parser = strchr( psz_key, '-' );
1815 while( psz_parser && psz_parser != psz_key )
1817 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1819 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1820 strlen( vlc_modifiers[i].psz_key_string ) ) )
1822 i_key |= vlc_modifiers[i].i_key_code;
1825 psz_key = psz_parser + 1;
1826 psz_parser = strchr( psz_key, '-' );
1828 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1830 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1832 i_key |= vlc_keys[i].i_key_code;
1839 static char *ConfigKeyToString( int i_key )
1841 char *psz_key = malloc( 100 );
1851 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1854 if( i_key & vlc_modifiers[index].i_key_code )
1856 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1859 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1862 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1864 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );