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