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