]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
Port the http interface to the Lua Interface Module framework.
[vlc] / modules / misc / lua / intf.c
1 /*****************************************************************************
2  * intf.c: Generic lua inteface functions
3  *****************************************************************************
4  * Copyright (C) 2007 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 #include <vlc/vlc.h>
32 #include <vlc_meta.h>
33 #include <vlc_charset.h>
34
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
37 #include <vlc_aout.h>
38 #include <vlc_vout.h>
39 #include <vlc_osd.h>
40
41 #include <lua.h>        /* Low level lua C API */
42 #include <lauxlib.h>    /* Higher level C API */
43 #include <lualib.h>     /* Lua libs */
44
45 #include "vlc.h"
46
47 struct intf_sys_t
48 {
49     char *psz_filename;
50     lua_State *L;
51 };
52
53 /*****************************************************************************
54  * Internal lua<->vlc utils
55  *****************************************************************************/
56 static inline playlist_t *vlclua_get_playlist_internal( lua_State *L )
57 {
58     vlc_object_t *p_this = vlclua_get_this( L );
59     return pl_Yield( p_this );
60 }
61
62 static input_thread_t * vlclua_get_input_internal( lua_State *L )
63 {
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 );
68     return p_input;
69 }
70
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 )
76 {
77     vlc_bool_t b_bool;
78     if( lua_gettop( L ) > 1 ) return vlclua_error( L );
79
80     if( lua_gettop( L ) == 0 )
81         b_bool = !var_GetBool( p_obj, psz_name );
82     else /* lua_gettop( L ) == 1 */
83     {
84         b_bool = luaL_checkboolean( L, -1 )?VLC_TRUE:VLC_FALSE;
85         lua_pop( L, 1 );
86     }
87
88     if( b_bool != var_GetBool( p_obj, psz_name ) )
89         var_SetBool( p_obj, psz_name, b_bool );
90
91     lua_pushboolean( L, b_bool );
92     return 1;
93 }
94
95 /*****************************************************************************
96  * Libvlc TODO: move to vlc.c
97  *****************************************************************************/
98 static int vlclua_get_libvlc( lua_State *L )
99 {
100     vlclua_push_vlc_object( L, vlclua_get_this( L )->p_libvlc,
101                             NULL );
102     return 1;
103 }
104
105 /*****************************************************************************
106  * Input handling
107  *****************************************************************************/
108 static int vlclua_get_input( lua_State *L )
109 {
110     input_thread_t *p_input = vlclua_get_input_internal( L );
111     if( p_input )
112     {
113         vlclua_push_vlc_object( L, p_input, vlclua_gc_release );
114     }
115     else lua_pushnil( L );
116     return 1;
117 }
118
119 static int vlclua_input_info( lua_State *L )
120 {
121     input_thread_t * p_input = vlclua_get_input_internal( L );
122     int i_cat;
123     int i;
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++ )
129     {
130         info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
131         int i_infos = p_category->i_infos;
132         int j;
133         lua_pushstring( L, p_category->psz_name );
134         lua_createtable( L, 0, i_infos );
135         for( j = 0; j < i_infos; j++ )
136         {
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 );
141         }
142         lua_settable( L, -3 );
143     }
144     //vlc_object_release( p_input );
145     return 1;
146 }
147
148 static int vlclua_is_playing( lua_State *L )
149 {
150     input_thread_t * p_input = vlclua_get_input_internal( L );
151     lua_pushboolean( L, !!p_input );
152     return 1;
153 }
154
155 static int vlclua_get_title( lua_State *L )
156 {
157     input_thread_t *p_input = vlclua_get_input_internal( L );
158     if( !p_input )
159         lua_pushnil( L );
160     else
161     {
162         lua_pushstring( L, input_GetItem(p_input)->psz_name );
163         vlc_object_release( p_input );
164     }
165     return 1;
166 }
167
168 static int vlclua_input_stats( lua_State *L )
169 {
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;
172     lua_newtable( L );
173     if( p_item )
174     {
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 )
192 #undef STATS_INT
193 #undef STATS_FLOAT
194     }
195     return 1;
196 }
197
198 /*****************************************************************************
199  * Vout control
200  *****************************************************************************/
201 static int vlclua_fullscreen( lua_State *L )
202 {
203     vout_thread_t *p_vout;
204     int i_ret;
205
206     input_thread_t * p_input = vlclua_get_input_internal( L );
207     if( !p_input ) return vlclua_error( L );
208
209     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
210     if( !p_vout ) return vlclua_error( L );
211
212     i_ret = vlclua_var_toggle_or_set( L, p_vout, "fullscreen" );
213     vlc_object_release( p_vout );
214     vlc_object_release( p_input );
215     return i_ret;
216 }
217
218 static int vlc_osd_icon_from_string( const char *psz_name )
219 {
220     static const struct
221     {
222         int i_icon;
223         const char *psz_name;
224     } pp_icons[] =
225         { { OSD_PAUSE_ICON, "pause" },
226           { OSD_PLAY_ICON, "play" },
227           { OSD_SPEAKER_ICON, "speaker" },
228           { OSD_MUTE_ICON, "mute" },
229           { 0, NULL } };
230     int i;
231     for( i = 0; pp_icons[i].psz_name; i++ )
232     {
233         if( !strcmp( psz_name, pp_icons[i].psz_name ) )
234             return pp_icons[i].i_icon;
235     }
236     return 0;
237 }
238
239 static int vlclua_osd_icon( lua_State *L )
240 {
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 );
244     if( !i_icon )
245         return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );
246     else
247     {
248         vlc_object_t *p_this = vlclua_get_this( L );
249         vout_OSDIcon( p_this, i_chan, i_icon );
250         return 0;
251     }
252 }
253
254 static int vlclua_osd_message( lua_State *L )
255 {
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 );
260     return 0;
261 }
262
263 static int vlc_osd_slider_type_from_string( const char *psz_name )
264 {
265     static const struct
266     {
267         int i_type;
268         const char *psz_name;
269     } pp_types[] =
270         { { OSD_HOR_SLIDER, "horizontal" },
271           { OSD_VERT_SLIDER, "vertical" },
272           { 0, NULL } };
273     int i;
274     for( i = 0; pp_types[i].psz_name; i++ )
275     {
276         if( !strcmp( psz_name, pp_types[i].psz_name ) )
277             return pp_types[i].i_type;
278     }
279     return 0;
280 }
281
282 static int vlclua_osd_slider( lua_State *L )
283 {
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 );
288     if( !i_type )
289         return luaL_error( L, "\"%s\" is not a valid slider type.",
290                            psz_type );
291     else
292     {
293         vlc_object_t *p_this = vlclua_get_this( L );
294         vout_OSDSlider( p_this, i_chan, i_position, i_type );
295         return 0;
296     }
297 }
298
299 static int vlclua_spu_channel_register( lua_State *L )
300 {
301     int i_chan;
302     vlc_object_t *p_this = vlclua_get_this( L );
303     vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
304                                              FIND_ANYWHERE );
305     if( !p_vout )
306         return luaL_error( L, "Unable to find vout." );
307
308     spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER, &i_chan );
309     vlc_object_release( p_vout );
310     lua_pushinteger( L, i_chan );
311     return 1;
312 }
313
314 static int vlclua_spu_channel_clear( lua_State *L )
315 {
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,
319                                              FIND_ANYWHERE );
320     if( !p_vout )
321         return luaL_error( L, "Unable to find vout." );
322
323     spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, i_chan );
324     vlc_object_release( p_vout );
325     return 0;
326 }
327
328 /*****************************************************************************
329  * Playlist control
330  *****************************************************************************/
331 static int vlclua_get_playlist( lua_State *L )
332 {
333     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
334     if( p_playlist )
335     {
336         vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
337     }
338     else lua_pushnil( L );
339     return 1;
340 }
341
342 static int vlclua_playlist_prev( lua_State * L )
343 {
344     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
345     playlist_Prev( p_playlist );
346     vlc_object_release( p_playlist );
347     return 0;
348 }
349
350 static int vlclua_playlist_next( lua_State * L )
351 {
352     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
353     playlist_Next( p_playlist );
354     vlc_object_release( p_playlist );
355     return 0;
356 }
357
358 static int vlclua_playlist_play( lua_State * L )
359 {
360     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
361     playlist_Play( p_playlist );
362     vlc_object_release( p_playlist );
363     return 0;
364 }
365
366 static int vlclua_playlist_stop( lua_State * L )
367 {
368     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
369     playlist_Stop( p_playlist );
370     vlc_object_release( p_playlist );
371     return 0;
372 }
373
374 static int vlclua_playlist_clear( lua_State * L )
375 {
376     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
377     playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
378     playlist_Clear( p_playlist, VLC_FALSE );
379     vlc_object_release( p_playlist );
380     return 0;
381 }
382
383 static int vlclua_playlist_repeat( lua_State * L )
384 {
385     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
386     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
387     vlc_object_release( p_playlist );
388     return i_ret;
389 }
390
391 static int vlclua_playlist_loop( lua_State * L )
392 {
393     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
394     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
395     vlc_object_release( p_playlist );
396     return i_ret;
397 }
398
399 static int vlclua_playlist_random( lua_State * L )
400 {
401     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
402     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
403     vlc_object_release( p_playlist );
404     return i_ret;
405 }
406
407 static int vlclua_playlist_goto( lua_State * L )
408 {
409     /* XXX: logic copied from rc.c ... i'm not sure that it's ok as it
410      *      implies knowledge of the playlist internals. */
411     playlist_t *p_playlist;
412     int i_size;
413     playlist_item_t *p_item, *p_parent;
414
415     int i_pos;
416     if( lua_gettop( L ) != 1 ) return vlclua_error( L );
417     i_pos = luaL_checkint( L, -1 );
418     lua_pop( L, 1 );
419     if( i_pos <= 0 ) return 0;
420
421     p_playlist = vlclua_get_playlist_internal( L );
422     /* The playlist stores 2 times the same item: onelevel & category */
423     i_size = p_playlist->items.i_size / 2;
424
425     if( i_pos > i_size )
426     {
427         vlc_object_release( p_playlist );
428         return 0;
429     }
430
431     p_item = p_parent = p_playlist->items.p_elems[i_pos*2-1];
432     while( p_parent->p_parent )
433         p_parent = p_parent->p_parent;
434     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
435                       p_parent, p_item );
436
437     vlc_object_release( p_playlist );
438     lua_pushboolean( L, 1 );
439     return 1;
440 }
441
442 static int vlclua_playlist_add( lua_State *L )
443 {
444     int i_count;
445     vlc_object_t *p_this = vlclua_get_this( L );
446     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
447     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
448                                             NULL, VLC_TRUE );
449     vlc_object_release( p_playlist );
450     lua_pushinteger( L, i_count );
451     return 1;
452 }
453
454 static int vlclua_playlist_enqueue( lua_State *L )
455 {
456     int i_count;
457     vlc_object_t *p_this = vlclua_get_this( L );
458     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
459     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
460                                             NULL, VLC_FALSE );
461     vlc_object_release( p_playlist );
462     lua_pushinteger( L, i_count );
463     return 1;
464 }
465
466 static int vlclua_playlist_get( lua_State *L )
467 {
468     /* TODO: make it possible to get the tree playlist */
469     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
470     playlist_item_t *p_root;
471     int i;
472     if( lua_isboolean( L, 1 ) && lua_toboolean( L, 1 ) )
473         p_root = p_playlist->p_ml_onelevel; /* media library */
474     else
475         p_root = p_playlist->p_local_onelevel; /* local/normal playlist */
476     lua_createtable( L, p_root->i_children, 0 );
477     for( i = 0; i < p_root->i_children; i++ )
478     {
479         playlist_item_t *p_item = p_root->pp_children[i];
480         input_item_t *p_input = p_item->p_input;
481         lua_pushinteger( L, i+1 );
482         lua_newtable( L );
483         lua_pushstring( L, p_input->psz_name );
484         lua_setfield( L, -2, "name" );
485         lua_pushstring( L, p_input->psz_uri );
486         lua_setfield( L, -2, "path" );
487         if( p_input->i_duration < 0 )
488             lua_pushnumber( L, -1 );
489         else
490             lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
491         lua_setfield( L, -2, "duration" );
492         lua_pushinteger( L, p_input->i_nb_played );
493         lua_setfield( L, -2, "nb_played" );
494         /* TODO: add (optional) info categories, meta, options, es */
495         lua_settable( L, -3 );
496     }
497     vlc_object_release( p_playlist );
498     return 1;
499 }
500
501 static int vlclua_playlist_sort( lua_State *L )
502 {
503     /* allow setting the different sort keys */
504     return 0;
505 }
506
507 /* FIXME: split this in 3 different functions? */
508 static int vlclua_playlist_status( lua_State *L )
509 {
510     intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
511     playlist_t *p_playlist = pl_Yield( p_intf );
512     /*
513     int i_count = 0;
514     lua_settop( L, 0 );*/
515     if( p_playlist->p_input )
516     {
517         /*char *psz_uri =
518             input_item_GetURI( input_GetItem( p_playlist->p_input ) );
519         lua_pushstring( L, psz_uri );
520         free( psz_uri );
521         lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
522         vlc_mutex_lock( &p_playlist->object_lock );
523         switch( p_playlist->status.i_status )
524         {
525             case PLAYLIST_STOPPED:
526                 lua_pushstring( L, "stopped" );
527                 break;
528             case PLAYLIST_RUNNING:
529                 lua_pushstring( L, "playing" );
530                 break;
531             case PLAYLIST_PAUSED:
532                 lua_pushstring( L, "paused" );
533                 break;
534             default:
535                 lua_pushstring( L, "unknown" );
536                 break;
537         }
538         vlc_mutex_unlock( &p_playlist->object_lock );
539         /*i_count += 3;*/
540     }
541     else
542     {
543         lua_pushstring( L, "stopped" );
544     }
545     vlc_object_release( p_playlist );
546     return 1;
547 }
548
549
550 static int vlclua_lock_and_wait( lua_State *L )
551 {
552     vlc_object_t *p_this = vlclua_get_this( L );
553     int b_quit = vlc_object_lock_and_wait( p_this );
554     lua_pushboolean( L, b_quit );
555     return 1;
556 }
557
558 static int vlclua_signal( lua_State *L )
559 {
560     vlc_object_t *p_this = vlclua_get_this( L );
561     vlc_object_signal( p_this );
562     return 0;
563 }
564
565 static int vlclua_mdate( lua_State *L )
566 {
567     lua_pushnumber( L, mdate() );
568     return 1;
569 }
570
571 static int vlclua_intf_should_die( lua_State *L )
572 {
573     intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
574     lua_pushboolean( L, intf_ShouldDie( p_intf ) );
575     return 1;
576 }
577
578 static luaL_Reg p_reg[] =
579 {
580     { "input_info", vlclua_input_info },
581     { "is_playing", vlclua_is_playing },
582     { "get_title", vlclua_get_title },
583
584     { "fullscreen", vlclua_fullscreen },
585
586     { "mdate", vlclua_mdate },
587
588     { "module_command", vlclua_module_command },
589     { "libvlc_command", vlclua_libvlc_command },
590
591     { "decode_uri", vlclua_decode_uri },
592     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
593     { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
594
595     { "lock_and_wait", vlclua_lock_and_wait },
596     { "signal", vlclua_signal },
597
598     { "version", vlclua_version },
599     { "license", vlclua_license },
600     { "copyright", vlclua_copyright },
601     { "should_die", vlclua_intf_should_die },
602     { "quit", vlclua_quit },
603
604     { "homedir", vlclua_homedir },
605     { "datadir", vlclua_datadir },
606     { "configdir", vlclua_configdir },
607     { "cachedir", vlclua_cachedir },
608     { "datadir_list", vlclua_datadir_list },
609
610     { NULL, NULL }
611 };
612
613 static luaL_Reg p_reg_object[] =
614 {
615     { "input", vlclua_get_input },              /* This is fast */
616     { "playlist", vlclua_get_playlist },        /* This is fast */
617     { "libvlc", vlclua_get_libvlc },            /* This is fast */
618
619     { "find", vlclua_object_find },             /* This is slow */
620     { "find_name", vlclua_object_find_name },   /* This is slow */
621
622     { NULL, NULL }
623 };
624
625 static luaL_Reg p_reg_var[] =
626 {
627     { "get", vlclua_var_get },
628     { "get_list", vlclua_var_get_list },
629     { "set", vlclua_var_set },
630     { "add_callback", vlclua_add_callback },
631     { "del_callback", vlclua_del_callback },
632
633     { NULL, NULL }
634 };
635
636 static luaL_Reg p_reg_config[] =
637 {
638     { "get", vlclua_config_get },
639     { "set", vlclua_config_set },
640
641     { NULL, NULL }
642 };
643
644 static luaL_Reg p_reg_msg[] =
645 {
646     { "dbg", vlclua_msg_dbg },
647     { "warn", vlclua_msg_warn },
648     { "err", vlclua_msg_err },
649     { "info", vlclua_msg_info },
650
651     { NULL, NULL }
652 };
653
654 static luaL_Reg p_reg_playlist[] =
655 {
656     { "prev", vlclua_playlist_prev },
657     { "next", vlclua_playlist_next },
658     { "play", vlclua_playlist_play },
659     { "stop", vlclua_playlist_stop },
660     { "clear", vlclua_playlist_clear },
661     { "repeat_", vlclua_playlist_repeat },
662     { "loop", vlclua_playlist_loop },
663     { "random", vlclua_playlist_random },
664     { "goto", vlclua_playlist_goto },
665     { "status", vlclua_playlist_status },
666     { "add", vlclua_playlist_add },
667     { "enqueue", vlclua_playlist_enqueue },
668     { "get", vlclua_playlist_get },
669     { "stats", vlclua_input_stats },
670
671     { NULL, NULL }
672 };
673
674 static luaL_Reg p_reg_volume[] =
675 {
676     { "get", vlclua_volume_get },
677     { "set", vlclua_volume_set },
678     { "up", vlclua_volume_up },
679     { "down", vlclua_volume_down },
680
681     { NULL, NULL }
682 };
683
684 static luaL_Reg p_reg_osd[] =
685 {
686     { "icon", vlclua_osd_icon },
687     { "message", vlclua_osd_message },
688     { "slider", vlclua_osd_slider },
689     { "channel_register", vlclua_spu_channel_register },
690     { "channel_clear", vlclua_spu_channel_clear },
691
692     { NULL, NULL }
693 };
694
695 static luaL_Reg p_reg_net[] =
696 {
697     { "url_parse", vlclua_url_parse },
698     { "listen_tcp", vlclua_net_listen_tcp },
699     { "listen_close", vlclua_net_listen_close },
700     { "accept", vlclua_net_accept },
701     { "close", vlclua_net_close },
702     { "send", vlclua_net_send },
703     { "recv", vlclua_net_recv },
704     { "select", vlclua_net_select },
705
706     { NULL, NULL }
707 };
708
709 static luaL_Reg p_reg_fd[] =
710 {
711 /*    { "open", vlclua_fd_open },*/
712     { "read", vlclua_fd_read },
713     { "write", vlclua_fd_write },
714     { "stat", vlclua_stat },
715
716     { "opendir", vlclua_opendir },
717
718     { "new_fd_set", vlclua_fd_set_new },
719     { "fd_clr", vlclua_fd_clr },
720     { "fd_isset", vlclua_fd_isset },
721     { "fd_set", vlclua_fd_set },
722     { "fd_zero", vlclua_fd_zero },
723
724     { NULL, NULL }
725 };
726
727 static luaL_Reg p_reg_vlm[] =
728 {
729     { "new", vlclua_vlm_new },
730     { "delete", vlclua_vlm_delete },
731     { "execute_command", vlclua_vlm_execute_command },
732
733     { NULL, NULL }
734 };
735
736 static luaL_Reg p_reg_httpd[] =
737 {
738     { "host_new", vlclua_httpd_tls_host_new },
739     { "host_delete", vlclua_httpd_host_delete },
740     { "handler_new", vlclua_httpd_handler_new },
741     { "handler_delete", vlclua_httpd_handler_delete },
742     { "file_new", vlclua_httpd_file_new },
743     { "file_delete", vlclua_httpd_file_delete },
744     { "redirect_new", vlclua_httpd_redirect_new },
745     { "redirect_delete", vlclua_httpd_redirect_delete },
746
747     { NULL, NULL }
748 };
749
750 static luaL_Reg p_reg_acl[] =
751 {
752     { "create", vlclua_acl_create },
753     { "delete", vlclua_acl_delete },
754     { "check", vlclua_acl_check },
755     { "duplicate", vlclua_acl_duplicate },
756     { "add_host", vlclua_acl_add_host },
757     { "add_net", vlclua_acl_add_net },
758     { "load_file", vlclua_acl_load_file },
759
760     { NULL, NULL }
761 };
762
763 static void Run( intf_thread_t *p_intf );
764
765 static char *FindFile( intf_thread_t *p_intf, const char *psz_name )
766 {
767     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
768     char **ppsz_dir;
769     vlclua_dir_list( VLC_OBJECT(p_intf), "luaintf", ppsz_dir_list );
770     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
771     {
772         char *psz_filename;
773         FILE *fp;
774         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
775                       psz_name ) < 0 )
776         {
777             return NULL;
778         }
779         fp = fopen( psz_filename, "r" );
780         if( fp )
781         {
782             fclose( fp );
783             return psz_filename;
784         }
785         free( psz_filename );
786     }
787     return NULL;
788 }
789
790 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
791                                             const luaL_Reg *l )
792 {
793     lua_newtable( L );
794     luaL_register( L, NULL, l );
795     lua_setfield( L, -2, psz_name );
796 }
797
798 static struct
799 {
800     const char *psz_shortcut;
801     const char *psz_name;
802 } pp_shortcuts[] = {
803     { "luarc", "rc" },
804     /* { "rc", "rc" }, */
805     { "luahotkeys", "hotkeys" },
806     /* { "hotkeys", "hotkeys" }, */
807     { "luatelnet", "telnet" },
808     /* { "telnet", "telnet" }, */
809     { NULL, NULL } };
810
811 static vlc_bool_t WordInList( const char *psz_list, const char *psz_word )
812 {
813     const char *psz_str = strstr( psz_list, psz_word );
814     int i_len = strlen( psz_word );
815     while( psz_str )
816     {
817         if( (psz_str == psz_list || *(psz_str-1) == ',' )
818          /* it doesn't start in middle of a word */
819          /* it doest end in middle of a word */
820          && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
821             return VLC_TRUE;
822         psz_str = strstr( psz_str, psz_word );
823     }
824     return VLC_FALSE;
825 }
826
827 static const char *GetModuleName( intf_thread_t *p_intf )
828 {
829     int i;
830     const char *psz_intf;
831     if( *p_intf->psz_intf == '$' )
832         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
833     else
834         psz_intf = p_intf->psz_intf;
835     for( i = 0; pp_shortcuts[i].psz_name; i++ )
836     {
837         if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
838             return pp_shortcuts[i].psz_name;
839     }
840
841     return config_GetPsz( p_intf, "lua-intf" );
842 }
843
844 int E_(Open_LuaIntf)( vlc_object_t *p_this )
845 {
846     intf_thread_t *p_intf = (intf_thread_t*)p_this;
847     intf_sys_t *p_sys;
848     lua_State *L;
849
850     const char *psz_name = GetModuleName( p_intf );
851     const char *psz_config;
852     vlc_bool_t b_config_set = VLC_FALSE;
853     if( !psz_name ) psz_name = "dummy";
854
855     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
856     if( !p_intf->p_sys )
857     {
858         return VLC_ENOMEM;
859     }
860     p_sys = p_intf->p_sys;
861     p_sys->psz_filename = FindFile( p_intf, psz_name );
862     if( !p_sys->psz_filename )
863     {
864         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
865                  psz_name );
866         return VLC_EGENERIC;
867     }
868     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
869
870     L = luaL_newstate();
871     if( !L )
872     {
873         msg_Err( p_intf, "Could not create new Lua State" );
874         free( p_sys );
875         return VLC_EGENERIC;
876     }
877
878     luaL_openlibs( L ); /* FIXME: we don't want to have all the libs */
879
880     /* register our functions */
881     luaL_register( L, "vlc", p_reg );
882     /* store a pointer to p_intf */
883     lua_pushlightuserdata( L, p_intf );
884     lua_setfield( L, -2, "private" );
885     /* register submodules */
886     luaL_register_submodule( L, "object", p_reg_object );
887     luaL_register_submodule( L, "var", p_reg_var );
888     luaL_register_submodule( L, "config", p_reg_config );
889     luaL_register_submodule( L, "msg", p_reg_msg );
890     luaL_register_submodule( L, "playlist", p_reg_playlist );
891     luaL_register_submodule( L, "volume", p_reg_volume );
892     luaL_register_submodule( L, "osd", p_reg_osd );
893     luaL_register_submodule( L, "net", p_reg_net );
894     luaL_register_submodule( L, "fd", p_reg_fd );
895     luaL_register_submodule( L, "vlm", p_reg_vlm );
896     luaL_register_submodule( L, "httpd", p_reg_httpd );
897     luaL_register_submodule( L, "acl", p_reg_acl );
898     /* clean up */
899     lua_pop( L, 1 );
900
901     /* <gruik> */
902     /* Setup the module search path */
903     {
904     char *psz_command;
905     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
906     *psz_char = '\0';
907     /* FIXME: don't use luaL_dostring */
908     if( asprintf( &psz_command,
909                   "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
910                   p_sys->psz_filename ) < 0 )
911         return VLC_EGENERIC;
912     *psz_char = DIR_SEP_CHAR;
913     if( luaL_dostring( L, psz_command ) )
914         return VLC_EGENERIC;
915     }
916     /* </gruik> */
917
918     psz_config = config_GetPsz( p_intf, "lua-config" );
919     if( psz_config && *psz_config )
920     {
921         char *psz_buffer;
922         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
923         {
924             printf("%s\n", psz_buffer);
925             if( luaL_dostring( L, psz_buffer ) == 1 )
926                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
927             free( psz_buffer );
928             lua_getglobal( L, "config" );
929             if( lua_istable( L, -1 ) )
930             {
931                 lua_getfield( L, -1, psz_name );
932                 if( lua_istable( L, -1 ) )
933                 {
934                     lua_setglobal( L, "config" );
935                     b_config_set = VLC_TRUE;
936                 }
937             }
938         }
939     }
940     if( b_config_set == VLC_FALSE )
941     {
942         lua_newtable( L );
943         lua_setglobal( L, "config" );
944     }
945
946     p_sys->L = L;
947
948     p_intf->pf_run = Run;
949     p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in E_(Close_LuaIntf)? */
950
951     return VLC_SUCCESS;
952 }
953
954 void E_(Close_LuaIntf)( vlc_object_t *p_this )
955 {
956     intf_thread_t *p_intf = (intf_thread_t*)p_this;
957
958     lua_close( p_intf->p_sys->L );
959     free( p_intf->p_sys );
960 }
961
962 static void Run( intf_thread_t *p_intf )
963 {
964     lua_State *L = p_intf->p_sys->L;
965
966     if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
967     {
968         msg_Err( p_intf, "Error loading script %s: %s",
969                  p_intf->p_sys->psz_filename,
970                  lua_tostring( L, lua_gettop( L ) ) );
971         lua_pop( L, 1 );
972         p_intf->b_die = VLC_TRUE;
973         return;
974     }
975     p_intf->b_die = VLC_TRUE;
976 }