]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/configuration.c
1ef90acc57c36d393eb157a85a63fc43831a123b
[vlc] / modules / misc / lua / libs / configuration.c
1 /*****************************************************************************
2  * configuration.c: Generic lua<->vlc config interface
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_common.h>
36
37 #include <lua.h>        /* Low level lua C API */
38 #include <lauxlib.h>    /* Higher level C API */
39
40 #include "../vlc.h"
41 #include "../libs.h"
42
43 /*****************************************************************************
44  * Config handling
45  *****************************************************************************/
46 static int vlclua_config_get( lua_State *L )
47 {
48     vlc_object_t * p_this = vlclua_get_this( L );
49     const char *psz_name;
50     psz_name = luaL_checkstring( L, 1 );
51     switch( config_GetType( p_this, psz_name ) )
52     {
53         case VLC_VAR_MODULE:
54         case VLC_VAR_STRING:
55         case VLC_VAR_FILE:
56         case VLC_VAR_DIRECTORY:
57             lua_pushstring( L, config_GetPsz( p_this, psz_name ) );
58             break;
59
60         case VLC_VAR_INTEGER:
61             lua_pushinteger( L, config_GetInt( p_this, psz_name ) );
62             break;
63
64         case VLC_VAR_BOOL:
65             lua_pushboolean( L, config_GetInt( p_this, psz_name ) );
66             break;
67
68         case VLC_VAR_FLOAT:
69             lua_pushnumber( L, config_GetFloat( p_this, psz_name ) );
70             break;
71
72         default:
73             return vlclua_error( L );
74
75     }
76     return 1;
77 }
78
79 static int vlclua_config_set( lua_State *L )
80 {
81     vlc_object_t *p_this = vlclua_get_this( L );
82     const char *psz_name;
83     psz_name = luaL_checkstring( L, 1 );
84     switch( config_GetType( p_this, psz_name ) )
85     {
86         case VLC_VAR_MODULE:
87         case VLC_VAR_STRING:
88         case VLC_VAR_FILE:
89         case VLC_VAR_DIRECTORY:
90             config_PutPsz( p_this, psz_name, luaL_checkstring( L, 2 ) );
91             break;
92
93         case VLC_VAR_INTEGER:
94             config_PutInt( p_this, psz_name, luaL_checkint( L, 2 ) );
95             break;
96
97         case VLC_VAR_BOOL:
98             config_PutInt( p_this, psz_name, luaL_checkboolean( L, 2 ) );
99             break;
100
101         case VLC_VAR_FLOAT:
102             config_PutFloat( p_this, psz_name,
103                              luaL_checknumber( L, 2 ) );
104             break;
105
106         default:
107             return vlclua_error( L );
108     }
109     return 0;
110 }
111
112 /*****************************************************************************
113  *
114  *****************************************************************************/
115 static const luaL_Reg vlclua_config_reg[] = {
116     { "get", vlclua_config_get },
117     { "set", vlclua_config_set },
118     { NULL, NULL }
119 };
120
121 void luaopen_config( lua_State *L )
122 {
123     lua_newtable( L );
124     luaL_register( L, NULL, vlclua_config_reg );
125     lua_setfield( L, -2, "config" );
126 }