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