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