1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
40 #include <vlc_interface.h>
43 #include <lua.h> /* Low level lua C API */
44 #include <lauxlib.h> /* Higher level C API */
45 #include <lualib.h> /* Lua libs */
50 /*****************************************************************************
51 * Internal lua<->vlc utils
52 *****************************************************************************/
53 static void vlclua_set_object( lua_State *L, void *id, void *value )
55 lua_pushlightuserdata( L, id );
56 lua_pushlightuserdata( L, value );
57 lua_rawset( L, LUA_REGISTRYINDEX );
60 static void *vlclua_get_object( lua_State *L, void *id )
62 lua_pushlightuserdata( L, id );
63 lua_rawget( L, LUA_REGISTRYINDEX );
64 const void *p = lua_topointer( L, -1 );
69 #undef vlclua_set_this
70 void vlclua_set_this( lua_State *L, vlc_object_t *p_this )
72 vlclua_set_object( L, vlclua_set_this, p_this );
75 vlc_object_t * vlclua_get_this( lua_State *L )
77 return vlclua_get_object( L, vlclua_set_this );
80 void vlclua_set_intf( lua_State *L, intf_sys_t *p_intf )
82 vlclua_set_object( L, vlclua_set_intf, p_intf );
85 static intf_sys_t * vlclua_get_intf( lua_State *L )
87 return vlclua_get_object( L, vlclua_set_intf );
90 /*****************************************************************************
91 * VLC error code translation
92 *****************************************************************************/
93 int vlclua_push_ret( lua_State *L, int i_error )
95 lua_pushnumber( L, i_error );
96 lua_pushstring( L, vlc_error( i_error ) );
100 /*****************************************************************************
101 * Get the VLC version string
102 *****************************************************************************/
103 static int vlclua_version( lua_State *L )
105 lua_pushstring( L, VLC_Version() );
109 /*****************************************************************************
110 * Get the VLC copyright
111 *****************************************************************************/
112 static int vlclua_copyright( lua_State *L )
114 lua_pushliteral( L, COPYRIGHT_MESSAGE );
118 /*****************************************************************************
119 * Get the VLC license msg/disclaimer
120 *****************************************************************************/
121 static int vlclua_license( lua_State *L )
123 lua_pushstring( L, LICENSE_MSG );
127 /*****************************************************************************
129 *****************************************************************************/
130 static int vlclua_quit( lua_State *L )
132 vlc_object_t *p_this = vlclua_get_this( L );
133 /* The rc.c code also stops the playlist ... not sure if this is needed
135 libvlc_Quit( p_this->p_libvlc );
139 /*****************************************************************************
140 * Global properties getters
141 *****************************************************************************/
142 static int vlclua_datadir( lua_State *L )
144 char *psz_data = config_GetDataDir( vlclua_get_this( L ) );
145 lua_pushstring( L, psz_data );
150 static int vlclua_userdatadir( lua_State *L )
152 char *dir = config_GetUserDir( VLC_DATA_DIR );
153 lua_pushstring( L, dir );
158 static int vlclua_homedir( lua_State *L )
160 char *home = config_GetUserDir( VLC_HOME_DIR );
161 lua_pushstring( L, home );
166 static int vlclua_configdir( lua_State *L )
168 char *dir = config_GetUserDir( VLC_CONFIG_DIR );
169 lua_pushstring( L, dir );
174 static int vlclua_cachedir( lua_State *L )
176 char *dir = config_GetUserDir( VLC_CACHE_DIR );
177 lua_pushstring( L, dir );
182 static int vlclua_datadir_list( lua_State *L )
184 const char *psz_dirname = luaL_checkstring( L, 1 );
185 char **ppsz_dir_list = NULL;
188 if( vlclua_dir_list( vlclua_get_this( L ), psz_dirname, &ppsz_dir_list )
192 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
194 lua_pushstring( L, *ppsz_dir );
195 lua_rawseti( L, -2, i );
198 vlclua_dir_list_free( ppsz_dir_list );
201 /*****************************************************************************
203 *****************************************************************************/
204 static int vlclua_lock_and_wait( lua_State *L )
206 intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
207 intf_sys_t *p_sys = p_intf->p_sys;
209 vlc_mutex_lock( &p_sys->lock );
210 mutex_cleanup_push( &p_sys->lock );
211 while( !p_sys->exiting )
212 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
214 lua_pushboolean( L, 1 );
218 static int vlclua_mdate( lua_State *L )
220 lua_pushnumber( L, mdate() );
224 static int vlclua_mwait( lua_State *L )
226 double f = luaL_checknumber( L, 1 );
231 static int vlclua_intf_should_die( lua_State *L )
233 intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
234 lua_pushboolean( L, p_intf->p_sys->exiting );
238 static int vlclua_action_id( lua_State *L )
240 vlc_key_t i_key = vlc_GetActionId( luaL_checkstring( L, 1 ) );
243 lua_pushnumber( L, i_key );
247 /*****************************************************************************
249 *****************************************************************************/
250 static const luaL_Reg vlclua_misc_reg[] = {
251 { "version", vlclua_version },
252 { "copyright", vlclua_copyright },
253 { "license", vlclua_license },
255 { "datadir", vlclua_datadir },
256 { "userdatadir", vlclua_userdatadir },
257 { "homedir", vlclua_homedir },
258 { "configdir", vlclua_configdir },
259 { "cachedir", vlclua_cachedir },
260 { "datadir_list", vlclua_datadir_list },
262 { "action_id", vlclua_action_id },
264 { "mdate", vlclua_mdate },
265 { "mwait", vlclua_mwait },
267 { "lock_and_wait", vlclua_lock_and_wait },
269 { "should_die", vlclua_intf_should_die },
270 { "quit", vlclua_quit },
275 void luaopen_misc( lua_State *L )
278 luaL_register( L, NULL, vlclua_misc_reg );
279 lua_setfield( L, -2, "misc" );