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. );
84 static int vlclua_pushlist( lua_State *L, vlc_list_t *p_list )
86 int i_count = p_list->i_count;
88 lua_createtable( L, i_count, 0 );
89 for( int i = 0; i < i_count; i++ )
91 lua_pushinteger( L, i+1 );
92 if( !vlclua_pushvalue( L, p_list->pi_types[i],
93 p_list->p_values[i] ) )
95 lua_settable( L, -3 );
100 static int vlclua_tovalue( lua_State *L, int i_type, vlc_value_t *val )
102 switch( i_type & VLC_VAR_CLASS )
107 val->b_bool = luaL_checkboolean( L, -1 );
109 case VLC_VAR_INTEGER:
110 val->i_int = luaL_checkint( L, -1 );
113 val->psz_string = (char*)luaL_checkstring( L, -1 ); /* XXX: Beware, this only stays valid as long as (L,-1) stays in the stack */
116 val->f_float = luaL_checknumber( L, -1 );
120 double f = luaL_checknumber( L, -1 );
121 val->i_time = (int64_t)(f*1000000.);
124 case VLC_VAR_ADDRESS:
136 static int vlclua_var_get( lua_State *L )
140 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
141 const char *psz_var = luaL_checkstring( L, 2 );
142 i_type = var_Type( *pp_obj, psz_var );
143 if( var_Get( *pp_obj, psz_var, &val ) != VLC_SUCCESS )
146 return vlclua_pushvalue( L, i_type, val );
149 static int vlclua_var_set( lua_State *L )
153 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
154 const char *psz_var = luaL_checkstring( L, 2 );
156 i_type = var_Type( *pp_obj, psz_var );
157 vlclua_tovalue( L, i_type, &val );
158 i_ret = var_Set( *pp_obj, psz_var, val );
160 return vlclua_push_ret( L, i_ret );
163 static int vlclua_var_create( lua_State *L )
165 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
166 const char *psz_var = luaL_checkstring( L, 2 );
168 switch( lua_type( L, 3 ) )
171 i_type = VLC_VAR_FLOAT;
174 i_type = VLC_VAR_BOOL;
177 i_type = VLC_VAR_STRING;
183 int i_ret = var_Create( *pp_obj, psz_var, i_type );
184 if( i_ret != VLC_SUCCESS )
185 return vlclua_push_ret( L, i_ret );
187 vlclua_tovalue( L, i_type, &val );
188 return vlclua_push_ret( L, var_Set( *pp_obj, psz_var, val ) );
191 static int vlclua_var_get_list( lua_State *L )
195 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
196 const char *psz_var = luaL_checkstring( L, 2 );
197 int i_ret = var_Change( *pp_obj, psz_var, VLC_VAR_GETLIST, &val, &text );
198 if( i_ret < 0 ) return vlclua_push_ret( L, i_ret );
199 vlclua_pushlist( L, val.p_list );
200 vlclua_pushlist( L, text.p_list );
201 var_FreeList( &val, &text );
205 static int vlclua_command( lua_State *L )
207 vlc_object_t * p_this = vlclua_get_this( L );
208 const char *psz_name;
213 psz_name = luaL_checkstring( L, 1 );
214 psz_cmd = luaL_checkstring( L, 2 );
215 psz_arg = luaL_checkstring( L, 3 );
217 ret = var_Command( p_this, psz_name, psz_cmd, psz_arg, &psz_msg );
220 lua_pushstring( L, psz_msg );
225 lua_pushliteral( L, "" );
227 return vlclua_push_ret( L, ret ) + 1;
230 static int vlclua_libvlc_command( lua_State *L )
232 vlc_object_t * p_this = vlclua_get_this( L );
235 psz_cmd = luaL_checkstring( L, 1 );
236 val_arg.psz_string = strdup( luaL_optstring( L, 2, "" ) );
238 int i_type = var_Type( p_this->p_libvlc, psz_cmd );
239 if( ! (i_type & VLC_VAR_ISCOMMAND) )
241 free( val_arg.psz_string );
242 return luaL_error( L, "libvlc's \"%s\" is not a command",
246 return vlclua_push_ret( L,
247 var_Set( p_this->p_libvlc, psz_cmd, val_arg ) );
250 int __vlclua_var_toggle_or_set( lua_State *L, vlc_object_t *p_obj,
251 const char *psz_name )
254 if( lua_gettop( L ) > 1 ) return vlclua_error( L );
256 if( lua_gettop( L ) == 0 )
257 b_bool = !var_GetBool( p_obj, psz_name );
258 else /* lua_gettop( L ) == 1 */
260 b_bool = luaL_checkboolean( L, -1 )?true:false;
264 if( b_bool != var_GetBool( p_obj, psz_name ) )
265 var_SetBool( p_obj, psz_name, b_bool );
267 lua_pushboolean( L, b_bool );
271 static inline const void *luaL_checklightuserdata( lua_State *L, int narg )
273 luaL_checktype( L, narg, LUA_TLIGHTUSERDATA ); /* can raise an error */
274 return lua_topointer( L, narg );
284 static int vlclua_callback( vlc_object_t *p_this, char const *psz_var,
285 vlc_value_t oldval, vlc_value_t newval,
288 vlclua_callback_t *p_callback = (vlclua_callback_t*)p_data;
289 lua_State *L = p_callback->L;
292 lua_getglobal( L, "vlc" );
294 lua_getfield( L, -1, "callbacks" );
298 lua_pushinteger( L, p_callback->i_index );
299 /* callbacks index */
300 lua_gettable( L, -2 );
301 /* callbacks callbacks[index] */
303 /* callbacks[index] */
304 lua_getfield( L, -1, "callback" );
305 /* callbacks[index] callback */
306 lua_pushstring( L, psz_var );
307 /* callbacks[index] callback var */
308 vlclua_pushvalue( L, p_callback->i_type, oldval );
309 /* callbacks[index] callback var oldval */
310 vlclua_pushvalue( L, p_callback->i_type, newval );
311 /* callbacks[index] callback var oldval newval */
312 lua_getfield( L, -5, "data" );
313 /* callbacks[index] callback var oldval newval data */
315 /* callback var oldval newval data */
317 if( lua_pcall( L, 4, 0, 0 ) )
320 const char *psz_err = lua_tostring( L, -1 );
321 msg_Err( p_this, "Error while running lua interface callback: %s",
323 /* empty the stack (should only contain the error message) */
328 /* empty the stack (should already be empty) */
334 static int vlclua_add_callback( lua_State *L )
336 vlclua_callback_t *p_callback;
337 static int i_index = 0;
338 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
339 const char *psz_var = luaL_checkstring( L, 2 );
340 lua_settop( L, 4 ); /* makes sure that optional data arg is set */
341 if( !lua_isfunction( L, 3 ) )
342 return vlclua_error( L );
344 if( !pp_obj || !*pp_obj )
345 return vlclua_error( L );
347 /* Check variable type, in order to avoid PANIC */
348 switch( var_Type( *pp_obj, psz_var ) )
351 case VLC_VAR_INTEGER:
356 case VLC_VAR_ADDRESS:
360 return vlclua_error( L );
365 p_callback = (vlclua_callback_t*)malloc( sizeof( vlclua_callback_t ) );
367 return vlclua_error( L );
369 /* obj var func data */
370 lua_getglobal( L, "vlc" );
371 /* obj var func data vlc */
372 lua_getfield( L, -1, "callbacks" );
373 if( lua_isnil( L, -1 ) )
377 lua_setfield( L, -2, "callbacks" );
378 lua_getfield( L, -1, "callbacks" );
380 /* obj var func data vlc callbacks */
382 /* obj var func data callbacks */
383 lua_pushinteger( L, i_index );
384 /* obj var func data callbacks index */
386 /* obj var index func data callbacks */
388 /* obj var callbacks index func data */
389 lua_createtable( L, 0, 0 );
390 /* obj var callbacks index func data cbtable */
392 /* obj var callbacks index func cbtable data */
393 lua_setfield( L, -2, "data" );
394 /* obj var callbacks index func cbtable */
396 /* obj var callbacks index cbtable func */
397 lua_setfield( L, -2, "callback" );
398 /* obj var callbacks index cbtable */
399 lua_pushlightuserdata( L, *pp_obj ); /* will be needed in vlclua_del_callback */
400 /* obj var callbacks index cbtable p_obj */
401 lua_setfield( L, -2, "private1" );
402 /* obj var callbacks index cbtable */
403 lua_pushvalue( L, 2 ); /* will be needed in vlclua_del_callback */
404 /* obj var callbacks index cbtable var */
405 lua_setfield( L, -2, "private2" );
406 /* obj var callbacks index cbtable */
407 lua_pushlightuserdata( L, p_callback ); /* will be needed in vlclua_del_callback */
408 /* obj var callbacks index cbtable p_callback */
409 lua_setfield( L, -2, "private3" );
410 /* obj var callbacks index cbtable */
411 lua_settable( L, -3 );
412 /* obj var callbacks */
416 /* Do not move this before the lua specific code (it somehow changes
417 * the function in the stack to nil) */
418 p_callback->i_index = i_index;
419 p_callback->i_type = var_Type( *pp_obj, psz_var );
420 p_callback->L = lua_newthread( L ); /* Do we have to keep a reference to this thread somewhere to prevent garbage collection? */
422 var_AddCallback( *pp_obj, psz_var, vlclua_callback, p_callback );
426 static int vlclua_del_callback( lua_State *L )
428 vlclua_callback_t *p_callback;
429 bool b_found = false;
430 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
431 const char *psz_var = luaL_checkstring( L, 2 );
432 lua_settop( L, 4 ); /* makes sure that optional data arg is set */
433 if( !lua_isfunction( L, 3 ) )
434 return vlclua_error( L );
436 /* obj var func data */
437 lua_getglobal( L, "vlc" );
438 /* obj var func data vlc */
439 lua_getfield( L, -1, "callbacks" );
440 if( lua_isnil( L, -1 ) )
441 return luaL_error( L, "Couldn't find matching callback." );
442 /* obj var func data vlc callbacks */
444 /* obj var func data callbacks */
446 /* obj var func data callbacks index */
447 while( lua_next( L, -2 ) )
449 /* obj var func data callbacks index value */
450 if( lua_isnumber( L, -2 ) )
452 lua_getfield( L, -1, "private2" );
453 /* obj var func data callbacks index value private2 */
454 if( lua_equal( L, 2, -1 ) ) /* var name is equal */
457 /* obj var func data callbacks index value */
458 lua_getfield( L, -1, "callback" );
459 /* obj var func data callbacks index value callback */
460 if( lua_equal( L, 3, -1 ) ) /* callback function is equal */
463 /* obj var func data callbacks index value */
464 lua_getfield( L, -1, "data" ); /* callback data is equal */
465 /* obj var func data callbacks index value data */
466 if( lua_equal( L, 4, -1 ) )
468 vlc_object_t *p_obj2;
470 /* obj var func data callbacks index value */
471 lua_getfield( L, -1, "private1" );
472 /* obj var func data callbacks index value private1 */
473 p_obj2 = (vlc_object_t*)luaL_checklightuserdata( L, -1 );
474 if( p_obj2 == *pp_obj ) /* object is equal */
477 /* obj var func data callbacks index value */
478 lua_getfield( L, -1, "private3" );
479 /* obj var func data callbacks index value private3 */
480 p_callback = (vlclua_callback_t*)luaL_checklightuserdata( L, -1 );
482 /* obj var func data callbacks index */
488 /* obj var func data callbacks index value private1 */
490 /* obj var func data callbacks index value */
495 /* obj var func data callbacks index value data */
497 /* obj var func data callbacks index value */
502 /* obj var func data callbacks index value callback */
504 /* obj var func data callbacks index value */
509 /* obj var func data callbacks index value private2 */
511 /* obj var func data callbacks index value */
514 /* obj var func data callbacks index value */
516 /* obj var func data callbacks index */
518 if( b_found == false )
519 /* obj var func data callbacks */
520 return luaL_error( L, "Couldn't find matching callback." );
522 /* obj var func data callbacks index*/
524 var_DelCallback( *pp_obj, psz_var, vlclua_callback, p_callback );
527 /* obj var func data callbacks index */
529 /* obj var func data callbacks index nil */
530 lua_settable( L, -3 ); /* delete the callback table entry */
531 /* obj var func data callbacks */
537 static int vlclua_trigger_callback( lua_State *L )
539 vlc_object_t **pp_obj = luaL_checkudata( L, 1, "vlc_object" );
540 const char *psz_var = luaL_checkstring( L, 2 );
542 return vlclua_push_ret( L, var_TriggerCallback( *pp_obj, psz_var ) );
545 /*****************************************************************************
547 *****************************************************************************/
548 static const luaL_Reg vlclua_var_reg[] = {
549 { "get", vlclua_var_get },
550 { "get_list", vlclua_var_get_list },
551 { "set", vlclua_var_set },
552 { "create", vlclua_var_create },
553 { "add_callback", vlclua_add_callback },
554 { "del_callback", vlclua_del_callback },
555 { "trigger_callback", vlclua_trigger_callback },
556 { "command", vlclua_command },
557 { "libvlc_command", vlclua_libvlc_command },
561 void luaopen_variables( lua_State *L )
564 luaL_register( L, NULL, vlclua_var_reg );
565 lua_setfield( L, -2, "var" );