]> git.sesse.net Git - vlc/blob - modules/misc/lua/meta.c
Don't segfault when looking for art with the lua scripts. Thanks to ILEoo for spoting...
[vlc] / modules / misc / lua / meta.c
1 /*****************************************************************************
2  * meta.c: Get meta/artwork using lua scripts
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 <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_playlist.h>
39 #include <vlc_meta.h>
40 #include <vlc_url.h>
41 #include <vlc_strings.h>
42 #include <vlc_stream.h>
43 #include <vlc_charset.h>
44
45 #include "vlc.h"
46 #include "libs.h"
47
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
53                       lua_State * L, void * user_data );
54 static lua_State *vlclua_meta_init( vlc_object_t *p_this,
55                                     input_item_t * p_item );
56
57
58 /*****************************************************************************
59  * Init lua
60  *****************************************************************************/
61 static luaL_Reg p_reg[] = { { NULL, NULL } };
62
63 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
64 {
65     lua_State * L = luaL_newstate();
66     if( !L )
67     {
68         msg_Err( p_this, "Could not create new Lua State" );
69         return NULL;
70     }
71     char *psz_meta;
72
73     /* Load Lua libraries */
74     luaL_openlibs( L ); /* XXX: Don't open all the libs? */
75
76     luaL_register( L, "vlc", p_reg );
77
78     luaopen_msg( L );
79     luaopen_stream( L );
80     luaopen_strings( L );
81
82     lua_pushlightuserdata( L, p_this );
83     lua_setfield( L, -2, "private" );
84
85     psz_meta = input_item_GetName( p_item );
86     lua_pushstring( L, psz_meta );
87     lua_setfield( L, -2, "name" );
88     free( psz_meta );
89
90     psz_meta = input_item_GetArtist( p_item );
91     lua_pushstring( L, psz_meta );
92     lua_setfield( L, -2, "artist" );
93     free( psz_meta );
94
95     psz_meta = input_item_GetTitle( p_item ) ;
96     lua_pushstring( L, psz_meta );
97     lua_setfield( L, -2, "title" );
98     free( psz_meta );
99
100     psz_meta = input_item_GetAlbum( p_item );
101     lua_pushstring( L, psz_meta );
102     lua_setfield( L, -2, "album" );
103     free( psz_meta );
104
105     psz_meta = input_item_GetArtURL( p_item );
106     lua_pushstring( L, psz_meta );
107     lua_setfield( L, -2, "arturl" );
108     free( psz_meta );
109     /* XXX: all should be passed ( could use macro ) */
110
111     return L;
112 }
113
114 /*****************************************************************************
115  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
116  * pointed by psz_filename.
117  *****************************************************************************/
118 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
119                       lua_State * L, void * user_data )
120 {
121     int i_ret = VLC_EGENERIC;
122     input_item_t * p_input = user_data;
123     int s;
124
125     /* Ugly hack to delete previous versions of the fetchart()
126     * functions. */
127     lua_pushnil( L );
128     lua_setglobal( L, "fetch_art" );
129
130     /* Load and run the script(s) */
131     if( luaL_dofile( L, psz_filename ) )
132     {
133         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
134                   lua_tostring( L, lua_gettop( L ) ) );
135         lua_pop( L, 1 );
136         return VLC_EGENERIC;
137     }
138
139     lua_getglobal( L, "fetch_art" );
140
141     if( !lua_isfunction( L, lua_gettop( L ) ) )
142     {
143         msg_Warn( p_this, "Error while runing script %s, "
144                   "function fetch_art() not found", psz_filename );
145         lua_pop( L, 1 );
146         return VLC_EGENERIC;
147     }
148
149     if( lua_pcall( L, 0, 1, 0 ) )
150     {
151         msg_Warn( p_this, "Error while runing script %s, "
152                   "function fetch_art(): %s", psz_filename,
153                   lua_tostring( L, lua_gettop( L ) ) );
154         lua_pop( L, 1 );
155         return VLC_EGENERIC;
156     }
157
158     if((s = lua_gettop( L )))
159     {
160         const char * psz_value;
161
162         if( lua_isstring( L, s ) )
163         {
164             psz_value = lua_tostring( L, s );
165             if( psz_value && *psz_value != 0 )
166             {
167                 lua_Dbg( p_this, "setting arturl: %s", psz_value );
168                 input_item_SetArtURL ( p_input, psz_value );
169                 i_ret = VLC_SUCCESS;
170             }
171         }
172         else if( !lua_isnil( L, s ) )
173         {
174             msg_Err( p_this, "Lua art fetcher script %s: "
175                  "didn't return a string", psz_filename );
176         }
177     }
178     else
179     {
180         msg_Err( p_this, "Script went completely foobar" );
181     }
182
183     return i_ret;
184 }
185
186 /*****************************************************************************
187  * Module entry point for art.
188  *****************************************************************************/
189 int FindArt( vlc_object_t *p_this )
190 {
191     playlist_t *p_playlist = (playlist_t *)p_this;
192     input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
193     lua_State *L = vlclua_meta_init( p_this, p_item );
194
195     int i_ret = vlclua_scripts_batch_execute( p_this, "meta", &fetch_art, L, p_item );
196     lua_close( L );
197     return i_ret;
198 }
199