]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/libs/misc.c
Use pushliteral instead of pushstring where possible. Saves a strlen call.
[vlc] / modules / misc / lua / libs / misc.c
index 37fdb72c95ae9143c47351260b5043fae34261fa..ca1e69ced821326b86b3e359e16b62a66cd5877d 100644 (file)
@@ -36,9 +36,9 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_meta.h>
-#include <vlc_charset.h>
 #include <vlc_aout.h>
 #include <vlc_interface.h>
+#include <vlc_keys.h>
 
 #include <lua.h>        /* Low level lua C API */
 #include <lauxlib.h>    /* Higher level C API */
 /*****************************************************************************
  * Internal lua<->vlc utils
  *****************************************************************************/
+void __vlclua_set_this( lua_State *L, vlc_object_t *p_this )
+{
+    lua_pushlightuserdata( L, __vlclua_set_this );
+    lua_pushlightuserdata( L, p_this );
+    lua_rawset( L, LUA_REGISTRYINDEX );
+}
+
 vlc_object_t * vlclua_get_this( lua_State *L )
 {
-    vlc_object_t * p_this;
-    lua_getglobal( L, "vlc" );
-    lua_getfield( L, -1, "private" );
-    p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
-    lua_pop( L, 2 );
+    lua_pushlightuserdata( L, __vlclua_set_this );
+    lua_rawget( L, LUA_REGISTRYINDEX );
+    vlc_object_t *p_this = (vlc_object_t*)lua_topointer( L, -1 );
+    lua_pop( L, 1 );
     return p_this;
 }
 
@@ -84,7 +90,7 @@ static int vlclua_version( lua_State *L )
  *****************************************************************************/
 static int vlclua_copyright( lua_State *L )
 {
-    lua_pushstring( L, COPYRIGHT_MESSAGE );
+    lua_pushliteral( L, COPYRIGHT_MESSAGE );
     return 1;
 }
 
@@ -93,7 +99,7 @@ static int vlclua_copyright( lua_State *L )
  *****************************************************************************/
 static int vlclua_license( lua_State *L )
 {
-    lua_pushstring( L, LICENSE_MSG );
+    lua_pushliteral( L, LICENSE_MSG );
     return 1;
 }
 
@@ -114,7 +120,9 @@ static int vlclua_quit( lua_State *L )
  *****************************************************************************/
 static int vlclua_datadir( lua_State *L )
 {
-    lua_pushstring( L, config_GetDataDir() );
+    char *psz_data = config_GetDataDir( vlclua_get_this( L ) );
+    lua_pushstring( L, psz_data );
+    free( psz_data );
     return 1;
 }
 
@@ -153,14 +161,14 @@ static int vlclua_cachedir( lua_State *L )
 static int vlclua_datadir_list( lua_State *L )
 {
     const char *psz_dirname = luaL_checkstring( L, 1 );
-    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
-    char **ppsz_dir = ppsz_dir_list;
+    char **ppsz_dir_list = NULL;
     int i = 1;
 
-    if( vlclua_dir_list( psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
+    if( vlclua_dir_list( vlclua_get_this( L ), psz_dirname, &ppsz_dir_list )
+        != VLC_SUCCESS )
         return 0;
     lua_newtable( L );
-    for( ; *ppsz_dir; ppsz_dir++ )
+    for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
     {
         lua_pushstring( L, *ppsz_dir );
         lua_rawseti( L, -2, i );
@@ -178,9 +186,10 @@ static int vlclua_lock_and_wait( lua_State *L )
     intf_sys_t *p_sys = p_intf->p_sys;
 
     vlc_mutex_lock( &p_sys->lock );
+    mutex_cleanup_push( &p_sys->lock );
     while( !p_sys->exiting )
         vlc_cond_wait( &p_sys->wait, &p_sys->lock );
-    vlc_mutex_unlock( &p_sys->lock );
+    vlc_cleanup_run();
     lua_pushboolean( L, 1 );
     return 1;
 }
@@ -205,6 +214,15 @@ static int vlclua_intf_should_die( lua_State *L )
     return 1;
 }
 
+static int vlclua_action_id( lua_State *L )
+{
+    vlc_key_t i_key = vlc_GetActionId( luaL_checkstring( L, 1 ) );
+    if (i_key == 0)
+        return 0;
+    lua_pushnumber( L, i_key );
+    return 1;
+}
+
 /*****************************************************************************
  *
  *****************************************************************************/
@@ -220,6 +238,8 @@ static const luaL_Reg vlclua_misc_reg[] = {
     { "cachedir", vlclua_cachedir },
     { "datadir_list", vlclua_datadir_list },
 
+    { "action_id", vlclua_action_id },
+
     { "mdate", vlclua_mdate },
     { "mwait", vlclua_mwait },