]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/objects.c
2166657a54dda4e2b4ae6c9d64ecdc6ce9d4a636
[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_MODULE, "module" },
94           { VLC_OBJECT_INTF, "intf" },
95           { VLC_OBJECT_INPUT, "input" },
96           { VLC_OBJECT_DECODER, "decoder" },
97           { VLC_OBJECT_VOUT, "vout" },
98           { VLC_OBJECT_AOUT, "aout" },
99           { VLC_OBJECT_PACKETIZER, "packetizer" },
100           { VLC_OBJECT_ENCODER, "encoder" },
101           { VLC_OBJECT_ANNOUNCE, "announce" },
102           { VLC_OBJECT_OPENGL, "opengl" },
103           { VLC_OBJECT_FILTER, "filter" },
104           { VLC_OBJECT_OSDMENU, "osdmenu" },
105           { VLC_OBJECT_GENERIC, "generic" },
106           { 0, "" } };
107     int i;
108     for( i = 0; pp_objects[i].i_type; i++ )
109     {
110         if( !strcmp( psz_name, pp_objects[i].psz_name ) )
111             return pp_objects[i].i_type;
112     }
113     return 0;
114 }
115
116 static int vlc_object_search_mode_from_string( const char *psz_name )
117 {
118     static const struct
119     {
120         int i_mode;
121         const char *psz_name;
122     } pp_modes[] =
123         { { FIND_PARENT, "parent" },
124           { FIND_CHILD, "child" },
125           { FIND_ANYWHERE, "anywhere" },
126           { 0, "" } };
127     int i;
128     for( i = 0; pp_modes[i].i_mode; i++ )
129     {
130         if( !strcmp( psz_name, pp_modes[i].psz_name ) )
131             return pp_modes[i].i_mode;
132     }
133     return 0;
134 }
135
136 static int vlclua_object_find( lua_State *L )
137 {
138     const char *psz_type = luaL_checkstring( L, 2 );
139     const char *psz_mode = luaL_checkstring( L, 3 );
140
141     vlc_object_t *p_this;
142     int i_type = vlc_object_type_from_string( psz_type );
143     int i_mode = vlc_object_search_mode_from_string( psz_mode );
144     vlc_object_t *p_result;
145
146     if( !i_type )
147         return luaL_error( L, "\"%s\" is not a valid object type.", psz_type );
148     if( !i_mode )
149         return luaL_error( L, "\"%s\" is not a valid search mode.", psz_mode );
150
151     if( lua_type( L, 1 ) == LUA_TNIL )
152         p_this = vlclua_get_this( L );
153     else
154     {
155         vlc_object_t **p_obj = luaL_checkudata( L, 1, "vlc_object" );
156         p_this = *p_obj;
157     }
158
159     p_result = vlc_object_find( p_this, i_type, i_mode );
160     if( !p_result )
161         lua_pushnil( L );
162     else
163         vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
164     return 1;
165 }
166
167 static int vlclua_object_find_name( lua_State *L )
168 {
169     const char *psz_name = luaL_checkstring( L, 2 );
170     const char *psz_mode = luaL_checkstring( L, 3 );
171
172     vlc_object_t *p_this;
173     int i_mode = vlc_object_search_mode_from_string( psz_mode );
174     vlc_object_t *p_result;
175
176     if( !i_mode )
177         return luaL_error( L, "\"%s\" is not a valid search mode.",
178                            psz_mode );
179
180     if( lua_type( L, 1 ) == LUA_TNIL )
181         p_this = vlclua_get_this( L );
182     else
183     {
184         vlc_object_t **p_obj = luaL_checkudata( L, 1, "vlc_object" );
185         p_this = *p_obj;
186     }
187
188     p_result = vlc_object_find_name( p_this, psz_name, i_mode );
189     if( !p_result )
190         lua_pushnil( L );
191     else
192         vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
193     return 1;
194 }
195
196 static int vlclua_get_libvlc( lua_State *L )
197 {
198     vlclua_push_vlc_object( L, vlclua_get_this( L )->p_libvlc,
199                             NULL );
200     return 1;
201 }
202
203 static int vlclua_get_playlist( lua_State *L )
204 {
205     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
206     if( p_playlist )
207     {
208         vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
209     }
210     else lua_pushnil( L );
211     return 1;
212 }
213
214 static int vlclua_get_input( lua_State *L )
215 {
216     input_thread_t *p_input = vlclua_get_input_internal( L );
217     if( p_input )
218     {
219         vlclua_push_vlc_object( L, p_input, vlclua_gc_release );
220     }
221     else lua_pushnil( L );
222     return 1;
223 }
224
225 /*****************************************************************************
226  *
227  *****************************************************************************/
228 static const luaL_Reg vlclua_object_reg[] = {
229     { "input", vlclua_get_input },
230     { "playlist", vlclua_get_playlist },
231     { "libvlc", vlclua_get_libvlc },
232     { "find", vlclua_object_find },
233     { "find_name", vlclua_object_find_name },
234     { NULL, NULL }
235 };
236
237 void luaopen_object( lua_State *L )
238 {
239     lua_newtable( L );
240     luaL_register( L, NULL, vlclua_object_reg );
241     lua_setfield( L, -2, "object" );
242 }