1 /*****************************************************************************
2 * osd.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id: 917e52eab6aeea0e3df9c8545c624e6a9d15b7cc $
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_vout_osd.h>
37
38 #include "../vlc.h"
39 #include "../libs.h"
40 #include "input.h"
41
42 /*****************************************************************************
43 * OSD
44 *****************************************************************************/
vlc_osd_icon_from_string(const char * psz_name)45 static int vlc_osd_icon_from_string( const char *psz_name )
46 {
47 static const struct
48 {
49 int i_icon;
50 const char *psz_name;
51 } pp_icons[] =
52 { { OSD_PAUSE_ICON, "pause" },
53 { OSD_PLAY_ICON, "play" },
54 { OSD_SPEAKER_ICON, "speaker" },
55 { OSD_MUTE_ICON, "mute" },
56 { 0, NULL } };
57 int i;
58 for( i = 0; pp_icons[i].psz_name; i++ )
59 {
60 if( !strcmp( psz_name, pp_icons[i].psz_name ) )
61 return pp_icons[i].i_icon;
62 }
63 return 0;
64 }
65
vlclua_osd_icon(lua_State * L)66 static int vlclua_osd_icon( lua_State *L )
67 {
68 const char *psz_icon = luaL_checkstring( L, 1 );
69 int i_icon = vlc_osd_icon_from_string( psz_icon );
70 int i_chan = (int)luaL_optinteger( L, 2, VOUT_SPU_CHANNEL_OSD );
71 if( !i_icon )
72 return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );
73
74 input_thread_t *p_input = vlclua_get_input_internal( L );
75 if( p_input )
76 {
77 vout_thread_t *p_vout = input_GetVout( p_input );
78 if( p_vout )
79 {
80 vout_OSDIcon( p_vout, i_chan, i_icon );
81 vlc_object_release( p_vout );
82 }
83 vlc_object_release( p_input );
84 }
85 return 0;
86 }
87
vlc_osd_position_from_string(const char * psz_name)88 static int vlc_osd_position_from_string( const char *psz_name )
89 {
90 static const struct
91 {
92 int i_position;
93 const char *psz_name;
94 } pp_icons[] =
95 { { 0, "center" },
96 { SUBPICTURE_ALIGN_LEFT, "left" },
97 { SUBPICTURE_ALIGN_RIGHT, "right" },
98 { SUBPICTURE_ALIGN_TOP, "top" },
99 { SUBPICTURE_ALIGN_BOTTOM, "bottom" },
100 { SUBPICTURE_ALIGN_TOP |SUBPICTURE_ALIGN_LEFT, "top-left" },
101 { SUBPICTURE_ALIGN_TOP |SUBPICTURE_ALIGN_RIGHT, "top-right" },
102 { SUBPICTURE_ALIGN_BOTTOM|SUBPICTURE_ALIGN_LEFT, "bottom-left" },
103 { SUBPICTURE_ALIGN_BOTTOM|SUBPICTURE_ALIGN_RIGHT, "bottom-right" },
104 { 0, NULL } };
105 int i;
106 for( i = 0; pp_icons[i].psz_name; i++ )
107 {
108 if( !strcmp( psz_name, pp_icons[i].psz_name ) )
109 return pp_icons[i].i_position;
110 }
111 return 0;
112 }
113
vlclua_osd_message(lua_State * L)114 static int vlclua_osd_message( lua_State *L )
115 {
116 const char *psz_message = luaL_checkstring( L, 1 );
117 int i_chan = (int)luaL_optinteger( L, 2, VOUT_SPU_CHANNEL_OSD );
118 const char *psz_position = luaL_optstring( L, 3, "top-right" );
119 mtime_t duration = (mtime_t)luaL_optinteger( L, 4, 1000000 );
120
121 input_thread_t *p_input = vlclua_get_input_internal( L );
122 if( p_input )
123 {
124 vout_thread_t *p_vout = input_GetVout( p_input );
125 if( p_vout )
126 {
127 vout_OSDText( p_vout, i_chan, vlc_osd_position_from_string( psz_position ),
128 duration, psz_message );
129 vlc_object_release( p_vout );
130 }
131 vlc_object_release( p_input );
132 }
133 return 0;
134 }
135
vlc_osd_slider_type_from_string(const char * psz_name)136 static int vlc_osd_slider_type_from_string( const char *psz_name )
137 {
138 static const struct
139 {
140 int i_type;
141 const char *psz_name;
142 } pp_types[] =
143 { { OSD_HOR_SLIDER, "horizontal" },
144 { OSD_VERT_SLIDER, "vertical" },
145 { 0, NULL } };
146 int i;
147 for( i = 0; pp_types[i].psz_name; i++ )
148 {
149 if( !strcmp( psz_name, pp_types[i].psz_name ) )
150 return pp_types[i].i_type;
151 }
152 return 0;
153 }
154
vlclua_osd_slider(lua_State * L)155 static int vlclua_osd_slider( lua_State *L )
156 {
157 int i_position = luaL_checkint( L, 1 );
158 const char *psz_type = luaL_checkstring( L, 2 );
159 int i_type = vlc_osd_slider_type_from_string( psz_type );
160 int i_chan = (int)luaL_optinteger( L, 3, VOUT_SPU_CHANNEL_OSD );
161 if( !i_type )
162 return luaL_error( L, "\"%s\" is not a valid slider type.",
163 psz_type );
164
165 input_thread_t *p_input = vlclua_get_input_internal( L );
166 if( p_input )
167 {
168 vout_thread_t *p_vout = input_GetVout( p_input );
169 if( p_vout )
170 {
171 vout_OSDSlider( p_vout, i_chan, i_position, i_type );
172 vlc_object_release( p_vout );
173 }
174 vlc_object_release( p_input );
175 }
176 return 0;
177 }
178
vlclua_spu_channel_register(lua_State * L)179 static int vlclua_spu_channel_register( lua_State *L )
180 {
181 input_thread_t *p_input = vlclua_get_input_internal( L );
182 if( !p_input )
183 return luaL_error( L, "Unable to find input." );
184
185 vout_thread_t *p_vout = input_GetVout( p_input );
186 if( !p_vout )
187 {
188 vlc_object_release( p_input );
189 return luaL_error( L, "Unable to find vout." );
190 }
191
192 int i_chan = vout_RegisterSubpictureChannel( p_vout );
193 vlc_object_release( p_vout );
194 vlc_object_release( p_input );
195 lua_pushinteger( L, i_chan );
196 return 1;
197 }
198
vlclua_spu_channel_clear(lua_State * L)199 static int vlclua_spu_channel_clear( lua_State *L )
200 {
201 int i_chan = luaL_checkint( L, 1 );
202 input_thread_t *p_input = vlclua_get_input_internal( L );
203 if( !p_input )
204 return luaL_error( L, "Unable to find input." );
205 vout_thread_t *p_vout = input_GetVout( p_input );
206 if( !p_vout )
207 {
208 vlc_object_release( p_input );
209 return luaL_error( L, "Unable to find vout." );
210 }
211
212 vout_FlushSubpictureChannel( p_vout, i_chan );
213 vlc_object_release( p_vout );
214 vlc_object_release( p_input );
215 return 0;
216 }
217
218 /*****************************************************************************
219 *
220 *****************************************************************************/
221 static const luaL_Reg vlclua_osd_reg[] = {
222 { "icon", vlclua_osd_icon },
223 { "message", vlclua_osd_message },
224 { "slider", vlclua_osd_slider },
225 { "channel_register", vlclua_spu_channel_register },
226 { "channel_clear", vlclua_spu_channel_clear },
227 { NULL, NULL }
228 };
229
luaopen_osd(lua_State * L)230 void luaopen_osd( lua_State *L )
231 {
232 lua_newtable( L );
233 luaL_register( L, NULL, vlclua_osd_reg );
234 lua_setfield( L, -2, "osd" );
235 }
236