]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/osd.c
86388767a2ddfdf460dcd571115a69cd34638a92
[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 vlclua_osd_message( lua_State *L )
92 {
93     const char *psz_message = luaL_checkstring( L, 1 );
94     int i_chan = luaL_optint( L, 2, SPU_DEFAULT_CHANNEL );
95
96     input_thread_t *p_input = vlclua_get_input_internal( L );
97     if( p_input )
98     {
99         vout_thread_t *p_vout = input_GetVout( p_input );
100         if( p_vout )
101         {
102             vout_OSDMessage( p_vout, i_chan, "%s", psz_message );
103             vlc_object_release( p_vout );
104         }
105         vlc_object_release( p_input );
106     }
107     return 0;
108 }
109
110 static int vlc_osd_slider_type_from_string( const char *psz_name )
111 {
112     static const struct
113     {
114         int i_type;
115         const char *psz_name;
116     } pp_types[] =
117         { { OSD_HOR_SLIDER, "horizontal" },
118           { OSD_VERT_SLIDER, "vertical" },
119           { 0, NULL } };
120     int i;
121     for( i = 0; pp_types[i].psz_name; i++ )
122     {
123         if( !strcmp( psz_name, pp_types[i].psz_name ) )
124             return pp_types[i].i_type;
125     }
126     return 0;
127 }
128
129 static int vlclua_osd_slider( lua_State *L )
130 {
131     int i_position = luaL_checkint( L, 1 );
132     const char *psz_type = luaL_checkstring( L, 2 );
133     int i_type = vlc_osd_slider_type_from_string( psz_type );
134     int i_chan = luaL_optint( L, 3, SPU_DEFAULT_CHANNEL );
135     if( !i_type )
136         return luaL_error( L, "\"%s\" is not a valid slider type.",
137                            psz_type );
138
139     input_thread_t *p_input = vlclua_get_input_internal( L );
140     if( p_input )
141     {
142         vout_thread_t *p_vout = input_GetVout( p_input );
143         if( p_vout )
144         {
145             vout_OSDSlider( p_vout, i_chan, i_position, i_type );
146             vlc_object_release( p_vout );
147         }
148         vlc_object_release( p_input );
149     }
150     return 0;
151 }
152
153 static int vlclua_spu_channel_register( lua_State *L )
154 {
155     input_thread_t *p_input = vlclua_get_input_internal( L );
156     if( !p_input )
157         return luaL_error( L, "Unable to find input." );
158
159     vout_thread_t *p_vout = input_GetVout( p_input );
160     if( !p_vout )
161     {
162         vlc_object_release( p_input );
163         return luaL_error( L, "Unable to find vout." );
164     }
165
166     int i_chan = vout_RegisterSubpictureChannel( p_vout );
167     vlc_object_release( p_vout );
168     vlc_object_release( p_input );
169     lua_pushinteger( L, i_chan );
170     return 1;
171 }
172
173 static int vlclua_spu_channel_clear( lua_State *L )
174 {
175     int i_chan = luaL_checkint( L, 1 );
176     input_thread_t *p_input = vlclua_get_input_internal( L );
177     if( !p_input )
178         return luaL_error( L, "Unable to find input." );
179     vout_thread_t *p_vout = input_GetVout( p_input );
180     if( !p_vout )
181     {
182         vlc_object_release( p_input );
183         return luaL_error( L, "Unable to find vout." );
184     }
185
186     vout_FlushSubpictureChannel( p_vout, i_chan );
187     vlc_object_release( p_vout );
188     vlc_object_release( p_input );
189     return 0;
190 }
191
192 static int vlclua_menu_show( lua_State *L )
193 {
194     vlc_object_t *p_this = vlclua_get_this( L );
195     osd_MenuShow( p_this );
196     return 0;
197 }
198
199 static int vlclua_menu_hide( lua_State *L )
200 {
201     vlc_object_t *p_this = vlclua_get_this( L );
202     osd_MenuHide( p_this );
203     return 0;
204 }
205
206 static int vlclua_menu_prev( lua_State *L )
207 {
208     vlc_object_t *p_this = vlclua_get_this( L );
209     osd_MenuPrev( p_this );
210     return 0;
211 }
212
213 static int vlclua_menu_next( lua_State *L )
214 {
215     vlc_object_t *p_this = vlclua_get_this( L );
216     osd_MenuNext( p_this );
217     return 0;
218 }
219
220 static int vlclua_menu_up( lua_State *L )
221 {
222     vlc_object_t *p_this = vlclua_get_this( L );
223     osd_MenuUp( p_this );
224     return 0;
225 }
226
227 static int vlclua_menu_down( lua_State *L )
228 {
229     vlc_object_t *p_this = vlclua_get_this( L );
230     osd_MenuDown( p_this );
231     return 0;
232 }
233
234 static int vlclua_menu_activate( lua_State *L )
235 {
236     vlc_object_t *p_this = vlclua_get_this( L );
237     osd_MenuActivate( p_this );
238     return 0;
239 }
240
241 /*****************************************************************************
242  *
243  *****************************************************************************/
244 static const luaL_Reg vlclua_osd_reg[] = {
245     { "icon", vlclua_osd_icon },
246     { "message", vlclua_osd_message },
247     { "slider", vlclua_osd_slider },
248     { "channel_register", vlclua_spu_channel_register },
249     { "channel_clear", vlclua_spu_channel_clear },
250     { NULL, NULL }
251 };
252
253 static const luaL_Reg vlclua_menu_reg[] = {
254     { "show", vlclua_menu_show },
255     { "hide", vlclua_menu_hide },
256     { "prev", vlclua_menu_prev },
257     { "next", vlclua_menu_next },
258     { "up", vlclua_menu_up },
259     { "down", vlclua_menu_down },
260     { "activate", vlclua_menu_activate },
261     { NULL, NULL }
262 };
263
264 void luaopen_osd( lua_State *L )
265 {
266     lua_newtable( L );
267     luaL_register( L, NULL, vlclua_osd_reg );
268     lua_newtable( L );
269     luaL_register( L, NULL, vlclua_menu_reg );
270     lua_setfield( L, -2, "menu" );
271     lua_setfield( L, -2, "osd" );
272 }