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