Decklink: Fix swapped format arguments
[vlc.git] / modules / lua / libs / misc.c
blob3d367c13707fe3e2b2bbf0ec734414374f363b66
1 /*****************************************************************************
2 * misc.c
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
9 * RĂ©mi Duraffort <ivoire # videolan tod org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifndef _GNU_SOURCE
30 # define _GNU_SOURCE
31 #endif
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include <math.h>
38 #include <stdlib.h>
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_meta.h>
43 #include <vlc_interface.h>
44 #include <vlc_actions.h>
45 #include <vlc_interrupt.h>
46 #include <vlc_rand.h>
48 #include "../vlc.h"
49 #include "../libs.h"
50 #include "misc.h"
52 /*****************************************************************************
53 * Internal lua<->vlc utils
54 *****************************************************************************/
55 void vlclua_set_object( lua_State *L, void *id, void *value )
57 lua_pushlightuserdata( L, id );
58 lua_pushlightuserdata( L, value );
59 lua_rawset( L, LUA_REGISTRYINDEX );
62 void *vlclua_get_object( lua_State *L, void *id )
64 lua_pushlightuserdata( L, id );
65 lua_rawget( L, LUA_REGISTRYINDEX );
66 const void *p = lua_topointer( L, -1 );
67 lua_pop( L, 1 );
68 return (void *)p;
71 #undef vlclua_set_this
72 void vlclua_set_this( lua_State *L, vlc_object_t *p_this )
74 vlclua_set_object( L, vlclua_set_this, p_this );
77 vlc_object_t * vlclua_get_this( lua_State *L )
79 return vlclua_get_object( L, vlclua_set_this );
82 /*****************************************************************************
83 * VLC error code translation
84 *****************************************************************************/
85 int vlclua_push_ret( lua_State *L, int i_error )
87 lua_pushnumber( L, i_error );
88 lua_pushstring( L, vlc_error( i_error ) );
89 return 2;
92 /*****************************************************************************
93 * Get the VLC version string
94 *****************************************************************************/
95 static int vlclua_version( lua_State *L )
97 lua_pushstring( L, VERSION_MESSAGE );
98 return 1;
101 /*****************************************************************************
102 * Get the VLC copyright
103 *****************************************************************************/
104 static int vlclua_copyright( lua_State *L )
106 lua_pushliteral( L, COPYRIGHT_MESSAGE );
107 return 1;
110 /*****************************************************************************
111 * Get the VLC license msg/disclaimer
112 *****************************************************************************/
113 static int vlclua_license( lua_State *L )
115 lua_pushstring( L, LICENSE_MSG );
116 return 1;
119 /*****************************************************************************
120 * Quit VLC
121 *****************************************************************************/
122 static int vlclua_quit( lua_State *L )
124 vlc_object_t *p_this = vlclua_get_this( L );
125 /* The rc.c code also stops the playlist ... not sure if this is needed
126 * though. */
127 libvlc_Quit( p_this->obj.libvlc );
128 return 0;
131 static int vlclua_mdate( lua_State *L )
133 lua_pushnumber( L, vlc_tick_now() );
134 return 1;
137 static int vlclua_mwait( lua_State *L )
139 double f = luaL_checknumber( L, 1 );
141 vlc_interrupt_t *oint = vlclua_set_interrupt( L );
142 int ret = vlc_mwait_i11e( llround(f) );
144 vlc_interrupt_set( oint );
145 if( ret )
146 return luaL_error( L, "Interrupted." );
147 return 0;
150 static int vlclua_action_id( lua_State *L )
152 vlc_action_id_t i_key = vlc_actions_get_id( luaL_checkstring( L, 1 ) );
153 if (i_key == 0)
154 return 0;
155 lua_pushnumber( L, i_key );
156 return 1;
159 /*****************************************************************************
161 *****************************************************************************/
162 static const luaL_Reg vlclua_misc_reg[] = {
163 { "version", vlclua_version },
164 { "copyright", vlclua_copyright },
165 { "license", vlclua_license },
167 { "action_id", vlclua_action_id },
169 { "mdate", vlclua_mdate },
170 { "mwait", vlclua_mwait },
172 { "quit", vlclua_quit },
174 { NULL, NULL }
177 void luaopen_misc( lua_State *L )
179 lua_newtable( L );
180 luaL_register( L, NULL, vlclua_misc_reg );
181 lua_setfield( L, -2, "misc" );