1 /*****************************************************************************
2 * variables.c: Generic lua<->vlc variables interface
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
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 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <lua.h> /* Low level lua C API */
38 #include <lauxlib.h> /* Higher level C API */
39 #include <lualib.h> /* Lua libs */
43 /*****************************************************************************
45 *****************************************************************************/
46 int vlclua_pushvalue( lua_State *L, int i_type, vlc_value_t val )
48 switch( i_type &= 0xf0 )
54 lua_pushboolean( L, val.b_bool );
57 lua_pushinteger( L, val.i_int );
60 lua_pushstring( L, val.psz_string );
63 lua_pushnumber( L, val.f_float );
66 /* FIXME? (we're losing some precision, but does it really matter?) */
67 lua_pushnumber( L, ((double)val.i_time)/1000000. );
77 int i_count = val.p_list->i_count;
79 lua_createtable( L, i_count, 0 );
80 for( i = 0; i < i_count; i++ )
82 lua_pushinteger( L, i+1 );
83 if( !vlclua_pushvalue( L, val.p_list->pi_types[i],
84 val.p_list->p_values[i] ) )
86 lua_settable( L, -3 );
96 static int vlclua_tovalue( lua_State *L, int i_type, vlc_value_t *val )
98 switch( i_type & 0xf0 )
103 val->b_bool = luaL_checkboolean( L, -1 );
105 case VLC_VAR_INTEGER:
106 val->i_int = luaL_checkint( L, -1 );
109 val->psz_string = (char*)luaL_checkstring( L, -1 ); /* XXX: Beware, this only stays valid as long as (L,-1) stays in the stack */
112 val->f_float = luaL_checknumber( L, -1 );
116 double f = luaL_checknumber( L, -1 );
117 val->i_time = (int64_t)(f*1000000.);
120 case VLC_VAR_ADDRESS:
135 int vlclua_var_get( lua_State *L )
139 vlc_object_t *p_obj = vlclua_checkobject( L, 1, 0 );
140 const char *psz_var = luaL_checkstring( L, 2 );
141 i_type = var_Type( p_obj, psz_var );
142 var_Get( p_obj, psz_var, &val );
144 return vlclua_pushvalue( L, i_type, val );
147 int vlclua_var_set( lua_State *L )
151 vlc_object_t *p_obj = vlclua_checkobject( L, 1, 0 );
152 const char *psz_var = luaL_checkstring( L, 2 );
154 i_type = var_Type( p_obj, psz_var );
155 vlclua_tovalue( L, i_type, &val );
156 i_ret = var_Set( p_obj, psz_var, val );
158 return vlclua_push_ret( L, i_ret );
161 int vlclua_var_get_list( lua_State *L )
165 vlc_object_t *p_obj = vlclua_checkobject( L, 1, 0 );
166 const char *psz_var = luaL_checkstring( L, 2 );
167 int i_ret = var_Change( p_obj, psz_var, VLC_VAR_GETLIST, &val, &text );
168 if( i_ret < 0 ) return vlclua_push_ret( L, i_ret );
169 vlclua_pushvalue( L, VLC_VAR_LIST, val );
170 vlclua_pushvalue( L, VLC_VAR_LIST, text );
171 var_Change( p_obj, psz_var, VLC_VAR_FREELIST, &val, &text );
175 int vlclua_module_command( lua_State *L )
177 vlc_object_t * p_this = vlclua_get_this( L );
178 const char *psz_name;
182 psz_name = luaL_checkstring( L, 1 );
183 psz_cmd = luaL_checkstring( L, 2 );
184 psz_arg = luaL_checkstring( L, 3 );
186 var_Command( p_this, psz_name, psz_cmd, psz_arg, &psz_msg );
189 lua_pushstring( L, psz_msg );
194 lua_pushstring( L, "" );
199 int vlclua_libvlc_command( lua_State *L )
201 vlc_object_t * p_this = vlclua_get_this( L );
204 psz_cmd = luaL_checkstring( L, 1 );
205 val_arg.psz_string = strdup( luaL_optstring( L, 2, "" ) );
207 if( !var_Type( p_this->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
209 free( val_arg.psz_string );
210 return luaL_error( L, "libvlc's \"%s\" is not a command",
214 return vlclua_push_ret( L,
215 var_Set( p_this->p_libvlc, psz_cmd, val_arg ) );