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