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