]> git.sesse.net Git - vlc/blob - modules/lua/libs/misc.c
2f0d4dc429c6e4edb2d4b52bcdaf75e208f9cf82
[vlc] / modules / 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  *          RĂ©mi Duraffort <ivoire # videolan tod org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifndef  _GNU_SOURCE
30 #   define  _GNU_SOURCE
31 #endif
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_meta.h>
40 #include <vlc_interface.h>
41 #include <vlc_keys.h>
42
43 #include "../vlc.h"
44 #include "../libs.h"
45
46 /*****************************************************************************
47  * Internal lua<->vlc utils
48  *****************************************************************************/
49 static void vlclua_set_object( lua_State *L, void *id, void *value )
50 {
51     lua_pushlightuserdata( L, id );
52     lua_pushlightuserdata( L, value );
53     lua_rawset( L, LUA_REGISTRYINDEX );
54 }
55
56 static void *vlclua_get_object( lua_State *L, void *id )
57 {
58     lua_pushlightuserdata( L, id );
59     lua_rawget( L, LUA_REGISTRYINDEX );
60     const void *p = lua_topointer( L, -1 );
61     lua_pop( L, 1 );
62     return (void *)p;
63 }
64
65 #undef vlclua_set_this
66 void vlclua_set_this( lua_State *L, vlc_object_t *p_this )
67 {
68     vlclua_set_object( L, vlclua_set_this, p_this );
69 }
70
71 vlc_object_t * vlclua_get_this( lua_State *L )
72 {
73     return vlclua_get_object( L, vlclua_set_this );
74 }
75
76 void vlclua_set_intf( lua_State *L, intf_sys_t *p_intf )
77 {
78     vlclua_set_object( L, vlclua_set_intf, p_intf );
79 }
80
81 /*****************************************************************************
82  * VLC error code translation
83  *****************************************************************************/
84 int vlclua_push_ret( lua_State *L, int i_error )
85 {
86     lua_pushnumber( L, i_error );
87     lua_pushstring( L, vlc_error( i_error ) );
88     return 2;
89 }
90
91 /*****************************************************************************
92  * Get the VLC version string
93  *****************************************************************************/
94 static int vlclua_version( lua_State *L )
95 {
96     lua_pushstring( L, VERSION_MESSAGE );
97     return 1;
98 }
99
100 /*****************************************************************************
101  * Get the VLC copyright
102  *****************************************************************************/
103 static int vlclua_copyright( lua_State *L )
104 {
105     lua_pushliteral( L, COPYRIGHT_MESSAGE );
106     return 1;
107 }
108
109 /*****************************************************************************
110  * Get the VLC license msg/disclaimer
111  *****************************************************************************/
112 static int vlclua_license( lua_State *L )
113 {
114     lua_pushstring( L, LICENSE_MSG );
115     return 1;
116 }
117
118 /*****************************************************************************
119  * Quit VLC
120  *****************************************************************************/
121 static int vlclua_quit( lua_State *L )
122 {
123     vlc_object_t *p_this = vlclua_get_this( L );
124     /* The rc.c code also stops the playlist ... not sure if this is needed
125      * though. */
126     libvlc_Quit( p_this->p_libvlc );
127     return 0;
128 }
129
130 static int vlclua_mdate( lua_State *L )
131 {
132     lua_pushnumber( L, mdate() );
133     return 1;
134 }
135
136 static int vlclua_mwait( lua_State *L )
137 {
138     double f = luaL_checknumber( L, 1 );
139     mwait( (int64_t)f );
140     return 0;
141 }
142
143 static int vlclua_action_id( lua_State *L )
144 {
145     vlc_action_t i_key = vlc_GetActionId( luaL_checkstring( L, 1 ) );
146     if (i_key == 0)
147         return 0;
148     lua_pushnumber( L, i_key );
149     return 1;
150 }
151
152 /*****************************************************************************
153  *
154  *****************************************************************************/
155 static const luaL_Reg vlclua_misc_reg[] = {
156     { "version", vlclua_version },
157     { "copyright", vlclua_copyright },
158     { "license", vlclua_license },
159
160     { "action_id", vlclua_action_id },
161
162     { "mdate", vlclua_mdate },
163     { "mwait", vlclua_mwait },
164
165     { "quit", vlclua_quit },
166
167     { NULL, NULL }
168 };
169
170 void luaopen_misc( lua_State *L )
171 {
172     lua_newtable( L );
173     luaL_register( L, NULL, vlclua_misc_reg );
174     lua_setfield( L, -2, "misc" );
175 }