[vlc-commits] [Git][videolan/vlc][master] 7 commits: vlc_extensions: rename __extension_GetBool

Steve Lhomme (@robUx4) gitlab at videolan.org
Sat Feb 4 05:02:41 UTC 2023



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
9b3f841a by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
vlc_extensions: rename __extension_GetBool

__-prefixed symbols are reserved by POSIX.

- - - - -
b76f6acf by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
lua: extension: simplify manifest fetching

DRY for the checks on the different strings that are fetched from the
extension descriptor.

- - - - -
1248f831 by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
vlc_extensions: change sys to void*

- - - - -
cfb5d9fa by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
lua: extension: rewrite .vle file detection

Extract the extension to detect a .vle file and use asprintf for the
whole path instead of relying on string index.

- - - - -
cb2dd7d5 by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
lua: extension: avoid cast from void*

void* can be automatically casted to any type pointer, there's no need
to have a cast operator there.

- - - - -
7bd66a29 by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
lua: extension: use ARRAY_SIZE directly

Avoid repeating the element twice in sizeof() and explicitely signal
that we want to use the number of elements from the capability array.

- - - - -
c80ca84a by Alexandre Janniaux at 2023-02-04T04:45:02+00:00
test: lua: check that extension are correctly probed

The test included in this commit checks that lua will correctly probe
extension scripts, but without ensuring that it correctly follows the
data home. It only check that lua creates the correct access to load
the files.

In particular, the test fails to pass if the condition for loading .vle
files is not correct.

- - - - -


5 changed files:

- include/vlc_extensions.h
- modules/lua/extension.c
- test/Makefile.am
- + test/modules/lua/extension.c
- + test/modules/lua/extensions/extensions.lua


Changes:

=====================================
include/vlc_extensions.h
=====================================
@@ -54,7 +54,7 @@ struct extensions_manager_t
     struct vlc_object_t obj;
 
     module_t *p_module;                /**< Extensions manager module */
-    extensions_manager_sys_t *p_sys;   /**< Reserved for the module */
+    void *p_sys;              /**< Reserved for the module */
 
     DECL_ARRAY(extension_t*) extensions; /**< Array of extension descriptors */
     vlc_mutex_t lock;                  /**< A lock for the extensions array */
@@ -98,10 +98,9 @@ static inline int extension_Control( extensions_manager_t *p_mgr,
  * Helper for extension_HasMenu, extension_IsActivated...
  * Do not use.
  **/
-static inline bool __extension_GetBool( extensions_manager_t *p_mgr,
-                                        extension_t *p_ext,
-                                        int i_flag,
-                                        bool b_default )
+static inline bool
+vlc_extension_GetBool( extensions_manager_t *p_mgr, extension_t *p_ext,
+                       int i_flag, bool b_default )
 {
     bool b = b_default;
     int i_ret = extension_Control( p_mgr, i_flag, p_ext, &b );
@@ -125,11 +124,11 @@ static inline bool __extension_GetBool( extensions_manager_t *p_mgr,
 
 /** Is this extension activated? */
 #define extension_IsActivated( mgr, ext ) \
-        __extension_GetBool( mgr, ext, EXTENSION_IS_ACTIVATED, false )
+        vlc_extension_GetBool( mgr, ext, EXTENSION_IS_ACTIVATED, false )
 
 /** Does this extension have a sub-menu? */
 #define extension_HasMenu( mgr, ext ) \
-        __extension_GetBool( mgr, ext, EXTENSION_HAS_MENU, false )
+        vlc_extension_GetBool( mgr, ext, EXTENSION_HAS_MENU, false )
 
 /** Get this extension's sub-menu */
 static inline int extension_GetMenu( extensions_manager_t *p_mgr,
@@ -172,7 +171,7 @@ static inline int extension_MetaChanged( extensions_manager_t *p_mgr,
 /** Can this extension only be triggered but not activated?
     Not compatible with HasMenu */
 #define extension_TriggerOnly( mgr, ext ) \
-        __extension_GetBool( mgr, ext, EXTENSION_TRIGGER_ONLY, false )
+        vlc_extension_GetBool( mgr, ext, EXTENSION_TRIGGER_ONLY, false )
 
 
 /*****************************************************************************


=====================================
modules/lua/extension.c
=====================================
@@ -259,6 +259,15 @@ static int vlclua_extension_require( lua_State *L )
     return 0;
 }
 
+static char *
+GetStringFieldOrNull(lua_State *L, const char *name)
+{
+    lua_getfield(L, -1, name);
+    char *value = luaL_strdupornull(L, -1);
+    lua_pop(L, 1);
+    return value;
+}
+
 /**
  * Batch scan all Lua files in folder "extensions": callback
  * @param p_this This extensions_manager_t object
@@ -277,16 +286,12 @@ int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
 
     /* Experimental: read .vle packages (Zip archives) */
     char *psz_script = NULL;
-    int i_flen = strlen( psz_filename );
-    if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
+    char *extension = strrchr(psz_filename, '.');
+    if (extension != NULL && strcmp(extension, ".vle") == 0)
     {
         msg_Dbg( p_this, "reading Lua script in a zip archive" );
-        psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
-        if( !psz_script )
-            return 0;
-        strcpy( psz_script, "zip://" );
-        strncat( psz_script, psz_filename, i_flen + 19 );
-        strncat( psz_script, "!/script.lua", i_flen + 19 );
+        if (asprintf(&psz_script, "zip://%s!/script.lua", psz_filename) == -1)
+            return VLC_ENOMEM;
     }
     else
     {
@@ -296,7 +301,7 @@ int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
     }
 
     /* Create new script descriptor */
-    extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
+    extension_t *p_ext = calloc( 1, sizeof( extension_t ) );
     if( !p_ext )
     {
         free( psz_script );
@@ -304,7 +309,7 @@ int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
     }
 
     p_ext->psz_name = psz_script;
-    p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
+    p_ext->p_sys = calloc( 1, sizeof( extension_sys_t ) );
     if( !p_ext->p_sys || !p_ext->psz_name )
     {
         free( p_ext->psz_name );
@@ -374,7 +379,7 @@ int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
                     const char *psz_cap = luaL_checkstring( L, -1 );
                     bool found = false;
                     /* Find this capability's flag */
-                    for( size_t i = 0; i < sizeof(caps)/sizeof(caps[0]); i++ )
+                    for( size_t i = 0; i < ARRAY_SIZE(caps); i++ )
                     {
                         if( !strcmp( caps[i], psz_cap ) )
                         {
@@ -416,31 +421,12 @@ int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
             }
             lua_pop( L, 1 );
 
-            /* Get author */
-            lua_getfield( L, -1, "author" );
-            p_ext->psz_author = luaL_strdupornull( L, -1 );
-            lua_pop( L, 1 );
-
-            /* Get description */
-            lua_getfield( L, -1, "description" );
-            p_ext->psz_description = luaL_strdupornull( L, -1 );
-            lua_pop( L, 1 );
-
-            /* Get short description */
-            lua_getfield( L, -1, "shortdesc" );
-            p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
-            lua_pop( L, 1 );
-
-            /* Get URL */
-            lua_getfield( L, -1, "url" );
-            p_ext->psz_url = luaL_strdupornull( L, -1 );
-            lua_pop( L, 1 );
-
-            /* Get version */
-            lua_getfield( L, -1, "version" );
-            p_ext->psz_version = luaL_strdupornull( L, -1 );
-            lua_pop( L, 1 );
-
+            /* Get the fields from the extension manifest. */
+            p_ext->psz_author = GetStringFieldOrNull(L, "author");
+            p_ext->psz_description = GetStringFieldOrNull(L, "description");
+            p_ext->psz_shortdescription = GetStringFieldOrNull(L, "shortdesc");
+            p_ext->psz_url = GetStringFieldOrNull(L, "url");
+            p_ext->psz_version = GetStringFieldOrNull(L, "version");
             /* Get icon data */
             lua_getfield( L, -1, "icon" );
             if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )


=====================================
test/Makefile.am
=====================================
@@ -38,6 +38,7 @@ check_PROGRAMS = \
 	test_src_misc_image \
 	test_src_video_output \
 	test_src_video_output_opengl \
+	test_modules_lua_extension \
 	test_modules_misc_medialibrary \
 	test_modules_packetizer_helpers \
 	test_modules_packetizer_hxxx \
@@ -149,6 +150,10 @@ test_src_interface_dialog_LDADD = $(LIBVLCCORE) $(LIBVLC)
 test_src_media_source_LDADD = $(LIBVLCCORE) $(LIBVLC)
 test_src_media_source_SOURCES = src/media_source/media_source.c
 
+test_modules_lua_extension_SOURCES = modules/lua/extension.c
+test_modules_lua_extension_LDADD = $(LIBVLCCORE) $(LIBVLC)
+test_modules_lua_extension_CPPFLAGS = $(AM_CPPFLAGS) \
+	-DLUA_EXTENSION_DIR=\"$(srcdir)/modules/\"
 test_modules_misc_medialibrary_SOURCES = modules/misc/medialibrary.c
 test_modules_misc_medialibrary_LDADD = $(LIBVLCCORE) $(LIBVLC)
 test_modules_packetizer_helpers_SOURCES = modules/packetizer/helpers.c


=====================================
test/modules/lua/extension.c
=====================================
@@ -0,0 +1,106 @@
+/*****************************************************************************
+ * extension.c: test for the lua extension module
+ *****************************************************************************
+ * Copyright (C) 2023 Videolabs
+ *
+ * Authors: Alexandre Janniaux <ajanni at videolabs.io>
+ *
+ * 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 Lesser General Public License for more details.
+ *
+ * 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 <config.h>
+#endif
+
+/* Define a builtin module for mocked parts */
+#define MODULE_NAME test_lua_extension
+#define MODULE_STRING "test_lua_extension"
+#undef __PLUGIN__
+const char vlc_module_name[] = MODULE_STRING;
+
+#include "../../libvlc/test.h"
+
+#include <vlc/vlc.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_modules.h>
+#include <vlc_extensions.h>
+
+#include <limits.h>
+
+static int exitcode = 0;
+
+static int OpenIntf(vlc_object_t *root)
+{
+    extensions_manager_t *mgr =
+        vlc_object_create(root, sizeof *mgr);
+    assert(mgr);
+
+    setenv("XDG_DATA_HOME", LUA_EXTENSION_DIR, 1);
+    setenv("VLC_DATA_PATH", LUA_EXTENSION_DIR, 1);
+    setenv("VLC_LIB_PATH", LUA_EXTENSION_DIR, 1);
+
+    mgr->p_module = module_need(mgr, "extension", "lua", true);
+
+    if (mgr->p_module == NULL)
+    {
+        exitcode = 77;
+        goto end;
+    }
+
+    /* Check that the extension from the test is correctly probed. */
+    assert(mgr->extensions.i_size == 1);
+    extension_Activate(mgr, mgr->extensions.p_elems[0]);
+    extension_Deactivate(mgr, mgr->extensions.p_elems[0]);
+
+    module_unneed(mgr, mgr->p_module);
+end:
+    vlc_object_delete(mgr);
+    return VLC_SUCCESS;
+}
+
+/** Inject the mocked modules as a static plugin: **/
+vlc_module_begin()
+    set_callback(OpenIntf)
+    set_capability("interface", 0)
+vlc_module_end()
+
+/* Helper typedef for vlc_static_modules */
+typedef int (*vlc_plugin_cb)(vlc_set_cb, void*);
+
+VLC_EXPORT const vlc_plugin_cb vlc_static_modules[] = {
+    VLC_SYMBOL(vlc_entry),
+    NULL
+};
+
+
+int main()
+{
+    test_init();
+
+    const char * const args[] = {
+        "-vvv", "--vout=dummy", "--aout=dummy", "--text-renderer=dummy",
+        "--no-auto-preparse",
+    };
+
+    libvlc_instance_t *vlc = libvlc_new(ARRAY_SIZE(args), args);
+
+    libvlc_add_intf(vlc, MODULE_STRING);
+    libvlc_playlist_play(vlc);
+
+    libvlc_release(vlc);
+    return 0;
+}


=====================================
test/modules/lua/extensions/extensions.lua
=====================================
@@ -0,0 +1,38 @@
+function descriptor()
+  return {
+    title = "test",
+    version = "0.0.1",
+    author = "VideoLAN",
+    shortdesc = "Test example",
+    description = "Test description",
+    capabilities = {
+        "input-listener",
+        "meta-listener",
+        "playing-listener",
+     }
+  }
+end
+
+function activate()
+  vlc.msg.dbg("Activate")
+end
+
+function close()
+  vlc.msg.dbg("Close")
+end
+
+function deactivate()
+  vlc.msg.dbg("Deactivate")
+end
+
+function input_changed()
+  vlc.msg.dbg("Input changed")
+end
+
+function playing_changed()
+  vlc.msg.dbg("Playing changed")
+end
+
+function meta_changed()
+  vlc.msg.dbg("Meta changed")
+end



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/39a634b6ed021a24d4c774b416cd48d6bfa8aa7e...c80ca84a14370e3cc38c9bc7ebdd05c94b28e21f

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/39a634b6ed021a24d4c774b416cd48d6bfa8aa7e...c80ca84a14370e3cc38c9bc7ebdd05c94b28e21f
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list