]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlm.c
cbdf5ba87eb6357bcdadd78d78908614bb5fcde5
[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 #include <vlc/vlc.h>
32 #include <vlc_vlm.h>
33
34 #include <lua.h>        /* Low level lua C API */
35 #include <lauxlib.h>    /* Higher level C API */
36 #include <lualib.h>     /* Lua libs */
37
38 #include "vlc.h"
39
40 /*****************************************************************************
41  *
42  *****************************************************************************/
43 int vlclua_vlm_new( lua_State *L )
44 {
45     vlc_object_t *p_this = vlclua_get_this( L );
46     vlm_t *p_vlm = vlm_New( p_this );
47     if( !p_vlm )
48         return luaL_error( L, "Cannot start VLM." );
49     __vlclua_push_vlc_object( L, (vlc_object_t*)p_vlm, NULL );
50     return 1;
51 }
52
53 int vlclua_vlm_delete( lua_State *L )
54 {
55     vlm_t *p_vlm = (vlm_t*)vlclua_checkobject( L, 1, VLC_OBJECT_VLM );
56     vlm_Delete( p_vlm );
57     return 0;
58 }
59
60 void push_message( lua_State *L, vlm_message_t *message );
61 void push_message( lua_State *L, vlm_message_t *message )
62 {
63     lua_createtable( L, 0, 2 );
64     lua_pushstring( L, message->psz_name );
65     lua_setfield( L, -2, "name" );
66     if( message->i_child > 0 )
67     {
68         int i;
69         lua_createtable( L, message->i_child, 0 );
70         for( i = 0; i < message->i_child; i++ )
71         {
72             lua_pushinteger( L, i+1 );
73             push_message( L, message->child[i] );
74             lua_settable( L, -3 );
75         }
76         lua_setfield( L, -2, "children" );
77     }
78     else
79     {
80         lua_pushstring( L, message->psz_value );
81         lua_setfield( L, -2, "value" );
82     }
83 }
84
85 int vlclua_vlm_execute_command( lua_State *L )
86 {
87     vlm_t *p_vlm = (vlm_t*)vlclua_checkobject( L, 1, VLC_OBJECT_VLM );
88     const char *psz_command = luaL_checkstring( L, 2 );
89     vlm_message_t *message;
90     vlm_ExecuteCommand( p_vlm, psz_command, &message );
91     lua_settop( L, 0 );
92     push_message( L, message );
93     vlm_MessageDelete( message );
94     return 1;
95 }