Decklink: Fix swapped format arguments
[vlc.git] / modules / lua / libs / vlm.c
blobd8ee4d7a34b584013cb60ee7403201ed0201120f
1 /*****************************************************************************
2 * vlm.c: Generic lua VLM wrapper
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifndef _GNU_SOURCE
28 # define _GNU_SOURCE
29 #endif
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <vlc_common.h>
36 #include <vlc_vlm.h>
38 #include "../vlc.h"
39 #include "../libs.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 #ifdef ENABLE_VLM
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 },
50 { NULL, NULL }
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 );
57 if( !p_vlm )
58 return luaL_error( L, "Cannot start VLM." );
60 vlm_t **pp_vlm = lua_newuserdata( L, sizeof( vlm_t * ) );
61 *pp_vlm = p_vlm;
63 if( luaL_newmetatable( L, "vlm" ) )
65 lua_newtable( L );
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 );
73 return 1;
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 );
80 return 0;
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 )
90 int i;
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;
112 int i_ret;
113 i_ret = vlm_ExecuteCommand( *pp_vlm, psz_command, &message );
114 lua_settop( L, 0 );
115 push_message( L, message );
116 vlm_MessageDelete( message );
117 return 1 + vlclua_push_ret( L, i_ret );
120 #else
121 static int vlclua_vlm_new( lua_State *L )
123 return luaL_error( L, "Cannot start VLM because it was disabled when compiling VLC." );
125 #endif
127 /*****************************************************************************
129 *****************************************************************************/
130 void luaopen_vlm( lua_State *L )
132 lua_pushcfunction( L, vlclua_vlm_new );
133 lua_setfield( L, -2, "vlm" );