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