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