1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*****************************************************************************
* objects.c: Generic lua<->vlc object wrapper
*****************************************************************************
* Copyright (C) 2007-2008 the VideoLAN team
* $Id: a10a5f3d4a4e94c23563f3ce0376c1bb0538d1af $
*
* Authors: Antoine Cellerier <dionoea at videolan tod org>
*
* 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
* (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.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <lua.h> /* Low level lua C API */
#include <lauxlib.h> /* Higher level C API */
#include "../vlc.h"
#include "../libs.h"
#include "objects.h"
#include "playlist.h"
#include "input.h"
typedef struct
{
vlc_object_t *p_obj;
} vlclua_object_t;
/*****************************************************************************
* Generic vlc_object_t wrapper creation
*****************************************************************************/
int __vlclua_push_vlc_object( lua_State *L, vlc_object_t *p_obj,
lua_CFunction pf_gc )
{
vlc_object_t **udata = (vlc_object_t **)
lua_newuserdata( L, sizeof( vlc_object_t * ) );
*udata = p_obj;
if( luaL_newmetatable( L, "vlc_object" ) )
{
/* Hide the metatable */
lua_pushliteral( L, "none of your business" );
lua_setfield( L, -2, "__metatable" );
if( pf_gc ) /* FIXME */
{
/* Set the garbage collector if needed */
lua_pushcfunction( L, pf_gc );
lua_setfield( L, -2, "__gc" );
}
}
lua_setmetatable( L, -2 );
return 1;
}
int vlclua_gc_release( lua_State *L )
{
vlc_object_t **p_obj = (vlc_object_t **)luaL_checkudata( L, 1, "vlc_object" );
lua_pop( L, 1 );
vlc_object_release( *p_obj );
return 0;
}
static int vlc_object_type_from_string( const char *psz_name )
{
static const struct
{
int i_type;
const char *psz_name;
} pp_objects[] =
{ { VLC_OBJECT_INPUT, "input" },
{ VLC_OBJECT_VOUT, "vout" },
{ VLC_OBJECT_AOUT, "aout" },
{ 0, "" } };
int i;
for( i = 0; pp_objects[i].i_type; i++ )
{
if( !strcmp( psz_name, pp_objects[i].psz_name ) )
return pp_objects[i].i_type;
}
return 0;
}
static int vlc_object_search_mode_from_string( const char *psz_name )
{
static const struct
{
int i_mode;
const char *psz_name;
} pp_modes[] =
{ { FIND_PARENT, "parent" },
{ FIND_CHILD, "child" },
{ FIND_ANYWHERE, "anywhere" },
{ 0, "" } };
int i;
for( i = 0; pp_modes[i].i_mode; i++ )
{
if( !strcmp( psz_name, pp_modes[i].psz_name ) )
return pp_modes[i].i_mode;
}
return 0;
}
static int vlclua_object_find( lua_State *L )
{
const char *psz_type = luaL_checkstring( L, 2 );
const char *psz_mode = luaL_checkstring( L, 3 );
vlc_object_t *p_this;
int i_type = vlc_object_type_from_string( psz_type );
int i_mode = vlc_object_search_mode_from_string( psz_mode );
vlc_object_t *p_result;
if( !i_type )
return luaL_error( L, "\"%s\" is not a valid object type.", psz_type );
if( !i_mode )
return luaL_error( L, "\"%s\" is not a valid search mode.", psz_mode );
if( lua_type( L, 1 ) == LUA_TNIL )
p_this = vlclua_get_this( L );
else
{
vlc_object_t **p_obj = luaL_checkudata( L, 1, "vlc_object" );
p_this = *p_obj;
}
p_result = vlc_object_find( p_this, i_type, i_mode );
if( !p_result )
lua_pushnil( L );
else
vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
return 1;
}
static int vlclua_object_find_name( lua_State *L )
{
const char *psz_name = luaL_checkstring( L, 2 );
const char *psz_mode = luaL_checkstring( L, 3 );
vlc_object_t *p_this;
int i_mode = vlc_object_search_mode_from_string( psz_mode );
vlc_object_t *p_result;
if( !i_mode )
return luaL_error( L, "\"%s\" is not a valid search mode.",
psz_mode );
if( lua_type( L, 1 ) == LUA_TNIL )
p_this = vlclua_get_this( L );
else
{
vlc_object_t **p_obj = luaL_checkudata( L, 1, "vlc_object" );
p_this = *p_obj;
}
p_result = vlc_object_find_name( p_this, psz_name, i_mode );
if( !p_result )
lua_pushnil( L );
else
vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
return 1;
}
static int vlclua_get_libvlc( lua_State *L )
{
vlclua_push_vlc_object( L, vlclua_get_this( L )->p_libvlc,
NULL );
return 1;
}
static int vlclua_get_playlist( lua_State *L )
{
playlist_t *p_playlist = vlclua_get_playlist_internal( L );
if( p_playlist )
{
vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
vlc_object_hold( p_playlist );
}
else lua_pushnil( L );
return 1;
}
static int vlclua_get_input( lua_State *L )
{
input_thread_t *p_input = vlclua_get_input_internal( L );
if( p_input )
{
vlclua_push_vlc_object( L, p_input, vlclua_gc_release );
}
else lua_pushnil( L );
return 1;
}
/*****************************************************************************
*
*****************************************************************************/
static const luaL_Reg vlclua_object_reg[] = {
{ "input", vlclua_get_input },
{ "playlist", vlclua_get_playlist },
{ "libvlc", vlclua_get_libvlc },
{ "find", vlclua_object_find },
{ "find_name", vlclua_object_find_name },
{ NULL, NULL }
};
void luaopen_object( lua_State *L )
{
lua_newtable( L );
luaL_register( L, NULL, vlclua_object_reg );
lua_setfield( L, -2, "object" );
}
|