]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/misc.c
3b1fbae65aa95697750d40189319385f05f37635
[vlc] / modules / misc / lua / libs / misc.c
1 /*****************************************************************************
2  * misc.c
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_plugin.h>
38 #include <vlc_meta.h>
39 #include <vlc_aout.h>
40 #include <vlc_interface.h>
41 #include <vlc_keys.h>
42
43 #include <lua.h>        /* Low level lua C API */
44 #include <lauxlib.h>    /* Higher level C API */
45 #include <lualib.h>     /* Lua libs */
46
47 #include "../vlc.h"
48 #include "../libs.h"
49
50 #undef vlclua_set_this
51 /*****************************************************************************
52  * Internal lua<->vlc utils
53  *****************************************************************************/
54 void vlclua_set_this( lua_State *L, vlc_object_t *p_this )
55 {
56     lua_pushlightuserdata( L, vlclua_set_this );
57     lua_pushlightuserdata( L, p_this );
58     lua_rawset( L, LUA_REGISTRYINDEX );
59 }
60
61 vlc_object_t * vlclua_get_this( lua_State *L )
62 {
63     lua_pushlightuserdata( L, vlclua_set_this );
64     lua_rawget( L, LUA_REGISTRYINDEX );
65     vlc_object_t *p_this = (vlc_object_t*)lua_topointer( L, -1 );
66     lua_pop( L, 1 );
67     return p_this;
68 }
69
70 /*****************************************************************************
71  * VLC error code translation
72  *****************************************************************************/
73 int vlclua_push_ret( lua_State *L, int i_error )
74 {
75     lua_pushnumber( L, i_error );
76     lua_pushstring( L, vlc_error( i_error ) );
77     return 2;
78 }
79
80 /*****************************************************************************
81  * Get the VLC version string
82  *****************************************************************************/
83 static int vlclua_version( lua_State *L )
84 {
85     lua_pushstring( L, VLC_Version() );
86     return 1;
87 }
88
89 /*****************************************************************************
90  * Get the VLC copyright
91  *****************************************************************************/
92 static int vlclua_copyright( lua_State *L )
93 {
94     lua_pushliteral( L, COPYRIGHT_MESSAGE );
95     return 1;
96 }
97
98 /*****************************************************************************
99  * Get the VLC license msg/disclaimer
100  *****************************************************************************/
101 static int vlclua_license( lua_State *L )
102 {
103     lua_pushstring( L, LICENSE_MSG );
104     return 1;
105 }
106
107 /*****************************************************************************
108  * Quit VLC
109  *****************************************************************************/
110 static int vlclua_quit( lua_State *L )
111 {
112     vlc_object_t *p_this = vlclua_get_this( L );
113     /* The rc.c code also stops the playlist ... not sure if this is needed
114      * though. */
115     libvlc_Quit( p_this->p_libvlc );
116     return 0;
117 }
118
119 /*****************************************************************************
120  * Global properties getters
121  *****************************************************************************/
122 static int vlclua_datadir( lua_State *L )
123 {
124     char *psz_data = config_GetDataDir( vlclua_get_this( L ) );
125     lua_pushstring( L, psz_data );
126     free( psz_data );
127     return 1;
128 }
129
130 static int vlclua_userdatadir( lua_State *L )
131 {
132     char *dir = config_GetUserDir( VLC_DATA_DIR );
133     lua_pushstring( L, dir );
134     free( dir );
135     return 1;
136 }
137
138 static int vlclua_homedir( lua_State *L )
139 {
140     char *home = config_GetUserDir( VLC_HOME_DIR );
141     lua_pushstring( L, home );
142     free( home );
143     return 1;
144 }
145
146 static int vlclua_configdir( lua_State *L )
147 {
148     char *dir = config_GetUserDir( VLC_CONFIG_DIR );
149     lua_pushstring( L, dir );
150     free( dir );
151     return 1;
152 }
153
154 static int vlclua_cachedir( lua_State *L )
155 {
156     char *dir = config_GetUserDir( VLC_CACHE_DIR );
157     lua_pushstring( L, dir );
158     free( dir );
159     return 1;
160 }
161
162 static int vlclua_datadir_list( lua_State *L )
163 {
164     const char *psz_dirname = luaL_checkstring( L, 1 );
165     char **ppsz_dir_list = NULL;
166     int i = 1;
167
168     if( vlclua_dir_list( vlclua_get_this( L ), psz_dirname, &ppsz_dir_list )
169         != VLC_SUCCESS )
170         return 0;
171     lua_newtable( L );
172     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
173     {
174         lua_pushstring( L, *ppsz_dir );
175         lua_rawseti( L, -2, i );
176         i ++;
177     }
178     vlclua_dir_list_free( ppsz_dir_list );
179     return 1;
180 }
181 /*****************************************************************************
182  *
183  *****************************************************************************/
184 static int vlclua_lock_and_wait( lua_State *L )
185 {
186     intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
187     intf_sys_t *p_sys = p_intf->p_sys;
188
189     vlc_mutex_lock( &p_sys->lock );
190     mutex_cleanup_push( &p_sys->lock );
191     while( !p_sys->exiting )
192         vlc_cond_wait( &p_sys->wait, &p_sys->lock );
193     vlc_cleanup_run();
194     lua_pushboolean( L, 1 );
195     return 1;
196 }
197
198 static int vlclua_mdate( lua_State *L )
199 {
200     lua_pushnumber( L, mdate() );
201     return 1;
202 }
203
204 static int vlclua_mwait( lua_State *L )
205 {
206     double f = luaL_checknumber( L, 1 );
207     mwait( (int64_t)f );
208     return 0;
209 }
210
211 static int vlclua_intf_should_die( lua_State *L )
212 {
213     intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
214     lua_pushboolean( L, p_intf->p_sys->exiting );
215     return 1;
216 }
217
218 static int vlclua_action_id( lua_State *L )
219 {
220     vlc_key_t i_key = vlc_GetActionId( luaL_checkstring( L, 1 ) );
221     if (i_key == 0)
222         return 0;
223     lua_pushnumber( L, i_key );
224     return 1;
225 }
226
227 /*****************************************************************************
228  *
229  *****************************************************************************/
230 static const luaL_Reg vlclua_misc_reg[] = {
231     { "version", vlclua_version },
232     { "copyright", vlclua_copyright },
233     { "license", vlclua_license },
234
235     { "datadir", vlclua_datadir },
236     { "userdatadir", vlclua_userdatadir },
237     { "homedir", vlclua_homedir },
238     { "configdir", vlclua_configdir },
239     { "cachedir", vlclua_cachedir },
240     { "datadir_list", vlclua_datadir_list },
241
242     { "action_id", vlclua_action_id },
243
244     { "mdate", vlclua_mdate },
245     { "mwait", vlclua_mwait },
246
247     { "lock_and_wait", vlclua_lock_and_wait },
248
249     { "should_die", vlclua_intf_should_die },
250     { "quit", vlclua_quit },
251
252     { NULL, NULL }
253 };
254
255 void luaopen_misc( lua_State *L )
256 {
257     lua_newtable( L );
258     luaL_register( L, NULL, vlclua_misc_reg );
259     lua_setfield( L, -2, "misc" );
260 }