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