1 /*****************************************************************************
2 * intf.c: Generic lua inteface functions
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 *****************************************************************************/
33 #include <vlc_charset.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
41 #include <lua.h> /* Low level lua C API */
42 #include <lauxlib.h> /* Higher level C API */
43 #include <lualib.h> /* Lua libs */
53 /*****************************************************************************
54 * Internal lua<->vlc utils
55 *****************************************************************************/
56 playlist_t *vlclua_get_playlist_internal( lua_State *L )
58 vlc_object_t *p_this = vlclua_get_this( L );
59 return pl_Yield( p_this );
62 static input_thread_t * vlclua_get_input_internal( lua_State *L )
64 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
65 input_thread_t *p_input = p_playlist->p_input;
66 if( p_input ) vlc_object_yield( p_input );
67 vlc_object_release( p_playlist );
71 /* FIXME: This is high level logic. Should be implemented in lua */
72 #define vlclua_var_toggle_or_set(a,b,c) \
73 __vlclua_var_toggle_or_set(a,VLC_OBJECT(b),c)
74 static int __vlclua_var_toggle_or_set( lua_State *L, vlc_object_t *p_obj,
75 const char *psz_name )
78 if( lua_gettop( L ) > 1 ) return vlclua_error( L );
80 if( lua_gettop( L ) == 0 )
81 b_bool = !var_GetBool( p_obj, psz_name );
82 else /* lua_gettop( L ) == 1 */
84 b_bool = luaL_checkboolean( L, -1 )?VLC_TRUE:VLC_FALSE;
88 if( b_bool != var_GetBool( p_obj, psz_name ) )
89 var_SetBool( p_obj, psz_name, b_bool );
91 lua_pushboolean( L, b_bool );
95 /*****************************************************************************
96 * Libvlc TODO: move to vlc.c
97 *****************************************************************************/
98 static int vlclua_get_libvlc( lua_State *L )
100 vlclua_push_vlc_object( L, vlclua_get_this( L )->p_libvlc,
105 /*****************************************************************************
107 *****************************************************************************/
108 static int vlclua_get_input( lua_State *L )
110 input_thread_t *p_input = vlclua_get_input_internal( L );
113 vlclua_push_vlc_object( L, p_input, vlclua_gc_release );
115 else lua_pushnil( L );
119 static int vlclua_input_info( lua_State *L )
121 input_thread_t * p_input = vlclua_get_input_internal( L );
124 if( !p_input ) return vlclua_error( L );
125 //vlc_mutex_lock( &input_GetItem(p_input)->lock );
126 i_cat = input_GetItem(p_input)->i_categories;
127 lua_createtable( L, 0, i_cat );
128 for( i = 0; i < i_cat; i++ )
130 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
131 int i_infos = p_category->i_infos;
133 lua_pushstring( L, p_category->psz_name );
134 lua_createtable( L, 0, i_infos );
135 for( j = 0; j < i_infos; j++ )
137 info_t *p_info = p_category->pp_infos[j];
138 lua_pushstring( L, p_info->psz_name );
139 lua_pushstring( L, p_info->psz_value );
140 lua_settable( L, -3 );
142 lua_settable( L, -3 );
144 //vlc_object_release( p_input );
148 static int vlclua_is_playing( lua_State *L )
150 input_thread_t * p_input = vlclua_get_input_internal( L );
151 lua_pushboolean( L, !!p_input );
155 static int vlclua_get_title( lua_State *L )
157 input_thread_t *p_input = vlclua_get_input_internal( L );
162 lua_pushstring( L, input_GetItem(p_input)->psz_name );
163 vlc_object_release( p_input );
168 static int vlclua_input_stats( lua_State *L )
170 input_thread_t *p_input = vlclua_get_input_internal( L );
171 input_item_t *p_item = p_input && p_input->p ? input_GetItem( p_input ) : NULL;
175 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
176 lua_setfield( L, -2, #n );
177 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
178 lua_setfield( L, -2, #n );
179 STATS_INT( read_bytes )
180 STATS_FLOAT( input_bitrate )
181 STATS_INT( demux_read_bytes )
182 STATS_FLOAT( demux_bitrate )
183 STATS_INT( decoded_video )
184 STATS_INT( displayed_pictures )
185 STATS_INT( lost_pictures )
186 STATS_INT( decoded_audio )
187 STATS_INT( played_abuffers )
188 STATS_INT( lost_abuffers )
189 STATS_INT( sent_packets )
190 STATS_INT( sent_bytes )
191 STATS_FLOAT( send_bitrate )
198 /*****************************************************************************
200 *****************************************************************************/
201 static int vlclua_fullscreen( lua_State *L )
203 vout_thread_t *p_vout;
206 input_thread_t * p_input = vlclua_get_input_internal( L );
207 if( !p_input ) return vlclua_error( L );
209 p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
210 if( !p_vout ) return vlclua_error( L );
212 i_ret = vlclua_var_toggle_or_set( L, p_vout, "fullscreen" );
213 vlc_object_release( p_vout );
214 vlc_object_release( p_input );
218 static int vlc_osd_icon_from_string( const char *psz_name )
223 const char *psz_name;
225 { { OSD_PAUSE_ICON, "pause" },
226 { OSD_PLAY_ICON, "play" },
227 { OSD_SPEAKER_ICON, "speaker" },
228 { OSD_MUTE_ICON, "mute" },
231 for( i = 0; pp_icons[i].psz_name; i++ )
233 if( !strcmp( psz_name, pp_icons[i].psz_name ) )
234 return pp_icons[i].i_icon;
239 static int vlclua_osd_icon( lua_State *L )
241 const char *psz_icon = luaL_checkstring( L, 1 );
242 int i_icon = vlc_osd_icon_from_string( psz_icon );
243 int i_chan = luaL_optint( L, 2, DEFAULT_CHAN );
245 return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );
248 vlc_object_t *p_this = vlclua_get_this( L );
249 vout_OSDIcon( p_this, i_chan, i_icon );
254 static int vlclua_osd_message( lua_State *L )
256 const char *psz_message = luaL_checkstring( L, 1 );
257 int i_chan = luaL_optint( L, 2, DEFAULT_CHAN );
258 vlc_object_t *p_this = vlclua_get_this( L );
259 vout_OSDMessage( p_this, i_chan, psz_message );
263 static int vlc_osd_slider_type_from_string( const char *psz_name )
268 const char *psz_name;
270 { { OSD_HOR_SLIDER, "horizontal" },
271 { OSD_VERT_SLIDER, "vertical" },
274 for( i = 0; pp_types[i].psz_name; i++ )
276 if( !strcmp( psz_name, pp_types[i].psz_name ) )
277 return pp_types[i].i_type;
282 static int vlclua_osd_slider( lua_State *L )
284 int i_position = luaL_checkint( L, 1 );
285 const char *psz_type = luaL_checkstring( L, 2 );
286 int i_type = vlc_osd_slider_type_from_string( psz_type );
287 int i_chan = luaL_optint( L, 3, DEFAULT_CHAN );
289 return luaL_error( L, "\"%s\" is not a valid slider type.",
293 vlc_object_t *p_this = vlclua_get_this( L );
294 vout_OSDSlider( p_this, i_chan, i_position, i_type );
299 static int vlclua_spu_channel_register( lua_State *L )
302 vlc_object_t *p_this = vlclua_get_this( L );
303 vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
306 return luaL_error( L, "Unable to find vout." );
308 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER, &i_chan );
309 vlc_object_release( p_vout );
310 lua_pushinteger( L, i_chan );
314 static int vlclua_spu_channel_clear( lua_State *L )
316 int i_chan = luaL_checkint( L, 1 );
317 vlc_object_t *p_this = vlclua_get_this( L );
318 vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
321 return luaL_error( L, "Unable to find vout." );
323 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, i_chan );
324 vlc_object_release( p_vout );
328 /*****************************************************************************
330 *****************************************************************************/
331 static int vlclua_get_playlist( lua_State *L )
333 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
336 vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
338 else lua_pushnil( L );
342 static int vlclua_playlist_prev( lua_State * L )
344 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
345 playlist_Prev( p_playlist );
346 vlc_object_release( p_playlist );
350 static int vlclua_playlist_next( lua_State * L )
352 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
353 playlist_Next( p_playlist );
354 vlc_object_release( p_playlist );
358 static int vlclua_playlist_skip( lua_State * L )
360 int i_skip = luaL_checkint( L, 1 );
361 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
362 playlist_Skip( p_playlist, i_skip );
363 vlc_object_release( p_playlist );
367 static int vlclua_playlist_play( lua_State * L )
369 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
370 playlist_Play( p_playlist );
371 vlc_object_release( p_playlist );
375 static int vlclua_playlist_pause( lua_State * L )
377 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
378 playlist_Pause( p_playlist );
379 vlc_object_release( p_playlist );
383 static int vlclua_playlist_stop( lua_State * L )
385 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
386 playlist_Stop( p_playlist );
387 vlc_object_release( p_playlist );
391 static int vlclua_playlist_clear( lua_State * L )
393 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
394 playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
395 playlist_Clear( p_playlist, VLC_FALSE );
396 vlc_object_release( p_playlist );
400 static int vlclua_playlist_repeat( lua_State * L )
402 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
403 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
404 vlc_object_release( p_playlist );
408 static int vlclua_playlist_loop( lua_State * L )
410 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
411 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
412 vlc_object_release( p_playlist );
416 static int vlclua_playlist_random( lua_State * L )
418 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
419 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
420 vlc_object_release( p_playlist );
424 static int vlclua_playlist_goto( lua_State * L )
426 int i_id = luaL_checkint( L, 1 );
427 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
428 int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
430 playlist_ItemGetById( p_playlist, i_id,
432 vlc_object_release( p_playlist );
433 return vlclua_push_ret( L, i_ret );
436 static int vlclua_playlist_add( lua_State *L )
439 vlc_object_t *p_this = vlclua_get_this( L );
440 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
441 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
443 vlc_object_release( p_playlist );
444 lua_pushinteger( L, i_count );
448 static int vlclua_playlist_enqueue( lua_State *L )
451 vlc_object_t *p_this = vlclua_get_this( L );
452 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
453 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
455 vlc_object_release( p_playlist );
456 lua_pushinteger( L, i_count );
460 static void push_playlist_item( lua_State *L, playlist_item_t *p_item );
461 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
463 input_item_t *p_input = p_item->p_input;
464 int i_flags = p_item->i_flags;
466 lua_pushinteger( L, p_item->i_id );
467 lua_setfield( L, -2, "id" );
469 #define CHECK_AND_SET_FLAG( name, label ) \
470 if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
472 lua_pushboolean( L, 1 ); \
473 lua_setfield( L, -2, #label ); \
475 CHECK_AND_SET_FLAG( SAVE, save )
476 CHECK_AND_SET_FLAG( SKIP, skip )
477 CHECK_AND_SET_FLAG( DBL, disabled )
478 CHECK_AND_SET_FLAG( RO, ro )
479 CHECK_AND_SET_FLAG( REMOVE, remove )
480 CHECK_AND_SET_FLAG( EXPANDED, expanded )
481 #undef CHECK_AND_SET_FLAG
482 lua_setfield( L, -2, "flags" );
485 lua_pushstring( L, p_input->psz_name );
486 lua_setfield( L, -2, "name" );
487 lua_pushstring( L, p_input->psz_uri );
488 lua_setfield( L, -2, "path" );
489 if( p_input->i_duration < 0 )
490 lua_pushnumber( L, -1 );
492 lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
493 lua_setfield( L, -2, "duration" );
494 lua_pushinteger( L, p_input->i_nb_played );
495 lua_setfield( L, -2, "nb_played" );
496 /* TODO: add (optional) info categories, meta, options, es */
498 if( p_item->i_children >= 0 )
501 lua_createtable( L, p_item->i_children, 0 );
502 for( i = 0; i < p_item->i_children; i++ )
504 push_playlist_item( L, p_item->pp_children[i] );
505 lua_rawseti( L, -2, i+1 );
507 lua_setfield( L, -2, "children" );
511 static int vlclua_playlist_get( lua_State *L )
513 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
514 int b_category = luaL_optboolean( L, 2, 1 ); /* Default to tree playlist (discared when 1st argument is a playlist_item's id) */
515 playlist_item_t *p_item = NULL;
517 if( lua_isnumber( L, 1 ) )
519 int i_id = lua_tointeger( L, 1 );
520 p_item = playlist_ItemGetById( p_playlist, i_id, VLC_TRUE );
523 vlc_object_release( p_playlist );
524 return 0; /* Should we return an error instead? */
527 else if( lua_isstring( L, 1 ) )
529 const char *psz_what = lua_tostring( L, 1 );
530 if( !strcasecmp( psz_what, "normal" )
531 || !strcasecmp( psz_what, "playlist" ) )
532 p_item = b_category ? p_playlist->p_local_category
533 : p_playlist->p_local_onelevel;
534 else if( !strcasecmp( psz_what, "ml" )
535 || !strcasecmp( psz_what, "media library" ) )
536 p_item = b_category ? p_playlist->p_ml_category
537 : p_playlist->p_ml_onelevel;
538 else if( !strcasecmp( psz_what, "root" ) )
539 p_item = b_category ? p_playlist->p_root_category
540 : p_playlist->p_root_onelevel;
544 for( i = 0; i < p_playlist->i_sds; i++ )
546 if( !strcasecmp( psz_what,
547 p_playlist->pp_sds[i]->p_sd->psz_module ) )
549 p_item = b_category ? p_playlist->pp_sds[i]->p_cat
550 : p_playlist->pp_sds[i]->p_one;
556 vlc_object_release( p_playlist );
557 return 0; /* Should we return an error instead? */
563 p_item = b_category ? p_playlist->p_root_category
564 : p_playlist->p_root_onelevel;
566 push_playlist_item( L, p_item );
567 vlc_object_release( p_playlist );
571 static int vlclua_playlist_search( lua_State *L )
573 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
574 const char *psz_string = luaL_optstring( L, 1, "" );
575 int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
576 playlist_item_t *p_item = b_category ? p_playlist->p_root_category
577 : p_playlist->p_root_onelevel;
578 playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
579 push_playlist_item( L, p_item );
580 vlc_object_release( p_playlist );
584 static int vlc_sort_key_from_string( const char *psz_name )
588 const char *psz_name;
592 { "title", SORT_TITLE },
593 { "title nodes first", SORT_TITLE_NODES_FIRST },
594 { "artist", SORT_ARTIST },
595 { "genre", SORT_GENRE },
596 { "random", SORT_RANDOM },
597 { "duration", SORT_DURATION },
598 { "title numeric", SORT_TITLE_NUMERIC },
599 { "album", SORT_ALBUM },
602 for( i = 0; pp_keys[i].psz_name; i++ )
604 if( !strcmp( psz_name, pp_keys[i].psz_name ) )
605 return pp_keys[i].i_key;
610 static int vlclua_playlist_sort( lua_State *L )
612 /* allow setting the different sort keys */
613 int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
615 return luaL_error( L, "Invalid search key." );
616 int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
617 int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
618 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
619 playlist_item_t *p_root = b_category ? p_playlist->p_local_category
620 : p_playlist->p_local_onelevel;
621 int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
623 vlc_object_release( p_playlist );
624 return vlclua_push_ret( L, i_ret );
627 /* FIXME: split this in 3 different functions? */
628 static int vlclua_playlist_status( lua_State *L )
630 intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
631 playlist_t *p_playlist = pl_Yield( p_intf );
634 lua_settop( L, 0 );*/
635 if( p_playlist->p_input )
638 input_item_GetURI( input_GetItem( p_playlist->p_input ) );
639 lua_pushstring( L, psz_uri );
641 lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
642 vlc_mutex_lock( &p_playlist->object_lock );
643 switch( p_playlist->status.i_status )
645 case PLAYLIST_STOPPED:
646 lua_pushstring( L, "stopped" );
648 case PLAYLIST_RUNNING:
649 lua_pushstring( L, "playing" );
651 case PLAYLIST_PAUSED:
652 lua_pushstring( L, "paused" );
655 lua_pushstring( L, "unknown" );
658 vlc_mutex_unlock( &p_playlist->object_lock );
663 lua_pushstring( L, "stopped" );
665 vlc_object_release( p_playlist );
670 static int vlclua_lock_and_wait( lua_State *L )
672 vlc_object_t *p_this = vlclua_get_this( L );
673 int b_quit = vlc_object_lock_and_wait( p_this );
674 lua_pushboolean( L, b_quit );
678 static int vlclua_signal( lua_State *L )
680 vlc_object_t *p_this = vlclua_get_this( L );
681 vlc_object_signal( p_this );
685 static int vlclua_mdate( lua_State *L )
687 lua_pushnumber( L, mdate() );
691 static int vlclua_intf_should_die( lua_State *L )
693 intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
694 lua_pushboolean( L, intf_ShouldDie( p_intf ) );
698 static luaL_Reg p_reg[] =
700 { "input_info", vlclua_input_info },
701 { "is_playing", vlclua_is_playing },
702 { "get_title", vlclua_get_title },
704 { "fullscreen", vlclua_fullscreen },
706 { "mdate", vlclua_mdate },
708 { "module_command", vlclua_module_command },
709 { "libvlc_command", vlclua_libvlc_command },
711 { "decode_uri", vlclua_decode_uri },
712 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
713 { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
715 { "lock_and_wait", vlclua_lock_and_wait },
716 { "signal", vlclua_signal },
718 { "version", vlclua_version },
719 { "license", vlclua_license },
720 { "copyright", vlclua_copyright },
721 { "should_die", vlclua_intf_should_die },
722 { "quit", vlclua_quit },
724 { "homedir", vlclua_homedir },
725 { "datadir", vlclua_datadir },
726 { "configdir", vlclua_configdir },
727 { "cachedir", vlclua_cachedir },
728 { "datadir_list", vlclua_datadir_list },
733 static luaL_Reg p_reg_object[] =
735 { "input", vlclua_get_input }, /* This is fast */
736 { "playlist", vlclua_get_playlist }, /* This is fast */
737 { "libvlc", vlclua_get_libvlc }, /* This is fast */
739 { "find", vlclua_object_find }, /* This is slow */
740 { "find_name", vlclua_object_find_name }, /* This is slow */
745 static luaL_Reg p_reg_var[] =
747 { "get", vlclua_var_get },
748 { "get_list", vlclua_var_get_list },
749 { "set", vlclua_var_set },
750 { "add_callback", vlclua_add_callback },
751 { "del_callback", vlclua_del_callback },
756 static luaL_Reg p_reg_config[] =
758 { "get", vlclua_config_get },
759 { "set", vlclua_config_set },
764 static luaL_Reg p_reg_msg[] =
766 { "dbg", vlclua_msg_dbg },
767 { "warn", vlclua_msg_warn },
768 { "err", vlclua_msg_err },
769 { "info", vlclua_msg_info },
774 static luaL_Reg p_reg_playlist[] =
776 { "prev", vlclua_playlist_prev },
777 { "next", vlclua_playlist_next },
778 { "skip", vlclua_playlist_skip },
779 { "play", vlclua_playlist_play },
780 { "pause", vlclua_playlist_pause },
781 { "stop", vlclua_playlist_stop },
782 { "clear", vlclua_playlist_clear },
783 { "repeat_", vlclua_playlist_repeat },
784 { "loop", vlclua_playlist_loop },
785 { "random", vlclua_playlist_random },
786 { "goto", vlclua_playlist_goto },
787 { "status", vlclua_playlist_status },
788 { "add", vlclua_playlist_add },
789 { "enqueue", vlclua_playlist_enqueue },
790 { "get", vlclua_playlist_get },
791 { "search", vlclua_playlist_search },
792 { "sort", vlclua_playlist_sort },
794 { "stats", vlclua_input_stats },
799 static luaL_Reg p_reg_sd[] =
801 { "get_services_names", vlclua_sd_get_services_names },
802 { "add", vlclua_sd_add },
803 { "remove", vlclua_sd_remove },
804 { "is_loaded", vlclua_sd_is_loaded },
809 static luaL_Reg p_reg_volume[] =
811 { "get", vlclua_volume_get },
812 { "set", vlclua_volume_set },
813 { "up", vlclua_volume_up },
814 { "down", vlclua_volume_down },
819 static luaL_Reg p_reg_osd[] =
821 { "icon", vlclua_osd_icon },
822 { "message", vlclua_osd_message },
823 { "slider", vlclua_osd_slider },
824 { "channel_register", vlclua_spu_channel_register },
825 { "channel_clear", vlclua_spu_channel_clear },
830 static luaL_Reg p_reg_net[] =
832 { "url_parse", vlclua_url_parse },
833 { "listen_tcp", vlclua_net_listen_tcp },
834 { "listen_close", vlclua_net_listen_close },
835 { "accept", vlclua_net_accept },
836 { "close", vlclua_net_close },
837 { "send", vlclua_net_send },
838 { "recv", vlclua_net_recv },
839 { "select", vlclua_net_select },
844 static luaL_Reg p_reg_fd[] =
846 /* { "open", vlclua_fd_open },*/
847 { "read", vlclua_fd_read },
848 { "write", vlclua_fd_write },
849 { "stat", vlclua_stat },
851 { "opendir", vlclua_opendir },
853 { "new_fd_set", vlclua_fd_set_new },
854 { "fd_clr", vlclua_fd_clr },
855 { "fd_isset", vlclua_fd_isset },
856 { "fd_set", vlclua_fd_set },
857 { "fd_zero", vlclua_fd_zero },
862 static luaL_Reg p_reg_vlm[] =
864 { "new", vlclua_vlm_new },
865 { "delete", vlclua_vlm_delete },
866 { "execute_command", vlclua_vlm_execute_command },
871 static luaL_Reg p_reg_httpd[] =
873 { "host_new", vlclua_httpd_tls_host_new },
874 { "host_delete", vlclua_httpd_host_delete },
875 { "handler_new", vlclua_httpd_handler_new },
876 { "handler_delete", vlclua_httpd_handler_delete },
877 { "file_new", vlclua_httpd_file_new },
878 { "file_delete", vlclua_httpd_file_delete },
879 { "redirect_new", vlclua_httpd_redirect_new },
880 { "redirect_delete", vlclua_httpd_redirect_delete },
885 static luaL_Reg p_reg_acl[] =
887 { "create", vlclua_acl_create },
888 { "delete", vlclua_acl_delete },
889 { "check", vlclua_acl_check },
890 { "duplicate", vlclua_acl_duplicate },
891 { "add_host", vlclua_acl_add_host },
892 { "add_net", vlclua_acl_add_net },
893 { "load_file", vlclua_acl_load_file },
898 static void Run( intf_thread_t *p_intf );
900 static char *FindFile( intf_thread_t *p_intf, const char *psz_name )
902 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
904 vlclua_dir_list( VLC_OBJECT(p_intf), "luaintf", ppsz_dir_list );
905 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
909 if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
914 fp = fopen( psz_filename, "r" );
920 free( psz_filename );
925 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
929 luaL_register( L, NULL, l );
930 lua_setfield( L, -2, psz_name );
935 const char *psz_shortcut;
936 const char *psz_name;
939 /* { "rc", "rc" }, */
940 { "luahotkeys", "hotkeys" },
941 /* { "hotkeys", "hotkeys" }, */
942 { "luatelnet", "telnet" },
943 /* { "telnet", "telnet" }, */
946 static vlc_bool_t WordInList( const char *psz_list, const char *psz_word )
948 const char *psz_str = strstr( psz_list, psz_word );
949 int i_len = strlen( psz_word );
952 if( (psz_str == psz_list || *(psz_str-1) == ',' )
953 /* it doesn't start in middle of a word */
954 /* it doest end in middle of a word */
955 && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
957 psz_str = strstr( psz_str, psz_word );
962 static const char *GetModuleName( intf_thread_t *p_intf )
965 const char *psz_intf;
966 if( *p_intf->psz_intf == '$' )
967 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
969 psz_intf = p_intf->psz_intf;
970 for( i = 0; pp_shortcuts[i].psz_name; i++ )
972 if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
973 return pp_shortcuts[i].psz_name;
976 return config_GetPsz( p_intf, "lua-intf" );
979 int E_(Open_LuaIntf)( vlc_object_t *p_this )
981 intf_thread_t *p_intf = (intf_thread_t*)p_this;
985 const char *psz_name = GetModuleName( p_intf );
986 const char *psz_config;
987 vlc_bool_t b_config_set = VLC_FALSE;
988 if( !psz_name ) psz_name = "dummy";
990 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
995 p_sys = p_intf->p_sys;
996 p_sys->psz_filename = FindFile( p_intf, psz_name );
997 if( !p_sys->psz_filename )
999 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
1001 return VLC_EGENERIC;
1003 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
1005 L = luaL_newstate();
1008 msg_Err( p_intf, "Could not create new Lua State" );
1010 return VLC_EGENERIC;
1013 luaL_openlibs( L ); /* FIXME: we don't want to have all the libs */
1015 /* register our functions */
1016 luaL_register( L, "vlc", p_reg );
1017 /* store a pointer to p_intf */
1018 lua_pushlightuserdata( L, p_intf );
1019 lua_setfield( L, -2, "private" );
1020 /* register submodules */
1021 luaL_register_submodule( L, "object", p_reg_object );
1022 luaL_register_submodule( L, "var", p_reg_var );
1023 luaL_register_submodule( L, "config", p_reg_config );
1024 luaL_register_submodule( L, "msg", p_reg_msg );
1025 luaL_register_submodule( L, "playlist", p_reg_playlist );
1026 luaL_register_submodule( L, "sd", p_reg_sd );
1027 luaL_register_submodule( L, "volume", p_reg_volume );
1028 luaL_register_submodule( L, "osd", p_reg_osd );
1029 luaL_register_submodule( L, "net", p_reg_net );
1030 luaL_register_submodule( L, "fd", p_reg_fd );
1031 luaL_register_submodule( L, "vlm", p_reg_vlm );
1032 luaL_register_submodule( L, "httpd", p_reg_httpd );
1033 luaL_register_submodule( L, "acl", p_reg_acl );
1038 /* Setup the module search path */
1041 char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
1043 /* FIXME: don't use luaL_dostring */
1044 if( asprintf( &psz_command,
1045 "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
1046 p_sys->psz_filename ) < 0 )
1047 return VLC_EGENERIC;
1048 *psz_char = DIR_SEP_CHAR;
1049 if( luaL_dostring( L, psz_command ) )
1050 return VLC_EGENERIC;
1054 psz_config = config_GetPsz( p_intf, "lua-config" );
1055 if( psz_config && *psz_config )
1058 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
1060 printf("%s\n", psz_buffer);
1061 if( luaL_dostring( L, psz_buffer ) == 1 )
1062 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
1064 lua_getglobal( L, "config" );
1065 if( lua_istable( L, -1 ) )
1067 lua_getfield( L, -1, psz_name );
1068 if( lua_istable( L, -1 ) )
1070 lua_setglobal( L, "config" );
1071 b_config_set = VLC_TRUE;
1076 if( b_config_set == VLC_FALSE )
1079 lua_setglobal( L, "config" );
1084 p_intf->pf_run = Run;
1085 p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in E_(Close_LuaIntf)? */
1090 void E_(Close_LuaIntf)( vlc_object_t *p_this )
1092 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1094 lua_close( p_intf->p_sys->L );
1095 free( p_intf->p_sys );
1098 static void Run( intf_thread_t *p_intf )
1100 lua_State *L = p_intf->p_sys->L;
1102 if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
1104 msg_Err( p_intf, "Error loading script %s: %s",
1105 p_intf->p_sys->psz_filename,
1106 lua_tostring( L, lua_gettop( L ) ) );
1108 p_intf->b_die = VLC_TRUE;
1111 p_intf->b_die = VLC_TRUE;