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