1 /*****************************************************************************
2 * objects.c: Generic lua<->vlc object wrapper
3 *****************************************************************************
4 * Copyright (C) 2007 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 *****************************************************************************/
37 #include <lua.h> /* Low level lua C API */
38 #include <lauxlib.h> /* Higher level C API */
39 #include <lualib.h> /* Lua libs */
48 /*****************************************************************************
49 * Generic vlc_object_t wrapper creation
50 *****************************************************************************/
51 int __vlclua_push_vlc_object( lua_State *L, vlc_object_t *p_obj,
54 vlclua_object_t *p_ud = (vlclua_object_t *)
55 lua_newuserdata( L, sizeof( vlclua_object_t ) );
58 /* Hide the metatable */
59 lua_pushstring( L, "__metatable" );
60 lua_pushstring( L, "none of your business" );
61 lua_settable( L, -3 );
64 /* Set the garbage collector if needed */
65 lua_pushstring( L, "__gc" );
66 lua_pushcfunction( L, pf_gc );
67 lua_settable( L, -3 );
69 lua_setmetatable( L, -2 );
73 int vlclua_gc_release( lua_State *L )
75 vlclua_object_t *p_ud = (vlclua_object_t *)lua_touserdata( L, -1 );
77 vlc_object_release( p_ud->p_obj );
81 vlc_object_t *vlclua_checkobject( lua_State *L, int narg, int i_type )
83 vlclua_object_t *p_obj = (vlclua_object_t *)luaL_checkuserdata( L, narg, sizeof( vlclua_object_t ) );
84 /* TODO: add some metatable based method to check that this isn't
85 * any userdata, but really some vlc object */
88 if( p_obj->p_obj->i_object_type == i_type )
92 luaL_error( L, "VLC object type doesn't match requirements." );
93 return NULL; /* luaL_error alread longjmp-ed out of here.
94 * This is to make gcc happy */
103 static int vlc_object_type_from_string( const char *psz_name )
108 const char *psz_name;
110 { { VLC_OBJECT_LIBVLC, "libvlc" },
111 { VLC_OBJECT_MODULE, "module" },
112 { VLC_OBJECT_INTF, "intf" },
113 { VLC_OBJECT_PLAYLIST, "playlist" },
114 { VLC_OBJECT_INPUT, "input" },
115 { VLC_OBJECT_DECODER, "decoder" },
116 { VLC_OBJECT_VOUT, "vout" },
117 { VLC_OBJECT_AOUT, "aout" },
118 { VLC_OBJECT_SOUT, "sout" },
119 { VLC_OBJECT_PACKETIZER, "packetizer" },
120 { VLC_OBJECT_ENCODER, "encoder" },
121 { VLC_OBJECT_DIALOGS, "dialogs" },
122 { VLC_OBJECT_ANNOUNCE, "announce" },
123 { VLC_OBJECT_DEMUX, "demux" },
124 { VLC_OBJECT_ACCESS, "access" },
125 { VLC_OBJECT_STREAM, "stream" },
126 { VLC_OBJECT_OPENGL, "opengl" },
127 { VLC_OBJECT_FILTER, "filter" },
128 { VLC_OBJECT_OSDMENU, "osdmenu" },
129 { VLC_OBJECT_HTTPD_HOST, "httpd_host" },
130 { VLC_OBJECT_GENERIC, "generic" },
133 for( i = 0; pp_objects[i].i_type; i++ )
135 if( !strcmp( psz_name, pp_objects[i].psz_name ) )
136 return pp_objects[i].i_type;
141 static int vlc_object_search_mode_from_string( const char *psz_name )
146 const char *psz_name;
148 { { FIND_PARENT, "parent" },
149 { FIND_CHILD, "child" },
150 { FIND_ANYWHERE, "anywhere" },
153 for( i = 0; pp_modes[i].i_mode; i++ )
155 if( !strcmp( psz_name, pp_modes[i].psz_name ) )
156 return pp_modes[i].i_mode;
161 int vlclua_object_find( lua_State *L )
163 const char *psz_type = luaL_checkstring( L, 2 );
164 const char *psz_mode = luaL_checkstring( L, 3 );
166 vlc_object_t *p_this;
167 int i_type = vlc_object_type_from_string( psz_type );
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 object type.", psz_type );
174 return luaL_error( L, "\"%s\" is not a valid search mode.", psz_mode );
176 if( lua_type( L, 1 ) == LUA_TNIL )
177 p_this = vlclua_get_this( L );
179 p_this = vlclua_checkobject( L, 1, 0 );
181 p_result = vlc_object_find( p_this, i_type, i_mode );
185 vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
189 int vlclua_object_find_name( lua_State *L )
191 const char *psz_name = luaL_checkstring( L, 2 );
192 const char *psz_mode = luaL_checkstring( L, 3 );
194 vlc_object_t *p_this;
195 int i_mode = vlc_object_search_mode_from_string( psz_mode );
196 vlc_object_t *p_result;
199 return luaL_error( L, "\"%s\" is not a valid search mode.",
202 if( lua_type( L, 1 ) == LUA_TNIL )
203 p_this = vlclua_get_this( L );
205 p_this = vlclua_checkobject( L, 1, 0 );
207 p_result = vlc_object_find_name( p_this, psz_name, i_mode );
211 vlclua_push_vlc_object( L, p_result, vlclua_gc_release );