]> git.sesse.net Git - vlc/blobdiff - src/modules/bank.c
input: treat negative deadline as no deadline in ControlPop()
[vlc] / src / modules / bank.c
index fa5b7f1d50e71675838c1c6b8b80fd0971638554..f4fba50e301488042ef55dbd0e9185db880b090f 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * bank.c : Modules list
  *****************************************************************************
- * Copyright (C) 2001-2011 the VideoLAN team
+ * Copyright (C) 2001-2011 VLC authors and VideoLAN
  *
  * Authors: Sam Hocevar <sam@zoy.org>
  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
@@ -9,19 +9,19 @@
  *          Gildas Bazin <gbazin@videolan.org>
  *          RĂ©mi Denis-Courmont
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include <assert.h>
 
 #include <sys/types.h>
-#ifdef HAVE_SYS_STAT_H
-#   include <sys/stat.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#   include <unistd.h>
-#endif
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -71,6 +67,29 @@ static void module_StoreBank (module_t *module)
     modules.head = module;
 }
 
+#if defined(__ELF__) || !HAVE_DYNAMIC_PLUGINS
+# ifdef __GNUC__
+__attribute__((weak))
+# else
+#  pragma weak vlc_static_modules
+# endif
+extern vlc_plugin_cb vlc_static_modules[];
+
+static void module_InitStaticModules(void)
+{
+    if (!vlc_static_modules)
+        return;
+
+    for (unsigned i = 0; vlc_static_modules[i]; i++) {
+        module_t *module = module_InitStatic (vlc_static_modules[i]);
+        if (likely(module != NULL))
+            module_StoreBank (module);
+    }
+}
+#else
+static void module_InitStaticModules(void) { }
+#endif
+
 /**
  * Init bank
  *
@@ -83,16 +102,14 @@ void module_InitBank (void)
 
     if (modules.usage == 0)
     {
-        /* Fills the module bank structure with the main module infos.
-         * This is very useful as it will allow us to consider the main
+        /* Fills the module bank structure with the core module infos.
+         * This is very useful as it will allow us to consider the core
          * library just as another module, and for instance the configuration
-         * options of main will be available in the module bank structure just
+         * options of core will be available in the module bank structure just
          * as for every other module. */
-        module_t *module = module_InitStatic (vlc_entry__main);
+        module_t *module = module_InitStatic (vlc_entry__core);
         if (likely(module != NULL))
             module_StoreBank (module);
-
-        vlc_rwlock_init (&config_lock);
         config_SortConfig ();
     }
     modules.usage++;
@@ -100,7 +117,7 @@ void module_InitBank (void)
     /* We do retain the module bank lock until the plugins are loaded as well.
      * This is ugly, this staged loading approach is needed: LibVLC gets
      * some configuration parameters relevant to loading the plugins from
-     * the main (builtin) module. The module bank becomes shared read-only data
+     * the core (builtin) module. The module bank becomes shared read-only data
      * once it is ready, so we need to fully serialize initialization.
      * DO NOT UNCOMMENT the following line unless you managed to squeeze
      * module_LoadPlugins() before you unlock the mutex. */
@@ -126,7 +143,6 @@ void module_EndBank (bool b_plugins)
     if (--modules.usage == 0)
     {
         config_UnsortConfig ();
-        vlc_rwlock_destroy (&config_lock);
         head = modules.head;
         modules.head = NULL;
     }
@@ -154,22 +170,29 @@ void module_EndBank (bool b_plugins)
  * Fills the module bank structure with the plugin modules.
  *
  * \param p_this vlc object structure
- * \return nothing
+ * \return total number of modules in bank after loading all plug-ins
  */
-void module_LoadPlugins (vlc_object_t *obj)
+size_t module_LoadPlugins (vlc_object_t *obj)
 {
     /*vlc_assert_locked (&modules.lock); not for static mutexes :( */
 
-#ifdef HAVE_DYNAMIC_PLUGINS
     if (modules.usage == 1)
     {
+        module_InitStaticModules ();
+#ifdef HAVE_DYNAMIC_PLUGINS
         msg_Dbg (obj, "searching plug-in modules");
         AllocateAllPlugins (obj);
+#endif
         config_UnsortConfig ();
         config_SortConfig ();
     }
-#endif
     vlc_mutex_unlock (&modules.lock);
+
+    size_t count;
+    module_t **list = module_list_get (&count);
+    module_list_free (list);
+    msg_Dbg (obj, "plug-ins loaded: %zu modules", count);
+    return count;
 }
 
 /**
@@ -185,24 +208,25 @@ void module_list_free (module_t **list)
 
 /**
  * Gets the flat list of VLC modules.
- * @param n [OUT] pointer to the number of modules or NULL
- * @return NULL-terminated table of module pointers
- *         (release with module_list_free()), or NULL in case of error.
+ * @param n [OUT] pointer to the number of modules
+ * @return table of module pointers (release with module_list_free()),
+ *         or NULL in case of error (in that case, *n is zeroed).
  */
 module_t **module_list_get (size_t *n)
 {
-    /* TODO: this whole module lookup is quite inefficient */
-    /* Remove this and improve module_need */
     module_t **tab = NULL;
     size_t i = 0;
 
+    assert (n != NULL);
+
     for (module_t *mod = modules.head; mod; mod = mod->next)
     {
          module_t **nt;
-         nt  = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
-         if (nt == NULL)
+         nt  = realloc (tab, (i + 1 + mod->submodule_count) * sizeof (*tab));
+         if (unlikely(nt == NULL))
          {
-             module_list_free (tab);
+             free (tab);
+             *n = 0;
              return NULL;
          }
 
@@ -210,14 +234,61 @@ module_t **module_list_get (size_t *n)
          tab[i++] = mod;
          for (module_t *subm = mod->submodule; subm; subm = subm->next)
              tab[i++] = subm;
-         tab[i] = NULL;
     }
-    if (n != NULL)
-        *n = i;
+    *n = i;
     return tab;
 }
 
-char *psz_vlcpath = NULL;
+static int modulecmp (const void *a, const void *b)
+{
+    const module_t *const *ma = a, *const *mb = b;
+    /* Note that qsort() uses _ascending_ order,
+     * so the smallest module is the one with the biggest score. */
+    return (*mb)->i_score - (*ma)->i_score;
+}
+
+/**
+ * Builds a sorted list of all VLC modules with a given capability.
+ * The list is sorted from the highest module score to the lowest.
+ * @param list pointer to the table of modules [OUT]
+ * @param cap capability of modules to look for
+ * @return the number of matching found, or -1 on error (*list is then NULL).
+ * @note *list must be freed with module_list_free().
+ */
+ssize_t module_list_cap (module_t ***restrict list, const char *cap)
+{
+    /* TODO: This is quite inefficient. List should be sorted by capability. */
+    ssize_t n = 0;
+
+    assert (list != NULL);
+
+    for (module_t *mod = modules.head; mod != NULL; mod = mod->next)
+    {
+         if (module_provides (mod, cap))
+             n++;
+         for (module_t *subm = mod->submodule; subm != NULL; subm = subm->next)
+             if (module_provides (subm, cap))
+                 n++;
+    }
+
+    module_t **tab = malloc (sizeof (*tab) * n);
+    *list = tab;
+    if (unlikely(tab == NULL))
+        return -1;
+
+    for (module_t *mod = modules.head; mod != NULL; mod = mod->next)
+    {
+         if (module_provides (mod, cap))
+             *(tab++)= mod;
+         for (module_t *subm = mod->submodule; subm != NULL; subm = subm->next)
+             if (module_provides (subm, cap))
+                 *(tab++) = subm;
+    }
+
+    assert (tab == *list + n);
+    qsort (*list, n, sizeof (*tab), modulecmp);
+    return n;
+}
 
 #ifdef HAVE_DYNAMIC_PLUGINS
 typedef enum { CACHE_USE, CACHE_RESET, CACHE_IGNORE } cache_mode_t;
@@ -234,7 +305,6 @@ static void AllocatePluginPath (vlc_object_t *, const char *, cache_mode_t);
  */
 static void AllocateAllPlugins (vlc_object_t *p_this)
 {
-    const char *vlcpath = psz_vlcpath;
     char *paths;
     cache_mode_t mode;
 
@@ -245,15 +315,21 @@ static void AllocateAllPlugins (vlc_object_t *p_this)
     else
         mode = CACHE_USE;
 
+#if VLC_WINSTORE_APP
+    /* Windows Store Apps can not load external plugins with absolute paths. */
+    AllocatePluginPath (p_this, "plugins", mode);
+#else
     /* Contruct the special search path for system that have a relocatable
      * executable. Set it to <vlc path>/plugins. */
-    assert( vlcpath );
-
-    if( asprintf( &paths, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
+    char *vlcpath = config_GetLibDir ();
+    if (likely(vlcpath != NULL)
+     && likely(asprintf (&paths, "%s" DIR_SEP "plugins", vlcpath) != -1))
     {
         AllocatePluginPath (p_this, paths, mode);
         free( paths );
     }
+    free (vlcpath);
+#endif /* VLC_WINSTORE_APP */
 
     /* If the user provided a plugin path, we add it to the list */
     paths = getenv( "VLC_PLUGIN_PATH" );
@@ -368,7 +444,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
 
         /* Skip ".", ".." */
         if (!strcmp (file, ".") || !strcmp (file, ".."))
-            goto skip;
+            continue;
 
         /* Compute path relative to plug-in base directory */
         if (reldir != NULL)
@@ -379,7 +455,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
         else
             relpath = strdup (file);
         if (unlikely(relpath == NULL))
-            goto skip;
+            continue;
 
         /* Compute absolute path */
         if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1)
@@ -398,10 +474,17 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
             static const char suffix[] = "_plugin"LIBEXT;
             size_t len = strlen (file);
 
+#ifndef __OS2__
             /* Check that file matches the "lib*_plugin"LIBEXT pattern */
             if (len > strlen (suffix)
              && !strncmp (file, prefix, strlen (prefix))
              && !strcmp (file + len - strlen (suffix), suffix))
+#else
+            /* We load all the files ending with LIBEXT on OS/2,
+             * because OS/2 has a 8.3 length limitation for DLL name */
+            if (len > strlen (LIBEXT)
+             && !strcasecmp (file + len - strlen (LIBEXT), LIBEXT))
+#endif
                 AllocatePluginFile (bank, abspath, relpath, &st);
         }
         else if (S_ISDIR (st.st_mode))
@@ -410,7 +493,6 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
     skip:
         free (relpath);
         free (abspath);
-        free (file);
     }
     closedir (dh);
 }
@@ -455,15 +537,17 @@ static int AllocatePluginFile (module_bank_t *bank, const char *abspath,
         module->b_loaded = false;
     }
 
-    /* For now we force loading if the module's config contains
-     * callbacks or actions.
+    /* For now we force loading if the module's config contains callbacks.
      * Could be optimized by adding an API call.*/
     for (size_t n = module->confsize, i = 0; i < n; i++)
-         if (module->p_config[i].i_action)
+         if (module->p_config[i].list_count == 0
+          && (module->p_config[i].list.psz_cb != NULL || module->p_config[i].list.i_cb != NULL))
          {
              /* !unloadable not allowed for plugins with callbacks */
              vlc_module_destroy (module);
              module = module_InitDynamic (bank->obj, abspath, false);
+             if (unlikely(module == NULL))
+                 return -1;
              break;
          }
 
@@ -475,6 +559,13 @@ static int AllocatePluginFile (module_bank_t *bank, const char *abspath,
     return  0;
 }
 
+#ifdef __OS2__
+#   define EXTERN_PREFIX "_"
+#else
+#   define EXTERN_PREFIX
+#endif
+
+
 /**
  * Loads a dynamically-linked plug-in into memory and initialize it.
  *
@@ -493,7 +584,7 @@ static module_t *module_InitDynamic (vlc_object_t *obj, const char *path,
         return NULL;
 
     /* Try to resolve the symbol */
-    static const char entry_name[] = "vlc_entry" MODULE_SUFFIX;
+    static const char entry_name[] = EXTERN_PREFIX "vlc_entry" MODULE_SUFFIX;
     vlc_plugin_cb entry =
         (vlc_plugin_cb) module_Lookup (handle, entry_name);
     if (entry == NULL)
@@ -548,23 +639,31 @@ static module_t *module_InitStatic (vlc_plugin_cb entry)
  */
 int module_Map (vlc_object_t *obj, module_t *module)
 {
+    static vlc_mutex_t lock = VLC_STATIC_MUTEX;
+
     if (module->parent != NULL)
         module = module->parent;
 
-#warning FIXME: race condition!
-    if (module->b_loaded)
-        return 0;
-    assert (module->psz_filename != NULL);
+    vlc_mutex_lock(&lock);
+    if (!module->b_loaded)
+    {
+        module_t *uncache;
 
+        assert (module->psz_filename != NULL);
 #ifdef HAVE_DYNAMIC_PLUGINS
-    module_t *uncache = module_InitDynamic (obj, module->psz_filename, false);
-    if (uncache != NULL)
-    {
-        CacheMerge (obj, module, uncache);
-        vlc_module_destroy (uncache);
-        return 0;
-    }
+        uncache = module_InitDynamic (obj, module->psz_filename, false);
+        if (uncache != NULL)
+        {
+            CacheMerge (obj, module, uncache);
+            vlc_module_destroy (uncache);
+        }
+        else
 #endif
-    msg_Err (obj, "corrupt module: %s", module->psz_filename);
-    return -1;
+        {
+            msg_Err (obj, "corrupt module: %s", module->psz_filename);
+            module = NULL;
+        }
+    }
+    vlc_mutex_unlock(&lock);
+    return -(module == NULL);
 }