]> git.sesse.net Git - vlc/blob - modules/misc/lua/objects.c
65f1b5e943d28048ba38132d1cabc5826db1f88a
[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 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc/vlc.h>
36
37 #include <lua.h>        /* Low level lua C API */
38 #include <lauxlib.h>    /* Higher level C API */
39 #include <lualib.h>     /* Lua libs */
40
41 #include "vlc.h"
42
43 typedef struct
44 {
45     vlc_object_t *p_obj;
46 } vlclua_object_t;
47
48 /*****************************************************************************
49  * Generic vlc_object_t wrapper creation
50  *****************************************************************************/
51 int __vlclua_push_vlc_object( lua_State *L, vlc_object_t *p_obj,
52                               lua_CFunction pf_gc )
53 {
54     vlclua_object_t *p_ud = (vlclua_object_t *)
55         lua_newuserdata( L, sizeof( vlclua_object_t ) );
56     p_ud->p_obj = p_obj;
57     lua_newtable( L );
58     /* Hide the metatable */
59     lua_pushstring( L, "__metatable" );
60     lua_pushstring( L, "none of your business" );
61     lua_settable( L, -3 );
62     if( pf_gc )
63     {
64         /* Set the garbage collector if needed */
65         lua_pushstring( L, "__gc" );
66         lua_pushcfunction( L, pf_gc );
67         lua_settable( L, -3 );
68     }
69     lua_setmetatable( L, -2 );
70     return 1;
71 }
72
73 int vlclua_gc_release( lua_State *L )
74 {
75     vlclua_object_t *p_ud = (vlclua_object_t *)lua_touserdata( L, -1 );
76     lua_pop( L, 1 );
77     vlc_object_release( p_ud->p_obj );
78     return 0;
79 }
80
81 vlc_object_t *vlclua_checkobject( lua_State *L, int narg, int i_type )
82 {
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 */
86     if( i_type )
87     {
88         if( p_obj->p_obj->i_object_type == i_type )
89             return p_obj->p_obj;
90         else
91         {
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 */
95         }
96     }
97     else
98     {
99         return p_obj->p_obj;
100     }
101 }
102
103 static int vlc_object_type_from_string( const char *psz_name )
104 {
105     static const struct
106     {
107         int i_type;
108         const char *psz_name;
109     } pp_objects[] =
110         { { VLC_OBJECT_GLOBAL, "global" },
111           { VLC_OBJECT_LIBVLC, "libvlc" },
112           { VLC_OBJECT_MODULE, "module" },
113           { VLC_OBJECT_INTF, "intf" },
114           { VLC_OBJECT_PLAYLIST, "playlist" },
115           { VLC_OBJECT_INPUT, "input" },
116           { VLC_OBJECT_DECODER, "decoder" },
117           { VLC_OBJECT_VOUT, "vout" },
118           { VLC_OBJECT_AOUT, "aout" },
119           { VLC_OBJECT_SOUT, "sout" },
120           { VLC_OBJECT_HTTPD, "httpd" },
121           { VLC_OBJECT_PACKETIZER, "packetizer" },
122           { VLC_OBJECT_ENCODER, "encoder" },
123           { VLC_OBJECT_DIALOGS, "dialogs" },
124           { VLC_OBJECT_VLM, "vlm" },
125           { VLC_OBJECT_ANNOUNCE, "announce" },
126           { VLC_OBJECT_DEMUX, "demux" },
127           { VLC_OBJECT_ACCESS, "access" },
128           { VLC_OBJECT_STREAM, "stream" },
129           { VLC_OBJECT_OPENGL, "opengl" },
130           { VLC_OBJECT_FILTER, "filter" },
131           { VLC_OBJECT_OSDMENU, "osdmenu" },
132           { VLC_OBJECT_HTTPD_HOST, "httpd_host" },
133           { VLC_OBJECT_GENERIC, "generic" },
134           { 0, "" } };
135     int i;
136     for( i = 0; pp_objects[i].i_type; i++ )
137     {
138         if( !strcmp( psz_name, pp_objects[i].psz_name ) )
139             return pp_objects[i].i_type;
140     }
141     return 0;
142 }
143
144 static int vlc_object_search_mode_from_string( const char *psz_name )
145 {
146     static const struct
147     {
148         int i_mode;
149         const char *psz_name;
150     } pp_modes[] =
151         { { FIND_PARENT, "parent" },
152           { FIND_CHILD, "child" },
153           { FIND_ANYWHERE, "anywhere" },
154           { 0, "" } };
155     int i;
156     for( i = 0; pp_modes[i].i_mode; i++ )
157     {
158         if( !strcmp( psz_name, pp_modes[i].psz_name ) )
159             return pp_modes[i].i_mode;
160     }
161     return 0;
162 }
163
164 int vlclua_object_find( lua_State *L )
165 {
166     const char *psz_type = luaL_checkstring( L, 2 );
167     const char *psz_mode = luaL_checkstring( L, 3 );
168
169     vlc_object_t *p_this;
170     int i_type = vlc_object_type_from_string( psz_type );
171     int i_mode = vlc_object_search_mode_from_string( psz_mode );
172     vlc_object_t *p_result;
173
174     if( !i_type )
175         return luaL_error( L, "\"%s\" is not a valid object type.", psz_type );
176     if( !i_mode )
177         return luaL_error( L, "\"%s\" is not a valid search mode.", psz_mode );
178
179     if( lua_type( L, 1 ) == LUA_TNIL )
180         p_this = vlclua_get_this( L );
181     else
182         p_this = vlclua_checkobject( L, 1, 0 );
183
184     p_result = vlc_object_find( p_this, i_type, 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 int vlclua_object_find_name( lua_State *L )
193 {
194     const char *psz_name = luaL_checkstring( L, 2 );
195     const char *psz_mode = luaL_checkstring( L, 3 );
196
197     vlc_object_t *p_this;
198     int i_mode = vlc_object_search_mode_from_string( psz_mode );
199     vlc_object_t *p_result;
200
201     if( !i_mode )
202         return luaL_error( L, "\"%s\" is not a valid search mode.",
203                            psz_mode );
204
205     if( lua_type( L, 1 ) == LUA_TNIL )
206         p_this = vlclua_get_this( L );
207     else
208         p_this = vlclua_checkobject( L, 1, 0 );
209
210     p_result = vlc_object_find_name( p_this, psz_name, i_mode );
211     if( !p_result )
212         lua_pushnil( L );
213     else
214         vlclua_push_vlc_object( L, p_result, vlclua_gc_release );
215     return 1;
216 }