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