]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/osd.c
lua_osd: add some parameters to the osd.message function to allow the user to set...
[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 #include "input.h"
44
45 /*****************************************************************************
46  * OSD
47  *****************************************************************************/
48 static int vlc_osd_icon_from_string( const char *psz_name )
49 {
50     static const struct
51     {
52         int i_icon;
53         const char *psz_name;
54     } pp_icons[] =
55         { { OSD_PAUSE_ICON, "pause" },
56           { OSD_PLAY_ICON, "play" },
57           { OSD_SPEAKER_ICON, "speaker" },
58           { OSD_MUTE_ICON, "mute" },
59           { 0, NULL } };
60     int i;
61     for( i = 0; pp_icons[i].psz_name; i++ )
62     {
63         if( !strcmp( psz_name, pp_icons[i].psz_name ) )
64             return pp_icons[i].i_icon;
65     }
66     return 0;
67 }
68
69 static int vlclua_osd_icon( lua_State *L )
70 {
71     const char *psz_icon = luaL_checkstring( L, 1 );
72     int i_icon = vlc_osd_icon_from_string( psz_icon );
73     int i_chan = luaL_optint( L, 2, SPU_DEFAULT_CHANNEL );
74     if( !i_icon )
75         return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );
76
77     input_thread_t *p_input = vlclua_get_input_internal( L );
78     if( p_input )
79     {
80         vout_thread_t *p_vout = input_GetVout( p_input );
81         if( p_vout )
82         {
83             vout_OSDIcon( p_vout, i_chan, i_icon );
84             vlc_object_release( p_vout );
85         }
86         vlc_object_release( p_input );
87     }
88     return 0;
89 }
90
91 static int vlc_osd_position_from_string( const char *psz_name )
92 {
93     static const struct
94     {
95         int i_position;
96         const char *psz_name;
97     } pp_icons[] =
98         { { SUBPICTURE_ALIGN_MASK,                          "center"       },
99           { SUBPICTURE_ALIGN_LEFT,                          "left"         },
100           { SUBPICTURE_ALIGN_RIGHT,                         "rigth"        },
101           { SUBPICTURE_ALIGN_TOP,                           "top"          },
102           { SUBPICTURE_ALIGN_BOTTOM,                        "bottom"       },
103           { SUBPICTURE_ALIGN_TOP   |SUBPICTURE_ALIGN_LEFT,  "top-left"     },
104           { SUBPICTURE_ALIGN_TOP   |SUBPICTURE_ALIGN_RIGHT, "top-right"    },
105           { SUBPICTURE_ALIGN_BOTTOM|SUBPICTURE_ALIGN_LEFT,  "bottom-left"  },
106           { SUBPICTURE_ALIGN_BOTTOM|SUBPICTURE_ALIGN_RIGHT, "bottom-right" },
107           { 0, NULL } };
108     int i;
109     for( i = 0; pp_icons[i].psz_name; i++ )
110     {
111         if( !strcmp( psz_name, pp_icons[i].psz_name ) )
112             return pp_icons[i].i_position;
113     }
114     return 0;
115 }
116
117 static int vlclua_osd_message( lua_State *L )
118 {
119     const char *psz_message = luaL_checkstring( L, 1 );
120     int i_chan = luaL_optint( L, 2, SPU_DEFAULT_CHANNEL );
121     const char *psz_position = luaL_optstring( L, 3, "top-right" );
122     mtime_t duration = luaL_optint( L, 4, 1000000 );
123
124     input_thread_t *p_input = vlclua_get_input_internal( L );
125     if( p_input )
126     {
127         vout_thread_t *p_vout = input_GetVout( p_input );
128         if( p_vout )
129         {
130             vout_OSDText( p_vout, i_chan, vlc_osd_position_from_string( psz_position ),
131                           duration, psz_message );
132             vlc_object_release( p_vout );
133         }
134         vlc_object_release( p_input );
135     }
136     return 0;
137 }
138
139 static int vlc_osd_slider_type_from_string( const char *psz_name )
140 {
141     static const struct
142     {
143         int i_type;
144         const char *psz_name;
145     } pp_types[] =
146         { { OSD_HOR_SLIDER, "horizontal" },
147           { OSD_VERT_SLIDER, "vertical" },
148           { 0, NULL } };
149     int i;
150     for( i = 0; pp_types[i].psz_name; i++ )
151     {
152         if( !strcmp( psz_name, pp_types[i].psz_name ) )
153             return pp_types[i].i_type;
154     }
155     return 0;
156 }
157
158 static int vlclua_osd_slider( lua_State *L )
159 {
160     int i_position = luaL_checkint( L, 1 );
161     const char *psz_type = luaL_checkstring( L, 2 );
162     int i_type = vlc_osd_slider_type_from_string( psz_type );
163     int i_chan = luaL_optint( L, 3, SPU_DEFAULT_CHANNEL );
164     if( !i_type )
165         return luaL_error( L, "\"%s\" is not a valid slider type.",
166                            psz_type );
167
168     input_thread_t *p_input = vlclua_get_input_internal( L );
169     if( p_input )
170     {
171         vout_thread_t *p_vout = input_GetVout( p_input );
172         if( p_vout )
173         {
174             vout_OSDSlider( p_vout, i_chan, i_position, i_type );
175             vlc_object_release( p_vout );
176         }
177         vlc_object_release( p_input );
178     }
179     return 0;
180 }
181
182 static int vlclua_spu_channel_register( lua_State *L )
183 {
184     input_thread_t *p_input = vlclua_get_input_internal( L );
185     if( !p_input )
186         return luaL_error( L, "Unable to find input." );
187
188     vout_thread_t *p_vout = input_GetVout( p_input );
189     if( !p_vout )
190     {
191         vlc_object_release( p_input );
192         return luaL_error( L, "Unable to find vout." );
193     }
194
195     int i_chan = vout_RegisterSubpictureChannel( p_vout );
196     vlc_object_release( p_vout );
197     vlc_object_release( p_input );
198     lua_pushinteger( L, i_chan );
199     return 1;
200 }
201
202 static int vlclua_spu_channel_clear( lua_State *L )
203 {
204     int i_chan = luaL_checkint( L, 1 );
205     input_thread_t *p_input = vlclua_get_input_internal( L );
206     if( !p_input )
207         return luaL_error( L, "Unable to find input." );
208     vout_thread_t *p_vout = input_GetVout( p_input );
209     if( !p_vout )
210     {
211         vlc_object_release( p_input );
212         return luaL_error( L, "Unable to find vout." );
213     }
214
215     vout_FlushSubpictureChannel( p_vout, i_chan );
216     vlc_object_release( p_vout );
217     vlc_object_release( p_input );
218     return 0;
219 }
220
221 static int vlclua_menu_show( lua_State *L )
222 {
223     vlc_object_t *p_this = vlclua_get_this( L );
224     osd_MenuShow( p_this );
225     return 0;
226 }
227
228 static int vlclua_menu_hide( lua_State *L )
229 {
230     vlc_object_t *p_this = vlclua_get_this( L );
231     osd_MenuHide( p_this );
232     return 0;
233 }
234
235 static int vlclua_menu_prev( lua_State *L )
236 {
237     vlc_object_t *p_this = vlclua_get_this( L );
238     osd_MenuPrev( p_this );
239     return 0;
240 }
241
242 static int vlclua_menu_next( lua_State *L )
243 {
244     vlc_object_t *p_this = vlclua_get_this( L );
245     osd_MenuNext( p_this );
246     return 0;
247 }
248
249 static int vlclua_menu_up( lua_State *L )
250 {
251     vlc_object_t *p_this = vlclua_get_this( L );
252     osd_MenuUp( p_this );
253     return 0;
254 }
255
256 static int vlclua_menu_down( lua_State *L )
257 {
258     vlc_object_t *p_this = vlclua_get_this( L );
259     osd_MenuDown( p_this );
260     return 0;
261 }
262
263 static int vlclua_menu_activate( lua_State *L )
264 {
265     vlc_object_t *p_this = vlclua_get_this( L );
266     osd_MenuActivate( p_this );
267     return 0;
268 }
269
270 /*****************************************************************************
271  *
272  *****************************************************************************/
273 static const luaL_Reg vlclua_osd_reg[] = {
274     { "icon", vlclua_osd_icon },
275     { "message", vlclua_osd_message },
276     { "slider", vlclua_osd_slider },
277     { "channel_register", vlclua_spu_channel_register },
278     { "channel_clear", vlclua_spu_channel_clear },
279     { NULL, NULL }
280 };
281
282 static const luaL_Reg vlclua_menu_reg[] = {
283     { "show", vlclua_menu_show },
284     { "hide", vlclua_menu_hide },
285     { "prev", vlclua_menu_prev },
286     { "next", vlclua_menu_next },
287     { "up", vlclua_menu_up },
288     { "down", vlclua_menu_down },
289     { "activate", vlclua_menu_activate },
290     { NULL, NULL }
291 };
292
293 void luaopen_osd( lua_State *L )
294 {
295     lua_newtable( L );
296     luaL_register( L, NULL, vlclua_osd_reg );
297     lua_newtable( L );
298     luaL_register( L, NULL, vlclua_menu_reg );
299     lua_setfield( L, -2, "menu" );
300     lua_setfield( L, -2, "osd" );
301 }