]> git.sesse.net Git - vlc/blob - src/config/core.c
Split the big config file
[vlc] / src / config / core.c
1 /*****************************************************************************
2  * core.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25 #include "../libvlc.h"
26 #include "vlc_keys.h"
27 #include "vlc_charset.h"
28
29 #include <errno.h>                                                  /* errno */
30
31 #ifdef HAVE_LIMITS_H
32 #   include <limits.h>
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #    include <unistd.h>                                          /* getuid() */
37 #endif
38
39 #ifdef HAVE_GETOPT_LONG
40 #   ifdef HAVE_GETOPT_H
41 #       include <getopt.h>                                       /* getopt() */
42 #   endif
43 #else
44 #   include "../extras/getopt.h"
45 #endif
46
47 #if defined(HAVE_GETPWUID)
48 #   include <pwd.h>                                            /* getpwuid() */
49 #endif
50
51 #if defined( HAVE_SYS_STAT_H )
52 #   include <sys/stat.h>
53 #endif
54 #if defined( HAVE_SYS_TYPES_H )
55 #   include <sys/types.h>
56 #endif
57 #if defined( WIN32 )
58 #   if !defined( UNDER_CE )
59 #       include <direct.h>
60 #   endif
61 #include <tchar.h>
62 #endif
63
64 #include "config.h"
65 #include "modules/modules.h"
66
67 static inline char *strdupnull (const char *src)
68 {
69     return src ? strdup (src) : NULL;
70 }
71
72 static inline char *_strdupnull (const char *src)
73 {
74     return src ? strdup (_(src)) : NULL;
75 }
76
77 /* Item types that use a string value (i.e. serialized in the module cache) */
78 int IsConfigStringType (int type)
79 {
80     static const unsigned char config_types[] =
81     {
82         CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
83         CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
84         CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT
85     };
86
87     /* NOTE: this needs to be changed if we ever get more than 255 types */
88     return memchr (config_types, type, sizeof (config_types)) != NULL;
89 }
90
91
92 static int IsConfigIntegerType (int type)
93 {
94     static const unsigned char config_types[] =
95     {
96         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
97         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
98     };
99
100     return memchr (config_types, type, sizeof (config_types)) != NULL;
101 }
102
103
104 static inline int IsConfigFloatType (int type)
105 {
106     return type == CONFIG_ITEM_FLOAT;
107 }
108
109
110 /*****************************************************************************
111  * config_GetType: get the type of a variable (bool, int, float, string)
112  *****************************************************************************
113  * This function is used to get the type of a variable from its name.
114  * Beware, this is quite slow.
115  *****************************************************************************/
116 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
117 {
118     module_config_t *p_config;
119     int i_type;
120
121     p_config = config_FindConfig( p_this, psz_name );
122
123     /* sanity checks */
124     if( !p_config )
125     {
126         return 0;
127     }
128
129     switch( p_config->i_type )
130     {
131     case CONFIG_ITEM_BOOL:
132         i_type = VLC_VAR_BOOL;
133         break;
134
135     case CONFIG_ITEM_INTEGER:
136     case CONFIG_ITEM_KEY:
137         i_type = VLC_VAR_INTEGER;
138         break;
139
140     case CONFIG_ITEM_FLOAT:
141         i_type = VLC_VAR_FLOAT;
142         break;
143
144     case CONFIG_ITEM_MODULE:
145     case CONFIG_ITEM_MODULE_CAT:
146     case CONFIG_ITEM_MODULE_LIST:
147     case CONFIG_ITEM_MODULE_LIST_CAT:
148         i_type = VLC_VAR_MODULE;
149         break;
150
151     case CONFIG_ITEM_STRING:
152         i_type = VLC_VAR_STRING;
153         break;
154
155     case CONFIG_ITEM_PASSWORD:
156         i_type = VLC_VAR_STRING;
157         break;
158
159     case CONFIG_ITEM_FILE:
160         i_type = VLC_VAR_FILE;
161         break;
162
163     case CONFIG_ITEM_DIRECTORY:
164         i_type = VLC_VAR_DIRECTORY;
165         break;
166
167     default:
168         i_type = 0;
169         break;
170     }
171
172     return i_type;
173 }
174
175 /*****************************************************************************
176  * config_GetInt: get the value of an int variable
177  *****************************************************************************
178  * This function is used to get the value of variables which are internally
179  * represented by an integer (CONFIG_ITEM_INTEGER and
180  * CONFIG_ITEM_BOOL).
181  *****************************************************************************/
182 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
183 {
184     module_config_t *p_config;
185
186     p_config = config_FindConfig( p_this, psz_name );
187
188     /* sanity checks */
189     if( !p_config )
190     {
191         msg_Err( p_this, "option %s does not exist", psz_name );
192         return -1;
193     }
194
195     if (!IsConfigIntegerType (p_config->i_type))
196     {
197         msg_Err( p_this, "option %s does not refer to an int", psz_name );
198         return -1;
199     }
200
201     return p_config->value.i;
202 }
203
204 /*****************************************************************************
205  * config_GetFloat: get the value of a float variable
206  *****************************************************************************
207  * This function is used to get the value of variables which are internally
208  * represented by a float (CONFIG_ITEM_FLOAT).
209  *****************************************************************************/
210 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
211 {
212     module_config_t *p_config;
213
214     p_config = config_FindConfig( p_this, psz_name );
215
216     /* sanity checks */
217     if( !p_config )
218     {
219         msg_Err( p_this, "option %s does not exist", psz_name );
220         return -1;
221     }
222
223     if (!IsConfigFloatType (p_config->i_type))
224     {
225         msg_Err( p_this, "option %s does not refer to a float", psz_name );
226         return -1;
227     }
228
229     return p_config->value.f;
230 }
231
232 /*****************************************************************************
233  * config_GetPsz: get the string value of a string variable
234  *****************************************************************************
235  * This function is used to get the value of variables which are internally
236  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
237  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
238  *
239  * Important note: remember to free() the returned char* because it's a
240  *   duplicate of the actual value. It isn't safe to return a pointer to the
241  *   actual value as it can be modified at any time.
242  *****************************************************************************/
243 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
244 {
245     module_config_t *p_config;
246
247     p_config = config_FindConfig( p_this, psz_name );
248
249     /* sanity checks */
250     if( !p_config )
251     {
252         msg_Err( p_this, "option %s does not exist", psz_name );
253         return NULL;
254     }
255
256     if (!IsConfigStringType (p_config->i_type))
257     {
258         msg_Err( p_this, "option %s does not refer to a string", psz_name );
259         return NULL;
260     }
261
262     /* return a copy of the string */
263     vlc_mutex_lock( p_config->p_lock );
264     char *psz_value = strdupnull (p_config->value.psz);
265     vlc_mutex_unlock( p_config->p_lock );
266
267     return psz_value;
268 }
269
270 /*****************************************************************************
271  * config_PutPsz: set the string value of a string variable
272  *****************************************************************************
273  * This function is used to set the value of variables which are internally
274  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
275  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
276  *****************************************************************************/
277 void __config_PutPsz( vlc_object_t *p_this,
278                       const char *psz_name, const char *psz_value )
279 {
280     module_config_t *p_config;
281     vlc_value_t oldval, val;
282
283     p_config = config_FindConfig( p_this, psz_name );
284
285
286     /* sanity checks */
287     if( !p_config )
288     {
289         msg_Warn( p_this, "option %s does not exist", psz_name );
290         return;
291     }
292
293     if (!IsConfigStringType (p_config->i_type))
294     {
295         msg_Err( p_this, "option %s does not refer to a string", psz_name );
296         return;
297     }
298
299     vlc_mutex_lock( p_config->p_lock );
300
301     /* backup old value */
302     oldval.psz_string = (char *)p_config->value.psz;
303
304     if ((psz_value != NULL) && *psz_value)
305         p_config->value.psz = strdup (psz_value);
306     else
307         p_config->value.psz = NULL;
308
309     p_config->b_dirty = VLC_TRUE;
310
311     val.psz_string = (char *)p_config->value.psz;
312
313     vlc_mutex_unlock( p_config->p_lock );
314
315     if( p_config->pf_callback )
316     {
317         p_config->pf_callback( p_this, psz_name, oldval, val,
318                                p_config->p_callback_data );
319     }
320
321     /* free old string */
322     if( oldval.psz_string ) free( oldval.psz_string );
323 }
324
325 /*****************************************************************************
326  * config_PutInt: set the integer value of an int variable
327  *****************************************************************************
328  * This function is used to set the value of variables which are internally
329  * represented by an integer (CONFIG_ITEM_INTEGER and
330  * CONFIG_ITEM_BOOL).
331  *****************************************************************************/
332 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
333 {
334     module_config_t *p_config;
335     vlc_value_t oldval, val;
336
337     p_config = config_FindConfig( p_this, psz_name );
338
339     /* sanity checks */
340     if( !p_config )
341     {
342         msg_Warn( p_this, "option %s does not exist", psz_name );
343         return;
344     }
345
346     if (!IsConfigIntegerType (p_config->i_type))
347     {
348         msg_Err( p_this, "option %s does not refer to an int", psz_name );
349         return;
350     }
351
352     /* backup old value */
353     oldval.i_int = p_config->value.i;
354
355     /* if i_min == i_max == 0, then do not use them */
356     if ((p_config->min.i == 0) && (p_config->max.i == 0))
357     {
358         p_config->value.i = i_value;
359     }
360     else if (i_value < p_config->min.i)
361     {
362         p_config->value.i = p_config->min.i;
363     }
364     else if (i_value > p_config->max.i)
365     {
366         p_config->value.i = p_config->max.i;
367     }
368     else
369     {
370         p_config->value.i = i_value;
371     }
372
373     p_config->b_dirty = VLC_TRUE;
374
375     val.i_int = p_config->value.i;
376
377     if( p_config->pf_callback )
378     {
379         p_config->pf_callback( p_this, psz_name, oldval, val,
380                                p_config->p_callback_data );
381     }
382 }
383
384 /*****************************************************************************
385  * config_PutFloat: set the value of a float variable
386  *****************************************************************************
387  * This function is used to set the value of variables which are internally
388  * represented by a float (CONFIG_ITEM_FLOAT).
389  *****************************************************************************/
390 void __config_PutFloat( vlc_object_t *p_this,
391                         const char *psz_name, float f_value )
392 {
393     module_config_t *p_config;
394     vlc_value_t oldval, val;
395
396     p_config = config_FindConfig( p_this, psz_name );
397
398     /* sanity checks */
399     if( !p_config )
400     {
401         msg_Warn( p_this, "option %s does not exist", psz_name );
402         return;
403     }
404
405     if (!IsConfigFloatType (p_config->i_type))
406     {
407         msg_Err( p_this, "option %s does not refer to a float", psz_name );
408         return;
409     }
410
411     /* backup old value */
412     oldval.f_float = p_config->value.f;
413
414     /* if f_min == f_max == 0, then do not use them */
415     if ((p_config->min.f == 0) && (p_config->max.f == 0))
416     {
417         p_config->value.f = f_value;
418     }
419     else if (f_value < p_config->min.f)
420     {
421         p_config->value.f = p_config->min.f;
422     }
423     else if (f_value > p_config->max.f)
424     {
425         p_config->value.f = p_config->max.f;
426     }
427     else
428     {
429         p_config->value.f = f_value;
430     }
431
432     p_config->b_dirty = VLC_TRUE;
433
434     val.f_float = p_config->value.f;
435
436     if( p_config->pf_callback )
437     {
438         p_config->pf_callback( p_this, psz_name, oldval, val,
439                                p_config->p_callback_data );
440     }
441 }
442
443 /*****************************************************************************
444  * config_FindConfig: find the config structure associated with an option.
445  *****************************************************************************
446  * FIXME: This function really needs to be optimized.
447  * FIXME: And now even more.
448  *****************************************************************************/
449 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
450 {
451     vlc_list_t *p_list;
452     int i_index;
453
454     if( !psz_name ) return NULL;
455
456     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
457
458     for( i_index = 0; i_index < p_list->i_count; i_index++ )
459     {
460         module_config_t *p_item, *p_end;
461         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
462
463         if( !p_parser->i_config_items )
464             continue;
465
466         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
467              p_item < p_end;
468              p_item++ )
469         {
470             if( p_item->i_type & CONFIG_HINT )
471                 /* ignore hints */
472                 continue;
473             if( !strcmp( psz_name, p_item->psz_name ) )
474             {
475                 vlc_list_release( p_list );
476                 return p_item;
477             }
478         }
479     }
480
481     vlc_list_release( p_list );
482
483     return NULL;
484 }
485
486 /*****************************************************************************
487  * config_FindModule: find a specific module structure.
488  *****************************************************************************/
489 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
490 {
491     vlc_list_t *p_list;
492     module_t *p_module, *p_result = NULL;
493     int i_index;
494
495     if( !psz_name ) return NULL;
496
497     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
498
499     for( i_index = 0; i_index < p_list->i_count; i_index++ )
500     {
501         p_module = (module_t *)p_list->p_values[i_index].p_object;
502         if( !strcmp( p_module->psz_object_name, psz_name ) )
503         {
504              p_result = p_module;
505              break;
506         }
507     }
508
509     vlc_list_release( p_list );
510
511     return p_result;
512 }
513
514 /*****************************************************************************
515  * config_Duplicate: creates a duplicate of a module's configuration data.
516  *****************************************************************************
517  * Unfortunatly we cannot work directly with the module's config data as
518  * this module might be unloaded from memory at any time (remember HideModule).
519  * This is why we need to create an exact copy of the config data.
520  *****************************************************************************/
521 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
522                       size_t n )
523 {
524     int j;
525     const module_config_t *p_item, *p_end = p_orig + n;
526
527     /* Calculate the structure length */
528     p_module->i_config_items = 0;
529     p_module->i_bool_items = 0;
530
531     for( p_item = p_orig; p_item < p_end; p_item++ )
532     {
533         if( p_item->i_type & CONFIG_ITEM )
534         {
535             p_module->i_config_items++;
536         }
537
538         if( p_item->i_type == CONFIG_ITEM_BOOL )
539         {
540             p_module->i_bool_items++;
541         }
542     }
543
544     /* Allocate memory */
545     p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
546     if( p_module->p_config == NULL )
547     {
548         msg_Err( p_module, "config error: can't duplicate p_config" );
549         return VLC_ENOMEM;
550     }
551     p_module->confsize = n;
552
553     /* Do the duplication job */
554     for( size_t i = 0; i < n ; i++ )
555     {
556         p_module->p_config[i] = p_orig[i];
557
558         if (IsConfigIntegerType (p_module->p_config[i].i_type))
559         {
560             p_module->p_config[i].orig.i = p_orig[i].value.i;
561             p_module->p_config[i].saved.i = p_orig[i].value.i;
562         }
563         else
564         if (IsConfigFloatType (p_module->p_config[i].i_type))
565         {
566             p_module->p_config[i].orig.f = p_orig[i].value.f;
567             p_module->p_config[i].saved.f = p_orig[i].value.f;
568         }
569         else
570         if (IsConfigStringType (p_module->p_config[i].i_type))
571         {
572             p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
573             p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
574             p_module->p_config[i].saved.psz = NULL;
575         }
576
577         p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
578         p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
579         p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
580         p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
581         p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
582
583         p_module->p_config[i].p_lock = &p_module->object_lock;
584
585         /* duplicate the string list */
586         if( p_orig[i].i_list )
587         {
588             if( p_orig[i].ppsz_list )
589             {
590                 p_module->p_config[i].ppsz_list =
591                     malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
592                 if( p_module->p_config[i].ppsz_list )
593                 {
594                     for( j = 0; j < p_orig[i].i_list; j++ )
595                         p_module->p_config[i].ppsz_list[j] =
596                                 strdupnull (p_orig[i].ppsz_list[j]);
597                     p_module->p_config[i].ppsz_list[j] = NULL;
598                 }
599             }
600             if( p_orig[i].ppsz_list_text )
601             {
602                 p_module->p_config[i].ppsz_list_text =
603                     calloc( (p_orig[i].i_list + 1), sizeof(char *) );
604                 if( p_module->p_config[i].ppsz_list_text )
605                 {
606                     for( j = 0; j < p_orig[i].i_list; j++ )
607                         p_module->p_config[i].ppsz_list_text[j] =
608                                 strdupnull (_(p_orig[i].ppsz_list_text[j]));
609                     p_module->p_config[i].ppsz_list_text[j] = NULL;
610                 }
611             }
612             if( p_orig[i].pi_list )
613             {
614                 p_module->p_config[i].pi_list =
615                     malloc( (p_orig[i].i_list + 1) * sizeof(int) );
616                 if( p_module->p_config[i].pi_list )
617                 {
618                     for( j = 0; j < p_orig[i].i_list; j++ )
619                         p_module->p_config[i].pi_list[j] =
620                             p_orig[i].pi_list[j];
621                 }
622             }
623         }
624
625         /* duplicate the actions list */
626         if( p_orig[i].i_action )
627         {
628             int j;
629
630             p_module->p_config[i].ppf_action =
631                 malloc( p_orig[i].i_action * sizeof(void *) );
632             p_module->p_config[i].ppsz_action_text =
633                 malloc( p_orig[i].i_action * sizeof(char *) );
634
635             for( j = 0; j < p_orig[i].i_action; j++ )
636             {
637                 p_module->p_config[i].ppf_action[j] =
638                     p_orig[i].ppf_action[j];
639                 p_module->p_config[i].ppsz_action_text[j] =
640                     strdupnull (p_orig[i].ppsz_action_text[j]);
641             }
642         }
643
644         p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
645     }
646     return VLC_SUCCESS;
647 }
648
649
650 /*****************************************************************************
651  * config_Free: frees a duplicated module's configuration data.
652  *****************************************************************************
653  * This function frees all the data duplicated by config_Duplicate.
654  *****************************************************************************/
655 void config_Free( module_t *p_module )
656 {
657     int i;
658
659     for (size_t j = 0; j < p_module->confsize; j++)
660     {
661         module_config_t *p_item = p_module->p_config + j;
662
663         free( (char*) p_item->psz_type );
664         free( (char*) p_item->psz_name );
665         free( (char*) p_item->psz_current );
666         free( (char*) p_item->psz_text );
667         free( (char*) p_item->psz_longtext );
668
669         if (IsConfigStringType (p_item->i_type))
670         {
671             free ((char *)p_item->value.psz);
672             free ((char *)p_item->orig.psz);
673             free ((char *)p_item->saved.psz);
674         }
675
676         if( p_item->i_list )
677         {
678             for( i = 0; i < p_item->i_list; i++ )
679             {
680                 if( p_item->ppsz_list && p_item->ppsz_list[i] )
681                     free( (char*) p_item->ppsz_list[i] );
682                 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
683                     free( (char*) p_item->ppsz_list_text[i] );
684             }
685             if( p_item->ppsz_list ) free( p_item->ppsz_list );
686             if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
687             if( p_item->pi_list ) free( p_item->pi_list );
688         }
689
690         if( p_item->i_action )
691         {
692             for( i = 0; i < p_item->i_action; i++ )
693             {
694                 free( (char*) p_item->ppsz_action_text[i] );
695             }
696             if( p_item->ppf_action ) free( p_item->ppf_action );
697             if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
698         }
699     }
700
701     if (p_module->p_config != NULL)
702     {
703         free (p_module->p_config);
704         p_module->p_config = NULL;
705     }
706 }
707
708 /*****************************************************************************
709  * config_SetCallbacks: sets callback functions in the duplicate p_config.
710  *****************************************************************************
711  * Unfortunatly we cannot work directly with the module's config data as
712  * this module might be unloaded from memory at any time (remember HideModule).
713  * This is why we need to duplicate callbacks each time we reload the module.
714  *****************************************************************************/
715 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
716                           size_t n )
717 {
718     for (size_t i = 0; i < n; i++)
719     {
720         p_new->pf_callback = p_orig->pf_callback;
721         p_new++;
722         p_orig++;
723     }
724 }
725
726 /*****************************************************************************
727  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
728  *****************************************************************************
729  * We simply undo what we did in config_SetCallbacks.
730  *****************************************************************************/
731 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
732 {
733     for (size_t i = 0; i < n; i++)
734     {
735         p_new->pf_callback = NULL;
736         p_new++;
737     }
738 }
739
740 /*****************************************************************************
741  * config_ResetAll: reset the configuration data for all the modules.
742  *****************************************************************************/
743 void __config_ResetAll( vlc_object_t *p_this )
744 {
745     int i_index;
746     vlc_list_t *p_list;
747     module_t *p_module;
748
749     /* Acquire config file lock */
750     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
751
752     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
753
754     for( i_index = 0; i_index < p_list->i_count; i_index++ )
755     {
756         p_module = (module_t *)p_list->p_values[i_index].p_object ;
757         if( p_module->b_submodule ) continue;
758
759         for (size_t i = 0; i < p_module->confsize; i++ )
760         {
761             if (IsConfigIntegerType (p_module->p_config[i].i_type))
762                 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
763             else
764             if (IsConfigFloatType (p_module->p_config[i].i_type))
765                 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
766             else
767             if (IsConfigStringType (p_module->p_config[i].i_type))
768             {
769                 free ((char *)p_module->p_config[i].value.psz);
770                 p_module->p_config[i].value.psz =
771                         strdupnull (p_module->p_config[i].orig.psz);
772             }
773         }
774     }
775
776     vlc_list_release( p_list );
777     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
778 }
779
780 /**
781  * config_GetDataDir: find directory where shared data is installed
782  *
783  * @return a string (always succeeds).
784  */
785 const char *config_GetDataDir( void )
786 {
787 #if defined (WIN32) || defined (UNDER_CE)
788     return vlc_global()->psz_vlcpath;
789 #elif defined(__APPLE__) || defined (SYS_BEOS)
790     static char path[PATH_MAX] = "";
791
792     if( *path == '\0' )
793     {
794         snprintf( path, sizeof( path ), "%s/share",
795                   vlc_global()->psz_vlcpath );
796         path[sizeof( path ) - 1] = '\0';
797     }
798     return path;
799 #else
800     return DATA_PATH;
801 #endif
802 }
803
804 /*****************************************************************************
805  * config_GetHomeDir, config_GetUserDir: find the user's home directory.
806  *****************************************************************************
807  * This function will try by different ways to find the user's home path.
808  * Note that this function is not reentrant, it should be called only once
809  * at the beginning of main where the result will be stored for later use.
810  *****************************************************************************/
811 static char *GetDir( vlc_bool_t b_appdata )
812 {
813     const char *psz_localhome = NULL;
814
815 #if defined(WIN32) && !defined(UNDER_CE)
816     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
817                                                LPWSTR );
818 #ifndef CSIDL_FLAG_CREATE
819 #   define CSIDL_FLAG_CREATE 0x8000
820 #endif
821 #ifndef CSIDL_APPDATA
822 #   define CSIDL_APPDATA 0x1A
823 #endif
824 #ifndef CSIDL_PROFILE
825 #   define CSIDL_PROFILE 0x28
826 #endif
827 #ifndef SHGFP_TYPE_CURRENT
828 #   define SHGFP_TYPE_CURRENT 0
829 #endif
830
831     HINSTANCE shfolder_dll;
832     SHGETFOLDERPATH SHGetFolderPath ;
833
834     /* load the shfolder dll to retrieve SHGetFolderPath */
835     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
836     {
837         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
838                                                   _T("SHGetFolderPathW") );
839         if ( SHGetFolderPath != NULL )
840         {
841             wchar_t whomedir[MAX_PATH];
842
843             /* get the "Application Data" folder for the current user */
844             if( S_OK == SHGetFolderPath( NULL,
845                                          (b_appdata ? CSIDL_APPDATA :
846                                            CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
847                                          NULL, SHGFP_TYPE_CURRENT,
848                                          whomedir ) )
849             {
850                 FreeLibrary( shfolder_dll );
851                 return FromWide( whomedir );
852             }
853         }
854         FreeLibrary( shfolder_dll );
855     }
856
857 #elif defined(UNDER_CE)
858
859 #ifndef CSIDL_APPDATA
860 #   define CSIDL_APPDATA 0x1A
861 #endif
862
863     wchar_t whomedir[MAX_PATH];
864
865     /* get the "Application Data" folder for the current user */
866     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
867         return FromWide( whomedir );
868 #endif
869
870     psz_localhome = getenv( "HOME" );
871     if( psz_localhome == NULL )
872     {
873 #if defined(HAVE_GETPWUID)
874         struct passwd *p_pw;
875         (void)b_appdata;
876
877         if( ( p_pw = getpwuid( getuid() ) ) != NULL )
878             psz_localhome = p_pw->pw_dir;
879         else
880 #endif
881         {
882             psz_localhome = getenv( "TMP" );
883             if( psz_localhome == NULL )
884                 psz_localhome = "/tmp";
885         }
886     }
887
888     return FromLocaleDup( psz_localhome );
889 }
890
891 /**
892  * Get the user's home directory
893  */
894 char *config_GetHomeDir( void )
895 {
896     return GetDir( VLC_FALSE );
897 }
898
899 /**
900  * Get the user's main data and config directory:
901  *   - on windows that's the App Data directory;
902  *   - on other OSes it's the same as the home directory.
903  */
904 char *config_GetUserDir( void );
905 char *config_GetUserDir( void )
906 {
907     return GetDir( VLC_TRUE );
908 }
909
910 /**
911  * Get the user's VLC configuration directory
912  */
913 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
914 {
915     char *psz_dir;
916 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
917     char *psz_parent = config_GetUserDir();
918     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
919     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
920         return NULL;
921     return psz_dir;
922 #else
923     /* XDG Base Directory Specification - Version 0.6 */
924     char *psz_env = getenv( "XDG_CONFIG_HOME" );
925     if( psz_env )
926     {
927         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
928             return NULL;
929         return psz_dir;
930     }
931     psz_env = getenv( "HOME" );
932     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
933     if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
934         return NULL;
935     return psz_dir;
936 #endif
937 }
938
939 /**
940  * Get the user's VLC data directory
941  * (used for stuff like the skins, custom lua modules, ...)
942  */
943 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
944 {
945     char *psz_dir;
946 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
947     char *psz_parent = config_GetUserDir();
948     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
949     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
950         return NULL;
951     return psz_dir;
952 #else
953     /* XDG Base Directory Specification - Version 0.6 */
954     char *psz_env = getenv( "XDG_DATA_HOME" );
955     if( psz_env )
956     {
957         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
958             return NULL;
959         return psz_dir;
960     }
961     psz_env = getenv( "HOME" );
962     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
963     if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
964         return NULL;
965     return psz_dir;
966 #endif
967 }
968
969 /**
970  * Get the user's VLC cache directory
971  * (used for stuff like the modules cache, the album art cache, ...)
972  */
973 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
974 {
975     char *psz_dir;
976 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
977     char *psz_parent = config_GetUserDir();
978     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
979     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
980         return NULL;
981     return psz_dir;
982 #else
983     /* XDG Base Directory Specification - Version 0.6 */
984     char *psz_env = getenv( "XDG_CACHE_HOME" );
985     if( psz_env )
986     {
987         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
988             return NULL;
989         return psz_dir;
990     }
991     psz_env = getenv( "HOME" );
992     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
993     if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
994         return NULL;
995     return psz_dir;
996 #endif
997 }
998
999 /* Adds an extra interface to the configuration */
1000 void __config_AddIntf( vlc_object_t *p_this, const char *psz_intf )
1001 {
1002     assert( psz_intf );
1003
1004     char *psz_config, *psz_parser;
1005     size_t i_len = strlen( psz_intf );
1006
1007     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1008     while( psz_parser )
1009     {
1010         if( !strncmp( psz_intf, psz_parser, i_len ) )
1011         {
1012             free( psz_config );
1013             return;
1014         }
1015         psz_parser = strchr( psz_parser, ':' );
1016         if( psz_parser ) psz_parser++; /* skip the ':' */
1017     }
1018     free( psz_config );
1019
1020     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1021     while( psz_parser )
1022     {
1023         if( !strncmp( psz_intf, psz_parser, i_len ) )
1024         {
1025             free( psz_config );
1026             return;
1027         }
1028         psz_parser = strchr( psz_parser, ':' );
1029         if( psz_parser ) psz_parser++; /* skip the ':' */
1030     }
1031
1032     /* interface not found in the config, let's add it */
1033     if( psz_config && strlen( psz_config ) > 0 )
1034     {
1035         char *psz_newconfig;
1036         if( asprintf( &psz_newconfig, "%s:%s", psz_config, psz_intf ) != -1 )
1037         {
1038             config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1039             free( psz_newconfig );
1040         }
1041     }
1042     else
1043         config_PutPsz( p_this->p_libvlc, "extraintf", psz_intf );
1044
1045     free( psz_config );
1046 }
1047
1048 /* Removes an extra interface from the configuration */
1049 void __config_RemoveIntf( vlc_object_t *p_this, const char *psz_intf )
1050 {
1051     assert( psz_intf );
1052
1053     char *psz_config, *psz_parser;
1054     size_t i_len = strlen( psz_intf );
1055
1056     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1057     while( psz_parser )
1058     {
1059         if( !strncmp( psz_intf, psz_parser, i_len ) )
1060         {
1061             char *psz_newconfig;
1062             char *psz_end = psz_parser + i_len;
1063             if( *psz_end == ':' ) psz_end++;
1064             *psz_parser = '\0';
1065             if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1066             {
1067                 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1068                 free( psz_newconfig );
1069             }
1070             break;
1071         }
1072         psz_parser = strchr( psz_parser, ':' );
1073         if( psz_parser ) psz_parser++; /* skip the ':' */
1074     }
1075     free( psz_config );
1076
1077     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1078     while( psz_parser )
1079     {
1080         if( !strncmp( psz_intf, psz_parser, i_len ) )
1081         {
1082             char *psz_newconfig;
1083             char *psz_end = psz_parser + i_len;
1084             if( *psz_end == ':' ) psz_end++;
1085             *psz_parser = '\0';
1086             if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1087             {
1088                 config_PutPsz( p_this->p_libvlc, "control", psz_newconfig );
1089                 free( psz_newconfig );
1090             }
1091             break;
1092         }
1093         psz_parser = strchr( psz_parser, ':' );
1094         if( psz_parser ) psz_parser++; /* skip the ':' */
1095     }
1096     free( psz_config );
1097 }
1098
1099 /*
1100  * Returns VLC_TRUE if the specified extra interface is present in the
1101  * configuration, VLC_FALSE if not
1102  */
1103 vlc_bool_t __config_ExistIntf( vlc_object_t *p_this, const char *psz_intf )
1104 {
1105     assert( psz_intf );
1106
1107     char *psz_config, *psz_parser;
1108     size_t i_len = strlen( psz_intf );
1109
1110     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1111     while( psz_parser )
1112     {
1113         if( !strncmp( psz_parser, psz_intf, i_len ) )
1114         {
1115             free( psz_config );
1116             return VLC_TRUE;
1117         }
1118         psz_parser = strchr( psz_parser, ':' );
1119         if( psz_parser ) psz_parser++; /* skip the ':' */
1120     }
1121     free( psz_config );
1122
1123     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1124     while( psz_parser )
1125     {
1126         if( !strncmp( psz_parser, psz_intf, i_len ) )
1127         {
1128             free( psz_config );
1129             return VLC_TRUE;
1130         }
1131         psz_parser = strchr( psz_parser, ':' );
1132         if( psz_parser ) psz_parser++; /* skip the ':' */
1133     }
1134     free( psz_config );
1135
1136     return VLC_FALSE;
1137 }
1138