]> git.sesse.net Git - vlc/blob - modules/misc/lua/objects.c
Add a new type of VLC Lua module: Interfaces.
[vlc] / modules / misc / lua / objects.c
1 /*****************************************************************************
2  * objects.c: Generic lua<->vlc object wrapper
3  *****************************************************************************
4  * Copyright (C) 2007 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 #include <vlc/vlc.h>
32
33 #include <lua.h>        /* Low level lua C API */
34 #include <lauxlib.h>    /* Higher level C API */
35 #include <lualib.h>     /* Lua libs */
36
37 #include "vlc.h"
38
39 typedef struct
40 {
41     vlc_object_t *p_obj;
42 } vlclua_object_t;
43
44 /*****************************************************************************
45  * Generic vlc_object_t wrapper creation
46  *****************************************************************************/
47 int __vlclua_push_vlc_object( lua_State *L, vlc_object_t *p_obj,
48                               lua_CFunction pf_gc )
49 {
50     vlclua_object_t *p_ud = (vlclua_object_t *)
51         lua_newuserdata( L, sizeof( vlclua_object_t ) );
52     p_ud->p_obj = p_obj;
53     lua_newtable( L );
54     /* Hide the metatable */
55     lua_pushstring( L, "__metatable" );
56     lua_pushstring( L, "none of your business" );
57     lua_settable( L, -3 );
58     if( pf_gc )
59     {
60         /* Set the garbage collector if needed */
61         lua_pushstring( L, "__gc" );
62         lua_pushcfunction( L, pf_gc );
63         lua_settable( L, -3 );
64     }
65     lua_setmetatable( L, -2 );
66     return 1;
67 }
68
69 int vlclua_gc_release( lua_State *L )
70 {
71     vlclua_object_t *p_ud = (vlclua_object_t *)lua_touserdata( L, -1 );
72     lua_pop( L, 1 );
73     vlc_object_release( p_ud->p_obj );
74     return 0;
75 }
76
77 vlc_object_t *vlclua_checkobject( lua_State *L, int narg, int i_type )
78 {
79     vlclua_object_t *p_obj = (vlclua_object_t *)luaL_checkuserdata( L, narg, sizeof( vlclua_object_t ) );
80     /* TODO: add some metatable based method to check that this isn't
81      * any userdata, but really some vlc object */
82     if( i_type )
83     {
84         if( p_obj->p_obj->i_object_type == i_type )
85             return p_obj->p_obj;
86         else
87         {
88             luaL_error( L, "VLC object type doesn't match requirements." );
89             return NULL; /* luaL_error alread longjmp-ed out of here.
90                           * This is to make gcc happy */
91         }
92     }
93     else
94     {
95         return p_obj->p_obj;
96     }
97 }
98
99 static int vlc_object_type_from_string( const char *psz_name )
100 {
101     static const struct
102     {
103         int i_type;
104         const char *psz_name;
105     } pp_objects[] =
106         { { VLC_OBJECT_GLOBAL, "global" },
107           { VLC_OBJECT_LIBVLC, "libvlc" },
108           { VLC_OBJECT_MODULE, "module" },
109           { VLC_OBJECT_INTF, "intf" },
110           { VLC_OBJECT_PLAYLIST, "playlist" },
111           { VLC_OBJECT_ITEM, "item" },
112           { VLC_OBJECT_INPUT, "input" },
113           { VLC_OBJECT_DECODER, "decoder" },
114           { VLC_OBJECT_VOUT, "vout" },
115           { VLC_OBJECT_AOUT, "aout" },
116           { VLC_OBJECT_SOUT, "sout" },
117           { VLC_OBJECT_HTTPD, "httpd" },
118           { VLC_OBJECT_PACKETIZER, "packetizer" },
119           { VLC_OBJECT_ENCODER, "encoder" },
120           { VLC_OBJECT_DIALOGS, "dialogs" },
121           { VLC_OBJECT_VLM, "vlm" },
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_VOD, "vod" },
129           { VLC_OBJECT_SPU, "spu" },
130           { VLC_OBJECT_SD, "sd" },
131           { VLC_OBJECT_XML, "xml" },
132           { VLC_OBJECT_OSDMENU, "osdmenu" },
133           { VLC_OBJECT_HTTPD_HOST, "httpd_host" },
134           { VLC_OBJECT_META_ENGINE, "meta_engine" },
135           { VLC_OBJECT_GENERIC, "generic" },
136           { 0, "" } };
137     int i;
138     for( i = 0; pp_objects[i].i_type; i++ )
139     {
140         if( !strcmp( psz_name, pp_objects[i].psz_name ) )
141             return pp_objects[i].i_type;
142     }
143     return 0;
144 }
145
146 static int vlc_object_search_mode_from_string( const char *psz_name )
147 {
148     static const struct
149     {
150         int i_mode;
151         const char *psz_name;
152     } pp_modes[] =
153         { { FIND_PARENT, "parent" },
154           { FIND_CHILD, "child" },
155           { FIND_ANYWHERE, "anywhere" },
156           { 0, "" } };
157     int i;
158     for( i = 0; pp_modes[i].i_mode; i++ )
159     {
160         if( !strcmp( psz_name, pp_modes[i].psz_name ) )
161             return pp_modes[i].i_mode;
162     }
163     return 0;
164 }
165
166 int vlclua_object_find( lua_State *L )
167 {
168     const char *psz_type = luaL_checkstring( L, 2 );
169     const char *psz_mode = luaL_checkstring( L, 3 );
170
171     vlc_object_t *p_this;
172     int i_type = vlc_object_type_from_string( psz_type );
173     int i_mode = vlc_object_search_mode_from_string( psz_mode );
174     vlc_object_t *p_result;
175
176     if( !i_type )
177         return luaL_error( L, "\"%s\" is not a valid object type.", psz_type );
178     if( !i_mode )
179         return luaL_error( L, "\"%s\" is not a valid search mode.", psz_mode );
180
181     if( lua_type( L, 1 ) == LUA_TNIL )
182         p_this = vlclua_get_this( L );
183     else
184         p_this = vlclua_checkobject( L, 1, 0 );
185
186     p_result = vlc_object_find( p_this, i_type, i_mode );
187     if( !p_result )
188         lua_pushnil( L );
189     else
190         vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
191     return 1;
192 }
193
194 int vlclua_object_find_name( lua_State *L )
195 {
196     const char *psz_name = luaL_checkstring( L, 2 );
197     const char *psz_mode = luaL_checkstring( L, 3 );
198
199     vlc_object_t *p_this;
200     int i_mode = vlc_object_search_mode_from_string( psz_mode );
201     vlc_object_t *p_result;
202
203     if( !i_mode )
204         return luaL_error( L, "\"%s\" is not a valid search mode.",
205                            psz_mode );
206
207     if( lua_type( L, 1 ) == LUA_TNIL )
208         p_this = vlclua_get_this( L );
209     else
210         p_this = vlclua_checkobject( L, 1, 0 );
211
212     p_result = vlc_object_find_name( p_this, psz_name, i_mode );
213     if( !p_result )
214         lua_pushnil( L );
215     else
216         vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
217     return 1;
218 }