]> git.sesse.net Git - vlc/blob - modules/lua/libs/objects.c
LUA:Expose aout
[vlc] / modules / 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 #include <vlc_vout.h>
37
38 #include <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40
41 #include "../vlc.h"
42 #include "../libs.h"
43 #include "objects.h"
44 #include "playlist.h"
45 #include "input.h"
46
47 /*****************************************************************************
48  * Generic vlc_object_t wrapper creation
49  *****************************************************************************/
50 static int vlclua_object_release( lua_State *L )
51 {
52     vlc_object_t **p_obj = (vlc_object_t **)luaL_checkudata( L, 1, "vlc_object" );
53     lua_pop( L, 1 );
54     vlc_object_release( *p_obj );
55     return 0;
56 }
57
58 static int vlclua_object_find( lua_State *L )
59 {
60     lua_pushnil( L );
61     return 1;
62 }
63
64 static int vlclua_get_libvlc( lua_State *L )
65 {
66     libvlc_int_t *p_libvlc = vlclua_get_this( L )->p_libvlc;
67     vlc_object_hold( p_libvlc );
68     vlclua_push_vlc_object( L, p_libvlc );
69     return 1;
70 }
71
72 static int vlclua_get_playlist( lua_State *L )
73 {
74     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
75     if( p_playlist )
76     {
77         vlc_object_hold( p_playlist );
78         vlclua_push_vlc_object( L, p_playlist );
79     }
80     else lua_pushnil( L );
81     return 1;
82 }
83
84 static int vlclua_get_input( lua_State *L )
85 {
86     input_thread_t *p_input = vlclua_get_input_internal( L );
87     if( p_input )
88     {
89         /* NOTE: p_input is already held by vlclua_get_input_internal() */
90         vlclua_push_vlc_object( L, p_input );
91     }
92     else lua_pushnil( L );
93     return 1;
94 }
95
96 #undef vlclua_push_vlc_object
97 int vlclua_push_vlc_object( lua_State *L, vlc_object_t *p_obj )
98 {
99     vlc_object_t **udata = (vlc_object_t **)
100         lua_newuserdata( L, sizeof( vlc_object_t * ) );
101     *udata = p_obj;
102
103     if( luaL_newmetatable( L, "vlc_object" ) )
104     {
105         /* Hide the metatable */
106         lua_pushliteral( L, "none of your business" );
107         lua_setfield( L, -2, "__metatable" );
108         /* Set the garbage collector if needed */
109         lua_pushcfunction( L, vlclua_object_release );
110         lua_setfield( L, -2, "__gc" );
111     }
112     lua_setmetatable( L, -2 );
113     return 1;
114 }
115 static int vlclua_get_vout( lua_State *L )
116 {
117     input_thread_t *p_input= vlclua_get_input_internal( L );
118     if( p_input )
119     {
120         vout_thread_t *p_vout = input_GetVout( p_input );
121         vlc_object_release( p_input );
122         if(p_vout)
123         {
124             vlclua_push_vlc_object( L, (vlc_object_t *) p_vout );
125             return 1;
126         }
127     }
128     lua_pushnil( L );
129     return 1;
130 }
131 static int vlclua_get_aout( lua_State *L )
132 {
133     input_thread_t *p_input= vlclua_get_input_internal( L );
134     if( p_input )
135     {
136         audio_output_t *p_aout = input_GetAout( p_input );
137         vlc_object_release(p_input);
138         if(p_aout)
139         {
140             vlclua_push_vlc_object( L, (vlc_object_t *)p_aout );
141             return 1;
142         }
143     }
144     lua_pushnil( L );
145     return 1;
146 }
147 /*****************************************************************************
148  *
149  *****************************************************************************/
150 static const luaL_Reg vlclua_object_reg[] = {
151     { "input", vlclua_get_input },
152     { "playlist", vlclua_get_playlist },
153     { "libvlc", vlclua_get_libvlc },
154     { "find", vlclua_object_find },
155     { "vout", vlclua_get_vout},
156     { "aout", vlclua_get_aout},
157     { NULL, NULL }
158 };
159
160 void luaopen_object( lua_State *L )
161 {
162     lua_newtable( L );
163     luaL_register( L, NULL, vlclua_object_reg );
164     lua_setfield( L, -2, "object" );
165 }