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