]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
Add services discovery support and enhance playlist support for lua interface modules...
[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 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_skip( lua_State * L )
359 {
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 );
364     return 0;
365 }
366
367 static int vlclua_playlist_play( lua_State * L )
368 {
369     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
370     playlist_Play( p_playlist );
371     vlc_object_release( p_playlist );
372     return 0;
373 }
374
375 static int vlclua_playlist_pause( lua_State * L )
376 {
377     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
378     playlist_Pause( p_playlist );
379     vlc_object_release( p_playlist );
380     return 0;
381 }
382
383 static int vlclua_playlist_stop( lua_State * L )
384 {
385     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
386     playlist_Stop( p_playlist );
387     vlc_object_release( p_playlist );
388     return 0;
389 }
390
391 static int vlclua_playlist_clear( lua_State * L )
392 {
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 );
397     return 0;
398 }
399
400 static int vlclua_playlist_repeat( lua_State * L )
401 {
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 );
405     return i_ret;
406 }
407
408 static int vlclua_playlist_loop( lua_State * L )
409 {
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 );
413     return i_ret;
414 }
415
416 static int vlclua_playlist_random( lua_State * L )
417 {
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 );
421     return i_ret;
422 }
423
424 static int vlclua_playlist_goto( lua_State * L )
425 {
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,
429                                   VLC_TRUE, NULL,
430                                   playlist_ItemGetById( p_playlist, i_id,
431                                                         VLC_TRUE ) );
432     vlc_object_release( p_playlist );
433     return vlclua_push_ret( L, i_ret );
434 }
435
436 static int vlclua_playlist_add( lua_State *L )
437 {
438     int i_count;
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,
442                                             NULL, VLC_TRUE );
443     vlc_object_release( p_playlist );
444     lua_pushinteger( L, i_count );
445     return 1;
446 }
447
448 static int vlclua_playlist_enqueue( lua_State *L )
449 {
450     int i_count;
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,
454                                             NULL, VLC_FALSE );
455     vlc_object_release( p_playlist );
456     lua_pushinteger( L, i_count );
457     return 1;
458 }
459
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 )
462 {
463     input_item_t *p_input = p_item->p_input;
464     int i_flags = p_item->i_flags;
465     lua_newtable( L );
466     lua_pushinteger( L, p_item->i_id );
467     lua_setfield( L, -2, "id" );
468     lua_newtable( L );
469 #define CHECK_AND_SET_FLAG( name, label ) \
470     if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
471     { \
472         lua_pushboolean( L, 1 ); \
473         lua_setfield( L, -2, #label ); \
474     }
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" );
483     if( p_input )
484     {
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 );
491         else
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 */
497     }
498     if( p_item->i_children >= 0 )
499     {
500         int i;
501         lua_createtable( L, p_item->i_children, 0 );
502         for( i = 0; i < p_item->i_children; i++ )
503         {
504             push_playlist_item( L, p_item->pp_children[i] );
505             lua_rawseti( L, -2, i+1 );
506         }
507         lua_setfield( L, -2, "children" );
508     }
509 }
510
511 static int vlclua_playlist_get( lua_State *L )
512 {
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;
516
517     if( lua_isnumber( L, 1 ) )
518     {
519         int i_id = lua_tointeger( L, 1 );
520         p_item = playlist_ItemGetById( p_playlist, i_id, VLC_TRUE );
521         if( !p_item )
522         {
523             vlc_object_release( p_playlist );
524             return 0; /* Should we return an error instead? */
525         }
526     }
527     else if( lua_isstring( L, 1 ) )
528     {
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;
541         else
542         {
543             int i;
544             for( i = 0; i < p_playlist->i_sds; i++ )
545             {
546                 if( !strcasecmp( psz_what,
547                                  p_playlist->pp_sds[i]->p_sd->psz_module ) )
548                 {
549                     p_item = b_category ? p_playlist->pp_sds[i]->p_cat
550                                         : p_playlist->pp_sds[i]->p_one;
551                     break;
552                 }
553             }
554             if( !p_item )
555             {
556                 vlc_object_release( p_playlist );
557                 return 0; /* Should we return an error instead? */
558             }
559         }
560     }
561     else
562     {
563         p_item = b_category ? p_playlist->p_root_category
564                             : p_playlist->p_root_onelevel;
565     }
566     push_playlist_item( L, p_item );
567     vlc_object_release( p_playlist );
568     return 1;
569 }
570 #if 0
571     int s;
572     lua_createtable( L, 0, 2 + p_playlist->i_sds );
573     for( s = -2; s < p_playlist->i_sds; s++ )
574     {
575         playlist_item_t *p_root;
576         switch( s )
577         {
578             case -2:
579                 /* local/normal playlist */
580                 lua_pushstring( L, "local" );
581                 p_root = p_playlist->p_local_onelevel;
582                 break;
583             case -1:
584                 /* media library */
585                 lua_pushstring( L, "ml" );
586                 p_root = p_playlist->p_ml_onelevel;
587                 break;
588             default:
589                 lua_pushstring( L, p_playlist->pp_sds[s]->p_sd->psz_module );
590                 printf("%s\n", p_playlist->pp_sds[s]->p_sd->psz_module );
591                 p_root = p_playlist->pp_sds[s]->p_one;
592                 break;
593         }
594         printf("s = %d\n", s);
595         printf("children = %d\n", p_root->i_children );
596         push_playlist_item( L, p_root );
597         lua_settable( L, -3 );
598     }
599     printf("done\n");
600 #endif
601
602 static int vlclua_playlist_search( lua_State *L )
603 {
604     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
605     const char *psz_string = luaL_optstring( L, 1, "" );
606     int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
607     playlist_LiveSearchUpdate( p_playlist,
608                                b_category ? p_playlist->p_root_category
609                                           : p_playlist->p_root_onelevel,
610                                psz_string );
611     push_playlist_item( L, p_playlist->p_root_category );
612     vlc_object_release( p_playlist );
613     return 1;
614 }
615
616 static int vlc_sort_key_from_string( const char *psz_name )
617 {
618     static const struct
619     {
620         const char *psz_name;
621         int i_key;
622     } pp_keys[] =
623         { { "id", SORT_ID },
624           { "title", SORT_TITLE },
625           { "title nodes first", SORT_TITLE_NODES_FIRST },
626           { "artist", SORT_ARTIST },
627           { "genre", SORT_GENRE },
628           { "random", SORT_RANDOM },
629           { "duration", SORT_DURATION },
630           { "title numeric", SORT_TITLE_NUMERIC },
631           { "album", SORT_ALBUM },
632           { NULL, -1 } };
633     int i;
634     for( i = 0; pp_keys[i].psz_name; i++ )
635     {
636         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
637             return pp_keys[i].i_key;
638     }
639     return -1;
640 }
641
642 static int vlclua_playlist_sort( lua_State *L )
643 {
644     /* allow setting the different sort keys */
645     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
646     if( i_mode == -1 )
647         return luaL_error( L, "Invalid search key." );
648     int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
649     int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
650     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
651     playlist_item_t *p_root = b_category ? p_playlist->p_local_category
652                                          : p_playlist->p_local_onelevel;
653     int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
654                                             i_type );
655     vlc_object_release( p_playlist );
656     return vlclua_push_ret( L, i_ret );
657 }
658
659 /* FIXME: split this in 3 different functions? */
660 static int vlclua_playlist_status( lua_State *L )
661 {
662     intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
663     playlist_t *p_playlist = pl_Yield( p_intf );
664     /*
665     int i_count = 0;
666     lua_settop( L, 0 );*/
667     if( p_playlist->p_input )
668     {
669         /*char *psz_uri =
670             input_item_GetURI( input_GetItem( p_playlist->p_input ) );
671         lua_pushstring( L, psz_uri );
672         free( psz_uri );
673         lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
674         vlc_mutex_lock( &p_playlist->object_lock );
675         switch( p_playlist->status.i_status )
676         {
677             case PLAYLIST_STOPPED:
678                 lua_pushstring( L, "stopped" );
679                 break;
680             case PLAYLIST_RUNNING:
681                 lua_pushstring( L, "playing" );
682                 break;
683             case PLAYLIST_PAUSED:
684                 lua_pushstring( L, "paused" );
685                 break;
686             default:
687                 lua_pushstring( L, "unknown" );
688                 break;
689         }
690         vlc_mutex_unlock( &p_playlist->object_lock );
691         /*i_count += 3;*/
692     }
693     else
694     {
695         lua_pushstring( L, "stopped" );
696     }
697     vlc_object_release( p_playlist );
698     return 1;
699 }
700
701
702 static int vlclua_lock_and_wait( lua_State *L )
703 {
704     vlc_object_t *p_this = vlclua_get_this( L );
705     int b_quit = vlc_object_lock_and_wait( p_this );
706     lua_pushboolean( L, b_quit );
707     return 1;
708 }
709
710 static int vlclua_signal( lua_State *L )
711 {
712     vlc_object_t *p_this = vlclua_get_this( L );
713     vlc_object_signal( p_this );
714     return 0;
715 }
716
717 static int vlclua_mdate( lua_State *L )
718 {
719     lua_pushnumber( L, mdate() );
720     return 1;
721 }
722
723 static int vlclua_intf_should_die( lua_State *L )
724 {
725     intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
726     lua_pushboolean( L, intf_ShouldDie( p_intf ) );
727     return 1;
728 }
729
730 static luaL_Reg p_reg[] =
731 {
732     { "input_info", vlclua_input_info },
733     { "is_playing", vlclua_is_playing },
734     { "get_title", vlclua_get_title },
735
736     { "fullscreen", vlclua_fullscreen },
737
738     { "mdate", vlclua_mdate },
739
740     { "module_command", vlclua_module_command },
741     { "libvlc_command", vlclua_libvlc_command },
742
743     { "decode_uri", vlclua_decode_uri },
744     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
745     { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
746
747     { "lock_and_wait", vlclua_lock_and_wait },
748     { "signal", vlclua_signal },
749
750     { "version", vlclua_version },
751     { "license", vlclua_license },
752     { "copyright", vlclua_copyright },
753     { "should_die", vlclua_intf_should_die },
754     { "quit", vlclua_quit },
755
756     { "homedir", vlclua_homedir },
757     { "datadir", vlclua_datadir },
758     { "configdir", vlclua_configdir },
759     { "cachedir", vlclua_cachedir },
760     { "datadir_list", vlclua_datadir_list },
761
762     { NULL, NULL }
763 };
764
765 static luaL_Reg p_reg_object[] =
766 {
767     { "input", vlclua_get_input },              /* This is fast */
768     { "playlist", vlclua_get_playlist },        /* This is fast */
769     { "libvlc", vlclua_get_libvlc },            /* This is fast */
770
771     { "find", vlclua_object_find },             /* This is slow */
772     { "find_name", vlclua_object_find_name },   /* This is slow */
773
774     { NULL, NULL }
775 };
776
777 static luaL_Reg p_reg_var[] =
778 {
779     { "get", vlclua_var_get },
780     { "get_list", vlclua_var_get_list },
781     { "set", vlclua_var_set },
782     { "add_callback", vlclua_add_callback },
783     { "del_callback", vlclua_del_callback },
784
785     { NULL, NULL }
786 };
787
788 static luaL_Reg p_reg_config[] =
789 {
790     { "get", vlclua_config_get },
791     { "set", vlclua_config_set },
792
793     { NULL, NULL }
794 };
795
796 static luaL_Reg p_reg_msg[] =
797 {
798     { "dbg", vlclua_msg_dbg },
799     { "warn", vlclua_msg_warn },
800     { "err", vlclua_msg_err },
801     { "info", vlclua_msg_info },
802
803     { NULL, NULL }
804 };
805
806 static luaL_Reg p_reg_playlist[] =
807 {
808     { "prev", vlclua_playlist_prev },
809     { "next", vlclua_playlist_next },
810     { "skip", vlclua_playlist_skip },
811     { "play", vlclua_playlist_play },
812     { "pause", vlclua_playlist_pause },
813     { "stop", vlclua_playlist_stop },
814     { "clear", vlclua_playlist_clear },
815     { "repeat_", vlclua_playlist_repeat },
816     { "loop", vlclua_playlist_loop },
817     { "random", vlclua_playlist_random },
818     { "goto", vlclua_playlist_goto },
819     { "status", vlclua_playlist_status },
820     { "add", vlclua_playlist_add },
821     { "enqueue", vlclua_playlist_enqueue },
822     { "get", vlclua_playlist_get },
823     { "search", vlclua_playlist_search },
824     { "sort", vlclua_playlist_sort },
825
826     { "stats", vlclua_input_stats },
827
828     { NULL, NULL }
829 };
830
831 static luaL_Reg p_reg_sd[] =
832 {
833     { "get_services_names", vlclua_sd_get_services_names },
834     { "add", vlclua_sd_add },
835     { "remove", vlclua_sd_remove },
836     { "is_loaded", vlclua_sd_is_loaded },
837
838     { NULL, NULL }
839 };
840
841 static luaL_Reg p_reg_volume[] =
842 {
843     { "get", vlclua_volume_get },
844     { "set", vlclua_volume_set },
845     { "up", vlclua_volume_up },
846     { "down", vlclua_volume_down },
847
848     { NULL, NULL }
849 };
850
851 static luaL_Reg p_reg_osd[] =
852 {
853     { "icon", vlclua_osd_icon },
854     { "message", vlclua_osd_message },
855     { "slider", vlclua_osd_slider },
856     { "channel_register", vlclua_spu_channel_register },
857     { "channel_clear", vlclua_spu_channel_clear },
858
859     { NULL, NULL }
860 };
861
862 static luaL_Reg p_reg_net[] =
863 {
864     { "url_parse", vlclua_url_parse },
865     { "listen_tcp", vlclua_net_listen_tcp },
866     { "listen_close", vlclua_net_listen_close },
867     { "accept", vlclua_net_accept },
868     { "close", vlclua_net_close },
869     { "send", vlclua_net_send },
870     { "recv", vlclua_net_recv },
871     { "select", vlclua_net_select },
872
873     { NULL, NULL }
874 };
875
876 static luaL_Reg p_reg_fd[] =
877 {
878 /*    { "open", vlclua_fd_open },*/
879     { "read", vlclua_fd_read },
880     { "write", vlclua_fd_write },
881     { "stat", vlclua_stat },
882
883     { "opendir", vlclua_opendir },
884
885     { "new_fd_set", vlclua_fd_set_new },
886     { "fd_clr", vlclua_fd_clr },
887     { "fd_isset", vlclua_fd_isset },
888     { "fd_set", vlclua_fd_set },
889     { "fd_zero", vlclua_fd_zero },
890
891     { NULL, NULL }
892 };
893
894 static luaL_Reg p_reg_vlm[] =
895 {
896     { "new", vlclua_vlm_new },
897     { "delete", vlclua_vlm_delete },
898     { "execute_command", vlclua_vlm_execute_command },
899
900     { NULL, NULL }
901 };
902
903 static luaL_Reg p_reg_httpd[] =
904 {
905     { "host_new", vlclua_httpd_tls_host_new },
906     { "host_delete", vlclua_httpd_host_delete },
907     { "handler_new", vlclua_httpd_handler_new },
908     { "handler_delete", vlclua_httpd_handler_delete },
909     { "file_new", vlclua_httpd_file_new },
910     { "file_delete", vlclua_httpd_file_delete },
911     { "redirect_new", vlclua_httpd_redirect_new },
912     { "redirect_delete", vlclua_httpd_redirect_delete },
913
914     { NULL, NULL }
915 };
916
917 static luaL_Reg p_reg_acl[] =
918 {
919     { "create", vlclua_acl_create },
920     { "delete", vlclua_acl_delete },
921     { "check", vlclua_acl_check },
922     { "duplicate", vlclua_acl_duplicate },
923     { "add_host", vlclua_acl_add_host },
924     { "add_net", vlclua_acl_add_net },
925     { "load_file", vlclua_acl_load_file },
926
927     { NULL, NULL }
928 };
929
930 static void Run( intf_thread_t *p_intf );
931
932 static char *FindFile( intf_thread_t *p_intf, const char *psz_name )
933 {
934     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
935     char **ppsz_dir;
936     vlclua_dir_list( VLC_OBJECT(p_intf), "luaintf", ppsz_dir_list );
937     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
938     {
939         char *psz_filename;
940         FILE *fp;
941         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
942                       psz_name ) < 0 )
943         {
944             return NULL;
945         }
946         fp = fopen( psz_filename, "r" );
947         if( fp )
948         {
949             fclose( fp );
950             return psz_filename;
951         }
952         free( psz_filename );
953     }
954     return NULL;
955 }
956
957 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
958                                             const luaL_Reg *l )
959 {
960     lua_newtable( L );
961     luaL_register( L, NULL, l );
962     lua_setfield( L, -2, psz_name );
963 }
964
965 static struct
966 {
967     const char *psz_shortcut;
968     const char *psz_name;
969 } pp_shortcuts[] = {
970     { "luarc", "rc" },
971     /* { "rc", "rc" }, */
972     { "luahotkeys", "hotkeys" },
973     /* { "hotkeys", "hotkeys" }, */
974     { "luatelnet", "telnet" },
975     /* { "telnet", "telnet" }, */
976     { NULL, NULL } };
977
978 static vlc_bool_t WordInList( const char *psz_list, const char *psz_word )
979 {
980     const char *psz_str = strstr( psz_list, psz_word );
981     int i_len = strlen( psz_word );
982     while( psz_str )
983     {
984         if( (psz_str == psz_list || *(psz_str-1) == ',' )
985          /* it doesn't start in middle of a word */
986          /* it doest end in middle of a word */
987          && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
988             return VLC_TRUE;
989         psz_str = strstr( psz_str, psz_word );
990     }
991     return VLC_FALSE;
992 }
993
994 static const char *GetModuleName( intf_thread_t *p_intf )
995 {
996     int i;
997     const char *psz_intf;
998     if( *p_intf->psz_intf == '$' )
999         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
1000     else
1001         psz_intf = p_intf->psz_intf;
1002     for( i = 0; pp_shortcuts[i].psz_name; i++ )
1003     {
1004         if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
1005             return pp_shortcuts[i].psz_name;
1006     }
1007
1008     return config_GetPsz( p_intf, "lua-intf" );
1009 }
1010
1011 int E_(Open_LuaIntf)( vlc_object_t *p_this )
1012 {
1013     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1014     intf_sys_t *p_sys;
1015     lua_State *L;
1016
1017     const char *psz_name = GetModuleName( p_intf );
1018     const char *psz_config;
1019     vlc_bool_t b_config_set = VLC_FALSE;
1020     if( !psz_name ) psz_name = "dummy";
1021
1022     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
1023     if( !p_intf->p_sys )
1024     {
1025         return VLC_ENOMEM;
1026     }
1027     p_sys = p_intf->p_sys;
1028     p_sys->psz_filename = FindFile( p_intf, psz_name );
1029     if( !p_sys->psz_filename )
1030     {
1031         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
1032                  psz_name );
1033         return VLC_EGENERIC;
1034     }
1035     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
1036
1037     L = luaL_newstate();
1038     if( !L )
1039     {
1040         msg_Err( p_intf, "Could not create new Lua State" );
1041         free( p_sys );
1042         return VLC_EGENERIC;
1043     }
1044
1045     luaL_openlibs( L ); /* FIXME: we don't want to have all the libs */
1046
1047     /* register our functions */
1048     luaL_register( L, "vlc", p_reg );
1049     /* store a pointer to p_intf */
1050     lua_pushlightuserdata( L, p_intf );
1051     lua_setfield( L, -2, "private" );
1052     /* register submodules */
1053     luaL_register_submodule( L, "object", p_reg_object );
1054     luaL_register_submodule( L, "var", p_reg_var );
1055     luaL_register_submodule( L, "config", p_reg_config );
1056     luaL_register_submodule( L, "msg", p_reg_msg );
1057     luaL_register_submodule( L, "playlist", p_reg_playlist );
1058     luaL_register_submodule( L, "sd", p_reg_sd );
1059     luaL_register_submodule( L, "volume", p_reg_volume );
1060     luaL_register_submodule( L, "osd", p_reg_osd );
1061     luaL_register_submodule( L, "net", p_reg_net );
1062     luaL_register_submodule( L, "fd", p_reg_fd );
1063     luaL_register_submodule( L, "vlm", p_reg_vlm );
1064     luaL_register_submodule( L, "httpd", p_reg_httpd );
1065     luaL_register_submodule( L, "acl", p_reg_acl );
1066     /* clean up */
1067     lua_pop( L, 1 );
1068
1069     /* <gruik> */
1070     /* Setup the module search path */
1071     {
1072     char *psz_command;
1073     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
1074     *psz_char = '\0';
1075     /* FIXME: don't use luaL_dostring */
1076     if( asprintf( &psz_command,
1077                   "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
1078                   p_sys->psz_filename ) < 0 )
1079         return VLC_EGENERIC;
1080     *psz_char = DIR_SEP_CHAR;
1081     if( luaL_dostring( L, psz_command ) )
1082         return VLC_EGENERIC;
1083     }
1084     /* </gruik> */
1085
1086     psz_config = config_GetPsz( p_intf, "lua-config" );
1087     if( psz_config && *psz_config )
1088     {
1089         char *psz_buffer;
1090         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
1091         {
1092             printf("%s\n", psz_buffer);
1093             if( luaL_dostring( L, psz_buffer ) == 1 )
1094                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
1095             free( psz_buffer );
1096             lua_getglobal( L, "config" );
1097             if( lua_istable( L, -1 ) )
1098             {
1099                 lua_getfield( L, -1, psz_name );
1100                 if( lua_istable( L, -1 ) )
1101                 {
1102                     lua_setglobal( L, "config" );
1103                     b_config_set = VLC_TRUE;
1104                 }
1105             }
1106         }
1107     }
1108     if( b_config_set == VLC_FALSE )
1109     {
1110         lua_newtable( L );
1111         lua_setglobal( L, "config" );
1112     }
1113
1114     p_sys->L = L;
1115
1116     p_intf->pf_run = Run;
1117     p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in E_(Close_LuaIntf)? */
1118
1119     return VLC_SUCCESS;
1120 }
1121
1122 void E_(Close_LuaIntf)( vlc_object_t *p_this )
1123 {
1124     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1125
1126     lua_close( p_intf->p_sys->L );
1127     free( p_intf->p_sys );
1128 }
1129
1130 static void Run( intf_thread_t *p_intf )
1131 {
1132     lua_State *L = p_intf->p_sys->L;
1133
1134     if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
1135     {
1136         msg_Err( p_intf, "Error loading script %s: %s",
1137                  p_intf->p_sys->psz_filename,
1138                  lua_tostring( L, lua_gettop( L ) ) );
1139         lua_pop( L, 1 );
1140         p_intf->b_die = VLC_TRUE;
1141         return;
1142     }
1143     p_intf->b_die = VLC_TRUE;
1144 }