intf.c 8.29 KB
Newer Older
1
/*****************************************************************************
2
 * intf.c: Generic lua interface functions
3
 *****************************************************************************
4
 * Copyright (C) 2007-2008 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea at videolan tod org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifndef  _GNU_SOURCE
#   define  _GNU_SOURCE
#endif

31 32 33 34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
36 37 38 39 40 41 42 43 44 45 46 47
#include <vlc_meta.h>
#include <vlc_charset.h>

#include <vlc_interface.h>
#include <vlc_playlist.h>
#include <vlc_aout.h>

#include <lua.h>        /* Low level lua C API */
#include <lauxlib.h>    /* Higher level C API */
#include <lualib.h>     /* Lua libs */

#include "vlc.h"
48
#include "libs.h"
49

50 51 52
/*****************************************************************************
 * Prototypes
 *****************************************************************************/
53 54 55 56 57 58
struct intf_sys_t
{
    char *psz_filename;
    lua_State *L;
};

59
static void Run( intf_thread_t *p_intf );
60 61

/*****************************************************************************
62
 *
63
 *****************************************************************************/
64
static char *FindFile( const char *psz_name )
65 66 67
{
    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
    char **ppsz_dir;
68
    vlclua_dir_list( "intf", ppsz_dir_list );
69 70 71 72 73 74 75
    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
    {
        char *psz_filename;
        FILE *fp;
        if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
                      psz_name ) < 0 )
        {
76
            vlclua_dir_list_free( ppsz_dir_list );
77 78 79 80 81 82
            return NULL;
        }
        fp = fopen( psz_filename, "r" );
        if( fp )
        {
            fclose( fp );
83
            vlclua_dir_list_free( ppsz_dir_list );
84 85 86 87
            return psz_filename;
        }
        free( psz_filename );
    }
88
    vlclua_dir_list_free( ppsz_dir_list );
89 90 91 92 93 94 95 96 97 98 99
    return NULL;
}

static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
                                            const luaL_Reg *l )
{
    lua_newtable( L );
    luaL_register( L, NULL, l );
    lua_setfield( L, -2, psz_name );
}

100
static const struct
101 102 103 104 105 106 107 108 109 110
{
    const char *psz_shortcut;
    const char *psz_name;
} pp_shortcuts[] = {
    { "luarc", "rc" },
    /* { "rc", "rc" }, */
    { "luahotkeys", "hotkeys" },
    /* { "hotkeys", "hotkeys" }, */
    { "luatelnet", "telnet" },
    /* { "telnet", "telnet" }, */
Antoine Cellerier's avatar
Antoine Cellerier committed
111 112
    { "luahttp", "http" },
    /* { "http", "http" }, */
113 114
    { NULL, NULL } };

115
static bool WordInList( const char *psz_list, const char *psz_word )
116 117 118 119 120 121 122 123 124
{
    const char *psz_str = strstr( psz_list, psz_word );
    int i_len = strlen( psz_word );
    while( psz_str )
    {
        if( (psz_str == psz_list || *(psz_str-1) == ',' )
         /* it doesn't start in middle of a word */
         /* it doest end in middle of a word */
         && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
125
            return true;
126 127
        psz_str = strstr( psz_str, psz_word );
    }
128
    return false;
129 130
}

Rémi Duraffort's avatar
Rémi Duraffort committed
131
static char *GetModuleName( intf_thread_t *p_intf )
132 133 134 135 136 137 138 139 140 141
{
    int i;
    const char *psz_intf;
    if( *p_intf->psz_intf == '$' )
        psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
    else
        psz_intf = p_intf->psz_intf;
    for( i = 0; pp_shortcuts[i].psz_name; i++ )
    {
        if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
Rémi Duraffort's avatar
Rémi Duraffort committed
142
            return strdup( pp_shortcuts[i].psz_name );
143 144 145 146 147
    }

    return config_GetPsz( p_intf, "lua-intf" );
}

148
static const luaL_Reg p_reg[] = { { NULL, NULL } };
149

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
150
int Open_LuaIntf( vlc_object_t *p_this )
151 152 153 154 155
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    intf_sys_t *p_sys;
    lua_State *L;

Rémi Duraffort's avatar
Rémi Duraffort committed
156
    char *psz_name = GetModuleName( p_intf );
157
    const char *psz_config;
158
    bool b_config_set = false;
Rémi Duraffort's avatar
Rémi Duraffort committed
159
    if( !psz_name ) psz_name = strdup( "dummy" );
160 161 162 163

    p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
    if( !p_intf->p_sys )
    {
Rémi Duraffort's avatar
Rémi Duraffort committed
164
        free( psz_name );
165 166 167
        return VLC_ENOMEM;
    }
    p_sys = p_intf->p_sys;
168
    p_sys->psz_filename = FindFile( psz_name );
169 170 171 172
    if( !p_sys->psz_filename )
    {
        msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
                 psz_name );
Rémi Duraffort's avatar
Rémi Duraffort committed
173
        free( psz_name );
Rémi Duraffort's avatar
Rémi Duraffort committed
174
        free( p_sys );
175 176 177 178 179 180 181 182
        return VLC_EGENERIC;
    }
    msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );

    L = luaL_newstate();
    if( !L )
    {
        msg_Err( p_intf, "Could not create new Lua State" );
Rémi Duraffort's avatar
Rémi Duraffort committed
183
        free( psz_name );
184 185 186 187
        free( p_sys );
        return VLC_EGENERIC;
    }

188
    luaL_openlibs( L );
189 190 191

    /* register our functions */
    luaL_register( L, "vlc", p_reg );
192 193

    /* store a pointer to p_intf (FIXME: user could overwrite this) */
194 195
    lua_pushlightuserdata( L, p_intf );
    lua_setfield( L, -2, "private" );
196

197
    /* register submodules */
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    luaopen_acl( L );
    luaopen_config( L );
    luaopen_volume( L );
    luaopen_httpd( L );
    luaopen_input( L );
    luaopen_msg( L );
    luaopen_misc( L );
    luaopen_net( L );
    luaopen_object( L );
    luaopen_osd( L );
    luaopen_playlist( L );
    luaopen_sd( L );
    luaopen_stream( L );
    luaopen_strings( L );
    luaopen_variables( L );
    luaopen_video( L );
    luaopen_vlm( L );
    luaopen_volume( L );

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    /* clean up */
    lua_pop( L, 1 );

    /* <gruik> */
    /* Setup the module search path */
    {
    char *psz_command;
    char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
    *psz_char = '\0';
    /* FIXME: don't use luaL_dostring */
    if( asprintf( &psz_command,
                  "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
                  p_sys->psz_filename ) < 0 )
        return VLC_EGENERIC;
    *psz_char = DIR_SEP_CHAR;
    if( luaL_dostring( L, psz_command ) )
        return VLC_EGENERIC;
    }
    /* </gruik> */

    psz_config = config_GetPsz( p_intf, "lua-config" );
    if( psz_config && *psz_config )
    {
        char *psz_buffer;
        if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
        {
            printf("%s\n", psz_buffer);
            if( luaL_dostring( L, psz_buffer ) == 1 )
                msg_Err( p_intf, "Error while parsing \"lua-config\"." );
            free( psz_buffer );
            lua_getglobal( L, "config" );
            if( lua_istable( L, -1 ) )
            {
                lua_getfield( L, -1, psz_name );
                if( lua_istable( L, -1 ) )
                {
                    lua_setglobal( L, "config" );
254
                    b_config_set = true;
255 256 257 258
                }
            }
        }
    }
259
    if( b_config_set == false )
260 261 262 263 264 265 266 267
    {
        lua_newtable( L );
        lua_setglobal( L, "config" );
    }

    p_sys->L = L;

    p_intf->pf_run = Run;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
268
    p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in Close_LuaIntf? */
269

Rémi Duraffort's avatar
Rémi Duraffort committed
270
    free( psz_name );
271 272 273
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
274
void Close_LuaIntf( vlc_object_t *p_this )
275 276 277 278 279 280 281 282 283
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;

    lua_close( p_intf->p_sys->L );
    free( p_intf->p_sys );
}

static void Run( intf_thread_t *p_intf )
{
284
    int canc = vlc_savecancel( );
285 286 287 288 289 290 291 292 293
    lua_State *L = p_intf->p_sys->L;

    if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
    {
        msg_Err( p_intf, "Error loading script %s: %s",
                 p_intf->p_sys->psz_filename,
                 lua_tostring( L, lua_gettop( L ) ) );
        lua_pop( L, 1 );
    }
294
    vlc_restorecancel( canc );
295
}