]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/vlm.c
lua: remove "bla ? true : false"
[vlc] / modules / misc / lua / libs / vlm.c
1 /*****************************************************************************
2  * vlm.c: Generic lua VLM wrapper
3  *****************************************************************************
4  * Copyright (C) 2007-2008 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
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 /*****************************************************************************
45  *
46  *****************************************************************************/
47 #ifdef ENABLE_VLM
48 static int vlclua_vlm_delete( lua_State * );
49 static int vlclua_vlm_execute_command( lua_State * );
50
51 static const luaL_Reg vlclua_vlm_reg[] = {
52     { "execute_command", vlclua_vlm_execute_command },
53     { NULL, NULL }
54 };
55
56 static int vlclua_vlm_new( lua_State *L )
57 {
58     vlc_object_t *p_this = vlclua_get_this( L );
59     vlm_t *p_vlm = vlm_New( p_this );
60     if( !p_vlm )
61         return luaL_error( L, "Cannot start VLM." );
62
63     vlm_t **pp_vlm = lua_newuserdata( L, sizeof( vlm_t * ) );
64     *pp_vlm = p_vlm;
65
66     if( luaL_newmetatable( L, "vlm" ) )
67     {
68         lua_newtable( L );
69         luaL_register( L, NULL, vlclua_vlm_reg );
70         lua_setfield( L, -2, "__index" );
71         lua_pushcfunction( L, vlclua_vlm_delete );
72         lua_setfield( L, -2, "__gc" );
73     }
74
75     lua_setmetatable( L, -2 );
76     return 1;
77 }
78
79 static int vlclua_vlm_delete( lua_State *L )
80 {
81     vlm_t **pp_vlm = (vlm_t**)luaL_checkudata( L, 1, "vlm" );
82     vlm_Delete( *pp_vlm );
83     return 0;
84 }
85
86 static void push_message( lua_State *L, vlm_message_t *message )
87 {
88     lua_createtable( L, 0, 2 );
89     lua_pushstring( L, message->psz_name );
90     lua_setfield( L, -2, "name" );
91     if( message->i_child > 0 )
92     {
93         int i;
94         lua_createtable( L, message->i_child, 0 );
95         for( i = 0; i < message->i_child; i++ )
96         {
97             lua_pushinteger( L, i+1 );
98             push_message( L, message->child[i] );
99             lua_settable( L, -3 );
100         }
101         lua_setfield( L, -2, "children" );
102     }
103     if ( message->psz_value )
104     {
105         lua_pushstring( L, message->psz_value );
106         lua_setfield( L, -2, "value" );
107     }
108 }
109
110 static int vlclua_vlm_execute_command( lua_State *L )
111 {
112     vlm_t **pp_vlm = (vlm_t**)luaL_checkudata( L, 1, "vlm" );
113     const char *psz_command = luaL_checkstring( L, 2 );
114     vlm_message_t *message;
115     int i_ret;
116     i_ret = vlm_ExecuteCommand( *pp_vlm, psz_command, &message );
117     lua_settop( L, 0 );
118     push_message( L, message );
119     vlm_MessageDelete( message );
120     return 1 + vlclua_push_ret( L, i_ret );
121 }
122
123 #else
124 static int vlclua_vlm_new( lua_State *L )
125 {
126     return luaL_error( L, "Cannot start VLM because it was disabled when compiling VLC." );
127 }
128 #endif
129
130 /*****************************************************************************
131  *
132  *****************************************************************************/
133 void luaopen_vlm( lua_State *L )
134 {
135     lua_pushcfunction( L, vlclua_vlm_new );
136     lua_setfield( L, -2, "vlm" );
137 }