]> git.sesse.net Git - vlc/blob - src/config/core.c
611a852d745c257ac8c4b1dcab46bf3cf4c09c78
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #if defined( WIN32 )
29 #   if !defined( UNDER_CE )
30 #       define _WIN32_IE IE5
31 #       include <w32api.h>
32 #       include <direct.h>
33 #       include <shlobj.h>
34 #   endif
35 #   include <tchar.h>
36 #endif
37
38 #include <vlc/vlc.h>
39 #include "../libvlc.h"
40 #include "vlc_keys.h"
41 #include "vlc_charset.h"
42 #include "vlc_configuration.h"
43
44 #include <errno.h>                                                  /* errno */
45 #include <assert.h>
46 #include <limits.h>
47
48 #ifdef HAVE_UNISTD_H
49 #    include <unistd.h>                                          /* getuid() */
50 #endif
51
52 #if defined(HAVE_GETPWUID_R)
53 #   include <pwd.h>
54 #endif
55
56 #if defined( HAVE_SYS_STAT_H )
57 #   include <sys/stat.h>
58 #endif
59 #if defined( HAVE_SYS_TYPES_H )
60 #   include <sys/types.h>
61 #endif
62
63 #include "configuration.h"
64 #include "modules/modules.h"
65
66 static inline char *strdupnull (const char *src)
67 {
68     return src ? strdup (src) : NULL;
69 }
70
71 /* Item types that use a string value (i.e. serialized in the module cache) */
72 int IsConfigStringType (int type)
73 {
74     static const unsigned char config_types[] =
75     {
76         CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
77         CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
78         CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT
79     };
80
81     /* NOTE: this needs to be changed if we ever get more than 255 types */
82     return memchr (config_types, type, sizeof (config_types)) != NULL;
83 }
84
85
86 int IsConfigIntegerType (int type)
87 {
88     static const unsigned char config_types[] =
89     {
90         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
91         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
92     };
93
94     return memchr (config_types, type, sizeof (config_types)) != NULL;
95 }
96
97
98 /*****************************************************************************
99  * config_GetType: get the type of a variable (bool, int, float, string)
100  *****************************************************************************
101  * This function is used to get the type of a variable from its name.
102  * Beware, this is quite slow.
103  *****************************************************************************/
104 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
105 {
106     module_config_t *p_config;
107     int i_type;
108
109     p_config = config_FindConfig( p_this, psz_name );
110
111     /* sanity checks */
112     if( !p_config )
113     {
114         return 0;
115     }
116
117     switch( p_config->i_type )
118     {
119     case CONFIG_ITEM_BOOL:
120         i_type = VLC_VAR_BOOL;
121         break;
122
123     case CONFIG_ITEM_INTEGER:
124     case CONFIG_ITEM_KEY:
125         i_type = VLC_VAR_INTEGER;
126         break;
127
128     case CONFIG_ITEM_FLOAT:
129         i_type = VLC_VAR_FLOAT;
130         break;
131
132     case CONFIG_ITEM_MODULE:
133     case CONFIG_ITEM_MODULE_CAT:
134     case CONFIG_ITEM_MODULE_LIST:
135     case CONFIG_ITEM_MODULE_LIST_CAT:
136         i_type = VLC_VAR_MODULE;
137         break;
138
139     case CONFIG_ITEM_STRING:
140         i_type = VLC_VAR_STRING;
141         break;
142
143     case CONFIG_ITEM_PASSWORD:
144         i_type = VLC_VAR_STRING;
145         break;
146
147     case CONFIG_ITEM_FILE:
148         i_type = VLC_VAR_FILE;
149         break;
150
151     case CONFIG_ITEM_DIRECTORY:
152         i_type = VLC_VAR_DIRECTORY;
153         break;
154
155     default:
156         i_type = 0;
157         break;
158     }
159
160     return i_type;
161 }
162
163 /*****************************************************************************
164  * config_GetInt: get the value of an int variable
165  *****************************************************************************
166  * This function is used to get the value of variables which are internally
167  * represented by an integer (CONFIG_ITEM_INTEGER and
168  * CONFIG_ITEM_BOOL).
169  *****************************************************************************/
170 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
171 {
172     module_config_t *p_config;
173
174     p_config = config_FindConfig( p_this, psz_name );
175
176     /* sanity checks */
177     if( !p_config )
178     {
179         msg_Err( p_this, "option %s does not exist", psz_name );
180         return -1;
181     }
182
183     if (!IsConfigIntegerType (p_config->i_type))
184     {
185         msg_Err( p_this, "option %s does not refer to an int", psz_name );
186         return -1;
187     }
188
189     return p_config->value.i;
190 }
191
192 /*****************************************************************************
193  * config_GetFloat: get the value of a float variable
194  *****************************************************************************
195  * This function is used to get the value of variables which are internally
196  * represented by a float (CONFIG_ITEM_FLOAT).
197  *****************************************************************************/
198 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
199 {
200     module_config_t *p_config;
201
202     p_config = config_FindConfig( p_this, psz_name );
203
204     /* sanity checks */
205     if( !p_config )
206     {
207         msg_Err( p_this, "option %s does not exist", psz_name );
208         return -1;
209     }
210
211     if (!IsConfigFloatType (p_config->i_type))
212     {
213         msg_Err( p_this, "option %s does not refer to a float", psz_name );
214         return -1;
215     }
216
217     return p_config->value.f;
218 }
219
220 /*****************************************************************************
221  * config_GetPsz: get the string value of a string variable
222  *****************************************************************************
223  * This function is used to get the value of variables which are internally
224  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
225  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
226  *
227  * Important note: remember to free() the returned char* because it's a
228  *   duplicate of the actual value. It isn't safe to return a pointer to the
229  *   actual value as it can be modified at any time.
230  *****************************************************************************/
231 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
232 {
233     module_config_t *p_config;
234
235     p_config = config_FindConfig( p_this, psz_name );
236
237     /* sanity checks */
238     if( !p_config )
239     {
240         msg_Err( p_this, "option %s does not exist", psz_name );
241         return NULL;
242     }
243
244     if (!IsConfigStringType (p_config->i_type))
245     {
246         msg_Err( p_this, "option %s does not refer to a string", psz_name );
247         return NULL;
248     }
249
250     /* return a copy of the string */
251     vlc_mutex_lock( p_config->p_lock );
252     char *psz_value = strdupnull (p_config->value.psz);
253     vlc_mutex_unlock( p_config->p_lock );
254
255     return psz_value;
256 }
257
258 /*****************************************************************************
259  * config_PutPsz: set the string value of a string variable
260  *****************************************************************************
261  * This function is used to set the value of variables which are internally
262  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
263  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
264  *****************************************************************************/
265 void __config_PutPsz( vlc_object_t *p_this,
266                       const char *psz_name, const char *psz_value )
267 {
268     module_config_t *p_config;
269     vlc_value_t oldval, val;
270
271     p_config = config_FindConfig( p_this, psz_name );
272
273
274     /* sanity checks */
275     if( !p_config )
276     {
277         msg_Warn( p_this, "option %s does not exist", psz_name );
278         return;
279     }
280
281     if (!IsConfigStringType (p_config->i_type))
282     {
283         msg_Err( p_this, "option %s does not refer to a string", psz_name );
284         return;
285     }
286
287     vlc_mutex_lock( p_config->p_lock );
288
289     /* backup old value */
290     oldval.psz_string = (char *)p_config->value.psz;
291
292     if ((psz_value != NULL) && *psz_value)
293         p_config->value.psz = strdup (psz_value);
294     else
295         p_config->value.psz = NULL;
296
297     p_config->b_dirty = true;
298
299     val.psz_string = (char *)p_config->value.psz;
300
301     vlc_mutex_unlock( p_config->p_lock );
302
303     if( p_config->pf_callback )
304     {
305         p_config->pf_callback( p_this, psz_name, oldval, val,
306                                p_config->p_callback_data );
307     }
308
309     /* free old string */
310     free( oldval.psz_string );
311 }
312
313 /*****************************************************************************
314  * config_PutInt: set the integer value of an int variable
315  *****************************************************************************
316  * This function is used to set the value of variables which are internally
317  * represented by an integer (CONFIG_ITEM_INTEGER and
318  * CONFIG_ITEM_BOOL).
319  *****************************************************************************/
320 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
321 {
322     module_config_t *p_config;
323     vlc_value_t oldval, val;
324
325     p_config = config_FindConfig( p_this, psz_name );
326
327     /* sanity checks */
328     if( !p_config )
329     {
330         msg_Warn( p_this, "option %s does not exist", psz_name );
331         return;
332     }
333
334     if (!IsConfigIntegerType (p_config->i_type))
335     {
336         msg_Err( p_this, "option %s does not refer to an int", psz_name );
337         return;
338     }
339
340     /* backup old value */
341     oldval.i_int = p_config->value.i;
342
343     /* if i_min == i_max == 0, then do not use them */
344     if ((p_config->min.i == 0) && (p_config->max.i == 0))
345     {
346         p_config->value.i = i_value;
347     }
348     else if (i_value < p_config->min.i)
349     {
350         p_config->value.i = p_config->min.i;
351     }
352     else if (i_value > p_config->max.i)
353     {
354         p_config->value.i = p_config->max.i;
355     }
356     else
357     {
358         p_config->value.i = i_value;
359     }
360
361     p_config->b_dirty = true;
362
363     val.i_int = p_config->value.i;
364
365     if( p_config->pf_callback )
366     {
367         p_config->pf_callback( p_this, psz_name, oldval, val,
368                                p_config->p_callback_data );
369     }
370 }
371
372 /*****************************************************************************
373  * config_PutFloat: set the value of a float variable
374  *****************************************************************************
375  * This function is used to set the value of variables which are internally
376  * represented by a float (CONFIG_ITEM_FLOAT).
377  *****************************************************************************/
378 void __config_PutFloat( vlc_object_t *p_this,
379                         const char *psz_name, float f_value )
380 {
381     module_config_t *p_config;
382     vlc_value_t oldval, val;
383
384     p_config = config_FindConfig( p_this, psz_name );
385
386     /* sanity checks */
387     if( !p_config )
388     {
389         msg_Warn( p_this, "option %s does not exist", psz_name );
390         return;
391     }
392
393     if (!IsConfigFloatType (p_config->i_type))
394     {
395         msg_Err( p_this, "option %s does not refer to a float", psz_name );
396         return;
397     }
398
399     /* backup old value */
400     oldval.f_float = p_config->value.f;
401
402     /* if f_min == f_max == 0, then do not use them */
403     if ((p_config->min.f == 0) && (p_config->max.f == 0))
404     {
405         p_config->value.f = f_value;
406     }
407     else if (f_value < p_config->min.f)
408     {
409         p_config->value.f = p_config->min.f;
410     }
411     else if (f_value > p_config->max.f)
412     {
413         p_config->value.f = p_config->max.f;
414     }
415     else
416     {
417         p_config->value.f = f_value;
418     }
419
420     p_config->b_dirty = true;
421
422     val.f_float = p_config->value.f;
423
424     if( p_config->pf_callback )
425     {
426         p_config->pf_callback( p_this, psz_name, oldval, val,
427                                p_config->p_callback_data );
428     }
429 }
430
431 /*****************************************************************************
432  * config_FindConfig: find the config structure associated with an option.
433  *****************************************************************************
434  * FIXME: This function really needs to be optimized.
435  * FIXME: And now even more.
436  *****************************************************************************/
437 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
438 {
439     vlc_list_t *p_list;
440     int i_index;
441
442     if( !psz_name ) return NULL;
443
444     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
445
446     for( i_index = 0; i_index < p_list->i_count; i_index++ )
447     {
448         module_config_t *p_item, *p_end;
449         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
450
451         if( !p_parser->i_config_items )
452             continue;
453
454         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
455              p_item < p_end;
456              p_item++ )
457         {
458             if( p_item->i_type & CONFIG_HINT )
459                 /* ignore hints */
460                 continue;
461             if( !strcmp( psz_name, p_item->psz_name )
462              || ( p_item->psz_oldname
463               && !strcmp( psz_name, p_item->psz_oldname ) ) )
464             {
465                 vlc_list_release( p_list );
466                 return p_item;
467             }
468         }
469     }
470
471     vlc_list_release( p_list );
472
473     return NULL;
474 }
475
476 /*****************************************************************************
477  * config_Free: frees a duplicated module's configuration data.
478  *****************************************************************************
479  * This function frees all the data duplicated by config_Duplicate.
480  *****************************************************************************/
481 void config_Free( module_t *p_module )
482 {
483     int i;
484
485     for (size_t j = 0; j < p_module->confsize; j++)
486     {
487         module_config_t *p_item = p_module->p_config + j;
488
489         free( p_item->psz_type );
490         free( p_item->psz_name );
491         free( p_item->psz_text );
492         free( p_item->psz_longtext );
493         free( p_item->psz_oldname );
494
495         if (IsConfigStringType (p_item->i_type))
496         {
497             free (p_item->value.psz);
498             free (p_item->orig.psz);
499             free (p_item->saved.psz);
500         }
501
502         if( p_item->ppsz_list )
503             for( i = 0; i < p_item->i_list; i++ )
504                 free( p_item->ppsz_list[i] );
505         if( p_item->ppsz_list_text )
506             for( i = 0; i < p_item->i_list; i++ )
507                 free( p_item->ppsz_list_text[i] );
508         free( p_item->ppsz_list );
509         free( p_item->ppsz_list_text );
510         free( p_item->pi_list );
511
512         if( p_item->i_action )
513         {
514             for( i = 0; i < p_item->i_action; i++ )
515             {
516                 free( p_item->ppsz_action_text[i] );
517             }
518             free( p_item->ppf_action );
519             free( p_item->ppsz_action_text );
520         }
521     }
522
523     free (p_module->p_config);
524     p_module->p_config = NULL;
525 }
526
527 /*****************************************************************************
528  * config_SetCallbacks: sets callback functions in the duplicate p_config.
529  *****************************************************************************
530  * Unfortunatly we cannot work directly with the module's config data as
531  * this module might be unloaded from memory at any time (remember HideModule).
532  * This is why we need to duplicate callbacks each time we reload the module.
533  *****************************************************************************/
534 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
535                           size_t n )
536 {
537     for (size_t i = 0; i < n; i++)
538     {
539         p_new->pf_callback = p_orig->pf_callback;
540         p_new++;
541         p_orig++;
542     }
543 }
544
545 /*****************************************************************************
546  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
547  *****************************************************************************
548  * We simply undo what we did in config_SetCallbacks.
549  *****************************************************************************/
550 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
551 {
552     for (size_t i = 0; i < n; i++)
553     {
554         p_new->pf_callback = NULL;
555         p_new++;
556     }
557 }
558
559 /*****************************************************************************
560  * config_ResetAll: reset the configuration data for all the modules.
561  *****************************************************************************/
562 void __config_ResetAll( vlc_object_t *p_this )
563 {
564     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
565     int i_index;
566     vlc_list_t *p_list;
567     module_t *p_module;
568
569     /* Acquire config file lock */
570     vlc_mutex_lock( &priv->config_lock );
571
572     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
573
574     for( i_index = 0; i_index < p_list->i_count; i_index++ )
575     {
576         p_module = (module_t *)p_list->p_values[i_index].p_object ;
577         if( p_module->b_submodule ) continue;
578
579         for (size_t i = 0; i < p_module->confsize; i++ )
580         {
581             if (IsConfigIntegerType (p_module->p_config[i].i_type))
582                 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
583             else
584             if (IsConfigFloatType (p_module->p_config[i].i_type))
585                 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
586             else
587             if (IsConfigStringType (p_module->p_config[i].i_type))
588             {
589                 free ((char *)p_module->p_config[i].value.psz);
590                 p_module->p_config[i].value.psz =
591                         strdupnull (p_module->p_config[i].orig.psz);
592             }
593         }
594     }
595
596     vlc_list_release( p_list );
597     vlc_mutex_unlock( &priv->config_lock );
598 }
599
600 /**
601  * config_GetDataDir: find directory where shared data is installed
602  *
603  * @return a string (always succeeds).
604  */
605 const char *config_GetDataDir( void )
606 {
607 #if defined (WIN32) || defined (UNDER_CE)
608     return vlc_global()->psz_vlcpath;
609 #elif defined(__APPLE__) || defined (SYS_BEOS)
610     static char path[PATH_MAX] = "";
611
612     if( *path == '\0' )
613     {
614         snprintf( path, sizeof( path ), "%s/share",
615                   vlc_global()->psz_vlcpath );
616         path[sizeof( path ) - 1] = '\0';
617     }
618     return path;
619 #else
620     return DATA_PATH;
621 #endif
622 }
623
624 static char *GetDir( bool b_appdata )
625 {
626     const char *psz_localhome = NULL;
627
628 #if defined(WIN32) && !defined(UNDER_CE)
629     wchar_t whomedir[MAX_PATH];
630     /* Get the "Application Data" folder for the current user */
631     if( S_OK == SHGetFolderPathW( NULL,
632               (b_appdata ? CSIDL_APPDATA : CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
633                                   NULL, SHGFP_TYPE_CURRENT, whomedir ) )
634         return FromWide( whomedir );
635
636 #elif defined(UNDER_CE)
637     (void)b_appdata;
638 #ifndef CSIDL_APPDATA
639 #   define CSIDL_APPDATA 0x1A
640 #endif
641
642     wchar_t whomedir[MAX_PATH];
643
644     /* get the "Application Data" folder for the current user */
645     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
646         return FromWide( whomedir );
647 #else
648     (void)b_appdata;
649 #endif
650
651     psz_localhome = getenv( "HOME" );
652 #if defined(HAVE_GETPWUID_R)
653     char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
654     if( psz_localhome == NULL )
655     {
656         struct passwd pw, *res;
657
658         if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
659             psz_localhome = pw.pw_dir;
660     }
661 #endif
662     if (psz_localhome == NULL)
663         psz_localhome = getenv( "TMP" );
664     if (psz_localhome == NULL)
665         psz_localhome = "/tmp";
666
667     return FromLocaleDup( psz_localhome );
668 }
669
670 /**
671  * Get the user's home directory
672  */
673 char *config_GetHomeDir( void )
674 {
675     return GetDir( false );
676 }
677
678 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
679 {
680     char *psz_dir;
681 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
682     char *psz_parent = GetDir (true);
683
684     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
685         psz_dir = NULL;
686
687     free (psz_parent);
688     (void)xdg_name; (void)xdg_default;
689 #else
690     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
691
692     /* XDG Base Directory Specification - Version 0.6 */
693     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
694     char *psz_home = getenv( var );
695     psz_home = psz_home ? FromLocaleDup( psz_home ) : NULL;
696     if( psz_home )
697     {
698         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
699             psz_dir = NULL;
700         goto out;
701     }
702
703     /* Try HOME, then fallback to non-XDG dirs */
704     psz_home = config_GetHomeDir();
705     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
706         psz_dir = NULL;
707
708 out:
709     free (psz_home);
710 #endif
711     return psz_dir;
712 }
713
714 /**
715  * Get the user's VLC configuration directory
716  */
717 char *config_GetConfigDir( void )
718 {
719     return config_GetFooDir ("CONFIG", ".config");
720 }
721
722 /**
723  * Get the user's VLC data directory
724  * (used for stuff like the skins, custom lua modules, ...)
725  */
726 char *config_GetUserDataDir( void )
727 {
728     return config_GetFooDir ("DATA", ".local/share");
729 }
730
731 /**
732  * Get the user's VLC cache directory
733  * (used for stuff like the modules cache, the album art cache, ...)
734  */
735 char *config_GetCacheDir( void )
736 {
737     return config_GetFooDir ("CACHE", ".cache");
738 }