1 /*****************************************************************************
2 * vlm.c: Generic lua VLM wrapper
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
38 #include <lua.h> /* Low level lua C API */
39 #include <lauxlib.h> /* Higher level C API */
44 /*****************************************************************************
46 *****************************************************************************/
48 static int vlclua_vlm_delete( lua_State * );
49 static int vlclua_vlm_execute_command( lua_State * );
51 static const luaL_Reg vlclua_vlm_reg[] = {
52 { "execute_command", vlclua_vlm_execute_command },
56 static int vlclua_vlm_new( lua_State *L )
58 vlc_object_t *p_this = vlclua_get_this( L );
59 vlm_t *p_vlm = vlm_New( p_this );
61 return luaL_error( L, "Cannot start VLM." );
63 vlm_t **pp_vlm = lua_newuserdata( L, sizeof( vlm_t * ) );
66 if( luaL_newmetatable( L, "vlm" ) )
69 luaL_register( L, NULL, vlclua_vlm_reg );
70 lua_setfield( L, -2, "__index" );
71 lua_pushcfunction( L, vlclua_vlm_delete );
72 lua_setfield( L, -2, "__gc" );
75 lua_setmetatable( L, -2 );
79 static int vlclua_vlm_delete( lua_State *L )
81 vlm_t **pp_vlm = (vlm_t**)luaL_checkudata( L, 1, "vlm" );
82 vlm_Delete( *pp_vlm );
86 static void push_message( lua_State *L, vlm_message_t *message )
88 lua_createtable( L, 0, 2 );
89 lua_pushstring( L, message->psz_name );
90 lua_setfield( L, -2, "name" );
91 if( message->i_child > 0 )
94 lua_createtable( L, message->i_child, 0 );
95 for( i = 0; i < message->i_child; i++ )
97 lua_pushinteger( L, i+1 );
98 push_message( L, message->child[i] );
99 lua_settable( L, -3 );
101 lua_setfield( L, -2, "children" );
105 lua_pushstring( L, message->psz_value );
106 lua_setfield( L, -2, "value" );
110 static int vlclua_vlm_execute_command( lua_State *L )
112 vlm_t **pp_vlm = (vlm_t**)luaL_checkudata( L, 1, "vlm" );
113 const char *psz_command = luaL_checkstring( L, 2 );
114 vlm_message_t *message;
116 i_ret = vlm_ExecuteCommand( *pp_vlm, psz_command, &message );
118 push_message( L, message );
119 vlm_MessageDelete( message );
120 return 1 + vlclua_push_ret( L, i_ret );
124 static int vlclua_vlm_new( lua_State *L )
126 return luaL_error( L, "Cannot start VLM because it was disabled when compiling VLC." );
130 /*****************************************************************************
132 *****************************************************************************/
133 void luaopen_vlm( lua_State *L )
135 lua_pushcfunction( L, vlclua_vlm_new );
136 lua_setfield( L, -2, "vlm" );