]> git.sesse.net Git - vlc/blob - src/config/core.c
- Remove config_FindModule as module_Find nowadays does the same thing
[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_Duplicate: creates a duplicate of a module's configuration data.
488  *****************************************************************************
489  * Unfortunatly we cannot work directly with the module's config data as
490  * this module might be unloaded from memory at any time (remember HideModule).
491  * This is why we need to create an exact copy of the config data.
492  *****************************************************************************/
493 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
494                       size_t n )
495 {
496     int j;
497     const module_config_t *p_item, *p_end = p_orig + n;
498
499     /* Calculate the structure length */
500     p_module->i_config_items = 0;
501     p_module->i_bool_items = 0;
502
503     for( p_item = p_orig; p_item < p_end; p_item++ )
504     {
505         if( p_item->i_type & CONFIG_ITEM )
506         {
507             p_module->i_config_items++;
508         }
509
510         if( p_item->i_type == CONFIG_ITEM_BOOL )
511         {
512             p_module->i_bool_items++;
513         }
514     }
515
516     /* Allocate memory */
517     p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
518     if( p_module->p_config == NULL )
519     {
520         msg_Err( p_module, "config error: can't duplicate p_config" );
521         return VLC_ENOMEM;
522     }
523     p_module->confsize = n;
524
525     /* Do the duplication job */
526     for( size_t i = 0; i < n ; i++ )
527     {
528         p_module->p_config[i] = p_orig[i];
529
530         if (IsConfigIntegerType (p_module->p_config[i].i_type))
531         {
532             p_module->p_config[i].orig.i = p_orig[i].value.i;
533             p_module->p_config[i].saved.i = p_orig[i].value.i;
534         }
535         else
536         if (IsConfigFloatType (p_module->p_config[i].i_type))
537         {
538             p_module->p_config[i].orig.f = p_orig[i].value.f;
539             p_module->p_config[i].saved.f = p_orig[i].value.f;
540         }
541         else
542         if (IsConfigStringType (p_module->p_config[i].i_type))
543         {
544             p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
545             p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
546             p_module->p_config[i].saved.psz = NULL;
547         }
548
549         p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
550         p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
551         p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
552         p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
553         p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
554
555         p_module->p_config[i].p_lock = &p_module->object_lock;
556
557         /* duplicate the string list */
558         if( p_orig[i].i_list )
559         {
560             if( p_orig[i].ppsz_list )
561             {
562                 p_module->p_config[i].ppsz_list =
563                     malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
564                 if( p_module->p_config[i].ppsz_list )
565                 {
566                     for( j = 0; j < p_orig[i].i_list; j++ )
567                         p_module->p_config[i].ppsz_list[j] =
568                                 strdupnull (p_orig[i].ppsz_list[j]);
569                     p_module->p_config[i].ppsz_list[j] = NULL;
570                 }
571             }
572             if( p_orig[i].ppsz_list_text )
573             {
574                 p_module->p_config[i].ppsz_list_text =
575                     calloc( (p_orig[i].i_list + 1), sizeof(char *) );
576                 if( p_module->p_config[i].ppsz_list_text )
577                 {
578                     for( j = 0; j < p_orig[i].i_list; j++ )
579                         p_module->p_config[i].ppsz_list_text[j] =
580                                 strdupnull (_(p_orig[i].ppsz_list_text[j]));
581                     p_module->p_config[i].ppsz_list_text[j] = NULL;
582                 }
583             }
584             if( p_orig[i].pi_list )
585             {
586                 p_module->p_config[i].pi_list =
587                     malloc( (p_orig[i].i_list + 1) * sizeof(int) );
588                 if( p_module->p_config[i].pi_list )
589                 {
590                     for( j = 0; j < p_orig[i].i_list; j++ )
591                         p_module->p_config[i].pi_list[j] =
592                             p_orig[i].pi_list[j];
593                 }
594             }
595         }
596
597         /* duplicate the actions list */
598         if( p_orig[i].i_action )
599         {
600             int j;
601
602             p_module->p_config[i].ppf_action =
603                 malloc( p_orig[i].i_action * sizeof(void *) );
604             p_module->p_config[i].ppsz_action_text =
605                 malloc( p_orig[i].i_action * sizeof(char *) );
606
607             for( j = 0; j < p_orig[i].i_action; j++ )
608             {
609                 p_module->p_config[i].ppf_action[j] =
610                     p_orig[i].ppf_action[j];
611                 p_module->p_config[i].ppsz_action_text[j] =
612                     strdupnull (p_orig[i].ppsz_action_text[j]);
613             }
614         }
615
616         p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
617     }
618     return VLC_SUCCESS;
619 }
620
621
622 /*****************************************************************************
623  * config_Free: frees a duplicated module's configuration data.
624  *****************************************************************************
625  * This function frees all the data duplicated by config_Duplicate.
626  *****************************************************************************/
627 void config_Free( module_t *p_module )
628 {
629     int i;
630
631     for (size_t j = 0; j < p_module->confsize; j++)
632     {
633         module_config_t *p_item = p_module->p_config + j;
634
635         free( (char*) p_item->psz_type );
636         free( (char*) p_item->psz_name );
637         free( (char*) p_item->psz_current );
638         free( (char*) p_item->psz_text );
639         free( (char*) p_item->psz_longtext );
640
641         if (IsConfigStringType (p_item->i_type))
642         {
643             free ((char *)p_item->value.psz);
644             free ((char *)p_item->orig.psz);
645             free ((char *)p_item->saved.psz);
646         }
647
648         if( p_item->i_list )
649         {
650             for( i = 0; i < p_item->i_list; i++ )
651             {
652                 if( p_item->ppsz_list && p_item->ppsz_list[i] )
653                     free( (char*) p_item->ppsz_list[i] );
654                 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
655                     free( (char*) p_item->ppsz_list_text[i] );
656             }
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 );
660         }
661
662         if( p_item->i_action )
663         {
664             for( i = 0; i < p_item->i_action; i++ )
665             {
666                 free( (char*) p_item->ppsz_action_text[i] );
667             }
668             if( p_item->ppf_action ) free( p_item->ppf_action );
669             if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
670         }
671     }
672
673     if (p_module->p_config != NULL)
674     {
675         free (p_module->p_config);
676         p_module->p_config = NULL;
677     }
678 }
679
680 /*****************************************************************************
681  * config_SetCallbacks: sets callback functions in the duplicate p_config.
682  *****************************************************************************
683  * Unfortunatly we cannot work directly with the module's config data as
684  * this module might be unloaded from memory at any time (remember HideModule).
685  * This is why we need to duplicate callbacks each time we reload the module.
686  *****************************************************************************/
687 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
688                           size_t n )
689 {
690     for (size_t i = 0; i < n; i++)
691     {
692         p_new->pf_callback = p_orig->pf_callback;
693         p_new++;
694         p_orig++;
695     }
696 }
697
698 /*****************************************************************************
699  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
700  *****************************************************************************
701  * We simply undo what we did in config_SetCallbacks.
702  *****************************************************************************/
703 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
704 {
705     for (size_t i = 0; i < n; i++)
706     {
707         p_new->pf_callback = NULL;
708         p_new++;
709     }
710 }
711
712 /*****************************************************************************
713  * config_ResetAll: reset the configuration data for all the modules.
714  *****************************************************************************/
715 void __config_ResetAll( vlc_object_t *p_this )
716 {
717     int i_index;
718     vlc_list_t *p_list;
719     module_t *p_module;
720
721     /* Acquire config file lock */
722     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
723
724     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
725
726     for( i_index = 0; i_index < p_list->i_count; i_index++ )
727     {
728         p_module = (module_t *)p_list->p_values[i_index].p_object ;
729         if( p_module->b_submodule ) continue;
730
731         for (size_t i = 0; i < p_module->confsize; i++ )
732         {
733             if (IsConfigIntegerType (p_module->p_config[i].i_type))
734                 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
735             else
736             if (IsConfigFloatType (p_module->p_config[i].i_type))
737                 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
738             else
739             if (IsConfigStringType (p_module->p_config[i].i_type))
740             {
741                 free ((char *)p_module->p_config[i].value.psz);
742                 p_module->p_config[i].value.psz =
743                         strdupnull (p_module->p_config[i].orig.psz);
744             }
745         }
746     }
747
748     vlc_list_release( p_list );
749     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
750 }
751
752 /**
753  * config_GetDataDir: find directory where shared data is installed
754  *
755  * @return a string (always succeeds).
756  */
757 const char *config_GetDataDir( void )
758 {
759 #if defined (WIN32) || defined (UNDER_CE)
760     return vlc_global()->psz_vlcpath;
761 #elif defined(__APPLE__) || defined (SYS_BEOS)
762     static char path[PATH_MAX] = "";
763
764     if( *path == '\0' )
765     {
766         snprintf( path, sizeof( path ), "%s/share",
767                   vlc_global()->psz_vlcpath );
768         path[sizeof( path ) - 1] = '\0';
769     }
770     return path;
771 #else
772     return DATA_PATH;
773 #endif
774 }
775
776 /*****************************************************************************
777  * config_GetHomeDir, config_GetUserDir: find the user's home directory.
778  *****************************************************************************
779  * This function will try by different ways to find the user's home path.
780  * Note that this function is not reentrant, it should be called only once
781  * at the beginning of main where the result will be stored for later use.
782  *****************************************************************************/
783 static char *GetDir( vlc_bool_t b_appdata )
784 {
785     const char *psz_localhome = NULL;
786
787 #if defined(WIN32) && !defined(UNDER_CE)
788     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
789                                                LPWSTR );
790 #ifndef CSIDL_FLAG_CREATE
791 #   define CSIDL_FLAG_CREATE 0x8000
792 #endif
793 #ifndef CSIDL_APPDATA
794 #   define CSIDL_APPDATA 0x1A
795 #endif
796 #ifndef CSIDL_PROFILE
797 #   define CSIDL_PROFILE 0x28
798 #endif
799 #ifndef SHGFP_TYPE_CURRENT
800 #   define SHGFP_TYPE_CURRENT 0
801 #endif
802
803     HINSTANCE shfolder_dll;
804     SHGETFOLDERPATH SHGetFolderPath ;
805
806     /* load the shfolder dll to retrieve SHGetFolderPath */
807     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
808     {
809         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
810                                                   _T("SHGetFolderPathW") );
811         if ( SHGetFolderPath != NULL )
812         {
813             wchar_t whomedir[MAX_PATH];
814
815             /* get the "Application Data" folder for the current user */
816             if( S_OK == SHGetFolderPath( NULL,
817                                          (b_appdata ? CSIDL_APPDATA :
818                                            CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
819                                          NULL, SHGFP_TYPE_CURRENT,
820                                          whomedir ) )
821             {
822                 FreeLibrary( shfolder_dll );
823                 return FromWide( whomedir );
824             }
825         }
826         FreeLibrary( shfolder_dll );
827     }
828
829 #elif defined(UNDER_CE)
830
831 #ifndef CSIDL_APPDATA
832 #   define CSIDL_APPDATA 0x1A
833 #endif
834
835     wchar_t whomedir[MAX_PATH];
836
837     /* get the "Application Data" folder for the current user */
838     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
839         return FromWide( whomedir );
840 #endif
841
842     psz_localhome = getenv( "HOME" );
843     if( psz_localhome == NULL )
844     {
845 #if defined(HAVE_GETPWUID)
846         struct passwd *p_pw;
847         (void)b_appdata;
848
849         if( ( p_pw = getpwuid( getuid() ) ) != NULL )
850             psz_localhome = p_pw->pw_dir;
851         else
852 #endif
853         {
854             psz_localhome = getenv( "TMP" );
855             if( psz_localhome == NULL )
856                 psz_localhome = "/tmp";
857         }
858     }
859
860     return FromLocaleDup( psz_localhome );
861 }
862
863 /**
864  * Get the user's home directory
865  */
866 char *config_GetHomeDir( void )
867 {
868     return GetDir( VLC_FALSE );
869 }
870
871 /**
872  * Get the user's main data and config directory:
873  *   - on windows that's the App Data directory;
874  *   - on other OSes it's the same as the home directory.
875  */
876 char *config_GetUserDir( void );
877 char *config_GetUserDir( void )
878 {
879     return GetDir( VLC_TRUE );
880 }
881
882 /**
883  * Get the user's VLC configuration directory
884  */
885 char *config_GetConfigDir( libvlc_int_t *p_libvlc )
886 {
887     char *psz_dir;
888 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
889     char *psz_parent = config_GetUserDir();
890     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
891     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
892         return NULL;
893     return psz_dir;
894 #else
895     /* XDG Base Directory Specification - Version 0.6 */
896     char *psz_env = getenv( "XDG_CONFIG_HOME" );
897     if( psz_env )
898     {
899         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
900             return NULL;
901         return psz_dir;
902     }
903     psz_env = getenv( "HOME" );
904     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
905     if( asprintf( &psz_dir, "%s/.config/vlc", psz_env ) == -1 )
906         return NULL;
907     return psz_dir;
908 #endif
909 }
910
911 /**
912  * Get the user's VLC data directory
913  * (used for stuff like the skins, custom lua modules, ...)
914  */
915 char *config_GetUserDataDir( libvlc_int_t *p_libvlc )
916 {
917     char *psz_dir;
918 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
919     char *psz_parent = config_GetUserDir();
920     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
921     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
922         return NULL;
923     return psz_dir;
924 #else
925     /* XDG Base Directory Specification - Version 0.6 */
926     char *psz_env = getenv( "XDG_DATA_HOME" );
927     if( psz_env )
928     {
929         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
930             return NULL;
931         return psz_dir;
932     }
933     psz_env = getenv( "HOME" );
934     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
935     if( asprintf( &psz_dir, "%s/.local/share/vlc", psz_env ) == -1 )
936         return NULL;
937     return psz_dir;
938 #endif
939 }
940
941 /**
942  * Get the user's VLC cache directory
943  * (used for stuff like the modules cache, the album art cache, ...)
944  */
945 char *config_GetCacheDir( libvlc_int_t *p_libvlc )
946 {
947     char *psz_dir;
948 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
949     char *psz_parent = config_GetUserDir();
950     if( !psz_parent ) psz_parent = p_libvlc->psz_homedir;
951     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
952         return NULL;
953     return psz_dir;
954 #else
955     /* XDG Base Directory Specification - Version 0.6 */
956     char *psz_env = getenv( "XDG_CACHE_HOME" );
957     if( psz_env )
958     {
959         if( asprintf( &psz_dir, "%s/vlc", psz_env ) == -1 )
960             return NULL;
961         return psz_dir;
962     }
963     psz_env = getenv( "HOME" );
964     if( !psz_env ) psz_env = p_libvlc->psz_homedir; /* not part of XDG spec but we want a sensible fallback */
965     if( asprintf( &psz_dir, "%s/.cache/vlc", psz_env ) == -1 )
966         return NULL;
967     return psz_dir;
968 #endif
969 }
970
971 /* Adds an extra interface to the configuration */
972 void __config_AddIntf( vlc_object_t *p_this, const char *psz_intf )
973 {
974     assert( psz_intf );
975
976     char *psz_config, *psz_parser;
977     size_t i_len = strlen( psz_intf );
978
979     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
980     while( psz_parser )
981     {
982         if( !strncmp( psz_intf, psz_parser, i_len ) )
983         {
984             free( psz_config );
985             return;
986         }
987         psz_parser = strchr( psz_parser, ':' );
988         if( psz_parser ) psz_parser++; /* skip the ':' */
989     }
990     free( psz_config );
991
992     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
993     while( psz_parser )
994     {
995         if( !strncmp( psz_intf, psz_parser, i_len ) )
996         {
997             free( psz_config );
998             return;
999         }
1000         psz_parser = strchr( psz_parser, ':' );
1001         if( psz_parser ) psz_parser++; /* skip the ':' */
1002     }
1003
1004     /* interface not found in the config, let's add it */
1005     if( psz_config && strlen( psz_config ) > 0 )
1006     {
1007         char *psz_newconfig;
1008         if( asprintf( &psz_newconfig, "%s:%s", psz_config, psz_intf ) != -1 )
1009         {
1010             config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1011             free( psz_newconfig );
1012         }
1013     }
1014     else
1015         config_PutPsz( p_this->p_libvlc, "extraintf", psz_intf );
1016
1017     free( psz_config );
1018 }
1019
1020 /* Removes an extra interface from the configuration */
1021 void __config_RemoveIntf( vlc_object_t *p_this, const char *psz_intf )
1022 {
1023     assert( psz_intf );
1024
1025     char *psz_config, *psz_parser;
1026     size_t i_len = strlen( psz_intf );
1027
1028     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1029     while( psz_parser )
1030     {
1031         if( !strncmp( psz_intf, psz_parser, i_len ) )
1032         {
1033             char *psz_newconfig;
1034             char *psz_end = psz_parser + i_len;
1035             if( *psz_end == ':' ) psz_end++;
1036             *psz_parser = '\0';
1037             if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1038             {
1039                 config_PutPsz( p_this->p_libvlc, "extraintf", psz_newconfig );
1040                 free( psz_newconfig );
1041             }
1042             break;
1043         }
1044         psz_parser = strchr( psz_parser, ':' );
1045         if( psz_parser ) psz_parser++; /* skip the ':' */
1046     }
1047     free( psz_config );
1048
1049     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1050     while( psz_parser )
1051     {
1052         if( !strncmp( psz_intf, psz_parser, i_len ) )
1053         {
1054             char *psz_newconfig;
1055             char *psz_end = psz_parser + i_len;
1056             if( *psz_end == ':' ) psz_end++;
1057             *psz_parser = '\0';
1058             if( asprintf( &psz_newconfig, "%s%s", psz_config, psz_end ) != -1 )
1059             {
1060                 config_PutPsz( p_this->p_libvlc, "control", psz_newconfig );
1061                 free( psz_newconfig );
1062             }
1063             break;
1064         }
1065         psz_parser = strchr( psz_parser, ':' );
1066         if( psz_parser ) psz_parser++; /* skip the ':' */
1067     }
1068     free( psz_config );
1069 }
1070
1071 /*
1072  * Returns VLC_TRUE if the specified extra interface is present in the
1073  * configuration, VLC_FALSE if not
1074  */
1075 vlc_bool_t __config_ExistIntf( vlc_object_t *p_this, const char *psz_intf )
1076 {
1077     assert( psz_intf );
1078
1079     char *psz_config, *psz_parser;
1080     size_t i_len = strlen( psz_intf );
1081
1082     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "extraintf" );
1083     while( psz_parser )
1084     {
1085         if( !strncmp( psz_parser, psz_intf, i_len ) )
1086         {
1087             free( psz_config );
1088             return VLC_TRUE;
1089         }
1090         psz_parser = strchr( psz_parser, ':' );
1091         if( psz_parser ) psz_parser++; /* skip the ':' */
1092     }
1093     free( psz_config );
1094
1095     psz_config = psz_parser = config_GetPsz( p_this->p_libvlc, "control" );
1096     while( psz_parser )
1097     {
1098         if( !strncmp( psz_parser, psz_intf, i_len ) )
1099         {
1100             free( psz_config );
1101             return VLC_TRUE;
1102         }
1103         psz_parser = strchr( psz_parser, ':' );
1104         if( psz_parser ) psz_parser++; /* skip the ':' */
1105     }
1106     free( psz_config );
1107
1108     return VLC_FALSE;
1109 }
1110