]> git.sesse.net Git - vlc/blob - modules/lua/libs/osd.c
Update workarounds for incomplete mingw headers
[vlc] / modules / lua / libs / osd.c
1 /*****************************************************************************
2  * osd.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 "../vlc.h"
39 #include "../libs.h"
40 #include "input.h"
41
42 /*****************************************************************************
43  * OSD
44  *****************************************************************************/
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
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 = luaL_optint( L, 2, SPU_DEFAULT_CHANNEL );
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
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         { { SUBPICTURE_ALIGN_MASK,                          "center"       },
96           { SUBPICTURE_ALIGN_LEFT,                          "left"         },
97           { SUBPICTURE_ALIGN_RIGHT,                         "rigth"        },
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
114 static int vlclua_osd_message( lua_State *L )
115 {
116     const char *psz_message = luaL_checkstring( L, 1 );
117     int i_chan = luaL_optint( L, 2, SPU_DEFAULT_CHANNEL );
118     const char *psz_position = luaL_optstring( L, 3, "top-right" );
119     mtime_t duration = luaL_optint( 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
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
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 = luaL_optint( L, 3, SPU_DEFAULT_CHANNEL );
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
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
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 static int vlclua_menu_show( lua_State *L )
219 {
220     vlc_object_t *p_this = vlclua_get_this( L );
221     osd_MenuShow( p_this );
222     return 0;
223 }
224
225 static int vlclua_menu_hide( lua_State *L )
226 {
227     vlc_object_t *p_this = vlclua_get_this( L );
228     osd_MenuHide( p_this );
229     return 0;
230 }
231
232 static int vlclua_menu_prev( lua_State *L )
233 {
234     vlc_object_t *p_this = vlclua_get_this( L );
235     osd_MenuPrev( p_this );
236     return 0;
237 }
238
239 static int vlclua_menu_next( lua_State *L )
240 {
241     vlc_object_t *p_this = vlclua_get_this( L );
242     osd_MenuNext( p_this );
243     return 0;
244 }
245
246 static int vlclua_menu_up( lua_State *L )
247 {
248     vlc_object_t *p_this = vlclua_get_this( L );
249     osd_MenuUp( p_this );
250     return 0;
251 }
252
253 static int vlclua_menu_down( lua_State *L )
254 {
255     vlc_object_t *p_this = vlclua_get_this( L );
256     osd_MenuDown( p_this );
257     return 0;
258 }
259
260 static int vlclua_menu_activate( lua_State *L )
261 {
262     vlc_object_t *p_this = vlclua_get_this( L );
263     osd_MenuActivate( p_this );
264     return 0;
265 }
266
267 /*****************************************************************************
268  *
269  *****************************************************************************/
270 static const luaL_Reg vlclua_osd_reg[] = {
271     { "icon", vlclua_osd_icon },
272     { "message", vlclua_osd_message },
273     { "slider", vlclua_osd_slider },
274     { "channel_register", vlclua_spu_channel_register },
275     { "channel_clear", vlclua_spu_channel_clear },
276     { NULL, NULL }
277 };
278
279 static const luaL_Reg vlclua_menu_reg[] = {
280     { "show", vlclua_menu_show },
281     { "hide", vlclua_menu_hide },
282     { "prev", vlclua_menu_prev },
283     { "next", vlclua_menu_next },
284     { "up", vlclua_menu_up },
285     { "down", vlclua_menu_down },
286     { "activate", vlclua_menu_activate },
287     { NULL, NULL }
288 };
289
290 void luaopen_osd( lua_State *L )
291 {
292     lua_newtable( L );
293     luaL_register( L, NULL, vlclua_osd_reg );
294     lua_newtable( L );
295     luaL_register( L, NULL, vlclua_menu_reg );
296     lua_setfield( L, -2, "menu" );
297     lua_setfield( L, -2, "osd" );
298 }