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