]> git.sesse.net Git - vlc/blob - modules/lua/libs/objects.c
LUA: remove find_name() before something starts to use it
[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
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_pushliteral( 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 vlclua_object_find( lua_State *L )
86 {
87     lua_pushnil( L );
88     return 1;
89 }
90
91 static int vlclua_get_libvlc( lua_State *L )
92 {
93     vlclua_push_vlc_object( L, vlclua_get_this( L )->p_libvlc,
94                             NULL );
95     return 1;
96 }
97
98 static int vlclua_get_playlist( lua_State *L )
99 {
100     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
101     if( p_playlist )
102     {
103         vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
104         vlc_object_hold( p_playlist );
105     }
106     else lua_pushnil( L );
107     return 1;
108 }
109
110 static int vlclua_get_input( lua_State *L )
111 {
112     input_thread_t *p_input = vlclua_get_input_internal( L );
113     if( p_input )
114     {
115         vlclua_push_vlc_object( L, p_input, vlclua_gc_release );
116     }
117     else lua_pushnil( L );
118     return 1;
119 }
120
121 /*****************************************************************************
122  *
123  *****************************************************************************/
124 static const luaL_Reg vlclua_object_reg[] = {
125     { "input", vlclua_get_input },
126     { "playlist", vlclua_get_playlist },
127     { "libvlc", vlclua_get_libvlc },
128     { "find", vlclua_object_find },
129     { NULL, NULL }
130 };
131
132 void luaopen_object( lua_State *L )
133 {
134     lua_newtable( L );
135     luaL_register( L, NULL, vlclua_object_reg );
136     lua_setfield( L, -2, "object" );
137 }