1 /*****************************************************************************
2 * objects.c: Generic lua<->vlc object wrapper
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <lua.h> /* Low level lua C API */
38 #include <lauxlib.h> /* Higher level C API */
51 /*****************************************************************************
52 * Generic vlc_object_t wrapper creation
53 *****************************************************************************/
54 int __vlclua_push_vlc_object( lua_State *L, vlc_object_t *p_obj,
57 vlc_object_t **udata = (vlc_object_t **)
58 lua_newuserdata( L, sizeof( vlc_object_t * ) );
61 if( luaL_newmetatable( L, "vlc_object" ) )
63 /* Hide the metatable */
64 lua_pushstring( L, "none of your business" );
65 lua_setfield( L, -2, "__metatable" );
66 if( pf_gc ) /* FIXME */
68 /* Set the garbage collector if needed */
69 lua_pushcfunction( L, pf_gc );
70 lua_setfield( L, -2, "__gc" );
73 lua_setmetatable( L, -2 );
77 int vlclua_gc_release( lua_State *L )
79 vlc_object_t **p_obj = (vlc_object_t **)luaL_checkudata( L, 1, "vlc_object" );
81 vlc_object_release( *p_obj );
85 static int vlc_object_type_from_string( const char *psz_name )
92 { { VLC_OBJECT_INTF, "intf" },
93 { VLC_OBJECT_INPUT, "input" },
94 { VLC_OBJECT_DECODER, "decoder" },
95 { VLC_OBJECT_VOUT, "vout" },
96 { VLC_OBJECT_AOUT, "aout" },
97 { VLC_OBJECT_PACKETIZER, "packetizer" },
98 { VLC_OBJECT_ENCODER, "encoder" },
99 { VLC_OBJECT_OSDMENU, "osdmenu" },
100 { VLC_OBJECT_GENERIC, "generic" },
103 for( i = 0; pp_objects[i].i_type; i++ )
105 if( !strcmp( psz_name, pp_objects[i].psz_name ) )
106 return pp_objects[i].i_type;
111 static int vlc_object_search_mode_from_string( const char *psz_name )
116 const char *psz_name;
118 { { FIND_PARENT, "parent" },
119 { FIND_CHILD, "child" },
120 { FIND_ANYWHERE, "anywhere" },
123 for( i = 0; pp_modes[i].i_mode; i++ )
125 if( !strcmp( psz_name, pp_modes[i].psz_name ) )
126 return pp_modes[i].i_mode;
131 static int vlclua_object_find( lua_State *L )
133 const char *psz_type = luaL_checkstring( L, 2 );
134 const char *psz_mode = luaL_checkstring( L, 3 );
136 vlc_object_t *p_this;
137 int i_type = vlc_object_type_from_string( psz_type );
138 int i_mode = vlc_object_search_mode_from_string( psz_mode );
139 vlc_object_t *p_result;
142 return luaL_error( L, "\"%s\" is not a valid object type.", psz_type );
144 return luaL_error( L, "\"%s\" is not a valid search mode.", psz_mode );
146 if( lua_type( L, 1 ) == LUA_TNIL )
147 p_this = vlclua_get_this( L );
150 vlc_object_t **p_obj = luaL_checkudata( L, 1, "vlc_object" );
154 p_result = vlc_object_find( p_this, i_type, i_mode );
158 vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
162 static int vlclua_object_find_name( lua_State *L )
164 const char *psz_name = luaL_checkstring( L, 2 );
165 const char *psz_mode = luaL_checkstring( L, 3 );
167 vlc_object_t *p_this;
168 int i_mode = vlc_object_search_mode_from_string( psz_mode );
169 vlc_object_t *p_result;
172 return luaL_error( L, "\"%s\" is not a valid search mode.",
175 if( lua_type( L, 1 ) == LUA_TNIL )
176 p_this = vlclua_get_this( L );
179 vlc_object_t **p_obj = luaL_checkudata( L, 1, "vlc_object" );
183 p_result = vlc_object_find_name( p_this, psz_name, i_mode );
187 vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
191 static int vlclua_get_libvlc( lua_State *L )
193 vlclua_push_vlc_object( L, vlclua_get_this( L )->p_libvlc,
198 static int vlclua_get_playlist( lua_State *L )
200 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
203 vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
205 else lua_pushnil( L );
206 //vlclua_release_playlist_internal( p_playlist );
210 static int vlclua_get_input( lua_State *L )
212 input_thread_t *p_input = vlclua_get_input_internal( L );
215 vlclua_push_vlc_object( L, p_input, vlclua_gc_release );
217 else lua_pushnil( L );
221 /*****************************************************************************
223 *****************************************************************************/
224 static const luaL_Reg vlclua_object_reg[] = {
225 { "input", vlclua_get_input },
226 { "playlist", vlclua_get_playlist },
227 { "libvlc", vlclua_get_libvlc },
228 { "find", vlclua_object_find },
229 { "find_name", vlclua_object_find_name },
233 void luaopen_object( lua_State *L )
236 luaL_register( L, NULL, vlclua_object_reg );
237 lua_setfield( L, -2, "object" );