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