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>
39 #include <vlc_charset.h>
41 #include <vlc_interface.h>
44 #include <lua.h> /* Low level lua C API */
45 #include <lauxlib.h> /* Higher level C API */
46 #include <lualib.h> /* Lua libs */
51 /*****************************************************************************
52 * Internal lua<->vlc utils
53 *****************************************************************************/
54 vlc_object_t * vlclua_get_this( lua_State *L )
56 vlc_object_t * p_this;
57 lua_getglobal( L, "vlc" );
58 lua_getfield( L, -1, "private" );
59 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
64 /*****************************************************************************
65 * VLC error code translation
66 *****************************************************************************/
67 int vlclua_push_ret( lua_State *L, int i_error )
69 lua_pushnumber( L, i_error );
70 lua_pushstring( L, vlc_error( i_error ) );
74 /*****************************************************************************
75 * Get the VLC version string
76 *****************************************************************************/
77 static int vlclua_version( lua_State *L )
79 lua_pushstring( L, VLC_Version() );
83 /*****************************************************************************
84 * Get the VLC copyright
85 *****************************************************************************/
86 static int vlclua_copyright( lua_State *L )
88 lua_pushstring( L, COPYRIGHT_MESSAGE );
92 /*****************************************************************************
93 * Get the VLC license msg/disclaimer
94 *****************************************************************************/
95 static int vlclua_license( lua_State *L )
97 lua_pushstring( L, LICENSE_MSG );
101 /*****************************************************************************
103 *****************************************************************************/
104 static int vlclua_quit( lua_State *L )
106 vlc_object_t *p_this = vlclua_get_this( L );
107 /* The rc.c code also stops the playlist ... not sure if this is needed
109 libvlc_Quit( p_this->p_libvlc );
113 /*****************************************************************************
114 * Global properties getters
115 *****************************************************************************/
116 static int vlclua_datadir( lua_State *L )
118 char *psz_data = config_GetDataDir( vlclua_get_this( L ) );
119 lua_pushstring( L, psz_data );
124 static int vlclua_userdatadir( lua_State *L )
126 char *dir = config_GetUserDir( VLC_DATA_DIR );
127 lua_pushstring( L, dir );
132 static int vlclua_homedir( lua_State *L )
134 char *home = config_GetUserDir( VLC_HOME_DIR );
135 lua_pushstring( L, home );
140 static int vlclua_configdir( lua_State *L )
142 char *dir = config_GetUserDir( VLC_CONFIG_DIR );
143 lua_pushstring( L, dir );
148 static int vlclua_cachedir( lua_State *L )
150 char *dir = config_GetUserDir( VLC_CACHE_DIR );
151 lua_pushstring( L, dir );
156 static int vlclua_datadir_list( lua_State *L )
158 const char *psz_dirname = luaL_checkstring( L, 1 );
159 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
160 char **ppsz_dir = ppsz_dir_list;
163 if( vlclua_dir_list( vlclua_get_this( L ), psz_dirname, ppsz_dir_list )
167 for( ; *ppsz_dir; ppsz_dir++ )
169 lua_pushstring( L, *ppsz_dir );
170 lua_rawseti( L, -2, i );
173 vlclua_dir_list_free( ppsz_dir_list );
176 /*****************************************************************************
178 *****************************************************************************/
179 static int vlclua_lock_and_wait( lua_State *L )
181 intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
182 intf_sys_t *p_sys = p_intf->p_sys;
184 vlc_mutex_lock( &p_sys->lock );
185 mutex_cleanup_push( &p_sys->lock );
186 while( !p_sys->exiting )
187 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
189 lua_pushboolean( L, 1 );
193 static int vlclua_mdate( lua_State *L )
195 lua_pushnumber( L, mdate() );
199 static int vlclua_mwait( lua_State *L )
201 double f = luaL_checknumber( L, 1 );
206 static int vlclua_intf_should_die( lua_State *L )
208 intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
209 lua_pushboolean( L, p_intf->p_sys->exiting );
213 static int vlclua_action_id( lua_State *L )
215 vlc_key_t i_key = vlc_GetActionId( luaL_checkstring( L, 1 ) );
218 lua_pushnumber( L, i_key );
222 /*****************************************************************************
224 *****************************************************************************/
225 static const luaL_Reg vlclua_misc_reg[] = {
226 { "version", vlclua_version },
227 { "copyright", vlclua_copyright },
228 { "license", vlclua_license },
230 { "datadir", vlclua_datadir },
231 { "userdatadir", vlclua_userdatadir },
232 { "homedir", vlclua_homedir },
233 { "configdir", vlclua_configdir },
234 { "cachedir", vlclua_cachedir },
235 { "datadir_list", vlclua_datadir_list },
237 { "action_id", vlclua_action_id },
239 { "mdate", vlclua_mdate },
240 { "mwait", vlclua_mwait },
242 { "lock_and_wait", vlclua_lock_and_wait },
244 { "should_die", vlclua_intf_should_die },
245 { "quit", vlclua_quit },
250 void luaopen_misc( lua_State *L )
253 luaL_register( L, NULL, vlclua_misc_reg );
254 lua_setfield( L, -2, "misc" );