]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
Add a new type of VLC Lua module: Interfaces.
[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     { "should_die", vlclua_intf_should_die },
561     { "quit", vlclua_quit },
562
563     { NULL, NULL }
564 };
565
566 static luaL_Reg p_reg_object[] =
567 {
568     { "input", vlclua_get_input },              /* This is fast */
569     { "playlist", vlclua_get_playlist },        /* This is fast */
570     { "libvlc", vlclua_get_libvlc },            /* This is fast */
571
572     { "find", vlclua_object_find },             /* This is slow */
573     { "find_name", vlclua_object_find_name },   /* This is slow */
574
575     { NULL, NULL }
576 };
577
578 static luaL_Reg p_reg_var[] =
579 {
580     { "get", vlclua_var_get },
581     { "get_list", vlclua_var_get_list },
582     { "set", vlclua_var_set },
583     { "add_callback", vlclua_add_callback },
584     { "del_callback", vlclua_del_callback },
585
586     { NULL, NULL }
587 };
588
589 static luaL_Reg p_reg_config[] =
590 {
591     { "get", vlclua_config_get },
592     { "set", vlclua_config_set },
593
594     { NULL, NULL }
595 };
596
597 static luaL_Reg p_reg_msg[] =
598 {
599     { "dbg", vlclua_msg_dbg },
600     { "warn", vlclua_msg_warn },
601     { "err", vlclua_msg_err },
602     { "info", vlclua_msg_info },
603
604     { NULL, NULL }
605 };
606
607 static luaL_Reg p_reg_playlist[] =
608 {
609     { "prev", vlclua_playlist_prev },
610     { "next", vlclua_playlist_next },
611     { "play", vlclua_playlist_play },
612     { "stop", vlclua_playlist_stop },
613     { "clear", vlclua_playlist_clear },
614     { "repeat_", vlclua_playlist_repeat },
615     { "loop", vlclua_playlist_loop },
616     { "random", vlclua_playlist_random },
617     { "goto", vlclua_playlist_goto },
618     { "status", vlclua_playlist_status },
619     { "add", vlclua_playlist_add },
620     { "enqueue", vlclua_playlist_enqueue },
621     { "get", vlclua_playlist_get },
622
623     { NULL, NULL }
624 };
625
626 static luaL_Reg p_reg_volume[] =
627 {
628     { "get", vlclua_volume_get },
629     { "set", vlclua_volume_set },
630     { "up", vlclua_volume_up },
631     { "down", vlclua_volume_down },
632
633     { NULL, NULL }
634 };
635
636 static luaL_Reg p_reg_osd[] =
637 {
638     { "icon", vlclua_osd_icon },
639     { "message", vlclua_osd_message },
640     { "slider", vlclua_osd_slider },
641     { "channel_register", vlclua_spu_channel_register },
642     { "channel_clear", vlclua_spu_channel_clear },
643
644     { NULL, NULL }
645 };
646
647 static luaL_Reg p_reg_net[] =
648 {
649     { "url_parse", vlclua_url_parse },
650     { "listen_tcp", vlclua_net_listen_tcp },
651     { "listen_close", vlclua_net_listen_close },
652     { "accept", vlclua_net_accept },
653     { "close", vlclua_net_close },
654     { "send", vlclua_net_send },
655     { "recv", vlclua_net_recv },
656     { "select", vlclua_net_select },
657
658     { NULL, NULL }
659 };
660
661 static luaL_Reg p_reg_fd[] =
662 {
663 /*    { "open", vlclua_fd_open },*/
664     { "read", vlclua_fd_read },
665     { "write", vlclua_fd_write },
666
667     { "new_fd_set", vlclua_fd_set_new },
668     { "fd_clr", vlclua_fd_clr },
669     { "fd_isset", vlclua_fd_isset },
670     { "fd_set", vlclua_fd_set },
671     { "fd_zero", vlclua_fd_zero },
672
673     { NULL, NULL }
674 };
675
676 static luaL_Reg p_reg_vlm[] =
677 {
678     { "new", vlclua_vlm_new },
679     { "delete", vlclua_vlm_delete },
680     { "execute_command", vlclua_vlm_execute_command },
681
682     { NULL, NULL }
683 };
684
685
686 static void Run( intf_thread_t *p_intf );
687
688 static char *FindFile( intf_thread_t *p_intf, const char *psz_name )
689 {
690     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
691     char **ppsz_dir;
692     vlclua_dir_list( VLC_OBJECT(p_intf), "luaintf", ppsz_dir_list );
693     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
694     {
695         char *psz_filename;
696         FILE *fp;
697         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
698                       psz_name ) < 0 )
699         {
700             return NULL;
701         }
702         fp = fopen( psz_filename, "r" );
703         if( fp )
704         {
705             fclose( fp );
706             return psz_filename;
707         }
708         free( psz_filename );
709     }
710     return NULL;
711 }
712
713 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
714                                             const luaL_Reg *l )
715 {
716     lua_newtable( L );
717     luaL_register( L, NULL, l );
718     lua_setfield( L, -2, psz_name );
719 }
720
721 static struct
722 {
723     const char *psz_shortcut;
724     const char *psz_name;
725 } pp_shortcuts[] = {
726     { "luarc", "rc" },
727     /* { "rc", "rc" }, */
728     { "luahotkeys", "hotkeys" },
729     /* { "hotkeys", "hotkeys" }, */
730     { "luatelnet", "telnet" },
731     /* { "telnet", "telnet" }, */
732     { NULL, NULL } };
733
734 static vlc_bool_t WordInList( const char *psz_list, const char *psz_word )
735 {
736     const char *psz_str = strstr( psz_list, psz_word );
737     int i_len = strlen( psz_word );
738     while( psz_str )
739     {
740         if( (psz_str == psz_list || *(psz_str-1) == ',' )
741          /* it doesn't start in middle of a word */
742          /* it doest end in middle of a word */
743          && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
744             return VLC_TRUE;
745         psz_str = strstr( psz_str, psz_word );
746     }
747     return VLC_FALSE;
748 }
749
750 static const char *GetModuleName( intf_thread_t *p_intf )
751 {
752     int i;
753     const char *psz_intf;
754     if( *p_intf->psz_intf == '$' )
755         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
756     else
757         psz_intf = p_intf->psz_intf;
758     for( i = 0; pp_shortcuts[i].psz_name; i++ )
759     {
760         if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
761             return pp_shortcuts[i].psz_name;
762     }
763
764     return config_GetPsz( p_intf, "lua-intf" );
765 }
766
767 int E_(Open_LuaIntf)( vlc_object_t *p_this )
768 {
769     intf_thread_t *p_intf = (intf_thread_t*)p_this;
770     intf_sys_t *p_sys;
771     lua_State *L;
772
773     const char *psz_name = GetModuleName( p_intf );
774     const char *psz_config;
775     vlc_bool_t b_config_set = VLC_FALSE;
776     if( !psz_name ) psz_name = "dummy";
777
778     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
779     if( !p_intf->p_sys )
780     {
781         return VLC_ENOMEM;
782     }
783     p_sys = p_intf->p_sys;
784     p_sys->psz_filename = FindFile( p_intf, psz_name );
785     if( !p_sys->psz_filename )
786     {
787         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
788                  psz_name );
789         return VLC_EGENERIC;
790     }
791     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
792
793     L = luaL_newstate();
794     if( !L )
795     {
796         msg_Err( p_intf, "Could not create new Lua State" );
797         free( p_sys );
798         return VLC_EGENERIC;
799     }
800
801     luaL_openlibs( L ); /* FIXME: we don't want to have all the libs */
802
803     /* register our functions */
804     luaL_register( L, "vlc", p_reg );
805     /* store a pointer to p_intf */
806     lua_pushlightuserdata( L, p_intf );
807     lua_setfield( L, -2, "private" );
808     /* register submodules */
809     luaL_register_submodule( L, "object", p_reg_object );
810     luaL_register_submodule( L, "var", p_reg_var );
811     luaL_register_submodule( L, "config", p_reg_config );
812     luaL_register_submodule( L, "msg", p_reg_msg );
813     luaL_register_submodule( L, "playlist", p_reg_playlist );
814     luaL_register_submodule( L, "volume", p_reg_volume );
815     luaL_register_submodule( L, "osd", p_reg_osd );
816     luaL_register_submodule( L, "net", p_reg_net );
817     luaL_register_submodule( L, "fd", p_reg_fd );
818     luaL_register_submodule( L, "vlm", p_reg_vlm );
819     /* clean up */
820     lua_pop( L, 1 );
821
822     /* <gruik> */
823     /* Setup the module search path */
824     {
825     char *psz_command;
826     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
827     *psz_char = '\0';
828     /* FIXME: don't use luaL_dostring */
829     if( asprintf( &psz_command,
830                   "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
831                   p_sys->psz_filename ) < 0 )
832         return VLC_EGENERIC;
833     *psz_char = DIR_SEP_CHAR;
834     if( luaL_dostring( L, psz_command ) )
835         return VLC_EGENERIC;
836     }
837     /* </gruik> */
838
839     psz_config = config_GetPsz( p_intf, "lua-config" );
840     if( psz_config && *psz_config )
841     {
842         char *psz_buffer;
843         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
844         {
845             printf("%s\n", psz_buffer);
846             if( luaL_dostring( L, psz_buffer ) == 1 )
847                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
848             free( psz_buffer );
849             lua_getglobal( L, "config" );
850             if( lua_istable( L, -1 ) )
851             {
852                 lua_getfield( L, -1, psz_name );
853                 if( lua_istable( L, -1 ) )
854                 {
855                     lua_setglobal( L, "config" );
856                     b_config_set = VLC_TRUE;
857                 }
858             }
859         }
860     }
861     if( b_config_set == VLC_FALSE )
862     {
863         lua_newtable( L );
864         lua_setglobal( L, "config" );
865     }
866
867     p_sys->L = L;
868
869     p_intf->pf_run = Run;
870     p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in E_(Close_LuaIntf)? */
871
872     return VLC_SUCCESS;
873 }
874
875 void E_(Close_LuaIntf)( vlc_object_t *p_this )
876 {
877     intf_thread_t *p_intf = (intf_thread_t*)p_this;
878
879     lua_close( p_intf->p_sys->L );
880     free( p_intf->p_sys );
881 }
882
883 static void Run( intf_thread_t *p_intf )
884 {
885     lua_State *L = p_intf->p_sys->L;
886
887     if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
888     {
889         msg_Err( p_intf, "Error loading script %s: %s",
890                  p_intf->p_sys->psz_filename,
891                  lua_tostring( L, lua_gettop( L ) ) );
892         lua_pop( L, 1 );
893         p_intf->b_die = VLC_TRUE;
894         return;
895     }
896     p_intf->b_die = VLC_TRUE;
897 }