1 /*****************************************************************************
2 * variables.c: Generic lua<->vlc variables interface
3 *****************************************************************************
4 * Copyright (C) 2007-2008 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 #include "variables.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 int vlclua_pushvalue( lua_State *L, int i_type, vlc_value_t val )
51 switch( i_type &= VLC_VAR_CLASS )
57 lua_pushboolean( L, val.b_bool );
60 lua_pushinteger( L, val.i_int );
63 lua_pushstring( L, val.psz_string );
66 lua_pushnumber( L, val.f_float );
69 /* FIXME? (we're losing some precision, but does it really matter?) */
70 lua_pushnumber( L, ((double)val.i_time)/1000000. );
80 int i_count = val.p_list->i_count;
82 lua_createtable( L, i_count, 0 );
83 for( i = 0; i < i_count; i++ )
85 lua_pushinteger( L, i+1 );
86 if( !vlclua_pushvalue( L, val.p_list->pi_types[i],
87 val.p_list->p_values[i] ) )
89 lua_settable( L, -3 );
99 static int vlclua_tovalue( lua_State *L, int i_type, vlc_value_t *val )
101 switch( i_type & VLC_VAR_CLASS )
106 val->b_bool = luaL_checkboolean( L, -1 );
108 case VLC_VAR_INTEGER:
109 val->i_int = luaL_checkint( L, -1 );
112 val->psz_string = (char*)luaL_checkstring( L, -1 ); /* XXX: Beware, this only stays valid as long as (L,-1) stays in the stack */
115 val->f_float = luaL_checknumber( L, -1 );
119 double f = luaL_checknumber( L, -1 );
120 val->i_time = (int64_t)(f*1000000.);
123 case VLC_VAR_ADDRESS:
138 static int vlclua_var_get( lua_State *L )
142 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
143 const char *psz_var = luaL_checkstring( L, 2 );
144 i_type = var_Type( *pp_obj, psz_var );
145 if( var_Get( *pp_obj, psz_var, &val ) != VLC_SUCCESS )
148 return vlclua_pushvalue( L, i_type, val );
151 static int vlclua_var_set( lua_State *L )
155 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
156 const char *psz_var = luaL_checkstring( L, 2 );
158 i_type = var_Type( *pp_obj, psz_var );
159 vlclua_tovalue( L, i_type, &val );
160 i_ret = var_Set( *pp_obj, psz_var, val );
162 return vlclua_push_ret( L, i_ret );
165 static int vlclua_var_create( lua_State *L )
167 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
168 const char *psz_var = luaL_checkstring( L, 2 );
170 switch( lua_type( L, 3 ) )
173 i_type = VLC_VAR_FLOAT;
176 i_type = VLC_VAR_BOOL;
179 i_type = VLC_VAR_STRING;
185 int i_ret = var_Create( *pp_obj, psz_var, i_type );
186 if( i_ret != VLC_SUCCESS )
187 return vlclua_push_ret( L, i_ret );
189 vlclua_tovalue( L, i_type, &val );
190 return vlclua_push_ret( L, var_Set( *pp_obj, psz_var, val ) );
193 static int vlclua_var_get_list( lua_State *L )
197 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
198 const char *psz_var = luaL_checkstring( L, 2 );
199 int i_ret = var_Change( *pp_obj, psz_var, VLC_VAR_GETLIST, &val, &text );
200 if( i_ret < 0 ) return vlclua_push_ret( L, i_ret );
201 vlclua_pushvalue( L, VLC_VAR_LIST, val );
202 vlclua_pushvalue( L, VLC_VAR_LIST, text );
203 var_FreeList( &val, &text );
207 static int vlclua_command( lua_State *L )
209 vlc_object_t * p_this = vlclua_get_this( L );
210 const char *psz_name;
215 psz_name = luaL_checkstring( L, 1 );
216 psz_cmd = luaL_checkstring( L, 2 );
217 psz_arg = luaL_checkstring( L, 3 );
219 ret = var_Command( p_this, psz_name, psz_cmd, psz_arg, &psz_msg );
222 lua_pushstring( L, psz_msg );
227 lua_pushliteral( L, "" );
229 return vlclua_push_ret( L, ret ) + 1;
232 static int vlclua_libvlc_command( lua_State *L )
234 vlc_object_t * p_this = vlclua_get_this( L );
237 psz_cmd = luaL_checkstring( L, 1 );
238 val_arg.psz_string = strdup( luaL_optstring( L, 2, "" ) );
240 int i_type = var_Type( p_this->p_libvlc, psz_cmd );
241 if( ! (i_type & VLC_VAR_ISCOMMAND) )
243 free( val_arg.psz_string );
244 return luaL_error( L, "libvlc's \"%s\" is not a command",
248 return vlclua_push_ret( L,
249 var_Set( p_this->p_libvlc, psz_cmd, val_arg ) );
252 int __vlclua_var_toggle_or_set( lua_State *L, vlc_object_t *p_obj,
253 const char *psz_name )
256 if( lua_gettop( L ) > 1 ) return vlclua_error( L );
258 if( lua_gettop( L ) == 0 )
259 b_bool = !var_GetBool( p_obj, psz_name );
260 else /* lua_gettop( L ) == 1 */
262 b_bool = luaL_checkboolean( L, -1 )?true:false;
266 if( b_bool != var_GetBool( p_obj, psz_name ) )
267 var_SetBool( p_obj, psz_name, b_bool );
269 lua_pushboolean( L, b_bool );
273 static inline const void *luaL_checklightuserdata( lua_State *L, int narg )
275 luaL_checktype( L, narg, LUA_TLIGHTUSERDATA ); /* can raise an error */
276 return lua_topointer( L, narg );
286 static int vlclua_callback( vlc_object_t *p_this, char const *psz_var,
287 vlc_value_t oldval, vlc_value_t newval,
290 vlclua_callback_t *p_callback = (vlclua_callback_t*)p_data;
291 lua_State *L = p_callback->L;
294 lua_getglobal( L, "vlc" );
296 lua_getfield( L, -1, "callbacks" );
300 lua_pushinteger( L, p_callback->i_index );
301 /* callbacks index */
302 lua_gettable( L, -2 );
303 /* callbacks callbacks[index] */
305 /* callbacks[index] */
306 lua_getfield( L, -1, "callback" );
307 /* callbacks[index] callback */
308 lua_pushstring( L, psz_var );
309 /* callbacks[index] callback var */
310 vlclua_pushvalue( L, p_callback->i_type, oldval );
311 /* callbacks[index] callback var oldval */
312 vlclua_pushvalue( L, p_callback->i_type, newval );
313 /* callbacks[index] callback var oldval newval */
314 lua_getfield( L, -5, "data" );
315 /* callbacks[index] callback var oldval newval data */
317 /* callback var oldval newval data */
319 if( lua_pcall( L, 4, 0, 0 ) )
322 const char *psz_err = lua_tostring( L, -1 );
323 msg_Err( p_this, "Error while runing lua interface callback: %s",
325 /* empty the stack (should only contain the error message) */
330 /* empty the stack (should already be empty) */
336 static int vlclua_add_callback( lua_State *L )
338 vlclua_callback_t *p_callback;
339 static int i_index = 0;
340 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
341 const char *psz_var = luaL_checkstring( L, 2 );
342 lua_settop( L, 4 ); /* makes sure that optional data arg is set */
343 if( !lua_isfunction( L, 3 ) )
344 return vlclua_error( L );
346 if( !pp_obj || !*pp_obj )
347 return vlclua_error( L );
349 /* Check variable type, in order to avoid PANIC */
350 switch( var_Type( *pp_obj, psz_var ) )
353 case VLC_VAR_INTEGER:
358 case VLC_VAR_ADDRESS:
363 return vlclua_error( L );
368 p_callback = (vlclua_callback_t*)malloc( sizeof( vlclua_callback_t ) );
370 return vlclua_error( L );
372 /* obj var func data */
373 lua_getglobal( L, "vlc" );
374 /* obj var func data vlc */
375 lua_getfield( L, -1, "callbacks" );
376 if( lua_isnil( L, -1 ) )
380 lua_setfield( L, -2, "callbacks" );
381 lua_getfield( L, -1, "callbacks" );
383 /* obj var func data vlc callbacks */
385 /* obj var func data callbacks */
386 lua_pushinteger( L, i_index );
387 /* obj var func data callbacks index */
389 /* obj var index func data callbacks */
391 /* obj var callbacks index func data */
392 lua_createtable( L, 0, 0 );
393 /* obj var callbacks index func data cbtable */
395 /* obj var callbacks index func cbtable data */
396 lua_setfield( L, -2, "data" );
397 /* obj var callbacks index func cbtable */
399 /* obj var callbacks index cbtable func */
400 lua_setfield( L, -2, "callback" );
401 /* obj var callbacks index cbtable */
402 lua_pushlightuserdata( L, *pp_obj ); /* will be needed in vlclua_del_callback */
403 /* obj var callbacks index cbtable p_obj */
404 lua_setfield( L, -2, "private1" );
405 /* obj var callbacks index cbtable */
406 lua_pushvalue( L, 2 ); /* will be needed in vlclua_del_callback */
407 /* obj var callbacks index cbtable var */
408 lua_setfield( L, -2, "private2" );
409 /* obj var callbacks index cbtable */
410 lua_pushlightuserdata( L, p_callback ); /* will be needed in vlclua_del_callback */
411 /* obj var callbacks index cbtable p_callback */
412 lua_setfield( L, -2, "private3" );
413 /* obj var callbacks index cbtable */
414 lua_settable( L, -3 );
415 /* obj var callbacks */
419 /* Do not move this before the lua specific code (it somehow changes
420 * the function in the stack to nil) */
421 p_callback->i_index = i_index;
422 p_callback->i_type = var_Type( *pp_obj, psz_var );
423 p_callback->L = lua_newthread( L ); /* Do we have to keep a reference to this thread somewhere to prevent garbage collection? */
425 var_AddCallback( *pp_obj, psz_var, vlclua_callback, p_callback );
429 static int vlclua_del_callback( lua_State *L )
431 vlclua_callback_t *p_callback;
432 bool b_found = false;
433 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
434 const char *psz_var = luaL_checkstring( L, 2 );
435 lua_settop( L, 4 ); /* makes sure that optional data arg is set */
436 if( !lua_isfunction( L, 3 ) )
437 return vlclua_error( L );
439 /* obj var func data */
440 lua_getglobal( L, "vlc" );
441 /* obj var func data vlc */
442 lua_getfield( L, -1, "callbacks" );
443 if( lua_isnil( L, -1 ) )
444 return luaL_error( L, "Couldn't find matching callback." );
445 /* obj var func data vlc callbacks */
447 /* obj var func data callbacks */
449 /* obj var func data callbacks index */
450 while( lua_next( L, -2 ) )
452 /* obj var func data callbacks index value */
453 if( lua_isnumber( L, -2 ) )
455 lua_getfield( L, -1, "private2" );
456 /* obj var func data callbacks index value private2 */
457 if( lua_equal( L, 2, -1 ) ) /* var name is equal */
460 /* obj var func data callbacks index value */
461 lua_getfield( L, -1, "callback" );
462 /* obj var func data callbacks index value callback */
463 if( lua_equal( L, 3, -1 ) ) /* callback function is equal */
466 /* obj var func data callbacks index value */
467 lua_getfield( L, -1, "data" ); /* callback data is equal */
468 /* obj var func data callbacks index value data */
469 if( lua_equal( L, 4, -1 ) )
471 vlc_object_t *p_obj2;
473 /* obj var func data callbacks index value */
474 lua_getfield( L, -1, "private1" );
475 /* obj var func data callbacks index value private1 */
476 p_obj2 = (vlc_object_t*)luaL_checklightuserdata( L, -1 );
477 if( p_obj2 == *pp_obj ) /* object is equal */
480 /* obj var func data callbacks index value */
481 lua_getfield( L, -1, "private3" );
482 /* obj var func data callbacks index value private3 */
483 p_callback = (vlclua_callback_t*)luaL_checklightuserdata( L, -1 );
485 /* obj var func data callbacks index */
491 /* obj var func data callbacks index value private1 */
493 /* obj var func data callbacks index value */
498 /* obj var func data callbacks index value data */
500 /* obj var func data callbacks index value */
505 /* obj var func data callbacks index value callback */
507 /* obj var func data callbacks index value */
512 /* obj var func data callbacks index value private2 */
514 /* obj var func data callbacks index value */
517 /* obj var func data callbacks index value */
519 /* obj var func data callbacks index */
521 if( b_found == false )
522 /* obj var func data callbacks */
523 return luaL_error( L, "Couldn't find matching callback." );
525 /* obj var func data callbacks index*/
527 var_DelCallback( *pp_obj, psz_var, vlclua_callback, p_callback );
530 /* obj var func data callbacks index */
532 /* obj var func data callbacks index nil */
533 lua_settable( L, -3 ); /* delete the callback table entry */
534 /* obj var func data callbacks */
540 static int vlclua_trigger_callback( lua_State *L )
542 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
543 const char *psz_var = luaL_checkstring( L, 2 );
545 return vlclua_push_ret( L, var_TriggerCallback( *pp_obj, psz_var ) );
548 /*****************************************************************************
550 *****************************************************************************/
551 static const luaL_Reg vlclua_var_reg[] = {
552 { "get", vlclua_var_get },
553 { "get_list", vlclua_var_get_list },
554 { "set", vlclua_var_set },
555 { "create", vlclua_var_create },
556 { "add_callback", vlclua_add_callback },
557 { "del_callback", vlclua_del_callback },
558 { "trigger_callback", vlclua_trigger_callback },
559 { "command", vlclua_command },
560 { "libvlc_command", vlclua_libvlc_command },
564 void luaopen_variables( lua_State *L )
567 luaL_register( L, NULL, vlclua_var_reg );
568 lua_setfield( L, -2, "var" );