]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlm.c
Fix test program.
[vlc] / modules / misc / lua / vlm.c
1 /*****************************************************************************
2  * objects.c: Generic lua VLM wrapper
3  *****************************************************************************
4  * Copyright (C) 2007 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_vlm.h>
37
38 #include <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40 #include <lualib.h>     /* Lua libs */
41
42 #include "vlc.h"
43
44 /*****************************************************************************
45  *
46  *****************************************************************************/
47 int vlclua_vlm_new( lua_State *L )
48 {
49 #ifdef ENABLE_VLM
50     vlc_object_t *p_this = vlclua_get_this( L );
51     vlm_t *p_vlm = vlm_New( p_this );
52     if( !p_vlm )
53         goto err;
54     __vlclua_push_vlc_object( L, (vlc_object_t*)p_vlm, NULL );
55     return 1;
56 err:
57 #endif
58     return luaL_error( L, "Cannot start VLM." );
59 }
60
61 int vlclua_vlm_delete( lua_State *L )
62 {
63 #ifdef ENABLE_VLM
64     vlm_t *p_vlm = (vlm_t*)vlclua_checkobject( L, 1, VLC_OBJECT_GENERIC );
65     vlm_Delete( p_vlm );
66 #endif
67     return 0;
68 }
69
70 #ifdef ENABLE_VLM
71 static void push_message( lua_State *L, vlm_message_t *message )
72 {
73     lua_createtable( L, 0, 2 );
74     lua_pushstring( L, message->psz_name );
75     lua_setfield( L, -2, "name" );
76     if( message->i_child > 0 )
77     {
78         int i;
79         lua_createtable( L, message->i_child, 0 );
80         for( i = 0; i < message->i_child; i++ )
81         {
82             lua_pushinteger( L, i+1 );
83             push_message( L, message->child[i] );
84             lua_settable( L, -3 );
85         }
86         lua_setfield( L, -2, "children" );
87     }
88     else
89     {
90         lua_pushstring( L, message->psz_value );
91         lua_setfield( L, -2, "value" );
92     }
93 }
94
95 int vlclua_vlm_execute_command( lua_State *L )
96 {
97     vlm_t *p_vlm = (vlm_t*)vlclua_checkobject( L, 1, VLC_OBJECT_GENERIC );
98     const char *psz_command = luaL_checkstring( L, 2 );
99     vlm_message_t *message;
100     int i_ret;
101     i_ret = vlm_ExecuteCommand( p_vlm, psz_command, &message );
102     lua_settop( L, 0 );
103     push_message( L, message );
104     vlm_MessageDelete( message );
105     return 1 + vlclua_push_ret( L, i_ret );
106 }
107 #else
108 int vlclua_vlm_execute_command( lua_State *L )
109 {
110     return 1 + vlclua_push_ret( L, VLC_EGENERIC );
111 }
112 #endif