]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlc.c
luasd: the script loading module
[vlc] / modules / misc / lua / vlc.c
1 /*****************************************************************************
2  * vlc.c: Generic lua interface functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <assert.h>
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_meta.h>
41 #include <vlc_charset.h>
42 #include <vlc_aout.h>
43 #include <vlc_services_discovery.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 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define INTF_TEXT N_("Lua interface")
55 #define INTF_LONGTEXT N_("Lua interface module to load")
56
57 #define CONFIG_TEXT N_("Lua interface configuration")
58 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
59
60 static int vlc_sd_probe_Open( vlc_object_t * );
61
62 vlc_module_begin ()
63         set_shortname( N_( "Lua Art" ) )
64         set_description( N_("Fetch artwork using lua scripts") )
65         set_capability( "art finder", 10 )
66         set_callbacks( FindArt, NULL )
67
68     add_submodule ()
69         set_shortname( N_( "Lua Meta Fetcher" ) )
70         set_description( N_("Fetch meta data using lua scripts") )
71         set_capability( "meta fetcher", 10 )
72         set_callbacks( FetchMeta, NULL )
73
74     add_submodule ()
75         set_shortname( N_( "Lua Meta Reader" ) )
76         set_description( N_("Read meta data using lua scripts") )
77         set_capability( "meta reader", 10 )
78         set_callbacks( ReadMeta, NULL )
79
80     add_submodule ()
81         add_shortcut( "luaplaylist" )
82         set_category( CAT_INPUT )
83         set_subcategory( SUBCAT_INPUT_DEMUX )
84         set_shortname( N_("Lua Playlist") )
85         set_description( N_("Lua Playlist Parser Interface") )
86         set_capability( "demux", 2 )
87         set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
88
89     add_submodule ()
90         set_description( N_("Lua Interface Module (shortcuts)") )
91         add_shortcut( "luarc" )
92         add_shortcut( "rc" )
93         set_capability( "interface", 25 )
94         set_callbacks( Open_LuaIntf, Close_LuaIntf )
95
96     add_submodule ()
97         set_description( N_("Lua Interface Module") )
98         add_shortcut( "luaintf" )
99         add_shortcut( "luahttp" )
100         add_shortcut( "http" )
101         add_shortcut( "luatelnet" )
102         add_shortcut( "telnet" )
103         add_shortcut( "luahotkeys" )
104         /* add_shortcut( "hotkeys" ) */
105         set_capability( "interface", 0 )
106         add_string( "lua-intf", "dummy", NULL,
107                     INTF_TEXT, INTF_LONGTEXT, false )
108         add_string( "lua-config", "", NULL,
109                     CONFIG_TEXT, CONFIG_LONGTEXT, false )
110         set_callbacks( Open_LuaIntf, Close_LuaIntf )
111
112     add_submodule ()
113         set_shortname( N_("Lua Extension") )
114         add_shortcut( "luaextension" )
115         set_capability( "extension", 1 )
116         set_callbacks( Open_Extension, Close_Extension )
117
118     add_submodule ()
119         set_description( N_("Lua SD Module") )
120         add_shortcut( "luasd" )
121         set_capability( "services_discovery", 0 )
122         add_string( "lua-sd", "", NULL, "", "", false )
123         set_callbacks( Open_LuaSD, Close_LuaSD )
124
125     VLC_SD_PROBE_SUBMODULE
126
127 vlc_module_end ()
128
129 /*****************************************************************************
130  *
131  *****************************************************************************/
132 static int file_select( const char *file )
133 {
134     int i = strlen( file );
135     return i > 4 && !strcmp( file+i-4, ".lua" );
136 }
137
138 static int file_compare( const char **a, const char **b )
139 {
140     return strcmp( *a, *b );
141 }
142
143 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
144 {
145     int i = 0;
146     char *datadir = config_GetUserDir( VLC_DATA_DIR );
147     if( datadir == NULL )
148         return VLC_ENOMEM;
149
150     if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
151                    datadir, luadirname ) < 0 )
152     {
153         free( datadir );
154         return VLC_ENOMEM;
155     }
156     free( datadir );
157     i++;
158
159     char *psz_datapath = config_GetDataDir( p_this );
160 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
161     {
162         if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
163                       psz_datapath, luadirname )  < 0 )
164             return VLC_ENOMEM;
165         i++;
166         if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
167                       psz_datapath, luadirname )  < 0 )
168             return VLC_ENOMEM;
169         i++;
170
171     }
172 #   else
173     if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
174                   psz_datapath, luadirname ) < 0 )
175         return VLC_ENOMEM;
176     i++;
177 #   endif
178     free( psz_datapath );
179     return VLC_SUCCESS;
180 }
181
182 void vlclua_dir_list_free( char **ppsz_dir_list )
183 {
184     char **ppsz_dir;
185     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
186         free( *ppsz_dir );
187 }
188
189 /*****************************************************************************
190  * Will execute func on all scripts in luadirname, and stop if func returns
191  * success.
192  *****************************************************************************/
193 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
194                                   const char * luadirname,
195                                   int (*func)(vlc_object_t *, const char *, lua_State *, void *),
196                                   lua_State * L,
197                                   void * user_data)
198 {
199     int i_ret = VLC_EGENERIC;
200
201     char **ppsz_filelist = NULL;
202     char **ppsz_fileend  = NULL;
203     char **ppsz_file;
204
205     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
206     char **ppsz_dir;
207
208     i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
209     if( i_ret != VLC_SUCCESS )
210         return i_ret;
211     i_ret = VLC_EGENERIC;
212
213
214     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
215     {
216         int i_files;
217
218         if( ppsz_filelist )
219         {
220             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
221                  ppsz_file++ )
222                 free( *ppsz_file );
223             free( ppsz_filelist );
224             ppsz_filelist = NULL;
225         }
226
227         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
228         i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
229                                 file_compare );
230         if( i_files < 1 ) continue;
231         ppsz_fileend = ppsz_filelist + i_files;
232
233         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
234         {
235             char  *psz_filename;
236             if( asprintf( &psz_filename,
237                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
238             {
239                 vlclua_dir_list_free( ppsz_dir_list );
240                 return VLC_ENOMEM;
241             }
242             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
243
244             i_ret = func( p_this, psz_filename, L, user_data );
245
246             free( psz_filename );
247
248             if( i_ret == VLC_SUCCESS ) break;
249         }
250         if( i_ret == VLC_SUCCESS ) break;
251     }
252
253     if( ppsz_filelist )
254     {
255         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
256              ppsz_file++ )
257             free( *ppsz_file );
258         free( ppsz_filelist );
259     }
260     vlclua_dir_list_free( ppsz_dir_list );
261
262     return i_ret;
263 }
264
265
266 /*****************************************************************************
267  * Meta data setters utility.
268  * Playlist item table should be on top of the stack when these are called
269  *****************************************************************************/
270 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
271                               input_item_t *p_input )
272 {
273 #define TRY_META( a, b )                                        \
274     lua_getfield( L, -1, a );                                   \
275     if( lua_isstring( L, -1 ) )                                 \
276     {                                                           \
277         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
278         EnsureUTF8( psz_value );                                \
279         msg_Dbg( p_this, #b ": %s", psz_value );                \
280         input_item_Set ## b ( p_input, psz_value );             \
281         free( psz_value );                                      \
282     }                                                           \
283     lua_pop( L, 1 ); /* pop a */
284     TRY_META( "title", Title );
285     TRY_META( "artist", Artist );
286     TRY_META( "genre", Genre );
287     TRY_META( "copyright", Copyright );
288     TRY_META( "album", Album );
289     TRY_META( "tracknum", TrackNum );
290     TRY_META( "description", Description );
291     TRY_META( "rating", Rating );
292     TRY_META( "date", Date );
293     TRY_META( "setting", Setting );
294     TRY_META( "url", URL );
295     TRY_META( "language", Language );
296     TRY_META( "nowplaying", NowPlaying );
297     TRY_META( "publisher", Publisher );
298     TRY_META( "encodedby", EncodedBy );
299     TRY_META( "arturl", ArtURL );
300     TRY_META( "trackid", TrackID );
301 }
302
303 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
304                                      input_item_t *p_input )
305 {
306     /* ... item */
307     lua_getfield( L, -1, "meta" );
308     /* ... item meta */
309     if( lua_istable( L, -1 ) )
310     {
311         lua_pushnil( L );
312         /* ... item meta nil */
313         while( lua_next( L, -2 ) )
314         {
315             /* ... item meta key value */
316             if( !lua_isstring( L, -2 ) )
317             {
318                 msg_Warn( p_this, "Custom meta data category name must be "
319                                    "a string" );
320             }
321             else if( !lua_istable( L, -1 ) )
322             {
323                 msg_Warn( p_this, "Custom meta data category contents "
324                                    "must be a table" );
325             }
326             else
327             {
328                 const char *psz_meta_category = lua_tostring( L, -2 );
329                 msg_Dbg( p_this, "Found custom meta data category: %s",
330                          psz_meta_category );
331                 lua_pushnil( L );
332                 /* ... item meta key value nil */
333                 while( lua_next( L, -2 ) )
334                 {
335                     /* ... item meta key value key2 value2 */
336                     if( !lua_isstring( L, -2 ) )
337                     {
338                         msg_Warn( p_this, "Custom meta category item name "
339                                            "must be a string." );
340                     }
341                     else if( !lua_isstring( L, -1 ) )
342                     {
343                         msg_Warn( p_this, "Custom meta category item value "
344                                            "must be a string." );
345                     }
346                     else
347                     {
348                         const char *psz_meta_name =
349                             lua_tostring( L, -2 );
350                         const char *psz_meta_value =
351                             lua_tostring( L, -1 );
352                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
353                                  psz_meta_category, psz_meta_name,
354                                  psz_meta_value );
355                         input_item_AddInfo( p_input, psz_meta_category,
356                                            psz_meta_name, "%s", psz_meta_value );
357                     }
358                     lua_pop( L, 1 ); /* pop item */
359                     /* ... item meta key value key2 */
360                 }
361                 /* ... item meta key value */
362             }
363             lua_pop( L, 1 ); /* pop category */
364             /* ... item meta key */
365         }
366         /* ... item meta */
367     }
368     lua_pop( L, 1 ); /* pop "meta" */
369     /* ... item -> back to original stack */
370 }
371
372 /*****************************************************************************
373  * Playlist utilities
374  ****************************************************************************/
375 /**
376  * Playlist item table should be on top of the stack when this is called
377  */
378 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
379                             int *pi_options, char ***pppsz_options )
380 {
381     lua_getfield( L, -1, "options" );
382     if( lua_istable( L, -1 ) )
383     {
384         lua_pushnil( L );
385         while( lua_next( L, -2 ) )
386         {
387             if( lua_isstring( L, -1 ) )
388             {
389                 char *psz_option = strdup( lua_tostring( L, -1 ) );
390                 msg_Dbg( p_this, "Option: %s", psz_option );
391                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
392                              psz_option );
393             }
394             else
395             {
396                 msg_Warn( p_this, "Option should be a string" );
397             }
398             lua_pop( L, 1 ); /* pop option */
399         }
400     }
401     lua_pop( L, 1 ); /* pop "options" */
402 }
403
404 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
405                                     playlist_t *p_playlist,
406                                     input_item_t *p_parent, bool b_play )
407 {
408     int i_count = 0;
409
410     assert( p_parent || p_playlist );
411
412     /* playlist */
413     if( lua_istable( L, -1 ) )
414     {
415         lua_pushnil( L );
416         /* playlist nil */
417         while( lua_next( L, -2 ) )
418         {
419             /* playlist key item */
420             /* <Parse playlist item> */
421             if( lua_istable( L, -1 ) )
422             {
423                 lua_getfield( L, -1, "path" );
424                 /* playlist key item path */
425                 if( lua_isstring( L, -1 ) )
426                 {
427                     const char   *psz_path     = NULL;
428                     const char   *psz_name     = NULL;
429                     char        **ppsz_options = NULL;
430                     int           i_options    = 0;
431                     mtime_t       i_duration   = -1;
432                     input_item_t *p_input;
433
434                     /* Read path and name */
435                     psz_path = lua_tostring( L, -1 );
436                     msg_Dbg( p_this, "Path: %s", psz_path );
437                     lua_getfield( L, -2, "name" );
438                     /* playlist key item path name */
439                     if( lua_isstring( L, -1 ) )
440                     {
441                         psz_name = lua_tostring( L, -1 );
442                         msg_Dbg( p_this, "Name: %s", psz_name );
443                     }
444                     else
445                     {
446                         if( !lua_isnil( L, -1 ) )
447                             msg_Warn( p_this, "Playlist item name should be a string." );
448                         psz_name = psz_path;
449                     }
450
451                     /* Read duration */
452                     lua_getfield( L, -3, "duration" );
453                     /* playlist key item path name duration */
454                     if( lua_isnumber( L, -1 ) )
455                     {
456                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
457                     }
458                     else if( !lua_isnil( L, -1 ) )
459                     {
460                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
461                     }
462                     lua_pop( L, 1 ); /* pop "duration" */
463
464                     /* playlist key item path name */
465
466                     /* Read options: item must be on top of stack */
467                     lua_pushvalue( L, -3 );
468                     /* playlist key item path name item */
469                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
470
471                     /* Create input item */
472                     p_input = input_item_NewExt( p_playlist, psz_path,
473                                                 psz_name, i_options,
474                                                 (const char **)ppsz_options,
475                                                 VLC_INPUT_OPTION_TRUSTED,
476                                                 i_duration );
477                     lua_pop( L, 3 ); /* pop "path name item" */
478                     /* playlist key item */
479
480                     /* Read meta data: item must be on top of stack */
481                     vlclua_read_meta_data( p_this, L, p_input );
482
483                     /* Read custom meta data: item must be on top of stack*/
484                     vlclua_read_custom_meta_data( p_this, L, p_input );
485
486                     /* Append item to playlist */
487                     if( p_parent ) /* Add to node */
488                     {
489                         input_item_PostSubItem( p_parent, p_input );
490                     }
491                     else /* Play or Enqueue (preparse) */
492                         /* FIXME: playlist_AddInput() can fail */
493                         playlist_AddInput( p_playlist, p_input,
494                                PLAYLIST_APPEND |
495                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
496                                PLAYLIST_END, true, false );
497                     i_count ++; /* increment counter */
498                     vlc_gc_decref( p_input );
499                     while( i_options > 0 )
500                         free( ppsz_options[--i_options] );
501                     free( ppsz_options );
502                 }
503                 else
504                 {
505                     lua_pop( L, 1 ); /* pop "path" */
506                     msg_Warn( p_this,
507                              "Playlist item's path should be a string" );
508                 }
509                 /* playlist key item */
510             }
511             else
512             {
513                 msg_Warn( p_this, "Playlist item should be a table" );
514             }
515             /* <Parse playlist item> */
516             lua_pop( L, 1 ); /* pop the value, keep the key for
517                               * the next lua_next() call */
518             /* playlist key */
519         }
520         /* playlist */
521     }
522     else
523     {
524         msg_Warn( p_this, "Playlist should be a table." );
525     }
526     return i_count;
527 }
528
529 static int vlc_sd_probe_Open( vlc_object_t *obj )
530 {
531     vlc_probe_t *probe = (vlc_probe_t *)obj;
532     char **ppsz_filelist = NULL;
533     char **ppsz_fileend  = NULL;
534     char **ppsz_file;
535     char *psz_name;
536     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
537     char **ppsz_dir;
538     vlclua_dir_list( obj, "sd", ppsz_dir_list );
539     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
540     {
541         int i_files;
542         if( ppsz_filelist )
543         {
544             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
545                  ppsz_file++ )
546                 free( *ppsz_file );
547             free( ppsz_filelist );
548             ppsz_filelist = NULL;
549         }
550         i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
551                                 file_compare );
552         if( i_files < 1 ) continue;
553         ppsz_fileend = ppsz_filelist + i_files;
554         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
555         {
556             char  *psz_filename;
557             if( asprintf( &psz_filename,
558                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
559             {
560                 goto error;
561             }
562             FILE *fd = utf8_fopen( psz_filename, "r" );
563             if( fd )
564             {
565                 char description[256];
566                 if( fgets( description, 256, fd ) != NULL )
567                 {
568                     char *temp = strchr( description, '\n' );
569                     if( temp )
570                         *temp = '\0';
571                     *(*ppsz_file + strlen(*ppsz_file) - 4 )= '\0';
572                     if( asprintf( &psz_name, "lua{sd=%s}", *ppsz_file ) < 0 )
573                     {
574                         fclose( fd );
575                         free( psz_filename );
576                         goto error;
577                     }
578                     vlc_sd_probe_Add( probe, psz_name,
579                                       description + 17 );
580                     free( psz_name );
581                 }
582                 fclose( fd );
583             }
584             free( psz_filename );
585         }
586     }
587     if( ppsz_filelist )
588     {
589         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
590              ppsz_file++ )
591             free( *ppsz_file );
592         free( ppsz_filelist );
593     }
594     vlclua_dir_list_free( ppsz_dir_list );
595     return VLC_PROBE_CONTINUE;
596 error:
597     if( ppsz_filelist )
598     {
599         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
600              ppsz_file++ )
601             free( *ppsz_file );
602         free( ppsz_filelist );
603     }
604     vlclua_dir_list_free( ppsz_dir_list );
605     return VLC_ENOMEM;
606 }