]> git.sesse.net Git - vlc/blobdiff - src/misc/configuration.c
* ./src/playlist/playlist.c: don't run the playlist by default.
[vlc] / src / misc / configuration.c
index 160550e9591bdd9e6fc291610655ed5b8ea341e2..696ec872f09e3627f55463dd495a323394104cad 100644 (file)
@@ -2,7 +2,7 @@
  * configuration.c management of the modules configuration
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: configuration.c,v 1.34 2002/08/07 00:29:37 sam Exp $
+ * $Id: configuration.c,v 1.39 2002/09/29 18:19:53 sam Exp $
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  *
@@ -37,7 +37,7 @@
 #       include <getopt.h>                                       /* getopt() */
 #   endif
 #else
-#   include "GNUgetopt/getopt.h"
+#   include "extras/GNUgetopt/getopt.h"
 #endif
 
 #if defined(HAVE_GETPWUID)
@@ -258,19 +258,26 @@ void __config_PutFloat( vlc_object_t *p_this,
  * config_FindConfig: find the config structure associated with an option.
  *****************************************************************************
  * FIXME: This function really needs to be optimized.
+ * FIXME: And now even more.
  *****************************************************************************/
-module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name)
+module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
 {
-    module_t *p_module;
+    vlc_list_t *p_list; 
+    module_t **pp_parser;
     module_config_t *p_item;
 
     if( !psz_name ) return NULL;
 
-    for( p_module = p_this->p_vlc->p_module_bank->first ;
-         p_module != NULL ;
-         p_module = p_module->next )
+    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
+    for( pp_parser = (module_t **)p_list->pp_objects ;
+         *pp_parser ;
+         pp_parser++ )
     {
-        for( p_item = p_module->p_config;
+        if( !(*pp_parser)->i_config_items )
+            continue;
+
+        for( p_item = (*pp_parser)->p_config;
              p_item->i_type != CONFIG_HINT_END;
              p_item++ )
         {
@@ -278,10 +285,15 @@ module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name)
                 /* ignore hints */
                 continue;
             if( !strcmp( psz_name, p_item->psz_name ) )
+            {
+                vlc_list_release( p_list );
                 return p_item;
+            }
         }
     }
 
+    vlc_list_release( p_list );
+
     return NULL;
 }
 
@@ -325,9 +337,6 @@ void config_Duplicate( module_t *p_module, module_config_t *p_orig )
         return;
     }
 
-    /* Initialize the global lock */
-    vlc_mutex_init( p_module, &p_module->object_lock );
-
     /* Do the duplication job */
     for( i = 0; i < i_lines ; i++ )
     {
@@ -365,9 +374,7 @@ void config_Duplicate( module_t *p_module, module_config_t *p_orig )
             p_module->p_config[i].ppsz_list[j] = NULL;
         }
 
-        /* the callback pointer is only valid when the module is loaded so this
-         * value is set in module_activate() and reset in module_deactivate() */
-        p_module->p_config[i].pf_callback = NULL;
+        p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
     }
 }
 
@@ -381,6 +388,11 @@ void config_Free( module_t *p_module )
     module_config_t *p_item = p_module->p_config;
     int i;
 
+    if( p_item == NULL )
+    {
+        return;
+    }
+
     for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
     {
         if( p_item->psz_type )
@@ -408,9 +420,6 @@ void config_Free( module_t *p_module )
 
     free( p_module->p_config );
     p_module->p_config = NULL;
-
-    /* Remove the global lock */
-    vlc_mutex_destroy( &p_module->object_lock );
 }
 
 /*****************************************************************************
@@ -452,21 +461,18 @@ void config_UnsetCallbacks( module_config_t *p_new )
  *****************************************************************************/
 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 {
-    module_t *p_module;
+    vlc_list_t *p_list; 
+    module_t **pp_parser;
     module_config_t *p_item;
     FILE *file;
     char line[1024];
     char *p_index, *psz_option_name, *psz_option_value;
     char *psz_filename, *psz_homedir;
 
-    /* Acquire config file lock */
-    vlc_mutex_lock( &p_this->p_vlc->config_lock );
-
     psz_homedir = p_this->p_vlc->psz_homedir;
     if( !psz_homedir )
     {
         msg_Err( p_this, "psz_homedir is null" );
-        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
         return -1;
     }
     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
@@ -474,13 +480,15 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     if( !psz_filename )
     {
         msg_Err( p_this, "out of memory" );
-        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
         return -1;
     }
     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
 
     msg_Dbg( p_this, "opening config file %s", psz_filename );
 
+    /* Acquire config file lock */
+    vlc_mutex_lock( &p_this->p_vlc->config_lock );
+
     file = fopen( psz_filename, "rt" );
     if( !file )
     {
@@ -491,12 +499,15 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     }
 
     /* Look for the selected module, if NULL then save everything */
-    for( p_module = p_this->p_vlc->p_module_bank->first ; p_module != NULL ;
-         p_module = p_module->next )
+    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
+    for( pp_parser = (module_t **)p_list->pp_objects ;
+         *pp_parser ;
+         pp_parser++ )
     {
 
         if( psz_module_name
-             && strcmp( psz_module_name, p_module->psz_object_name ) )
+             && strcmp( psz_module_name, (*pp_parser)->psz_object_name ) )
         {
             continue;
         }
@@ -506,13 +517,16 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         rewind( file );
         while( fgets( line, 1024, file ) )
         {
-            if( (line[0] == '[') && (p_index = strchr(line,']')) &&
-                (p_index - &line[1] == strlen(p_module->psz_object_name) ) &&
-                !memcmp( &line[1], p_module->psz_object_name,
-                         strlen(p_module->psz_object_name) ) )
+            if( (line[0] == '[')
+               && (p_index = strchr(line,']'))
+               && (p_index - &line[1] == strlen((*pp_parser)->psz_object_name))
+               && !memcmp( &line[1], (*pp_parser)->psz_object_name,
+                           strlen((*pp_parser)->psz_object_name) ) )
             {
-                //msg_Dbg( p_this, "loading config for module \"%s\"",
-                //                 p_module->psz_object_name );
+#if 0
+                msg_Dbg( p_this, "loading config for module \"%s\"",
+                                 (*pp_parser)->psz_object_name );
+#endif
 
                 break;
             }
@@ -541,8 +555,13 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
             *p_index = (char)0;
             psz_option_value = p_index + 1;
 
+            if( !(*pp_parser)->i_config_items )
+            {
+                continue;
+            }
+
             /* try to match this option with one of the module's options */
-            for( p_item = p_module->p_config;
+            for( p_item = (*pp_parser)->p_config;
                  p_item->i_type != CONFIG_HINT_END;
                  p_item++ )
             {
@@ -560,16 +579,20 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
                         if( !*psz_option_value )
                             break;                    /* ignore empty option */
                         p_item->i_value = atoi( psz_option_value);
-                        //msg_Dbg( p_this, "option \"%s\", value %i",
-                        //         p_item->psz_name, p_item->i_value );
+#if 0
+                        msg_Dbg( p_this, "option \"%s\", value %i",
+                                 p_item->psz_name, p_item->i_value );
+#endif
                         break;
 
                     case CONFIG_ITEM_FLOAT:
                         if( !*psz_option_value )
                             break;                    /* ignore empty option */
                         p_item->f_value = (float)atof( psz_option_value);
-                        //msg_Dbg( p_this, "option \"%s\", value %f",
-                        //         p_item->psz_name, (double)p_item->f_value );
+#if O
+                        msg_Dbg( p_this, "option \"%s\", value %f",
+                                 p_item->psz_name, (double)p_item->f_value );
+#endif
                         break;
 
                     default:
@@ -584,9 +607,11 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 
                         vlc_mutex_unlock( p_item->p_lock );
 
-                        //msg_Dbg( p_this, "option \"%s\", value \"%s\"",
-                        //         p_item->psz_name,
-                        //         p_item->psz_value ? p_item->psz_value : "" );
+#if 0
+                        msg_Dbg( p_this, "option \"%s\", value \"%s\"",
+                                 p_item->psz_name,
+                                 p_item->psz_value ? p_item->psz_value : "" );
+#endif
                         break;
                     }
                 }
@@ -595,6 +620,8 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 
     }
     
+    vlc_list_release( p_list );
+
     fclose( file );
     free( psz_filename );
 
@@ -624,7 +651,8 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
  *****************************************************************************/
 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 {
-    module_t *p_module;
+    module_t **pp_parser;
+    vlc_list_t *p_list;
     module_config_t *p_item;
     FILE *file;
     char p_line[1024], *p_index2;
@@ -692,6 +720,9 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     }
     p_bigbuffer[0] = 0;
 
+    /* List all available modules */
+    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
     /* backup file into memory, we only need to backup the sections we won't
      * save later on */
     b_backup = 0;
@@ -699,25 +730,26 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     {
         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
         {
+
             /* we found a section, check if we need to do a backup */
-            for( p_module = p_this->p_vlc->p_module_bank->first;
-                 p_module != NULL;
-                 p_module = p_module->next )
+            for( pp_parser = (module_t **)p_list->pp_objects ;
+                 *pp_parser ;
+                 pp_parser++ )
             {
                 if( ((p_index2 - &p_line[1])
-                       == strlen(p_module->psz_object_name) ) &&
-                    !memcmp( &p_line[1], p_module->psz_object_name,
-                             strlen(p_module->psz_object_name) ) )
+                       == strlen((*pp_parser)->psz_object_name) ) &&
+                    !memcmp( &p_line[1], (*pp_parser)->psz_object_name,
+                             strlen((*pp_parser)->psz_object_name) ) )
                 {
                     if( !psz_module_name )
                         break;
                     else if( !strcmp( psz_module_name,
-                                      p_module->psz_object_name ) )
+                                      (*pp_parser)->psz_object_name ) )
                         break;
                 }
             }
 
-            if( !p_module )
+            if( !(*pp_parser) )
             {
                 /* we don't have this section in our list so we need to back
                  * it up */
@@ -756,6 +788,7 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         msg_Warn( p_this, "could not open config file %s for writing",
                           psz_filename );
         free( psz_filename );
+        vlc_list_release( p_list );
         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
         return -1;
     }
@@ -763,27 +796,28 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
 
     /* Look for the selected module, if NULL then save everything */
-    for( p_module = p_this->p_vlc->p_module_bank->first ; p_module != NULL ;
-         p_module = p_module->next )
+    for( pp_parser = (module_t **)p_list->pp_objects ;
+         *pp_parser ;
+         pp_parser++ )
     {
 
         if( psz_module_name && strcmp( psz_module_name,
-                                       p_module->psz_object_name ) )
+                                       (*pp_parser)->psz_object_name ) )
             continue;
 
-        if( !p_module->i_config_items )
+        if( !(*pp_parser)->i_config_items )
             continue;
 
         msg_Dbg( p_this, "saving config for module \"%s\"",
-                         p_module->psz_object_name );
+                         (*pp_parser)->psz_object_name );
 
-        fprintf( file, "[%s]", p_module->psz_object_name );
-        if( p_module->psz_longname )
-            fprintf( file, " # %s\n\n", p_module->psz_longname );
+        fprintf( file, "[%s]", (*pp_parser)->psz_object_name );
+        if( (*pp_parser)->psz_longname )
+            fprintf( file, " # %s\n\n", (*pp_parser)->psz_longname );
         else
             fprintf( file, "\n\n" );
 
-        for( p_item = p_module->p_config;
+        for( p_item = (*pp_parser)->p_config;
              p_item->i_type != CONFIG_HINT_END;
              p_item++ )
         {
@@ -822,6 +856,7 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         fprintf( file, "\n" );
     }
 
+    vlc_list_release( p_list );
 
     /*
      * Restore old settings from the config in file
@@ -849,7 +884,8 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
                           vlc_bool_t b_ignore_errors )
 {
     int i_cmd, i_index, i_opts, i_shortopts, flag;
-    module_t *p_module;
+    module_t **pp_parser;
+    vlc_list_t *p_list;
     module_config_t *p_item;
     struct option *p_longopts;
 
@@ -883,25 +919,30 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
     }
 #endif
 
+    /* List all modules */
+    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
     /*
      * Generate the longopts and shortopts structures used by getopt_long
      */
 
     i_opts = 0;
-    for( p_module = p_this->p_vlc->p_module_bank->first;
-         p_module != NULL ;
-         p_module = p_module->next )
+    for( pp_parser = (module_t **)p_list->pp_objects ;
+         *pp_parser ;
+         pp_parser++ )
     {
         /* count the number of exported configuration options (to allocate
          * longopts). We also need to allocate space for too options when
          * dealing with boolean to allow for --foo and --no-foo */
-        i_opts += (p_module->i_config_items + 2 * p_module->i_bool_items);
+        i_opts += (*pp_parser)->i_config_items
+                     + 2 * (*pp_parser)->i_bool_items;
     }
 
     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
     if( p_longopts == NULL )
     {
         msg_Err( p_this, "out of memory" );
+        vlc_list_release( p_list );
         return -1;
     }
 
@@ -910,6 +951,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
     {
         msg_Err( p_this, "out of memory" );
         free( p_longopts );
+        vlc_list_release( p_list );
         return -1;
     }
 
@@ -924,6 +966,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
             msg_Err( p_this, "out of memory" );
             free( psz_shortopts );
             free( p_longopts );
+            vlc_list_release( p_list );
             return -1;
         }
         memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
@@ -938,11 +981,14 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
 
     /* Fill the p_longopts and psz_shortopts structures */
     i_index = 0;
-    for( p_module = p_this->p_vlc->p_module_bank->first ;
-         p_module != NULL ;
-         p_module = p_module->next )
+    for( pp_parser = (module_t **)p_list->pp_objects ;
+         *pp_parser ;
+         pp_parser++ )
     {
-        for( p_item = p_module->p_config;
+        if( !(*pp_parser)->i_config_items )
+            continue;
+
+        for( p_item = (*pp_parser)->p_config;
              p_item->i_type != CONFIG_HINT_END;
              p_item++ )
         {
@@ -1002,6 +1048,9 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
         }
     }
 
+    /* We don't need the module list anymore */
+    vlc_list_release( p_list );
+
     /* Close the longopts and shortopts structures */
     memset( &p_longopts[i_index], 0, sizeof(struct option) );
     psz_shortopts[i_shortopts] = '\0';
@@ -1128,7 +1177,6 @@ char *config_GetHomeDir( void )
             p_homedir = (char *)malloc( MAX_PATH );
             if( !p_homedir )
             {
-//X                intf_ErrMsg( "config error: couldn't malloc p_homedir" );
                 return NULL;
             }
 
@@ -1155,15 +1203,11 @@ char *config_GetHomeDir( void )
         {
             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
             {
-                p_homedir = strdup( "/tmp" );
+                p_tmp = "/tmp";
             }
-            else p_homedir = strdup( p_tmp );
-
-//X            intf_ErrMsg( "config error: unable to get home directory, "
-//X                         "using %s instead", p_homedir );
-
         }
-        else p_homedir = strdup( p_tmp );
+
+        p_homedir = strdup( p_tmp );
     }
 #if defined(HAVE_GETPWUID)
     else