vlm.c 4.05 KB
Newer Older
1
/*****************************************************************************
2
 * vlm.c: Generic lua VLM wrapper
3
 *****************************************************************************
4
 * Copyright (C) 2007-2008 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea at videolan tod org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifndef  _GNU_SOURCE
#   define  _GNU_SOURCE
#endif

31 32 33 34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
36 37
#include <vlc_vlm.h>

38 39
#include "../vlc.h"
#include "../libs.h"
40 41 42 43

/*****************************************************************************
 *
 *****************************************************************************/
44
#ifdef ENABLE_VLM
45 46 47 48 49 50 51 52 53 54
static int vlclua_vlm_delete( lua_State * );
static int vlclua_vlm_execute_command( lua_State * );

static const luaL_Reg vlclua_vlm_reg[] = {
    { "execute_command", vlclua_vlm_execute_command },
    { NULL, NULL }
};

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        return luaL_error( L, "Cannot start VLM." );

    vlm_t **pp_vlm = lua_newuserdata( L, sizeof( vlm_t * ) );
    *pp_vlm = p_vlm;

    if( luaL_newmetatable( L, "vlm" ) )
    {
        lua_newtable( L );
        luaL_register( L, NULL, vlclua_vlm_reg );
        lua_setfield( L, -2, "__index" );
        lua_pushcfunction( L, vlclua_vlm_delete );
        lua_setfield( L, -2, "__gc" );
    }

    lua_setmetatable( L, -2 );
73 74 75
    return 1;
}

76
static int vlclua_vlm_delete( lua_State *L )
77
{
78 79
    vlm_t **pp_vlm = (vlm_t**)luaL_checkudata( L, 1, "vlm" );
    vlm_Delete( *pp_vlm );
80 81 82
    return 0;
}

83
static void push_message( lua_State *L, vlm_message_t *message )
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
    lua_createtable( L, 0, 2 );
    lua_pushstring( L, message->psz_name );
    lua_setfield( L, -2, "name" );
    if( message->i_child > 0 )
    {
        int i;
        lua_createtable( L, message->i_child, 0 );
        for( i = 0; i < message->i_child; i++ )
        {
            lua_pushinteger( L, i+1 );
            push_message( L, message->child[i] );
            lua_settable( L, -3 );
        }
        lua_setfield( L, -2, "children" );
    }
100
    if ( message->psz_value )
101 102 103 104 105 106
    {
        lua_pushstring( L, message->psz_value );
        lua_setfield( L, -2, "value" );
    }
}

107
static int vlclua_vlm_execute_command( lua_State *L )
108
{
109
    vlm_t **pp_vlm = (vlm_t**)luaL_checkudata( L, 1, "vlm" );
110 111
    const char *psz_command = luaL_checkstring( L, 2 );
    vlm_message_t *message;
112
    int i_ret;
113
    i_ret = vlm_ExecuteCommand( *pp_vlm, psz_command, &message );
114 115 116
    lua_settop( L, 0 );
    push_message( L, message );
    vlm_MessageDelete( message );
117
    return 1 + vlclua_push_ret( L, i_ret );
118
}
119

120
#else
121
static int vlclua_vlm_new( lua_State *L )
122
{
123
    return luaL_error( L, "Cannot start VLM because it was disabled when compiling VLC." );
124 125
}
#endif
126 127 128 129 130 131 132 133 134

/*****************************************************************************
 *
 *****************************************************************************/
void luaopen_vlm( lua_State *L )
{
    lua_pushcfunction( L, vlclua_vlm_new );
    lua_setfield( L, -2, "vlm" );
}