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>
41 /*****************************************************************************
43 *****************************************************************************/
45 static int vlclua_vlm_delete( lua_State
* );
46 static int vlclua_vlm_execute_command( lua_State
* );
48 static const luaL_Reg vlclua_vlm_reg
[] = {
49 { "execute_command", vlclua_vlm_execute_command
},
53 static int vlclua_vlm_new( lua_State
*L
)
55 vlc_object_t
*p_this
= vlclua_get_this( L
);
56 vlm_t
*p_vlm
= vlm_New( p_this
->obj
.libvlc
, NULL
);
58 return luaL_error( L
, "Cannot start VLM." );
60 vlm_t
**pp_vlm
= lua_newuserdata( L
, sizeof( vlm_t
* ) );
63 if( luaL_newmetatable( L
, "vlm" ) )
66 luaL_register( L
, NULL
, vlclua_vlm_reg
);
67 lua_setfield( L
, -2, "__index" );
68 lua_pushcfunction( L
, vlclua_vlm_delete
);
69 lua_setfield( L
, -2, "__gc" );
72 lua_setmetatable( L
, -2 );
76 static int vlclua_vlm_delete( lua_State
*L
)
78 vlm_t
**pp_vlm
= (vlm_t
**)luaL_checkudata( L
, 1, "vlm" );
79 vlm_Delete( *pp_vlm
);
83 static void push_message( lua_State
*L
, vlm_message_t
*message
)
85 lua_createtable( L
, 0, 2 );
86 lua_pushstring( L
, message
->psz_name
);
87 lua_setfield( L
, -2, "name" );
88 if( message
->i_child
> 0 )
91 lua_createtable( L
, message
->i_child
, 0 );
92 for( i
= 0; i
< message
->i_child
; i
++ )
94 lua_pushinteger( L
, i
+1 );
95 push_message( L
, message
->child
[i
] );
96 lua_settable( L
, -3 );
98 lua_setfield( L
, -2, "children" );
100 if ( message
->psz_value
)
102 lua_pushstring( L
, message
->psz_value
);
103 lua_setfield( L
, -2, "value" );
107 static int vlclua_vlm_execute_command( lua_State
*L
)
109 vlm_t
**pp_vlm
= (vlm_t
**)luaL_checkudata( L
, 1, "vlm" );
110 const char *psz_command
= luaL_checkstring( L
, 2 );
111 vlm_message_t
*message
;
113 i_ret
= vlm_ExecuteCommand( *pp_vlm
, psz_command
, &message
);
115 push_message( L
, message
);
116 vlm_MessageDelete( message
);
117 return 1 + vlclua_push_ret( L
, i_ret
);
121 static int vlclua_vlm_new( lua_State
*L
)
123 return luaL_error( L
, "Cannot start VLM because it was disabled when compiling VLC." );
127 /*****************************************************************************
129 *****************************************************************************/
130 void luaopen_vlm( lua_State
*L
)
132 lua_pushcfunction( L
, vlclua_vlm_new
);
133 lua_setfield( L
, -2, "vlm" );