1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <stdio.h> /* sprintf() */
29 #include <stdlib.h> /* free(), strtol() */
30 #include <string.h> /* strdup() */
31 #include <errno.h> /* errno */
38 # include <unistd.h> /* getuid() */
41 #ifdef HAVE_GETOPT_LONG
43 # include <getopt.h> /* getopt() */
46 # include "../extras/getopt.h"
49 #if defined(HAVE_GETPWUID)
50 # include <pwd.h> /* getpwuid() */
53 #if defined( HAVE_SYS_STAT_H )
54 # include <sys/stat.h>
56 #if defined( HAVE_SYS_TYPES_H )
57 # include <sys/types.h>
60 # if !defined( UNDER_CE )
67 static int ConfigStringToKey( char * );
68 static char *ConfigKeyToString( int );
70 /*****************************************************************************
71 * config_GetType: get the type of a variable (bool, int, float, string)
72 *****************************************************************************
73 * This function is used to get the type of a variable from its name.
74 * Beware, this is quite slow.
75 *****************************************************************************/
76 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
78 module_config_t *p_config;
81 p_config = config_FindConfig( p_this, psz_name );
89 switch( p_config->i_type )
91 case CONFIG_ITEM_BOOL:
92 i_type = VLC_VAR_BOOL;
95 case CONFIG_ITEM_INTEGER:
96 i_type = VLC_VAR_INTEGER;
99 case CONFIG_ITEM_FLOAT:
100 i_type = VLC_VAR_FLOAT;
103 case CONFIG_ITEM_MODULE:
104 case CONFIG_ITEM_MODULE_CAT:
105 case CONFIG_ITEM_MODULE_LIST:
106 case CONFIG_ITEM_MODULE_LIST_CAT:
107 i_type = VLC_VAR_MODULE;
110 case CONFIG_ITEM_STRING:
111 i_type = VLC_VAR_STRING;
114 case CONFIG_ITEM_FILE:
115 i_type = VLC_VAR_FILE;
118 case CONFIG_ITEM_DIRECTORY:
119 i_type = VLC_VAR_DIRECTORY;
130 /*****************************************************************************
131 * config_GetInt: get the value of an int variable
132 *****************************************************************************
133 * This function is used to get the value of variables which are internally
134 * represented by an integer (CONFIG_ITEM_INTEGER and
136 *****************************************************************************/
137 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
139 module_config_t *p_config;
141 p_config = config_FindConfig( p_this, psz_name );
146 msg_Err( p_this, "option %s does not exist", psz_name );
149 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
150 (p_config->i_type!=CONFIG_ITEM_KEY) &&
151 (p_config->i_type!=CONFIG_ITEM_BOOL) )
153 msg_Err( p_this, "option %s does not refer to an int", psz_name );
157 return p_config->i_value;
160 /*****************************************************************************
161 * config_GetFloat: get the value of a float variable
162 *****************************************************************************
163 * This function is used to get the value of variables which are internally
164 * represented by a float (CONFIG_ITEM_FLOAT).
165 *****************************************************************************/
166 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
168 module_config_t *p_config;
170 p_config = config_FindConfig( p_this, psz_name );
175 msg_Err( p_this, "option %s does not exist", psz_name );
178 if( p_config->i_type != CONFIG_ITEM_FLOAT )
180 msg_Err( p_this, "option %s does not refer to a float", psz_name );
184 return p_config->f_value;
187 /*****************************************************************************
188 * config_GetPsz: get the string value of a string variable
189 *****************************************************************************
190 * This function is used to get the value of variables which are internally
191 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
192 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
194 * Important note: remember to free() the returned char* because it's a
195 * duplicate of the actual value. It isn't safe to return a pointer to the
196 * actual value as it can be modified at any time.
197 *****************************************************************************/
198 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
200 module_config_t *p_config;
201 char *psz_value = NULL;
203 p_config = config_FindConfig( p_this, psz_name );
208 msg_Err( p_this, "option %s does not exist", psz_name );
211 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
212 (p_config->i_type!=CONFIG_ITEM_FILE) &&
213 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
214 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
215 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
216 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
217 (p_config->i_type!=CONFIG_ITEM_MODULE) )
219 msg_Err( p_this, "option %s does not refer to a string", psz_name );
224 /* return a copy of the string */
225 vlc_mutex_lock( p_config->p_lock );
226 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
227 vlc_mutex_unlock( p_config->p_lock );
232 /*****************************************************************************
233 * config_PutPsz: set the string value of a string variable
234 *****************************************************************************
235 * This function is used to set the value of variables which are internally
236 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
237 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
238 *****************************************************************************/
239 void __config_PutPsz( vlc_object_t *p_this,
240 const char *psz_name, const char *psz_value )
242 module_config_t *p_config;
243 vlc_value_t oldval, val;
245 p_config = config_FindConfig( p_this, psz_name );
251 msg_Warn( p_this, "option %s does not exist", psz_name );
254 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
255 (p_config->i_type!=CONFIG_ITEM_FILE) &&
256 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
257 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST) &&
258 (p_config->i_type!=CONFIG_ITEM_MODULE_CAT) &&
259 (p_config->i_type!=CONFIG_ITEM_MODULE_LIST_CAT) &&
260 (p_config->i_type!=CONFIG_ITEM_MODULE) )
262 msg_Err( p_this, "option %s does not refer to a string", psz_name );
266 vlc_mutex_lock( p_config->p_lock );
268 /* backup old value */
269 oldval.psz_string = p_config->psz_value;
271 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
272 else p_config->psz_value = NULL;
274 p_config->b_dirty = VLC_TRUE;
276 val.psz_string = p_config->psz_value;
278 vlc_mutex_unlock( p_config->p_lock );
280 if( p_config->pf_callback )
282 p_config->pf_callback( p_this, psz_name, oldval, val,
283 p_config->p_callback_data );
286 /* free old string */
287 if( oldval.psz_string ) free( oldval.psz_string );
290 /*****************************************************************************
291 * config_PutInt: set the integer value of an int variable
292 *****************************************************************************
293 * This function is used to set the value of variables which are internally
294 * represented by an integer (CONFIG_ITEM_INTEGER and
296 *****************************************************************************/
297 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
299 module_config_t *p_config;
300 vlc_value_t oldval, val;
302 p_config = config_FindConfig( p_this, psz_name );
307 msg_Warn( p_this, "option %s does not exist", psz_name );
310 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
311 (p_config->i_type!=CONFIG_ITEM_KEY) &&
312 (p_config->i_type!=CONFIG_ITEM_BOOL) )
314 msg_Err( p_this, "option %s does not refer to an int", psz_name );
318 /* backup old value */
319 oldval.i_int = p_config->i_value;
321 /* if i_min == i_max == 0, then do not use them */
322 if ((p_config->i_min == 0) && (p_config->i_max == 0))
324 p_config->i_value = i_value;
326 else if (i_value < p_config->i_min)
328 p_config->i_value = p_config->i_min;
330 else if (i_value > p_config->i_max)
332 p_config->i_value = p_config->i_max;
336 p_config->i_value = i_value;
339 p_config->b_dirty = VLC_TRUE;
341 val.i_int = p_config->i_value;
343 if( p_config->pf_callback )
345 p_config->pf_callback( p_this, psz_name, oldval, val,
346 p_config->p_callback_data );
350 /*****************************************************************************
351 * config_PutFloat: set the value of a float variable
352 *****************************************************************************
353 * This function is used to set the value of variables which are internally
354 * represented by a float (CONFIG_ITEM_FLOAT).
355 *****************************************************************************/
356 void __config_PutFloat( vlc_object_t *p_this,
357 const char *psz_name, float f_value )
359 module_config_t *p_config;
360 vlc_value_t oldval, val;
362 p_config = config_FindConfig( p_this, psz_name );
367 msg_Warn( p_this, "option %s does not exist", psz_name );
370 if( p_config->i_type != CONFIG_ITEM_FLOAT )
372 msg_Err( p_this, "option %s does not refer to a float", psz_name );
376 /* backup old value */
377 oldval.f_float = p_config->f_value;
379 /* if f_min == f_max == 0, then do not use them */
380 if ((p_config->f_min == 0) && (p_config->f_max == 0))
382 p_config->f_value = f_value;
384 else if (f_value < p_config->f_min)
386 p_config->f_value = p_config->f_min;
388 else if (f_value > p_config->f_max)
390 p_config->f_value = p_config->f_max;
394 p_config->f_value = f_value;
397 p_config->b_dirty = VLC_TRUE;
399 val.f_float = p_config->f_value;
401 if( p_config->pf_callback )
403 p_config->pf_callback( p_this, psz_name, oldval, val,
404 p_config->p_callback_data );
408 /*****************************************************************************
409 * config_FindConfig: find the config structure associated with an option.
410 *****************************************************************************
411 * FIXME: This function really needs to be optimized.
412 * FIXME: And now even more.
413 *****************************************************************************/
414 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
418 module_config_t *p_item;
421 if( !psz_name ) return NULL;
423 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
425 for( i_index = 0; i_index < p_list->i_count; i_index++ )
427 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
429 if( !p_parser->i_config_items )
432 for( p_item = p_parser->p_config;
433 p_item->i_type != CONFIG_HINT_END;
436 if( p_item->i_type & CONFIG_HINT )
439 if( !strcmp( psz_name, p_item->psz_name ) )
441 vlc_list_release( p_list );
447 vlc_list_release( p_list );
452 /*****************************************************************************
453 * config_FindModule: find a specific module structure.
454 *****************************************************************************/
455 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
458 module_t *p_module, *p_result = NULL;
461 if( !psz_name ) return NULL;
463 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
465 for( i_index = 0; i_index < p_list->i_count; i_index++ )
467 p_module = (module_t *)p_list->p_values[i_index].p_object;
468 if( !strcmp( p_module->psz_object_name, psz_name ) )
475 vlc_list_release( p_list );
480 /*****************************************************************************
481 * config_Duplicate: creates a duplicate of a module's configuration data.
482 *****************************************************************************
483 * Unfortunatly we cannot work directly with the module's config data as
484 * this module might be unloaded from memory at any time (remember HideModule).
485 * This is why we need to create an exact copy of the config data.
486 *****************************************************************************/
487 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
489 int i, j, i_lines = 1;
490 module_config_t *p_item;
492 /* Calculate the structure length */
493 p_module->i_config_items = 0;
494 p_module->i_bool_items = 0;
496 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
500 if( p_item->i_type & CONFIG_ITEM )
502 p_module->i_config_items++;
505 if( p_item->i_type == CONFIG_ITEM_BOOL )
507 p_module->i_bool_items++;
511 /* Allocate memory */
512 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
514 if( p_module->p_config == NULL )
516 msg_Err( p_module, "config error: can't duplicate p_config" );
520 /* Do the duplication job */
521 for( i = 0; i < i_lines ; i++ )
523 p_module->p_config[i] = p_orig[i];
525 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
526 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
527 p_module->p_config[i].i_value_saved = p_orig[i].i_value;
528 p_module->p_config[i].f_value_saved = p_orig[i].f_value;
529 p_module->p_config[i].psz_value_saved = 0;
531 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
532 strdup( p_orig[i].psz_type ) : NULL;
533 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
534 strdup( p_orig[i].psz_name ) : NULL;
535 p_module->p_config[i].psz_current = p_orig[i].psz_current?
536 strdup( p_orig[i].psz_current ) : NULL;
537 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
538 strdup( _(p_orig[i].psz_text) ) : NULL;
539 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
540 strdup( _(p_orig[i].psz_longtext) ) : NULL;
541 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
542 strdup( p_orig[i].psz_value ) : NULL;
543 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
544 strdup( p_orig[i].psz_value ) : NULL;
546 p_module->p_config[i].p_lock = &p_module->object_lock;
548 /* duplicate the string list */
549 if( p_orig[i].i_list )
551 if( p_orig[i].ppsz_list )
553 p_module->p_config[i].ppsz_list =
554 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
555 if( p_module->p_config[i].ppsz_list )
557 for( j = 0; j < p_orig[i].i_list; j++ )
558 p_module->p_config[i].ppsz_list[j] = p_orig[i].ppsz_list[j] ?
559 strdup( p_orig[i].ppsz_list[j] ) : NULL ;
560 p_module->p_config[i].ppsz_list[j] = NULL;
563 if( p_orig[i].ppsz_list_text )
565 p_module->p_config[i].ppsz_list_text =
566 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
567 if( p_module->p_config[i].ppsz_list_text )
569 for( j = 0; j < p_orig[i].i_list; j++ )
570 p_module->p_config[i].ppsz_list_text[j] = _(p_orig[i].ppsz_list_text[j]) ?
571 strdup( _(p_orig[i].ppsz_list_text[j] ) ) : NULL ;
572 p_module->p_config[i].ppsz_list_text[j] = NULL;
575 if( p_orig[i].pi_list )
577 p_module->p_config[i].pi_list =
578 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
579 if( p_module->p_config[i].pi_list )
581 for( j = 0; j < p_orig[i].i_list; j++ )
582 p_module->p_config[i].pi_list[j] =
583 p_orig[i].pi_list[j];
588 /* duplicate the actions list */
589 if( p_orig[i].i_action )
593 p_module->p_config[i].ppf_action =
594 malloc( p_orig[i].i_action * sizeof(void *) );
595 p_module->p_config[i].ppsz_action_text =
596 malloc( p_orig[i].i_action * sizeof(char *) );
598 for( j = 0; j < p_orig[i].i_action; j++ )
600 p_module->p_config[i].ppf_action[j] =
601 p_orig[i].ppf_action[j];
602 p_module->p_config[i].ppsz_action_text[j] =
603 p_orig[i].ppsz_action_text[j] ?
604 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
608 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
612 /*****************************************************************************
613 * config_Free: frees a duplicated module's configuration data.
614 *****************************************************************************
615 * This function frees all the data duplicated by config_Duplicate.
616 *****************************************************************************/
617 void config_Free( module_t *p_module )
619 module_config_t *p_item = p_module->p_config;
627 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
629 if( p_item->psz_type )
630 free( p_item->psz_type );
632 if( p_item->psz_name )
633 free( p_item->psz_name );
635 if( p_item->psz_current )
636 free( p_item->psz_current );
638 if( p_item->psz_text )
639 free( p_item->psz_text );
641 if( p_item->psz_longtext )
642 free( p_item->psz_longtext );
644 if( p_item->psz_value )
645 free( p_item->psz_value );
647 if( p_item->psz_value_orig )
648 free( p_item->psz_value_orig );
650 if( p_item->psz_value_saved )
651 free( p_item->psz_value_saved );
655 for( i = 0; i < p_item->i_list; i++ )
657 if( p_item->ppsz_list && p_item->ppsz_list[i] )
658 free( p_item->ppsz_list[i] );
659 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
660 free( p_item->ppsz_list_text[i] );
662 if( p_item->ppsz_list ) free( p_item->ppsz_list );
663 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
664 if( p_item->pi_list ) free( p_item->pi_list );
667 if( p_item->i_action )
669 for( i = 0; i < p_item->i_action; i++ )
671 if( p_item->ppsz_action_text[i] )
672 free( p_item->ppsz_action_text[i] );
674 if( p_item->ppf_action ) free( p_item->ppf_action );
675 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
679 free( p_module->p_config );
680 p_module->p_config = NULL;
683 /*****************************************************************************
684 * config_SetCallbacks: sets callback functions in the duplicate p_config.
685 *****************************************************************************
686 * Unfortunatly we cannot work directly with the module's config data as
687 * this module might be unloaded from memory at any time (remember HideModule).
688 * This is why we need to duplicate callbacks each time we reload the module.
689 *****************************************************************************/
690 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
692 while( p_new->i_type != CONFIG_HINT_END )
694 p_new->pf_callback = p_orig->pf_callback;
700 /*****************************************************************************
701 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
702 *****************************************************************************
703 * We simply undo what we did in config_SetCallbacks.
704 *****************************************************************************/
705 void config_UnsetCallbacks( module_config_t *p_new )
707 while( p_new->i_type != CONFIG_HINT_END )
709 p_new->pf_callback = NULL;
714 /*****************************************************************************
715 * config_ResetAll: reset the configuration data for all the modules.
716 *****************************************************************************/
717 void __config_ResetAll( vlc_object_t *p_this )
723 /* Acquire config file lock */
724 vlc_mutex_lock( &p_this->p_vlc->config_lock );
726 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
728 for( i_index = 0; i_index < p_list->i_count; i_index++ )
730 p_module = (module_t *)p_list->p_values[i_index].p_object ;
731 if( p_module->b_submodule ) continue;
733 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
735 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
736 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
737 if( p_module->p_config[i].psz_value )
738 free( p_module->p_config[i].psz_value );
739 p_module->p_config[i].psz_value =
740 p_module->p_config[i].psz_value_orig ?
741 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
745 vlc_list_release( p_list );
746 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
749 /*****************************************************************************
750 * config_LoadConfigFile: loads the configuration file.
751 *****************************************************************************
752 * This function is called to load the config options stored in the config
754 *****************************************************************************/
755 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
759 module_config_t *p_item;
762 char *p_index, *psz_option_name, *psz_option_value;
763 char *psz_filename, *psz_homedir, *psz_configfile;
766 psz_configfile = p_this->p_vlc->psz_configfile;
767 if( !psz_configfile || !psz_configfile )
769 psz_homedir = p_this->p_vlc->psz_homedir;
772 msg_Err( p_this, "psz_homedir is null" );
775 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
776 strlen(psz_homedir) );
778 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
783 psz_filename = strdup( psz_configfile );
788 msg_Err( p_this, "out of memory" );
792 msg_Dbg( p_this, "opening config file %s", psz_filename );
794 /* Acquire config file lock */
795 vlc_mutex_lock( &p_this->p_vlc->config_lock );
797 file = utf8_fopen( psz_filename, "rt" );
800 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
801 free( psz_filename );
802 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
806 /* Look for the selected module, if NULL then save everything */
807 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
809 for( i_index = 0; i_index < p_list->i_count; i_index++ )
811 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
814 && strcmp( psz_module_name, p_parser->psz_object_name ) )
819 /* The config file is organized in sections, one per module. Look for
820 * the interesting section ( a section is of the form [foo] ) */
821 fseek( file, 0L, SEEK_SET );
822 while( fgets( line, 1024, file ) )
825 && (p_index = strchr(line,']'))
826 && (p_index - &line[1]
827 == (int)strlen(p_parser->psz_object_name))
828 && !memcmp( &line[1], p_parser->psz_object_name,
829 strlen(p_parser->psz_object_name) ) )
832 msg_Dbg( p_this, "loading config for module \"%s\"",
833 p_parser->psz_object_name );
839 /* either we found the section or we're at the EOF */
841 /* Now try to load the options in this section */
842 while( fgets( line, 1024, file ) )
844 if( line[0] == '[' ) break; /* end of section */
846 /* ignore comments or empty lines */
847 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
850 /* get rid of line feed */
851 if( line[strlen(line)-1] == '\n' )
852 line[strlen(line)-1] = (char)0;
854 /* look for option name */
855 psz_option_name = line;
856 psz_option_value = NULL;
857 p_index = strchr( line, '=' );
858 if( !p_index ) break; /* this ain't an option!!! */
861 psz_option_value = p_index + 1;
863 if( !p_parser->i_config_items )
868 /* try to match this option with one of the module's options */
869 for( p_item = p_parser->p_config;
870 p_item->i_type != CONFIG_HINT_END;
873 if( p_item->i_type & CONFIG_HINT )
877 if( !strcmp( p_item->psz_name, psz_option_name ) )
880 switch( p_item->i_type )
882 case CONFIG_ITEM_BOOL:
883 case CONFIG_ITEM_INTEGER:
884 if( !*psz_option_value )
885 break; /* ignore empty option */
886 p_item->i_value = strtol( psz_option_value, 0, 0 );
887 p_item->i_value_saved = p_item->i_value;
889 msg_Dbg( p_this, "option \"%s\", value %i",
890 p_item->psz_name, p_item->i_value );
894 case CONFIG_ITEM_FLOAT:
895 if( !*psz_option_value )
896 break; /* ignore empty option */
897 p_item->f_value = (float)i18n_atof( psz_option_value);
898 p_item->f_value_saved = p_item->f_value;
900 msg_Dbg( p_this, "option \"%s\", value %f",
901 p_item->psz_name, (double)p_item->f_value );
904 case CONFIG_ITEM_KEY:
905 if( !*psz_option_value )
906 break; /* ignore empty option */
907 p_item->i_value = ConfigStringToKey(psz_option_value);
908 p_item->i_value_saved = p_item->i_value;
912 vlc_mutex_lock( p_item->p_lock );
914 /* free old string */
915 if( p_item->psz_value )
916 free( p_item->psz_value );
918 p_item->psz_value = *psz_option_value ?
919 strdup( psz_option_value ) : NULL;
921 if( p_item->psz_value_saved )
922 free( p_item->psz_value_saved );
923 p_item->psz_value_saved = 0;
924 if( !p_item->psz_value || !p_item->psz_value_orig ||
925 (p_item->psz_value && p_item->psz_value_orig &&
926 strcmp(p_item->psz_value,p_item->psz_value_orig)))
927 p_item->psz_value_saved = p_item->psz_value ?
928 strdup( p_item->psz_value ) : 0;
930 vlc_mutex_unlock( p_item->p_lock );
933 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
935 p_item->psz_value ? p_item->psz_value : "" );
945 vlc_list_release( p_list );
948 free( psz_filename );
950 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
955 /*****************************************************************************
956 * config_CreateDir: Create configuration directory if it doesn't exist.
957 *****************************************************************************/
958 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
960 if( !psz_dirname && !*psz_dirname ) return -1;
962 if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) )
964 msg_Err( p_this, "could not create %s (%s)",
965 psz_dirname, strerror(errno) );
972 /*****************************************************************************
973 * config_SaveConfigFile: Save a module's config options.
974 *****************************************************************************
975 * This will save the specified module's config options to the config file.
976 * If psz_module_name is NULL then we save all the modules config options.
977 * It's no use to save the config options that kept their default values, so
978 * we'll try to be a bit clever here.
980 * When we save we mustn't delete the config options of the modules that
981 * haven't been loaded. So we cannot just create a new config file with the
982 * config structures we've got in memory.
983 * I don't really know how to deal with this nicely, so I will use a completly
985 * I will load the config file in memory, but skipping all the sections of the
986 * modules we want to save. Then I will create a brand new file, dump the file
987 * loaded in memory and then append the sections of the modules we want to
990 *****************************************************************************/
991 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
992 vlc_bool_t b_autosave )
996 module_config_t *p_item;
998 char p_line[1024], *p_index2;
1000 char *p_bigbuffer, *p_index;
1001 vlc_bool_t b_backup;
1002 char *psz_filename, *psz_homedir, *psz_configfile;
1005 /* Acquire config file lock */
1006 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1008 psz_configfile = p_this->p_vlc->psz_configfile;
1009 if( !psz_configfile || !psz_configfile )
1011 psz_homedir = p_this->p_vlc->psz_homedir;
1014 msg_Err( p_this, "psz_homedir is null" );
1015 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1018 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1019 strlen(psz_homedir) );
1022 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1026 msg_Err( p_this, "out of memory" );
1027 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1031 config_CreateDir( p_this, psz_filename );
1033 strcat( psz_filename, "/" CONFIG_FILE );
1037 psz_filename = strdup( psz_configfile );
1040 msg_Err( p_this, "out of memory" );
1041 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1046 msg_Dbg( p_this, "opening config file %s", psz_filename );
1048 file = utf8_fopen( psz_filename, "rt" );
1051 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1055 /* look for file size */
1056 fseek( file, 0L, SEEK_END );
1057 i_sizebuf = ftell( file );
1058 fseek( file, 0L, SEEK_SET );
1061 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1064 msg_Err( p_this, "out of memory" );
1065 if( file ) fclose( file );
1066 free( psz_filename );
1067 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1072 /* List all available modules */
1073 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1075 /* backup file into memory, we only need to backup the sections we won't
1078 while( file && fgets( p_line, 1024, file ) )
1080 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1083 /* we found a section, check if we need to do a backup */
1084 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1086 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1088 if( ((p_index2 - &p_line[1])
1089 == (int)strlen(p_parser->psz_object_name) )
1090 && !memcmp( &p_line[1], p_parser->psz_object_name,
1091 strlen(p_parser->psz_object_name) ) )
1093 if( !psz_module_name )
1095 else if( !strcmp( psz_module_name,
1096 p_parser->psz_object_name ) )
1101 if( i_index == p_list->i_count )
1103 /* we don't have this section in our list so we need to back
1107 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1120 /* save line if requested and line is valid (doesn't begin with a
1121 * space, tab, or eol) */
1122 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1123 && (p_line[0] != '\t') )
1125 strcpy( p_index, p_line );
1126 p_index += strlen( p_line );
1129 if( file ) fclose( file );
1133 * Save module config in file
1136 file = utf8_fopen( psz_filename, "wt" );
1139 msg_Warn( p_this, "could not open config file %s for writing",
1141 free( psz_filename );
1142 vlc_list_release( p_list );
1143 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1147 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
1148 "###\n### lines begining with a '#' character are comments\n###\n\n" );
1150 /* Look for the selected module, if NULL then save everything */
1151 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1153 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1155 if( psz_module_name && strcmp( psz_module_name,
1156 p_parser->psz_object_name ) )
1159 if( !p_parser->i_config_items )
1162 if( psz_module_name )
1163 msg_Dbg( p_this, "saving config for module \"%s\"",
1164 p_parser->psz_object_name );
1166 fprintf( file, "[%s]", p_parser->psz_object_name );
1167 if( p_parser->psz_longname )
1168 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1170 fprintf( file, "\n\n" );
1172 for( p_item = p_parser->p_config;
1173 p_item->i_type != CONFIG_HINT_END;
1177 int i_value = p_item->i_value;
1178 float f_value = p_item->f_value;
1179 char *psz_value = p_item->psz_value;
1181 if( p_item->i_type & CONFIG_HINT )
1184 /* Ignore deprecated options */
1185 if( p_item->psz_current )
1187 if( b_autosave && !p_item->b_autosave )
1189 i_value = p_item->i_value_saved;
1190 f_value = p_item->f_value_saved;
1191 psz_value = p_item->psz_value_saved;
1192 if( !psz_value ) psz_value = p_item->psz_value_orig;
1196 p_item->b_dirty = VLC_FALSE;
1199 switch( p_item->i_type )
1201 case CONFIG_ITEM_BOOL:
1202 case CONFIG_ITEM_INTEGER:
1203 if( p_item->psz_text )
1204 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1205 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1206 _("boolean") : _("integer") );
1207 if( i_value == p_item->i_value_orig )
1208 fprintf( file, "#" );
1209 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1211 p_item->i_value_saved = i_value;
1214 case CONFIG_ITEM_KEY:
1215 if( p_item->psz_text )
1216 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1218 if( i_value == p_item->i_value_orig )
1219 fprintf( file, "#" );
1220 psz_key = ConfigKeyToString( i_value );
1221 fprintf( file, "%s=%s\n", p_item->psz_name,
1222 psz_key ? psz_key : "" );
1223 if ( psz_key ) free( psz_key );
1225 p_item->i_value_saved = i_value;
1228 case CONFIG_ITEM_FLOAT:
1229 if( p_item->psz_text )
1230 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1232 if( f_value == p_item->f_value_orig )
1233 fprintf( file, "#" );
1234 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1236 p_item->f_value_saved = f_value;
1240 if( p_item->psz_text )
1241 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1243 if( (!psz_value && !p_item->psz_value_orig) ||
1244 (psz_value && p_item->psz_value_orig &&
1245 !strcmp( psz_value, p_item->psz_value_orig )) )
1246 fprintf( file, "#" );
1247 fprintf( file, "%s=%s\n", p_item->psz_name,
1248 psz_value ? psz_value : "" );
1250 if( b_autosave && !p_item->b_autosave ) break;
1252 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1253 p_item->psz_value_saved = 0;
1254 if( (psz_value && p_item->psz_value_orig &&
1255 strcmp( psz_value, p_item->psz_value_orig )) ||
1256 !psz_value || !p_item->psz_value_orig)
1257 p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1261 fprintf( file, "\n" );
1264 vlc_list_release( p_list );
1267 * Restore old settings from the config in file
1269 fputs( p_bigbuffer, file );
1270 free( p_bigbuffer );
1273 free( psz_filename );
1274 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1279 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1283 module_config_t *p_item;
1284 int i_index, i_count;
1286 /* Check if there's anything to save */
1287 vlc_mutex_lock( &p_this->p_vlc->config_lock );
1288 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1289 i_count = p_list->i_count;
1290 for( i_index = 0; i_index < i_count; i_index++ )
1292 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1294 if( !p_parser->i_config_items ) continue;
1296 for( p_item = p_parser->p_config;
1297 p_item->i_type != CONFIG_HINT_END;
1300 if( p_item->b_autosave && p_item->b_dirty ) break;
1302 if( p_item->i_type != CONFIG_HINT_END ) break;
1304 vlc_list_release( p_list );
1305 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1307 if( i_index == i_count ) return VLC_SUCCESS;
1308 return SaveConfigFile( p_this, 0, VLC_TRUE );
1311 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1313 return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1316 /*****************************************************************************
1317 * config_LoadCmdLine: parse command line
1318 *****************************************************************************
1319 * Parse command line for configuration options.
1320 * Now that the module_bank has been initialized, we can dynamically
1321 * generate the longopts structure used by getops. We have to do it this way
1322 * because we don't know (and don't want to know) in advance the configuration
1323 * options used (ie. exported) by each module.
1324 *****************************************************************************/
1325 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1326 vlc_bool_t b_ignore_errors )
1328 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1331 module_config_t *p_item;
1332 struct option *p_longopts;
1333 int i_modules_index;
1336 module_config_t *pp_shortopts[256];
1337 char *psz_shortopts;
1339 /* Set default configuration and copy arguments */
1340 p_this->p_vlc->i_argc = *pi_argc;
1341 p_this->p_vlc->ppsz_argv = ppsz_argv;
1344 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1345 * is the PSN - process serial number (a unique PID-ish thingie)
1346 * still ok for real Darwin & when run from command line */
1347 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1348 /* for example -psn_0_9306113 */
1350 /* GDMF!... I can't do this or else the MacOSX window server will
1351 * not pick up the PSN and not register the app and we crash...
1352 * hence the following kludge otherwise we'll get confused w/ argv[1]
1353 * being an input file name */
1355 ppsz_argv[ 1 ] = NULL;
1357 *pi_argc = *pi_argc - 1;
1363 /* List all modules */
1364 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1367 * Generate the longopts and shortopts structures used by getopt_long
1371 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1374 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1376 /* count the number of exported configuration options (to allocate
1377 * longopts). We also need to allocate space for too options when
1378 * dealing with boolean to allow for --foo and --no-foo */
1379 i_opts += p_parser->i_config_items
1380 + 2 * p_parser->i_bool_items;
1383 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1384 if( p_longopts == NULL )
1386 msg_Err( p_this, "out of memory" );
1387 vlc_list_release( p_list );
1391 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1392 if( psz_shortopts == NULL )
1394 msg_Err( p_this, "out of memory" );
1396 vlc_list_release( p_list );
1400 /* If we are requested to ignore errors, then we must work on a copy
1401 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1402 * us, ignoring the arity of the options */
1403 if( b_ignore_errors )
1405 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1406 if( ppsz_argv == NULL )
1408 msg_Err( p_this, "out of memory" );
1409 free( psz_shortopts );
1411 vlc_list_release( p_list );
1414 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1415 *pi_argc * sizeof(char *) );
1419 for( i_index = 0; i_index < 256; i_index++ )
1421 pp_shortopts[i_index] = NULL;
1424 /* Fill the p_longopts and psz_shortopts structures */
1426 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1429 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1431 if( !p_parser->i_config_items )
1434 for( p_item = p_parser->p_config;
1435 p_item->i_type != CONFIG_HINT_END;
1439 if( p_item->i_type & CONFIG_HINT )
1442 /* Add item to long options */
1443 p_longopts[i_index].name = strdup( p_item->psz_name );
1444 if( p_longopts[i_index].name == NULL ) continue;
1445 p_longopts[i_index].has_arg =
1446 (p_item->i_type == CONFIG_ITEM_BOOL)?
1447 no_argument : required_argument;
1448 p_longopts[i_index].flag = &flag;
1449 p_longopts[i_index].val = 0;
1452 /* When dealing with bools we also need to add the --no-foo
1454 if( p_item->i_type == CONFIG_ITEM_BOOL )
1456 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1457 if( psz_name == NULL ) continue;
1458 strcpy( psz_name, "no" );
1459 strcat( psz_name, p_item->psz_name );
1461 p_longopts[i_index].name = psz_name;
1462 p_longopts[i_index].has_arg = no_argument;
1463 p_longopts[i_index].flag = &flag;
1464 p_longopts[i_index].val = 1;
1467 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1468 if( psz_name == NULL ) continue;
1469 strcpy( psz_name, "no-" );
1470 strcat( psz_name, p_item->psz_name );
1472 p_longopts[i_index].name = psz_name;
1473 p_longopts[i_index].has_arg = no_argument;
1474 p_longopts[i_index].flag = &flag;
1475 p_longopts[i_index].val = 1;
1479 /* If item also has a short option, add it */
1480 if( p_item->i_short )
1482 pp_shortopts[(int)p_item->i_short] = p_item;
1483 psz_shortopts[i_shortopts] = p_item->i_short;
1485 if( p_item->i_type != CONFIG_ITEM_BOOL )
1487 psz_shortopts[i_shortopts] = ':';
1490 if( p_item->i_short == 'v' )
1492 psz_shortopts[i_shortopts] = ':';
1500 /* We don't need the module list anymore */
1501 vlc_list_release( p_list );
1503 /* Close the longopts and shortopts structures */
1504 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1505 psz_shortopts[i_shortopts] = '\0';
1508 * Parse the command line options
1512 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1513 p_longopts, &i_index ) ) != EOF )
1515 /* A long option has been recognized */
1518 module_config_t *p_conf;
1519 char *psz_name = (char *)p_longopts[i_index].name;
1521 /* Check if we deal with a --nofoo or --no-foo long option */
1522 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1524 /* Store the configuration option */
1525 p_conf = config_FindConfig( p_this, psz_name );
1528 /* Check if the option is deprecated */
1529 if( p_conf->psz_current )
1531 if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1533 if( !b_ignore_errors )
1536 "Warning: option --%s is no longer used.\n",
1541 if( !b_ignore_errors )
1543 if( p_conf->b_strict )
1546 "Error: option --%s is deprecated. "
1547 "Use --%s instead.\n",
1548 p_conf->psz_name, p_conf->psz_current);
1550 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1551 free( (char *)p_longopts[i_index].name );
1554 free( psz_shortopts );
1558 "Warning: option --%s is deprecated. "
1559 "You should use --%s instead.\n",
1560 p_conf->psz_name, p_conf->psz_current);
1562 psz_name=p_conf->psz_current;
1563 p_conf = config_FindConfig( p_this, psz_name );
1566 switch( p_conf->i_type )
1568 case CONFIG_ITEM_STRING:
1569 case CONFIG_ITEM_FILE:
1570 case CONFIG_ITEM_DIRECTORY:
1571 case CONFIG_ITEM_MODULE:
1572 case CONFIG_ITEM_MODULE_LIST:
1573 case CONFIG_ITEM_MODULE_LIST_CAT:
1574 case CONFIG_ITEM_MODULE_CAT:
1575 config_PutPsz( p_this, psz_name, optarg );
1577 case CONFIG_ITEM_INTEGER:
1578 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1580 case CONFIG_ITEM_FLOAT:
1581 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1583 case CONFIG_ITEM_KEY:
1584 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1586 case CONFIG_ITEM_BOOL:
1587 config_PutInt( p_this, psz_name, !flag );
1594 /* A short option has been recognized */
1595 if( pp_shortopts[i_cmd] != NULL )
1597 switch( pp_shortopts[i_cmd]->i_type )
1599 case CONFIG_ITEM_STRING:
1600 case CONFIG_ITEM_FILE:
1601 case CONFIG_ITEM_DIRECTORY:
1602 case CONFIG_ITEM_MODULE:
1603 case CONFIG_ITEM_MODULE_CAT:
1604 case CONFIG_ITEM_MODULE_LIST:
1605 case CONFIG_ITEM_MODULE_LIST_CAT:
1606 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1608 case CONFIG_ITEM_INTEGER:
1613 if( *optarg == 'v' ) /* eg. -vvv */
1616 while( *optarg == 'v' )
1624 i_verbose += atoi( optarg ); /* eg. -v2 */
1629 i_verbose++; /* -v */
1631 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1636 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1637 strtol(optarg, 0, 0) );
1640 case CONFIG_ITEM_BOOL:
1641 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1648 /* Internal error: unknown option */
1649 if( !b_ignore_errors )
1651 fprintf( stderr, "%s: unknown option"
1652 " or missing mandatory argument ",
1653 p_this->p_vlc->psz_object_name );
1656 fprintf( stderr, "`-%c'\n", optopt );
1660 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1662 fprintf( stderr, "Try `%s --help' for more information.\n",
1663 p_this->p_vlc->psz_object_name );
1665 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1666 free( (char *)p_longopts[i_index].name );
1668 free( psz_shortopts );
1673 /* Free allocated resources */
1674 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1675 free( (char *)p_longopts[i_index].name );
1677 free( psz_shortopts );
1678 if( b_ignore_errors ) free( ppsz_argv );
1684 * config_GetDataDir: find directory where shared data is installed
1686 * @return a string (always succeeds).
1688 const char *config_GetDataDir( const vlc_object_t *p_this )
1690 #if defined (WIN32) || defined (UNDER_CE)
1691 return p_this->p_libvlc->psz_vlcpath;
1692 #elif defined(__APPLE__) || defined (SYS_BEOS)
1693 static char path[PATH_MAX] = "";
1697 snprintf( path, sizeof( path ), "%s/share",
1698 p_this->p_libvlc->psz_vlcpath );
1699 path[sizeof( path ) - 1] = '\0';
1707 /*****************************************************************************
1708 * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1709 *****************************************************************************
1710 * This function will try by different ways to find the user's home path.
1711 * Note that this function is not reentrant, it should be called only once
1712 * at the beginning of main where the result will be stored for later use.
1713 *****************************************************************************/
1714 static char *GetDir( vlc_bool_t b_appdata )
1716 char *psz_localhome = NULL;
1718 #if defined(HAVE_GETPWUID)
1719 struct passwd *p_pw = NULL;
1722 #if defined(WIN32) && !defined(UNDER_CE)
1723 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1725 #ifndef CSIDL_FLAG_CREATE
1726 # define CSIDL_FLAG_CREATE 0x8000
1728 #ifndef CSIDL_APPDATA
1729 # define CSIDL_APPDATA 0x1A
1731 #ifndef CSIDL_PROFILE
1732 # define CSIDL_PROFILE 0x28
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 char psz_ACPhome[MAX_PATH];
1750 /* get the "Application Data" folder for the current user */
1751 if( S_OK == SHGetFolderPath( NULL,
1752 (b_appdata ? CSIDL_APPDATA :
1753 CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1754 NULL, SHGFP_TYPE_CURRENT,
1757 FreeLibrary( shfolder_dll );
1758 return FromLocaleDup( psz_ACPhome );
1761 FreeLibrary( shfolder_dll );
1764 #elif defined(UNDER_CE)
1766 #ifndef CSIDL_APPDATA
1767 # define CSIDL_APPDATA 0x1A
1770 wchar_t p_whomedir[MAX_PATH];
1772 /* get the "Application Data" folder for the current user */
1773 if( SHGetSpecialFolderPath( NULL, p_whomedir, CSIDL_APPDATA, 1 ) )
1775 char psz_ACPhome[2 * MAX_PATH];
1777 sprintf( psz_ACPhome, "%ls", p_whomedir );
1778 return FromLocaleDup( psz_ACPhome );
1782 #if defined(HAVE_GETPWUID)
1783 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1786 psz_localhome = getenv( "HOME" );
1787 if( psz_localhome == NULL )
1789 psz_localhome = getenv( "TMP" );
1790 if( psz_localhome == NULL )
1791 psz_localhome = "/tmp";
1794 #if defined(HAVE_GETPWUID)
1796 psz_localhome = p_pw->pw_dir;
1799 return FromLocaleDup( psz_localhome );
1802 char *config_GetHomeDir( void )
1804 return GetDir( VLC_TRUE );
1807 char *config_GetUserDir( void )
1809 return GetDir( VLC_FALSE );
1813 static int ConfigStringToKey( char *psz_key )
1817 char *psz_parser = strchr( psz_key, '-' );
1818 while( psz_parser && psz_parser != psz_key )
1820 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1822 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1823 strlen( vlc_modifiers[i].psz_key_string ) ) )
1825 i_key |= vlc_modifiers[i].i_key_code;
1828 psz_key = psz_parser + 1;
1829 psz_parser = strchr( psz_key, '-' );
1831 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1833 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1835 i_key |= vlc_keys[i].i_key_code;
1842 static char *ConfigKeyToString( int i_key )
1844 char *psz_key = malloc( 100 );
1854 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1857 if( i_key & vlc_modifiers[index].i_key_code )
1859 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1862 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1865 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1867 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );