]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/volume.c
23a0d5c84092d0e2e6ec5d274371c1f725dd3b07
[vlc] / modules / misc / lua / libs / volume.c
1 /*****************************************************************************
2  * volume.c
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_meta.h>
39 #include <vlc_charset.h>
40 #include <vlc_aout.h>
41
42 #include <lua.h>        /* Low level lua C API */
43 #include <lauxlib.h>    /* Higher level C API */
44 #include <lualib.h>     /* Lua libs */
45
46 #include "../vlc.h"
47 #include "../libs.h"
48 #include "playlist.h"
49
50 /*****************************************************************************
51  * Volume related
52  *****************************************************************************/
53 static int vlclua_volume_set( lua_State *L )
54 {
55     playlist_t *p_this = vlclua_get_playlist_internal( L );
56     int i_volume = luaL_checkint( L, 1 );
57     /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
58     int i_ret = aout_VolumeSet( p_this, i_volume );
59     vlclua_release_playlist_internal( p_this );
60     return vlclua_push_ret( L, i_ret );
61 }
62
63 static int vlclua_volume_get( lua_State *L )
64 {
65     playlist_t *p_this = vlclua_get_playlist_internal( L );
66     audio_volume_t i_volume;
67     if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
68         lua_pushnumber( L, i_volume );
69     else
70         lua_pushnil( L );
71     vlclua_release_playlist_internal( p_this );
72     return 1;
73 }
74
75 static int vlclua_volume_up( lua_State *L )
76 {
77     audio_volume_t i_volume;
78     playlist_t *p_this = vlclua_get_playlist_internal( L );
79     aout_VolumeUp( p_this, luaL_optint( L, 1, 1 ), &i_volume );
80     lua_pushnumber( L, i_volume );
81     vlclua_release_playlist_internal( p_this );
82     return 1;
83 }
84
85 static int vlclua_volume_down( lua_State *L )
86 {
87     audio_volume_t i_volume;
88     playlist_t *p_this = vlclua_get_playlist_internal( L );
89     aout_VolumeDown( p_this, luaL_optint( L, 1, 1 ), &i_volume );
90     lua_pushnumber( L, i_volume );
91     vlclua_release_playlist_internal( p_this );
92     return 1;
93 }
94
95 /*****************************************************************************
96  *
97  *****************************************************************************/
98 static const luaL_Reg vlclua_volume_reg[] = {
99     { "get", vlclua_volume_get },
100     { "set", vlclua_volume_set },
101     { "up", vlclua_volume_up },
102     { "down", vlclua_volume_down },
103     { NULL, NULL }
104 };
105
106 void luaopen_volume( lua_State *L )
107 {
108     lua_newtable( L );
109     luaL_register( L, NULL, vlclua_volume_reg );
110     lua_setfield( L, -2, "volume" );
111 }