]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
* modules/misc/lua:
[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 /*****************************************************************************
169  * Vout control
170  *****************************************************************************/
171 static int vlclua_fullscreen( lua_State *L )
172 {
173     vout_thread_t *p_vout;
174     int i_ret;
175
176     input_thread_t * p_input = vlclua_get_input_internal( L );
177     if( !p_input ) return vlclua_error( L );
178
179     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
180     if( !p_vout ) return vlclua_error( L );
181
182     i_ret = vlclua_var_toggle_or_set( L, p_vout, "fullscreen" );
183     vlc_object_release( p_vout );
184     vlc_object_release( p_input );
185     return i_ret;
186 }
187
188 static int vlc_osd_icon_from_string( const char *psz_name )
189 {
190     static const struct
191     {
192         int i_icon;
193         const char *psz_name;
194     } pp_icons[] =
195         { { OSD_PAUSE_ICON, "pause" },
196           { OSD_PLAY_ICON, "play" },
197           { OSD_SPEAKER_ICON, "speaker" },
198           { OSD_MUTE_ICON, "mute" },
199           { 0, NULL } };
200     int i;
201     for( i = 0; pp_icons[i].psz_name; i++ )
202     {
203         if( !strcmp( psz_name, pp_icons[i].psz_name ) )
204             return pp_icons[i].i_icon;
205     }
206     return 0;
207 }
208
209 static int vlclua_osd_icon( lua_State *L )
210 {
211     const char *psz_icon = luaL_checkstring( L, 1 );
212     int i_icon = vlc_osd_icon_from_string( psz_icon );
213     int i_chan = luaL_optint( L, 2, DEFAULT_CHAN );
214     if( !i_icon )
215         return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );
216     else
217     {
218         vlc_object_t *p_this = vlclua_get_this( L );
219         vout_OSDIcon( p_this, i_chan, i_icon );
220         return 0;
221     }
222 }
223
224 static int vlclua_osd_message( lua_State *L )
225 {
226     const char *psz_message = luaL_checkstring( L, 1 );
227     int i_chan = luaL_optint( L, 2, DEFAULT_CHAN );
228     vlc_object_t *p_this = vlclua_get_this( L );
229     vout_OSDMessage( p_this, i_chan, psz_message );
230     return 0;
231 }
232
233 static int vlc_osd_slider_type_from_string( const char *psz_name )
234 {
235     static const struct
236     {
237         int i_type;
238         const char *psz_name;
239     } pp_types[] =
240         { { OSD_HOR_SLIDER, "horizontal" },
241           { OSD_VERT_SLIDER, "vertical" },
242           { 0, NULL } };
243     int i;
244     for( i = 0; pp_types[i].psz_name; i++ )
245     {
246         if( !strcmp( psz_name, pp_types[i].psz_name ) )
247             return pp_types[i].i_type;
248     }
249     return 0;
250 }
251
252 static int vlclua_osd_slider( lua_State *L )
253 {
254     int i_position = luaL_checkint( L, 1 );
255     const char *psz_type = luaL_checkstring( L, 2 );
256     int i_type = vlc_osd_slider_type_from_string( psz_type );
257     int i_chan = luaL_optint( L, 3, DEFAULT_CHAN );
258     if( !i_type )
259         return luaL_error( L, "\"%s\" is not a valid slider type.",
260                            psz_type );
261     else
262     {
263         vlc_object_t *p_this = vlclua_get_this( L );
264         vout_OSDSlider( p_this, i_chan, i_position, i_type );
265         return 0;
266     }
267 }
268
269 static int vlclua_spu_channel_register( lua_State *L )
270 {
271     int i_chan;
272     vlc_object_t *p_this = vlclua_get_this( L );
273     vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
274                                              FIND_ANYWHERE );
275     if( !p_vout )
276         return luaL_error( L, "Unable to find vout." );
277
278     spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER, &i_chan );
279     vlc_object_release( p_vout );
280     lua_pushinteger( L, i_chan );
281     return 1;
282 }
283
284 static int vlclua_spu_channel_clear( lua_State *L )
285 {
286     int i_chan = luaL_checkint( L, 1 );
287     vlc_object_t *p_this = vlclua_get_this( L );
288     vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
289                                              FIND_ANYWHERE );
290     if( !p_vout )
291         return luaL_error( L, "Unable to find vout." );
292
293     spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, i_chan );
294     vlc_object_release( p_vout );
295     return 0;
296 }
297
298 /*****************************************************************************
299  * Playlist control
300  *****************************************************************************/
301 static int vlclua_get_playlist( lua_State *L )
302 {
303     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
304     if( p_playlist )
305     {
306         vlclua_push_vlc_object( L, p_playlist, vlclua_gc_release );
307     }
308     else lua_pushnil( L );
309     return 1;
310 }
311
312 static int vlclua_playlist_prev( lua_State * L )
313 {
314     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
315     playlist_Prev( p_playlist );
316     vlc_object_release( p_playlist );
317     return 0;
318 }
319
320 static int vlclua_playlist_next( lua_State * L )
321 {
322     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
323     playlist_Next( p_playlist );
324     vlc_object_release( p_playlist );
325     return 0;
326 }
327
328 static int vlclua_playlist_play( lua_State * L )
329 {
330     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
331     playlist_Play( p_playlist );
332     vlc_object_release( p_playlist );
333     return 0;
334 }
335
336 static int vlclua_playlist_stop( lua_State * L )
337 {
338     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
339     playlist_Stop( p_playlist );
340     vlc_object_release( p_playlist );
341     return 0;
342 }
343
344 static int vlclua_playlist_clear( lua_State * L )
345 {
346     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
347     playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
348     playlist_Clear( p_playlist, VLC_FALSE );
349     vlc_object_release( p_playlist );
350     return 0;
351 }
352
353 static int vlclua_playlist_repeat( lua_State * L )
354 {
355     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
356     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
357     vlc_object_release( p_playlist );
358     return i_ret;
359 }
360
361 static int vlclua_playlist_loop( lua_State * L )
362 {
363     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
364     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
365     vlc_object_release( p_playlist );
366     return i_ret;
367 }
368
369 static int vlclua_playlist_random( lua_State * L )
370 {
371     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
372     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
373     vlc_object_release( p_playlist );
374     return i_ret;
375 }
376
377 static int vlclua_playlist_goto( lua_State * L )
378 {
379     /* XXX: logic copied from rc.c ... i'm not sure that it's ok as it
380      *      implies knowledge of the playlist internals. */
381     playlist_t *p_playlist;
382     int i_size;
383     playlist_item_t *p_item, *p_parent;
384
385     int i_pos;
386     if( lua_gettop( L ) != 1 ) return vlclua_error( L );
387     i_pos = luaL_checkint( L, -1 );
388     lua_pop( L, 1 );
389     if( i_pos <= 0 ) return 0;
390
391     p_playlist = vlclua_get_playlist_internal( L );
392     /* The playlist stores 2 times the same item: onelevel & category */
393     i_size = p_playlist->items.i_size / 2;
394
395     if( i_pos > i_size )
396     {
397         vlc_object_release( p_playlist );
398         return 0;
399     }
400
401     p_item = p_parent = p_playlist->items.p_elems[i_pos*2-1];
402     while( p_parent->p_parent )
403         p_parent = p_parent->p_parent;
404     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
405                       p_parent, p_item );
406
407     vlc_object_release( p_playlist );
408     lua_pushboolean( L, 1 );
409     return 1;
410 }
411
412 static int vlclua_playlist_add( lua_State *L )
413 {
414     int i_count;
415     vlc_object_t *p_this = vlclua_get_this( L );
416     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
417     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
418                                             NULL, VLC_TRUE );
419     vlc_object_release( p_playlist );
420     lua_pushinteger( L, i_count );
421     return 1;
422 }
423
424 static int vlclua_playlist_enqueue( lua_State *L )
425 {
426     int i_count;
427     vlc_object_t *p_this = vlclua_get_this( L );
428     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
429     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
430                                             NULL, VLC_FALSE );
431     vlc_object_release( p_playlist );
432     lua_pushinteger( L, i_count );
433     return 1;
434 }
435
436 static int vlclua_playlist_get( lua_State *L )
437 {
438     /* TODO: make it possible to get the tree playlist */
439     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
440     playlist_item_t *p_root;
441     int i;
442     if( lua_isboolean( L, 1 ) && lua_toboolean( L, 1 ) )
443         p_root = p_playlist->p_ml_onelevel; /* media library */
444     else
445         p_root = p_playlist->p_local_onelevel; /* local/normal playlist */
446     lua_createtable( L, p_root->i_children, 0 );
447     for( i = 0; i < p_root->i_children; i++ )
448     {
449         playlist_item_t *p_item = p_root->pp_children[i];
450         input_item_t *p_input = p_item->p_input;
451         lua_pushinteger( L, i+1 );
452         lua_newtable( L );
453         lua_pushstring( L, p_input->psz_name );
454         lua_setfield( L, -2, "name" );
455         lua_pushstring( L, p_input->psz_uri );
456         lua_setfield( L, -2, "path" );
457         lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
458         lua_setfield( L, -2, "duration" );
459         lua_pushinteger( L, p_input->i_nb_played );
460         lua_setfield( L, -2, "nb_played" );
461         /* TODO: add (optional) info categories, meta, options, es */
462         lua_settable( L, -3 );
463     }
464     vlc_object_release( p_playlist );
465     return 1;
466 }
467
468 static int vlclua_playlist_sort( lua_State *L )
469 {
470     /* allow setting the different sort keys */
471     return 0;
472 }
473
474 /* FIXME: split this in 3 different functions? */
475 static int vlclua_playlist_status( lua_State *L )
476 {
477     intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
478     playlist_t *p_playlist = pl_Yield( p_intf );
479     int i_count = 0;
480     lua_settop( L, 0 );
481     if( p_playlist->p_input )
482     {
483         char *psz_uri =
484             input_item_GetURI( input_GetItem( p_playlist->p_input ) );
485         lua_pushstring( L, psz_uri );
486         free( psz_uri );
487         lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );
488         vlc_mutex_lock( &p_playlist->object_lock );
489         switch( p_playlist->status.i_status )
490         {
491             case PLAYLIST_STOPPED:
492                 lua_pushstring( L, "stopped" );
493                 break;
494             case PLAYLIST_RUNNING:
495                 lua_pushstring( L, "running" );
496                 break;
497             case PLAYLIST_PAUSED:
498                 lua_pushstring( L, "paused" );
499                 break;
500             default:
501                 lua_pushstring( L, "unknown" );
502                 break;
503         }
504         vlc_mutex_unlock( &p_playlist->object_lock );
505         i_count += 3;
506     }
507     vlc_object_release( p_playlist );
508     return i_count;
509 }
510
511
512 static int vlclua_lock_and_wait( lua_State *L )
513 {
514     vlc_object_t *p_this = vlclua_get_this( L );
515     int b_quit = vlc_object_lock_and_wait( p_this );
516     lua_pushboolean( L, b_quit );
517     return 1;
518 }
519
520 static int vlclua_signal( lua_State *L )
521 {
522     vlc_object_t *p_this = vlclua_get_this( L );
523     vlc_object_signal( p_this );
524     return 0;
525 }
526
527 static int vlclua_mdate( lua_State *L )
528 {
529     lua_pushnumber( L, mdate() );
530     return 1;
531 }
532
533 static int vlclua_intf_should_die( lua_State *L )
534 {
535     intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
536     lua_pushboolean( L, intf_ShouldDie( p_intf ) );
537     return 1;
538 }
539
540 static luaL_Reg p_reg[] =
541 {
542     { "input_info", vlclua_input_info },
543     { "is_playing", vlclua_is_playing },
544     { "get_title", vlclua_get_title },
545
546     { "fullscreen", vlclua_fullscreen },
547
548     { "mdate", vlclua_mdate },
549
550     { "module_command", vlclua_module_command },
551     { "libvlc_command", vlclua_libvlc_command },
552
553     { "decode_uri", vlclua_decode_uri },
554     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
555
556     { "lock_and_wait", vlclua_lock_and_wait },
557     { "signal", vlclua_signal },
558
559     { "version", vlclua_version },
560     { "license", vlclua_license },
561     { "should_die", vlclua_intf_should_die },
562     { "quit", vlclua_quit },
563
564     { NULL, NULL }
565 };
566
567 static luaL_Reg p_reg_object[] =
568 {
569     { "input", vlclua_get_input },              /* This is fast */
570     { "playlist", vlclua_get_playlist },        /* This is fast */
571     { "libvlc", vlclua_get_libvlc },            /* This is fast */
572
573     { "find", vlclua_object_find },             /* This is slow */
574     { "find_name", vlclua_object_find_name },   /* This is slow */
575
576     { NULL, NULL }
577 };
578
579 static luaL_Reg p_reg_var[] =
580 {
581     { "get", vlclua_var_get },
582     { "get_list", vlclua_var_get_list },
583     { "set", vlclua_var_set },
584     { "add_callback", vlclua_add_callback },
585     { "del_callback", vlclua_del_callback },
586
587     { NULL, NULL }
588 };
589
590 static luaL_Reg p_reg_config[] =
591 {
592     { "get", vlclua_config_get },
593     { "set", vlclua_config_set },
594
595     { NULL, NULL }
596 };
597
598 static luaL_Reg p_reg_msg[] =
599 {
600     { "dbg", vlclua_msg_dbg },
601     { "warn", vlclua_msg_warn },
602     { "err", vlclua_msg_err },
603     { "info", vlclua_msg_info },
604
605     { NULL, NULL }
606 };
607
608 static luaL_Reg p_reg_playlist[] =
609 {
610     { "prev", vlclua_playlist_prev },
611     { "next", vlclua_playlist_next },
612     { "play", vlclua_playlist_play },
613     { "stop", vlclua_playlist_stop },
614     { "clear", vlclua_playlist_clear },
615     { "repeat_", vlclua_playlist_repeat },
616     { "loop", vlclua_playlist_loop },
617     { "random", vlclua_playlist_random },
618     { "goto", vlclua_playlist_goto },
619     { "status", vlclua_playlist_status },
620     { "add", vlclua_playlist_add },
621     { "enqueue", vlclua_playlist_enqueue },
622     { "get", vlclua_playlist_get },
623
624     { NULL, NULL }
625 };
626
627 static luaL_Reg p_reg_volume[] =
628 {
629     { "get", vlclua_volume_get },
630     { "set", vlclua_volume_set },
631     { "up", vlclua_volume_up },
632     { "down", vlclua_volume_down },
633
634     { NULL, NULL }
635 };
636
637 static luaL_Reg p_reg_osd[] =
638 {
639     { "icon", vlclua_osd_icon },
640     { "message", vlclua_osd_message },
641     { "slider", vlclua_osd_slider },
642     { "channel_register", vlclua_spu_channel_register },
643     { "channel_clear", vlclua_spu_channel_clear },
644
645     { NULL, NULL }
646 };
647
648 static luaL_Reg p_reg_net[] =
649 {
650     { "url_parse", vlclua_url_parse },
651     { "listen_tcp", vlclua_net_listen_tcp },
652     { "listen_close", vlclua_net_listen_close },
653     { "accept", vlclua_net_accept },
654     { "close", vlclua_net_close },
655     { "send", vlclua_net_send },
656     { "recv", vlclua_net_recv },
657     { "select", vlclua_net_select },
658
659     { NULL, NULL }
660 };
661
662 static luaL_Reg p_reg_fd[] =
663 {
664 /*    { "open", vlclua_fd_open },*/
665     { "read", vlclua_fd_read },
666     { "write", vlclua_fd_write },
667
668     { "new_fd_set", vlclua_fd_set_new },
669     { "fd_clr", vlclua_fd_clr },
670     { "fd_isset", vlclua_fd_isset },
671     { "fd_set", vlclua_fd_set },
672     { "fd_zero", vlclua_fd_zero },
673
674     { NULL, NULL }
675 };
676
677 static luaL_Reg p_reg_vlm[] =
678 {
679     { "new", vlclua_vlm_new },
680     { "delete", vlclua_vlm_delete },
681     { "execute_command", vlclua_vlm_execute_command },
682
683     { NULL, NULL }
684 };
685
686
687 static void Run( intf_thread_t *p_intf );
688
689 static char *FindFile( intf_thread_t *p_intf, const char *psz_name )
690 {
691     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
692     char **ppsz_dir;
693     vlclua_dir_list( VLC_OBJECT(p_intf), "luaintf", ppsz_dir_list );
694     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
695     {
696         char *psz_filename;
697         FILE *fp;
698         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
699                       psz_name ) < 0 )
700         {
701             return NULL;
702         }
703         fp = fopen( psz_filename, "r" );
704         if( fp )
705         {
706             fclose( fp );
707             return psz_filename;
708         }
709         free( psz_filename );
710     }
711     return NULL;
712 }
713
714 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
715                                             const luaL_Reg *l )
716 {
717     lua_newtable( L );
718     luaL_register( L, NULL, l );
719     lua_setfield( L, -2, psz_name );
720 }
721
722 static struct
723 {
724     const char *psz_shortcut;
725     const char *psz_name;
726 } pp_shortcuts[] = {
727     { "luarc", "rc" },
728     /* { "rc", "rc" }, */
729     { "luahotkeys", "hotkeys" },
730     /* { "hotkeys", "hotkeys" }, */
731     { "luatelnet", "telnet" },
732     /* { "telnet", "telnet" }, */
733     { NULL, NULL } };
734
735 static vlc_bool_t WordInList( const char *psz_list, const char *psz_word )
736 {
737     const char *psz_str = strstr( psz_list, psz_word );
738     int i_len = strlen( psz_word );
739     while( psz_str )
740     {
741         if( (psz_str == psz_list || *(psz_str-1) == ',' )
742          /* it doesn't start in middle of a word */
743          /* it doest end in middle of a word */
744          && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
745             return VLC_TRUE;
746         psz_str = strstr( psz_str, psz_word );
747     }
748     return VLC_FALSE;
749 }
750
751 static const char *GetModuleName( intf_thread_t *p_intf )
752 {
753     int i;
754     const char *psz_intf;
755     if( *p_intf->psz_intf == '$' )
756         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
757     else
758         psz_intf = p_intf->psz_intf;
759     for( i = 0; pp_shortcuts[i].psz_name; i++ )
760     {
761         if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
762             return pp_shortcuts[i].psz_name;
763     }
764
765     return config_GetPsz( p_intf, "lua-intf" );
766 }
767
768 int E_(Open_LuaIntf)( vlc_object_t *p_this )
769 {
770     intf_thread_t *p_intf = (intf_thread_t*)p_this;
771     intf_sys_t *p_sys;
772     lua_State *L;
773
774     const char *psz_name = GetModuleName( p_intf );
775     const char *psz_config;
776     vlc_bool_t b_config_set = VLC_FALSE;
777     if( !psz_name ) psz_name = "dummy";
778
779     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
780     if( !p_intf->p_sys )
781     {
782         return VLC_ENOMEM;
783     }
784     p_sys = p_intf->p_sys;
785     p_sys->psz_filename = FindFile( p_intf, psz_name );
786     if( !p_sys->psz_filename )
787     {
788         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
789                  psz_name );
790         return VLC_EGENERIC;
791     }
792     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
793
794     L = luaL_newstate();
795     if( !L )
796     {
797         msg_Err( p_intf, "Could not create new Lua State" );
798         free( p_sys );
799         return VLC_EGENERIC;
800     }
801
802     luaL_openlibs( L ); /* FIXME: we don't want to have all the libs */
803
804     /* register our functions */
805     luaL_register( L, "vlc", p_reg );
806     /* store a pointer to p_intf */
807     lua_pushlightuserdata( L, p_intf );
808     lua_setfield( L, -2, "private" );
809     /* register submodules */
810     luaL_register_submodule( L, "object", p_reg_object );
811     luaL_register_submodule( L, "var", p_reg_var );
812     luaL_register_submodule( L, "config", p_reg_config );
813     luaL_register_submodule( L, "msg", p_reg_msg );
814     luaL_register_submodule( L, "playlist", p_reg_playlist );
815     luaL_register_submodule( L, "volume", p_reg_volume );
816     luaL_register_submodule( L, "osd", p_reg_osd );
817     luaL_register_submodule( L, "net", p_reg_net );
818     luaL_register_submodule( L, "fd", p_reg_fd );
819     luaL_register_submodule( L, "vlm", p_reg_vlm );
820     /* clean up */
821     lua_pop( L, 1 );
822
823     /* <gruik> */
824     /* Setup the module search path */
825     {
826     char *psz_command;
827     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
828     *psz_char = '\0';
829     /* FIXME: don't use luaL_dostring */
830     if( asprintf( &psz_command,
831                   "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
832                   p_sys->psz_filename ) < 0 )
833         return VLC_EGENERIC;
834     *psz_char = DIR_SEP_CHAR;
835     if( luaL_dostring( L, psz_command ) )
836         return VLC_EGENERIC;
837     }
838     /* </gruik> */
839
840     psz_config = config_GetPsz( p_intf, "lua-config" );
841     if( psz_config && *psz_config )
842     {
843         char *psz_buffer;
844         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
845         {
846             printf("%s\n", psz_buffer);
847             if( luaL_dostring( L, psz_buffer ) == 1 )
848                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
849             free( psz_buffer );
850             lua_getglobal( L, "config" );
851             if( lua_istable( L, -1 ) )
852             {
853                 lua_getfield( L, -1, psz_name );
854                 if( lua_istable( L, -1 ) )
855                 {
856                     lua_setglobal( L, "config" );
857                     b_config_set = VLC_TRUE;
858                 }
859             }
860         }
861     }
862     if( b_config_set == VLC_FALSE )
863     {
864         lua_newtable( L );
865         lua_setglobal( L, "config" );
866     }
867
868     p_sys->L = L;
869
870     p_intf->pf_run = Run;
871     p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in E_(Close_LuaIntf)? */
872
873     return VLC_SUCCESS;
874 }
875
876 void E_(Close_LuaIntf)( vlc_object_t *p_this )
877 {
878     intf_thread_t *p_intf = (intf_thread_t*)p_this;
879
880     lua_close( p_intf->p_sys->L );
881     free( p_intf->p_sys );
882 }
883
884 static void Run( intf_thread_t *p_intf )
885 {
886     lua_State *L = p_intf->p_sys->L;
887
888     if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
889     {
890         msg_Err( p_intf, "Error loading script %s: %s",
891                  p_intf->p_sys->psz_filename,
892                  lua_tostring( L, lua_gettop( L ) ) );
893         lua_pop( L, 1 );
894         p_intf->b_die = VLC_TRUE;
895         return;
896     }
897     p_intf->b_die = VLC_TRUE;
898 }