]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlclua.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / misc / lua / vlclua.c
1 /*****************************************************************************
2  * vlclua.c: Generic lua inteface functions
3  *****************************************************************************
4  * Copyright (C) 2007 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 #include <vlc/vlc.h>
33 #include <vlc_meta.h>
34
35 #include <lua.h>        /* Low level lua C API */
36 #include <lauxlib.h>    /* Higher level C API */
37 #include <lualib.h>     /* Lua libs */
38
39 #include "vlclua.h"
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44
45 vlc_module_begin();
46     add_shortcut( "luameta" );
47     set_shortname( N_( "Lua Meta" ) );
48     set_description( _("Fetch metadata using lua scripts") );
49     set_capability( "meta fetcher", 10 );
50     set_callbacks( E_(FindMeta), NULL );
51     add_submodule();
52         set_shortname( N_( "Lua Art" ) );
53         set_description( _("Fetch artwork using lua scripts") );
54         set_capability( "art finder", 10 );
55         set_callbacks( E_(FindArt), NULL );
56     add_submodule();
57         add_shortcut( "luaplaylist" );
58         set_category( CAT_INPUT );
59         set_subcategory( SUBCAT_INPUT_DEMUX );
60         set_shortname( _("Lua Playlist") );
61         set_description( _("Lua Playlist Parser Interface") );
62         set_capability( "demux2", 9 );
63         set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Lua function bridge
68  *****************************************************************************/
69 vlc_object_t * vlclua_get_this( lua_State *p_state )
70 {
71     vlc_object_t * p_this;
72     lua_getglobal( p_state, "vlc" );
73     lua_getfield( p_state, lua_gettop( p_state ), "private" );
74     p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
75     lua_pop( p_state, 2 );
76     return p_this;
77 }
78
79 int vlclua_stream_new( lua_State *p_state )
80 {
81     vlc_object_t * p_this = vlclua_get_this( p_state );
82     int i = lua_gettop( p_state );
83     stream_t * p_stream;
84     const char * psz_url;
85     if( !i ) return 0;
86     psz_url = lua_tostring( p_state, 1 );
87     lua_pop( p_state, i );
88     p_stream = stream_UrlNew( p_this, psz_url );
89     lua_pushlightuserdata( p_state, p_stream );
90     return 1;
91 }
92
93 int vlclua_stream_read( lua_State *p_state )
94 {
95     int i = lua_gettop( p_state );
96     stream_t * p_stream;
97     int n;
98     byte_t *p_read;
99     int i_read;
100     if( !i ) return 0;
101     p_stream = (stream_t *)lua_topointer( p_state, 1 );
102     n = lua_tonumber( p_state, 2 );
103     lua_pop( p_state, i );
104     p_read = malloc( n );
105     if( !p_read ) return 0;
106     i_read = stream_Read( p_stream, p_read, n );
107     lua_pushlstring( p_state, (const char *)p_read, i_read );
108     free( p_read );
109     return 1;
110 }
111
112 int vlclua_stream_readline( lua_State *p_state )
113 {
114     int i = lua_gettop( p_state );
115     stream_t * p_stream;
116     if( !i ) return 0;
117     p_stream = (stream_t *)lua_topointer( p_state, 1 );
118     lua_pop( p_state, i );
119     char *psz_line = stream_ReadLine( p_stream );
120     if( psz_line )
121     {
122         lua_pushstring( p_state, psz_line );
123         free( psz_line );
124     }
125     else
126     {
127         lua_pushnil( p_state );
128     }
129     return 1;
130 }
131
132 int vlclua_stream_delete( lua_State *p_state )
133 {
134     int i = lua_gettop( p_state );
135     stream_t * p_stream;
136     if( !i ) return 0;
137     p_stream = (stream_t *)lua_topointer( p_state, 1 );
138     lua_pop( p_state, i );
139     stream_Delete( p_stream );
140     return 1;
141 }
142
143 int vlclua_decode_uri( lua_State *p_state )
144 {
145     int i = lua_gettop( p_state );
146     if( !i ) return 0;
147     const char *psz_cstring = lua_tostring( p_state, 1 );
148     if( !psz_cstring ) return 0;
149     char *psz_string = strdup( psz_cstring );
150     lua_pop( p_state, i );
151     decode_URI( psz_string );
152     lua_pushstring( p_state, psz_string );
153     free( psz_string );
154     return 1;
155 }
156
157 int vlclua_resolve_xml_special_chars( lua_State *p_state )
158 {
159     int i = lua_gettop( p_state );
160     if( !i ) return 0;
161     const char *psz_cstring = lua_tostring( p_state, 1 );
162     if( !psz_cstring ) return 0;
163     char *psz_string = strdup( psz_cstring );
164     lua_pop( p_state, i );
165     resolve_xml_special_chars( psz_string );
166     lua_pushstring( p_state, psz_string );
167     free( psz_string );
168     return 1;
169 }
170
171 int vlclua_msg_dbg( lua_State *p_state )
172 {
173     vlc_object_t *p_this = vlclua_get_this( p_state );
174     int i = lua_gettop( p_state );
175     if( !i ) return 0;
176     const char *psz_cstring = lua_tostring( p_state, 1 );
177     if( !psz_cstring ) return 0;
178     msg_Dbg( p_this, "%s", psz_cstring );
179     return 0;
180 }
181 int vlclua_msg_warn( lua_State *p_state )
182 {
183     vlc_object_t *p_this = vlclua_get_this( p_state );
184     int i = lua_gettop( p_state );
185     if( !i ) return 0;
186     const char *psz_cstring = lua_tostring( p_state, 1 );
187     if( !psz_cstring ) return 0;
188     msg_Warn( p_this, "%s", psz_cstring );
189     return 0;
190 }
191 int vlclua_msg_err( lua_State *p_state )
192 {
193     vlc_object_t *p_this = vlclua_get_this( p_state );
194     int i = lua_gettop( p_state );
195     if( !i ) return 0;
196     const char *psz_cstring = lua_tostring( p_state, 1 );
197     if( !psz_cstring ) return 0;
198     msg_Err( p_this, "%s", psz_cstring );
199     return 0;
200 }
201 int vlclua_msg_info( lua_State *p_state )
202 {
203     vlc_object_t *p_this = vlclua_get_this( p_state );
204     int i = lua_gettop( p_state );
205     if( !i ) return 0;
206     const char *psz_cstring = lua_tostring( p_state, 1 );
207     if( !psz_cstring ) return 0;
208     msg_Info( p_this, "%s", psz_cstring );
209     return 0;
210 }
211
212 /*****************************************************************************
213  *
214  *****************************************************************************/
215 static int file_select( const char *file )
216 {
217     int i = strlen( file );
218     return i > 4 && !strcmp( file+i-4, ".lua" );
219 }
220
221 static int file_compare( const char **a, const char **b )
222 {
223     return strcmp( *a, *b );
224 }
225
226
227 /*****************************************************************************
228  * Will execute func on all scripts in luadirname, and stop if func returns
229  * success.
230  *****************************************************************************/
231 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
232                                   const char * luadirname,
233                                   int (*func)(vlc_object_t *, const char *, lua_State *, void *),
234                                   lua_State * p_state,
235                                   void * user_data)
236 {
237     int i_ret = VLC_EGENERIC;
238
239     DIR   *dir           = NULL;
240     char **ppsz_filelist = NULL;
241     char **ppsz_fileend  = NULL;
242     char **ppsz_file;
243
244     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
245     char **ppsz_dir;
246
247     if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
248                    p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
249         return VLC_ENOMEM;
250
251 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
252     {
253         const char *psz_vlcpath = config_GetDataDir();
254         if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
255             return VLC_ENOMEM;
256
257         if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
258             return VLC_ENOMEM;
259     }
260 #   else
261     if( asprintf( &ppsz_dir_list[1],
262                   "share" DIR_SEP "%s", luadirname ) < 0 )
263         return VLC_ENOMEM;
264
265 #   ifdef HAVE_SYS_STAT_H
266     {
267         struct stat stat_info;
268         if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
269             || !S_ISDIR( stat_info.st_mode ) )
270         {
271             free(ppsz_dir_list[1]);
272             if( asprintf( &ppsz_dir_list[1],
273                           DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
274                 return VLC_ENOMEM;
275         }
276     }
277 #   endif
278 #   endif
279
280     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
281     {
282         int i_files;
283
284         if( ppsz_filelist )
285         {
286             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
287                  ppsz_file++ )
288                 free( *ppsz_file );
289             free( ppsz_filelist );
290             ppsz_filelist = NULL;
291         }
292
293         if( dir )
294         {
295             closedir( dir );
296         }
297
298         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
299         dir = utf8_opendir( *ppsz_dir );
300
301         if( !dir ) continue;
302         i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
303         if( i_files < 1 ) continue;
304         ppsz_fileend = ppsz_filelist + i_files;
305
306         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
307         {
308             char  *psz_filename;
309             if( asprintf( &psz_filename,
310                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
311                 return VLC_ENOMEM;
312             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
313  
314             i_ret = func( p_this, psz_filename, p_state, user_data );
315  
316             free( psz_filename );
317
318             if( i_ret == VLC_SUCCESS ) break;
319         }
320         if( i_ret == VLC_SUCCESS ) break;
321     }
322
323     if( ppsz_filelist )
324     {
325         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
326              ppsz_file++ )
327             free( *ppsz_file );
328         free( ppsz_filelist );
329     }
330     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
331         free( *ppsz_dir );
332
333     if( dir ) closedir( dir );
334
335     return i_ret;
336 }
337
338
339 /*****************************************************************************
340  * Meta data setters utility.
341  *****************************************************************************/
342 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *p_state,
343                             int o, int t, input_item_t *p_input )
344 {
345     const char *psz_value;
346 #define TRY_META( a, b )                                    \
347     lua_getfield( p_state, o, a );                          \
348     if( lua_isstring( p_state, t ) )                        \
349     {                                                       \
350         psz_value = lua_tostring( p_state, t );             \
351         msg_Dbg( p_this, #b ": %s", psz_value );           \
352         input_item_Set ## b ( p_input, psz_value );   \
353     }                                                       \
354     lua_pop( p_state, 1 ); /* pop a */
355     TRY_META( "title", Title );
356     TRY_META( "artist", Artist );
357     TRY_META( "genre", Genre );
358     TRY_META( "copyright", Copyright );
359     TRY_META( "album", Album );
360     TRY_META( "tracknum", TrackNum );
361     TRY_META( "description", Description );
362     TRY_META( "rating", Rating );
363     TRY_META( "date", Date );
364     TRY_META( "setting", Setting );
365     TRY_META( "url", URL );
366     TRY_META( "language", Language );
367     TRY_META( "nowplaying", NowPlaying );
368     TRY_META( "publisher", Publisher );
369     TRY_META( "encodedby", EncodedBy );
370     TRY_META( "arturl", ArtURL );
371     TRY_META( "trackid", TrackID );
372 }
373
374 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *p_state,
375                                    int o, int t, input_item_t *p_input )
376 {
377     lua_getfield( p_state, o, "meta" );
378     if( lua_istable( p_state, t ) )
379     {
380         lua_pushnil( p_state );
381         while( lua_next( p_state, t ) )
382         {
383             if( !lua_isstring( p_state, t+1 ) )
384             {
385                 msg_Warn( p_this, "Custom meta data category name must be "
386                                    "a string" );
387             }
388             else if( !lua_istable( p_state, t+2 ) )
389             {
390                 msg_Warn( p_this, "Custom meta data category contents "
391                                    "must be a table" );
392             }
393             else
394             {
395                 const char *psz_meta_category = lua_tostring( p_state, t+1 );
396                 msg_Dbg( p_this, "Found custom meta data category: %s",
397                          psz_meta_category );
398                 lua_pushnil( p_state );
399                 while( lua_next( p_state, t+2 ) )
400                 {
401                     if( !lua_isstring( p_state, t+3 ) )
402                     {
403                         msg_Warn( p_this, "Custom meta category item name "
404                                            "must be a string." );
405                     }
406                     else if( !lua_isstring( p_state, t+4 ) )
407                     {
408                         msg_Warn( p_this, "Custom meta category item value "
409                                            "must be a string." );
410                     }
411                     else
412                     {
413                         const char *psz_meta_name =
414                             lua_tostring( p_state, t+3 );
415                         const char *psz_meta_value =
416                             lua_tostring( p_state, t+4 );
417                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
418                                  psz_meta_category, psz_meta_name,
419                                  psz_meta_value );
420                         input_ItemAddInfo( p_input, psz_meta_category,
421                                            psz_meta_name, psz_meta_value );
422                     }
423                     lua_pop( p_state, 1 ); /* pop item */
424                 }
425             }
426             lua_pop( p_state, 1 ); /* pop category */
427         }
428     }
429     lua_pop( p_state, 1 ); /* pop "meta" */
430 }
431