]> git.sesse.net Git - vlc/blob - modules/lua/libs/misc.c
lua: never change index of lua file descriptors
[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 #include "misc.h"
46
47 /*****************************************************************************
48  * Internal lua<->vlc utils
49  *****************************************************************************/
50 void vlclua_set_object( lua_State *L, void *id, void *value )
51 {
52     lua_pushlightuserdata( L, id );
53     lua_pushlightuserdata( L, value );
54     lua_rawset( L, LUA_REGISTRYINDEX );
55 }
56
57 void *vlclua_get_object( lua_State *L, void *id )
58 {
59     lua_pushlightuserdata( L, id );
60     lua_rawget( L, LUA_REGISTRYINDEX );
61     const void *p = lua_topointer( L, -1 );
62     lua_pop( L, 1 );
63     return (void *)p;
64 }
65
66 #undef vlclua_set_this
67 void vlclua_set_this( lua_State *L, vlc_object_t *p_this )
68 {
69     vlclua_set_object( L, vlclua_set_this, p_this );
70 }
71
72 vlc_object_t * vlclua_get_this( lua_State *L )
73 {
74     return vlclua_get_object( L, vlclua_set_this );
75 }
76
77 /*****************************************************************************
78  * VLC error code translation
79  *****************************************************************************/
80 int vlclua_push_ret( lua_State *L, int i_error )
81 {
82     lua_pushnumber( L, i_error );
83     lua_pushstring( L, vlc_error( i_error ) );
84     return 2;
85 }
86
87 /*****************************************************************************
88  * Get the VLC version string
89  *****************************************************************************/
90 static int vlclua_version( lua_State *L )
91 {
92     lua_pushstring( L, VERSION_MESSAGE );
93     return 1;
94 }
95
96 /*****************************************************************************
97  * Get the VLC copyright
98  *****************************************************************************/
99 static int vlclua_copyright( lua_State *L )
100 {
101     lua_pushliteral( L, COPYRIGHT_MESSAGE );
102     return 1;
103 }
104
105 /*****************************************************************************
106  * Get the VLC license msg/disclaimer
107  *****************************************************************************/
108 static int vlclua_license( lua_State *L )
109 {
110     lua_pushstring( L, LICENSE_MSG );
111     return 1;
112 }
113
114 /*****************************************************************************
115  * Quit VLC
116  *****************************************************************************/
117 static int vlclua_quit( lua_State *L )
118 {
119     vlc_object_t *p_this = vlclua_get_this( L );
120     /* The rc.c code also stops the playlist ... not sure if this is needed
121      * though. */
122     libvlc_Quit( p_this->p_libvlc );
123     return 0;
124 }
125
126 static int vlclua_mdate( lua_State *L )
127 {
128     lua_pushnumber( L, mdate() );
129     return 1;
130 }
131
132 static int vlclua_mwait( lua_State *L )
133 {
134     double f = luaL_checknumber( L, 1 );
135     mwait( (int64_t)f );
136     return 0;
137 }
138
139 static int vlclua_action_id( lua_State *L )
140 {
141     vlc_action_t i_key = vlc_GetActionId( luaL_checkstring( L, 1 ) );
142     if (i_key == 0)
143         return 0;
144     lua_pushnumber( L, i_key );
145     return 1;
146 }
147
148 /*****************************************************************************
149  *
150  *****************************************************************************/
151 static const luaL_Reg vlclua_misc_reg[] = {
152     { "version", vlclua_version },
153     { "copyright", vlclua_copyright },
154     { "license", vlclua_license },
155
156     { "action_id", vlclua_action_id },
157
158     { "mdate", vlclua_mdate },
159     { "mwait", vlclua_mwait },
160
161     { "quit", vlclua_quit },
162
163     { NULL, NULL }
164 };
165
166 void luaopen_misc( lua_State *L )
167 {
168     lua_newtable( L );
169     luaL_register( L, NULL, vlclua_misc_reg );
170     lua_setfield( L, -2, "misc" );
171 }