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