]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/osd.c
Implement Lua objects in the C code directly. Fix most type checks. Move every thing...
[vlc] / modules / misc / lua / libs / osd.c
1 /*****************************************************************************
2  * intf.c: Generic lua interface functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_vout.h>
36 #include <vlc_osd.h>
37
38 #include <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 /*****************************************************************************
45  * OSD
46  *****************************************************************************/
47 static int vlc_osd_icon_from_string( const char *psz_name )
48 {
49     static const struct
50     {
51         int i_icon;
52         const char *psz_name;
53     } pp_icons[] =
54         { { OSD_PAUSE_ICON, "pause" },
55           { OSD_PLAY_ICON, "play" },
56           { OSD_SPEAKER_ICON, "speaker" },
57           { OSD_MUTE_ICON, "mute" },
58           { 0, NULL } };
59     int i;
60     for( i = 0; pp_icons[i].psz_name; i++ )
61     {
62         if( !strcmp( psz_name, pp_icons[i].psz_name ) )
63             return pp_icons[i].i_icon;
64     }
65     return 0;
66 }
67
68 static int vlclua_osd_icon( lua_State *L )
69 {
70     const char *psz_icon = luaL_checkstring( L, 1 );
71     int i_icon = vlc_osd_icon_from_string( psz_icon );
72     int i_chan = luaL_optint( L, 2, DEFAULT_CHAN );
73     if( !i_icon )
74         return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );
75     else
76     {
77         vlc_object_t *p_this = vlclua_get_this( L );
78         vout_OSDIcon( p_this, i_chan, i_icon );
79         return 0;
80     }
81 }
82
83 static int vlclua_osd_message( lua_State *L )
84 {
85     const char *psz_message = luaL_checkstring( L, 1 );
86     int i_chan = luaL_optint( L, 2, DEFAULT_CHAN );
87     vlc_object_t *p_this = vlclua_get_this( L );
88     vout_OSDMessage( p_this, i_chan, psz_message );
89     return 0;
90 }
91
92 static int vlc_osd_slider_type_from_string( const char *psz_name )
93 {
94     static const struct
95     {
96         int i_type;
97         const char *psz_name;
98     } pp_types[] =
99         { { OSD_HOR_SLIDER, "horizontal" },
100           { OSD_VERT_SLIDER, "vertical" },
101           { 0, NULL } };
102     int i;
103     for( i = 0; pp_types[i].psz_name; i++ )
104     {
105         if( !strcmp( psz_name, pp_types[i].psz_name ) )
106             return pp_types[i].i_type;
107     }
108     return 0;
109 }
110
111 static int vlclua_osd_slider( lua_State *L )
112 {
113     int i_position = luaL_checkint( L, 1 );
114     const char *psz_type = luaL_checkstring( L, 2 );
115     int i_type = vlc_osd_slider_type_from_string( psz_type );
116     int i_chan = luaL_optint( L, 3, DEFAULT_CHAN );
117     if( !i_type )
118         return luaL_error( L, "\"%s\" is not a valid slider type.",
119                            psz_type );
120     else
121     {
122         vlc_object_t *p_this = vlclua_get_this( L );
123         vout_OSDSlider( p_this, i_chan, i_position, i_type );
124         return 0;
125     }
126 }
127
128 static int vlclua_spu_channel_register( lua_State *L )
129 {
130     int i_chan;
131     vlc_object_t *p_this = vlclua_get_this( L );
132     vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
133                                              FIND_ANYWHERE );
134     if( !p_vout )
135         return luaL_error( L, "Unable to find vout." );
136
137     spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER, &i_chan );
138     vlc_object_release( p_vout );
139     lua_pushinteger( L, i_chan );
140     return 1;
141 }
142
143 static int vlclua_spu_channel_clear( lua_State *L )
144 {
145     int i_chan = luaL_checkint( L, 1 );
146     vlc_object_t *p_this = vlclua_get_this( L );
147     vout_thread_t *p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT,
148                                              FIND_ANYWHERE );
149     if( !p_vout )
150         return luaL_error( L, "Unable to find vout." );
151
152     spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, i_chan );
153     vlc_object_release( p_vout );
154     return 0;
155 }
156
157 /*****************************************************************************
158  *
159  *****************************************************************************/
160 static const luaL_Reg vlclua_osd_reg[] = {
161     { "icon", vlclua_osd_icon },
162     { "message", vlclua_osd_message },
163     { "slider", vlclua_osd_slider },
164     { "channel_register", vlclua_spu_channel_register },
165     { "channel_clear", vlclua_spu_channel_clear },
166     { NULL, NULL }
167 };
168
169 void luaopen_osd( lua_State *L )
170 {
171     lua_newtable( L );
172     luaL_register( L, NULL, vlclua_osd_reg );
173     lua_setfield( L, -2, "osd" );
174 }