1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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 psz_configfile = p_this->p_vlc->psz_configfile;
762 if( !psz_configfile || !psz_configfile )
764 psz_homedir = p_this->p_vlc->psz_homedir;
767 msg_Err( p_this, "psz_homedir is null" );
770 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
771 strlen(psz_homedir) );
773 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
778 psz_filename = strdup( psz_configfile );
783 msg_Err( p_this, "out of memory" );
787 msg_Dbg( p_this, "opening config file %s", psz_filename );
789 /* Acquire config file lock */
790 vlc_mutex_lock( &p_this->p_vlc->config_lock );
792 file = fopen( psz_filename, "rt" );
795 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
796 free( psz_filename );
797 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
801 /* Look for the selected module, if NULL then save everything */
802 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
804 for( i_index = 0; i_index < p_list->i_count; i_index++ )
806 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
809 && strcmp( psz_module_name, p_parser->psz_object_name ) )
814 /* The config file is organized in sections, one per module. Look for
815 * the interesting section ( a section is of the form [foo] ) */
816 fseek( file, 0L, SEEK_SET );
817 while( fgets( line, 1024, file ) )
820 && (p_index = strchr(line,']'))
821 && (p_index - &line[1]
822 == (int)strlen(p_parser->psz_object_name))
823 && !memcmp( &line[1], p_parser->psz_object_name,
824 strlen(p_parser->psz_object_name) ) )
827 msg_Dbg( p_this, "loading config for module \"%s\"",
828 p_parser->psz_object_name );
834 /* either we found the section or we're at the EOF */
836 /* Now try to load the options in this section */
837 while( fgets( line, 1024, file ) )
839 if( line[0] == '[' ) break; /* end of section */
841 /* ignore comments or empty lines */
842 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
845 /* get rid of line feed */
846 if( line[strlen(line)-1] == '\n' )
847 line[strlen(line)-1] = (char)0;
849 /* look for option name */
850 psz_option_name = line;
851 psz_option_value = NULL;
852 p_index = strchr( line, '=' );
853 if( !p_index ) break; /* this ain't an option!!! */
856 psz_option_value = p_index + 1;
858 if( !p_parser->i_config_items )
863 /* try to match this option with one of the module's options */
864 for( p_item = p_parser->p_config;
865 p_item->i_type != CONFIG_HINT_END;
868 if( p_item->i_type & CONFIG_HINT )
872 if( !strcmp( p_item->psz_name, psz_option_name ) )
875 switch( p_item->i_type )
877 case CONFIG_ITEM_BOOL:
878 case CONFIG_ITEM_INTEGER:
879 if( !*psz_option_value )
880 break; /* ignore empty option */
881 p_item->i_value = strtol( psz_option_value, 0, 0 );
882 p_item->i_value_saved = p_item->i_value;
884 msg_Dbg( p_this, "option \"%s\", value %i",
885 p_item->psz_name, p_item->i_value );
889 case CONFIG_ITEM_FLOAT:
890 if( !*psz_option_value )
891 break; /* ignore empty option */
892 p_item->f_value = (float)atof( psz_option_value);
893 p_item->f_value_saved = p_item->f_value;
895 msg_Dbg( p_this, "option \"%s\", value %f",
896 p_item->psz_name, (double)p_item->f_value );
899 case CONFIG_ITEM_KEY:
900 if( !*psz_option_value )
901 break; /* ignore empty option */
902 p_item->i_value = ConfigStringToKey(psz_option_value);
903 p_item->i_value_saved = p_item->i_value;
907 vlc_mutex_lock( p_item->p_lock );
909 /* free old string */
910 if( p_item->psz_value )
911 free( p_item->psz_value );
913 p_item->psz_value = *psz_option_value ?
914 strdup( psz_option_value ) : NULL;
916 if( p_item->psz_value_saved )
917 free( p_item->psz_value_saved );
918 p_item->psz_value_saved = 0;
919 if( !p_item->psz_value || !p_item->psz_value_orig ||
920 (p_item->psz_value && p_item->psz_value_orig &&
921 strcmp(p_item->psz_value,p_item->psz_value_orig)))
922 p_item->psz_value_saved = p_item->psz_value ?
923 strdup( p_item->psz_value ) : 0;
925 vlc_mutex_unlock( p_item->p_lock );
928 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
930 p_item->psz_value ? p_item->psz_value : "" );
940 vlc_list_release( p_list );
943 free( psz_filename );
945 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
950 /*****************************************************************************
951 * config_CreateDir: Create configuration directory if it doesn't exist.
952 *****************************************************************************/
953 int config_CreateDir( vlc_object_t *p_this, char *psz_dirname )
955 if( !psz_dirname && !*psz_dirname ) return -1;
957 #if defined( UNDER_CE )
959 wchar_t psz_new[ MAX_PATH ];
960 char psz_mod[MAX_PATH];
963 /* Convert '/' into '\' */
964 while( *psz_dirname )
966 if( *psz_dirname == '/' ) psz_mod[i] = '\\';
967 else psz_mod[i] = *psz_dirname;
973 MultiByteToWideChar( CP_ACP, 0, psz_mod, -1, psz_new, MAX_PATH );
974 if( CreateDirectory( psz_new, NULL ) )
976 msg_Err( p_this, "could not create %s", psz_mod );
981 # if defined( WIN32 )
982 if( mkdir( psz_dirname ) && errno != EEXIST )
984 if( mkdir( psz_dirname, 0755 ) && errno != EEXIST )
987 msg_Err( p_this, "could not create %s (%s)",
988 psz_dirname, strerror(errno) );
996 /*****************************************************************************
997 * config_SaveConfigFile: Save a module's config options.
998 *****************************************************************************
999 * This will save the specified module's config options to the config file.
1000 * If psz_module_name is NULL then we save all the modules config options.
1001 * It's no use to save the config options that kept their default values, so
1002 * we'll try to be a bit clever here.
1004 * When we save we mustn't delete the config options of the modules that
1005 * haven't been loaded. So we cannot just create a new config file with the
1006 * config structures we've got in memory.
1007 * I don't really know how to deal with this nicely, so I will use a completly
1009 * I will load the config file in memory, but skipping all the sections of the
1010 * modules we want to save. Then I will create a brand new file, dump the file
1011 * loaded in memory and then append the sections of the modules we want to
1013 * Really stupid no ?
1014 *****************************************************************************/
1015 int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
1016 vlc_bool_t b_autosave )
1020 module_config_t *p_item;
1022 char p_line[1024], *p_index2;
1024 char *p_bigbuffer, *p_index;
1025 vlc_bool_t b_backup;
1026 char *psz_filename, *psz_homedir, *psz_configfile;
1029 /* Acquire config file lock */
1030 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1032 psz_configfile = p_this->p_vlc->psz_configfile;
1033 if( !psz_configfile || !psz_configfile )
1035 psz_homedir = p_this->p_vlc->psz_homedir;
1038 msg_Err( p_this, "psz_homedir is null" );
1039 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1042 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1043 strlen(psz_homedir) );
1046 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1050 msg_Err( p_this, "out of memory" );
1051 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1055 config_CreateDir( p_this, psz_filename );
1057 strcat( psz_filename, "/" CONFIG_FILE );
1061 psz_filename = strdup( psz_configfile );
1064 msg_Err( p_this, "out of memory" );
1065 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1070 msg_Dbg( p_this, "opening config file %s", psz_filename );
1072 file = fopen( psz_filename, "rt" );
1075 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1079 /* look for file size */
1080 fseek( file, 0L, SEEK_END );
1081 i_sizebuf = ftell( file );
1082 fseek( file, 0L, SEEK_SET );
1085 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1088 msg_Err( p_this, "out of memory" );
1089 if( file ) fclose( file );
1090 free( psz_filename );
1091 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1096 /* List all available modules */
1097 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1099 /* backup file into memory, we only need to backup the sections we won't
1102 while( file && fgets( p_line, 1024, file ) )
1104 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1107 /* we found a section, check if we need to do a backup */
1108 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1110 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1112 if( ((p_index2 - &p_line[1])
1113 == (int)strlen(p_parser->psz_object_name) )
1114 && !memcmp( &p_line[1], p_parser->psz_object_name,
1115 strlen(p_parser->psz_object_name) ) )
1117 if( !psz_module_name )
1119 else if( !strcmp( psz_module_name,
1120 p_parser->psz_object_name ) )
1125 if( i_index == p_list->i_count )
1127 /* we don't have this section in our list so we need to back
1131 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1144 /* save line if requested and line is valid (doesn't begin with a
1145 * space, tab, or eol) */
1146 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1147 && (p_line[0] != '\t') )
1149 strcpy( p_index, p_line );
1150 p_index += strlen( p_line );
1153 if( file ) fclose( file );
1157 * Save module config in file
1160 file = fopen( psz_filename, "wt" );
1163 msg_Warn( p_this, "could not open config file %s for writing",
1165 free( psz_filename );
1166 vlc_list_release( p_list );
1167 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1171 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
1173 /* Look for the selected module, if NULL then save everything */
1174 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1176 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1178 if( psz_module_name && strcmp( psz_module_name,
1179 p_parser->psz_object_name ) )
1182 if( !p_parser->i_config_items )
1185 if( psz_module_name )
1186 msg_Dbg( p_this, "saving config for module \"%s\"",
1187 p_parser->psz_object_name );
1189 fprintf( file, "[%s]", p_parser->psz_object_name );
1190 if( p_parser->psz_longname )
1191 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1193 fprintf( file, "\n\n" );
1195 for( p_item = p_parser->p_config;
1196 p_item->i_type != CONFIG_HINT_END;
1200 int i_value = p_item->i_value;
1201 float f_value = p_item->f_value;
1202 char *psz_value = p_item->psz_value;
1204 if( p_item->i_type & CONFIG_HINT )
1207 /* Ignore deprecated options */
1208 if( p_item->psz_current )
1210 if( b_autosave && !p_item->b_autosave )
1212 i_value = p_item->i_value_saved;
1213 f_value = p_item->f_value_saved;
1214 psz_value = p_item->psz_value_saved;
1215 if( !psz_value ) psz_value = p_item->psz_value_orig;
1219 p_item->b_dirty = VLC_FALSE;
1222 switch( p_item->i_type )
1224 case CONFIG_ITEM_BOOL:
1225 case CONFIG_ITEM_INTEGER:
1226 if( p_item->psz_text )
1227 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1228 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1229 _("boolean") : _("integer") );
1230 if( i_value == p_item->i_value_orig )
1231 fprintf( file, "#" );
1232 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1234 p_item->i_value_saved = i_value;
1237 case CONFIG_ITEM_KEY:
1238 if( p_item->psz_text )
1239 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1241 if( i_value == p_item->i_value_orig )
1242 fprintf( file, "#" );
1243 psz_key = ConfigKeyToString( i_value );
1244 fprintf( file, "%s=%s\n", p_item->psz_name,
1245 psz_key ? psz_key : "" );
1246 if ( psz_key ) free( psz_key );
1248 p_item->i_value_saved = i_value;
1251 case CONFIG_ITEM_FLOAT:
1252 if( p_item->psz_text )
1253 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1255 if( f_value == p_item->f_value_orig )
1256 fprintf( file, "#" );
1257 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1259 p_item->f_value_saved = f_value;
1263 if( p_item->psz_text )
1264 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1266 if( (!psz_value && !p_item->psz_value_orig) ||
1267 (psz_value && p_item->psz_value_orig &&
1268 !strcmp( psz_value, p_item->psz_value_orig )) )
1269 fprintf( file, "#" );
1270 fprintf( file, "%s=%s\n", p_item->psz_name,
1271 psz_value ? psz_value : "" );
1273 if( b_autosave && !p_item->b_autosave ) break;
1275 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1276 p_item->psz_value_saved = 0;
1277 if( (psz_value && p_item->psz_value_orig &&
1278 strcmp( psz_value, p_item->psz_value_orig )) ||
1279 !psz_value || !p_item->psz_value_orig)
1280 p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1284 fprintf( file, "\n" );
1287 vlc_list_release( p_list );
1290 * Restore old settings from the config in file
1292 fputs( p_bigbuffer, file );
1293 free( p_bigbuffer );
1296 free( psz_filename );
1297 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1302 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1306 module_config_t *p_item;
1307 int i_index, i_count;
1309 /* Check if there's anything to save */
1310 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1311 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1312 i_count = p_list->i_count;
1313 for( i_index = 0; i_index < i_count; i_index++ )
1315 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1317 if( !p_parser->i_config_items ) continue;
1319 for( p_item = p_parser->p_config;
1320 p_item->i_type != CONFIG_HINT_END;
1323 if( p_item->b_autosave && p_item->b_dirty ) break;
1325 if( p_item->i_type != CONFIG_HINT_END ) break;
1327 vlc_list_release( p_list );
1328 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1330 if( i_index == i_count ) return VLC_SUCCESS;
1331 return SaveConfigFile( p_this, 0, VLC_TRUE );
1334 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1336 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1339 /*****************************************************************************
1340 * config_LoadCmdLine: parse command line
1341 *****************************************************************************
1342 * Parse command line for configuration options.
1343 * Now that the module_bank has been initialized, we can dynamically
1344 * generate the longopts structure used by getops. We have to do it this way
1345 * because we don't know (and don't want to know) in advance the configuration
1346 * options used (ie. exported) by each module.
1347 *****************************************************************************/
1348 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1349 vlc_bool_t b_ignore_errors )
1351 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1354 module_config_t *p_item;
1355 struct option *p_longopts;
1356 int i_modules_index;
1359 module_config_t *pp_shortopts[256];
1360 char *psz_shortopts;
1362 /* Set default configuration and copy arguments */
1363 p_this->p_vlc->i_argc = *pi_argc;
1364 p_this->p_vlc->ppsz_argv = ppsz_argv;
1367 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1368 * is the PSN - process serial number (a unique PID-ish thingie)
1369 * still ok for real Darwin & when run from command line */
1370 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1371 /* for example -psn_0_9306113 */
1373 /* GDMF!... I can't do this or else the MacOSX window server will
1374 * not pick up the PSN and not register the app and we crash...
1375 * hence the following kludge otherwise we'll get confused w/ argv[1]
1376 * being an input file name */
1378 ppsz_argv[ 1 ] = NULL;
1380 *pi_argc = *pi_argc - 1;
1386 /* List all modules */
1387 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1390 * Generate the longopts and shortopts structures used by getopt_long
1394 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1397 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1399 /* count the number of exported configuration options (to allocate
1400 * longopts). We also need to allocate space for too options when
1401 * dealing with boolean to allow for --foo and --no-foo */
1402 i_opts += p_parser->i_config_items
1403 + 2 * p_parser->i_bool_items;
1406 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1407 if( p_longopts == NULL )
1409 msg_Err( p_this, "out of memory" );
1410 vlc_list_release( p_list );
1414 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1415 if( psz_shortopts == NULL )
1417 msg_Err( p_this, "out of memory" );
1419 vlc_list_release( p_list );
1423 /* If we are requested to ignore errors, then we must work on a copy
1424 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1425 * us, ignoring the arity of the options */
1426 if( b_ignore_errors )
1428 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1429 if( ppsz_argv == NULL )
1431 msg_Err( p_this, "out of memory" );
1432 free( psz_shortopts );
1434 vlc_list_release( p_list );
1437 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1438 *pi_argc * sizeof(char *) );
1442 for( i_index = 0; i_index < 256; i_index++ )
1444 pp_shortopts[i_index] = NULL;
1447 /* Fill the p_longopts and psz_shortopts structures */
1449 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1452 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1454 if( !p_parser->i_config_items )
1457 for( p_item = p_parser->p_config;
1458 p_item->i_type != CONFIG_HINT_END;
1462 if( p_item->i_type & CONFIG_HINT )
1465 /* Add item to long options */
1466 p_longopts[i_index].name = strdup( p_item->psz_name );
1467 if( p_longopts[i_index].name == NULL ) continue;
1468 p_longopts[i_index].has_arg =
1469 (p_item->i_type == CONFIG_ITEM_BOOL)?
1470 no_argument : required_argument;
1471 p_longopts[i_index].flag = &flag;
1472 p_longopts[i_index].val = 0;
1475 /* When dealing with bools we also need to add the --no-foo
1477 if( p_item->i_type == CONFIG_ITEM_BOOL )
1479 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1480 if( psz_name == NULL ) continue;
1481 strcpy( psz_name, "no" );
1482 strcat( psz_name, p_item->psz_name );
1484 p_longopts[i_index].name = psz_name;
1485 p_longopts[i_index].has_arg = no_argument;
1486 p_longopts[i_index].flag = &flag;
1487 p_longopts[i_index].val = 1;
1490 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1491 if( psz_name == NULL ) continue;
1492 strcpy( psz_name, "no-" );
1493 strcat( psz_name, p_item->psz_name );
1495 p_longopts[i_index].name = psz_name;
1496 p_longopts[i_index].has_arg = no_argument;
1497 p_longopts[i_index].flag = &flag;
1498 p_longopts[i_index].val = 1;
1502 /* If item also has a short option, add it */
1503 if( p_item->i_short )
1505 pp_shortopts[(int)p_item->i_short] = p_item;
1506 psz_shortopts[i_shortopts] = p_item->i_short;
1508 if( p_item->i_type != CONFIG_ITEM_BOOL )
1510 psz_shortopts[i_shortopts] = ':';
1513 if( p_item->i_short == 'v' )
1515 psz_shortopts[i_shortopts] = ':';
1523 /* We don't need the module list anymore */
1524 vlc_list_release( p_list );
1526 /* Close the longopts and shortopts structures */
1527 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1528 psz_shortopts[i_shortopts] = '\0';
1531 * Parse the command line options
1535 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1536 p_longopts, &i_index ) ) != EOF )
1538 /* A long option has been recognized */
1541 module_config_t *p_conf;
1542 char *psz_name = (char *)p_longopts[i_index].name;
1544 /* Check if we deal with a --nofoo or --no-foo long option */
1545 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1547 /* Store the configuration option */
1548 p_conf = config_FindConfig( p_this, psz_name );
1551 /* Check if the option is derecated */
1552 if( p_conf->psz_current )
1554 if( !b_ignore_errors )
1556 if( p_conf->b_strict )
1559 "Error: option --%s is deprecated. "
1560 "Use --%s instead.\n",
1561 p_conf->psz_name, p_conf->psz_current);
1563 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1564 free( (char *)p_longopts[i_index].name );
1567 free( psz_shortopts );
1571 "Warning: option --%s is deprecated. "
1572 "You should use --%s instead.\n",
1573 p_conf->psz_name, p_conf->psz_current);
1575 psz_name=p_conf->psz_current;
1576 p_conf = config_FindConfig( p_this, psz_name );
1579 switch( p_conf->i_type )
1581 case CONFIG_ITEM_STRING:
1582 case CONFIG_ITEM_FILE:
1583 case CONFIG_ITEM_DIRECTORY:
1584 case CONFIG_ITEM_MODULE:
1585 case CONFIG_ITEM_MODULE_LIST:
1586 case CONFIG_ITEM_MODULE_LIST_CAT:
1587 case CONFIG_ITEM_MODULE_CAT:
1588 config_PutPsz( p_this, psz_name, optarg );
1590 case CONFIG_ITEM_INTEGER:
1591 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1593 case CONFIG_ITEM_FLOAT:
1594 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1596 case CONFIG_ITEM_KEY:
1597 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1599 case CONFIG_ITEM_BOOL:
1600 config_PutInt( p_this, psz_name, !flag );
1607 /* A short option has been recognized */
1608 if( pp_shortopts[i_cmd] != NULL )
1610 switch( pp_shortopts[i_cmd]->i_type )
1612 case CONFIG_ITEM_STRING:
1613 case CONFIG_ITEM_FILE:
1614 case CONFIG_ITEM_DIRECTORY:
1615 case CONFIG_ITEM_MODULE:
1616 case CONFIG_ITEM_MODULE_CAT:
1617 case CONFIG_ITEM_MODULE_LIST:
1618 case CONFIG_ITEM_MODULE_LIST_CAT:
1619 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1621 case CONFIG_ITEM_INTEGER:
1626 if( *optarg == 'v' ) /* eg. -vvv */
1629 while( *optarg == 'v' )
1637 i_verbose += atoi( optarg ); /* eg. -v2 */
1642 i_verbose++; /* -v */
1644 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1649 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1650 strtol(optarg, 0, 0) );
1653 case CONFIG_ITEM_BOOL:
1654 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1661 /* Internal error: unknown option */
1662 if( !b_ignore_errors )
1664 fprintf( stderr, "%s: unknown option ",
1665 p_this->p_vlc->psz_object_name );
1668 fprintf( stderr, "`-%c'\n", optopt );
1672 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1674 fprintf( stderr, "Try `%s --help' for more information.\n",
1675 p_this->p_vlc->psz_object_name );
1677 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1678 free( (char *)p_longopts[i_index].name );
1680 free( psz_shortopts );
1685 /* Free allocated resources */
1686 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1687 free( (char *)p_longopts[i_index].name );
1689 free( psz_shortopts );
1690 if( b_ignore_errors ) free( ppsz_argv );
1695 /*****************************************************************************
1696 * config_GetHomeDir: find the user's home directory.
1697 *****************************************************************************
1698 * This function will try by different ways to find the user's home path.
1699 * Note that this function is not reentrant, it should be called only once
1700 * at the beginning of main where the result will be stored for later use.
1701 *****************************************************************************/
1702 char *config_GetHomeDir( void )
1704 char *p_tmp, *p_homedir = NULL;
1706 #if defined(HAVE_GETPWUID)
1707 struct passwd *p_pw = NULL;
1710 #if defined(WIN32) && !defined(UNDER_CE)
1711 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1713 #ifndef CSIDL_FLAG_CREATE
1714 # define CSIDL_FLAG_CREATE 0x8000
1716 #ifndef CSIDL_APPDATA
1717 # define CSIDL_APPDATA 0x1A
1719 #ifndef SHGFP_TYPE_CURRENT
1720 # define SHGFP_TYPE_CURRENT 0
1723 HINSTANCE shfolder_dll;
1724 SHGETFOLDERPATH SHGetFolderPath ;
1726 /* load the shfolder dll to retrieve SHGetFolderPath */
1727 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1729 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1730 _T("SHGetFolderPathA") );
1731 if ( SHGetFolderPath != NULL )
1733 p_homedir = (char *)malloc( MAX_PATH );
1734 if( !p_homedir ) return NULL;
1736 /* get the "Application Data" folder for the current user */
1737 if( S_OK == SHGetFolderPath( NULL,
1738 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1739 NULL, SHGFP_TYPE_CURRENT,
1742 FreeLibrary( shfolder_dll );
1748 FreeLibrary( shfolder_dll );
1751 #elif defined(UNDER_CE)
1753 #ifndef CSIDL_APPDATA
1754 # define CSIDL_APPDATA 0x1A
1757 wchar_t p_whomedir[MAX_PATH];
1759 /* get the "Application Data" folder for the current user */
1760 if( SHGetSpecialFolderPath( NULL, p_whomedir, CSIDL_APPDATA, 1 ) )
1762 p_homedir = (char *)malloc( MAX_PATH );
1763 if( !p_homedir ) return NULL;
1765 sprintf( p_homedir, "%ls", p_whomedir );
1770 #if defined(HAVE_GETPWUID)
1771 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1774 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1776 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1782 p_homedir = strdup( p_tmp );
1784 #if defined(HAVE_GETPWUID)
1787 p_homedir = strdup( p_pw->pw_dir );
1795 static int ConfigStringToKey( char *psz_key )
1799 char *psz_parser = strchr( psz_key, '-' );
1800 while( psz_parser && psz_parser != psz_key )
1802 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1804 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1805 strlen( vlc_modifiers[i].psz_key_string ) ) )
1807 i_key |= vlc_modifiers[i].i_key_code;
1810 psz_key = psz_parser + 1;
1811 psz_parser = strchr( psz_key, '-' );
1813 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1815 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1817 i_key |= vlc_keys[i].i_key_code;
1824 static char *ConfigKeyToString( int i_key )
1826 char *psz_key = malloc( 100 );
1836 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1839 if( i_key & vlc_modifiers[index].i_key_code )
1841 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1844 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1847 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1849 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );