]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/misc.c
Add --data-path option. Access the src share directory now works from build tree.
[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     char *psz_data = config_GetDataDir( vlclua_get_this( L ) );
118     lua_pushstring( L, psz_data );
119     free( psz_data );
120     return 1;
121 }
122
123 static int vlclua_userdatadir( lua_State *L )
124 {
125     char *dir = config_GetUserDir( VLC_DATA_DIR );
126     lua_pushstring( L, dir );
127     free( dir );
128     return 1;
129 }
130
131 static int vlclua_homedir( lua_State *L )
132 {
133     char *home = config_GetUserDir( VLC_HOME_DIR );
134     lua_pushstring( L, home );
135     free( home );
136     return 1;
137 }
138
139 static int vlclua_configdir( lua_State *L )
140 {
141     char *dir = config_GetUserDir( VLC_CONFIG_DIR );
142     lua_pushstring( L, dir );
143     free( dir );
144     return 1;
145 }
146
147 static int vlclua_cachedir( lua_State *L )
148 {
149     char *dir = config_GetUserDir( VLC_CACHE_DIR );
150     lua_pushstring( L, dir );
151     free( dir );
152     return 1;
153 }
154
155 static int vlclua_datadir_list( lua_State *L )
156 {
157     const char *psz_dirname = luaL_checkstring( L, 1 );
158     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
159     char **ppsz_dir = ppsz_dir_list;
160     int i = 1;
161
162     if( vlclua_dir_list( vlclua_get_this( L ), psz_dirname, ppsz_dir_list )
163         != VLC_SUCCESS )
164         return 0;
165     lua_newtable( L );
166     for( ; *ppsz_dir; ppsz_dir++ )
167     {
168         lua_pushstring( L, *ppsz_dir );
169         lua_rawseti( L, -2, i );
170         i ++;
171     }
172     vlclua_dir_list_free( ppsz_dir_list );
173     return 1;
174 }
175 /*****************************************************************************
176  *
177  *****************************************************************************/
178 static int vlclua_lock_and_wait( lua_State *L )
179 {
180     intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
181     intf_sys_t *p_sys = p_intf->p_sys;
182
183     vlc_mutex_lock( &p_sys->lock );
184     while( !p_sys->exiting )
185         vlc_cond_wait( &p_sys->wait, &p_sys->lock );
186     vlc_mutex_unlock( &p_sys->lock );
187     lua_pushboolean( L, 1 );
188     return 1;
189 }
190
191 static int vlclua_mdate( lua_State *L )
192 {
193     lua_pushnumber( L, mdate() );
194     return 1;
195 }
196
197 static int vlclua_mwait( lua_State *L )
198 {
199     double f = luaL_checknumber( L, 1 );
200     mwait( (int64_t)f );
201     return 0;
202 }
203
204 static int vlclua_intf_should_die( lua_State *L )
205 {
206     intf_thread_t *p_intf = (intf_thread_t*)vlclua_get_this( L );
207     lua_pushboolean( L, p_intf->p_sys->exiting );
208     return 1;
209 }
210
211 /*****************************************************************************
212  *
213  *****************************************************************************/
214 static const luaL_Reg vlclua_misc_reg[] = {
215     { "version", vlclua_version },
216     { "copyright", vlclua_copyright },
217     { "license", vlclua_license },
218
219     { "datadir", vlclua_datadir },
220     { "userdatadir", vlclua_userdatadir },
221     { "homedir", vlclua_homedir },
222     { "configdir", vlclua_configdir },
223     { "cachedir", vlclua_cachedir },
224     { "datadir_list", vlclua_datadir_list },
225
226     { "mdate", vlclua_mdate },
227     { "mwait", vlclua_mwait },
228
229     { "lock_and_wait", vlclua_lock_and_wait },
230
231     { "should_die", vlclua_intf_should_die },
232     { "quit", vlclua_quit },
233
234     { NULL, NULL }
235 };
236
237 void luaopen_misc( lua_State *L )
238 {
239     lua_newtable( L );
240     luaL_register( L, NULL, vlclua_misc_reg );
241     lua_setfield( L, -2, "misc" );
242 }