intf.c 9.79 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
static void *Run( void * );
54

55 56
static const char * const ppsz_intf_options[] = { "intf", "config", NULL };

57
/*****************************************************************************
58
 *
59
 *****************************************************************************/
60
static char *FindFile( vlc_object_t *p_this, const char *psz_name )
61 62 63
{
    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
    char **ppsz_dir;
64
    vlclua_dir_list( p_this, "intf", ppsz_dir_list );
65 66 67 68 69 70 71
    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 )
        {
72
            vlclua_dir_list_free( ppsz_dir_list );
73 74 75 76 77 78
            return NULL;
        }
        fp = fopen( psz_filename, "r" );
        if( fp )
        {
            fclose( fp );
79
            vlclua_dir_list_free( ppsz_dir_list );
80 81 82 83
            return psz_filename;
        }
        free( psz_filename );
    }
84
    vlclua_dir_list_free( ppsz_dir_list );
85 86 87 88 89 90 91 92 93 94 95
    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 );
}

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

111
static const char *WordInList( const char *psz_list, const char *psz_word )
112 113 114 115 116 117 118 119 120
{
    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] == ',' ) )
121
            return psz_str;
122 123
        psz_str = strstr( psz_str, psz_word );
    }
124
    return NULL;
125 126
}

Rémi Duraffort's avatar
Rémi Duraffort committed
127
static char *GetModuleName( intf_thread_t *p_intf )
128 129 130
{
    int i;
    const char *psz_intf;
131
    /*if( *p_intf->psz_intf == '$' )
132
        psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
133
    else*/
134
        psz_intf = p_intf->psz_intf;
135 136 137

    int i_candidate = -1;
    const char *psz_candidate = NULL;
138 139
    for( i = 0; pp_shortcuts[i].psz_name; i++ )
    {
140 141 142 143 144 145 146 147 148
        const char *psz_match;
        if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
        {
            if( !psz_candidate || psz_match < psz_candidate )
            {
                psz_candidate = psz_match;
                i_candidate = i;
            }
        }
149 150
    }

151 152 153
    if( i_candidate >= 0 )
        return strdup( pp_shortcuts[i_candidate].psz_name );

154
    return var_CreateGetString( p_intf, "lua-intf" );
155 156
}

157
static const luaL_Reg p_reg[] = { { NULL, NULL } };
158

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
159
int Open_LuaIntf( vlc_object_t *p_this )
160 161 162 163 164
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    intf_sys_t *p_sys;
    lua_State *L;

165
    config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
166 167 168 169 170 171
    char *psz_name = NULL;

    if( !p_intf->b_force )
        psz_name = strdup( "rc" );
    else
        psz_name = GetModuleName( p_intf );
Antoine Cellerier's avatar
Antoine Cellerier committed
172 173

    if( !psz_name ) psz_name = strdup( "dummy" );
174

Rémi Duraffort's avatar
Rémi Duraffort committed
175
    char *psz_config;
176
    bool b_config_set = false;
177

178
    p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
179 180
    if( !p_intf->p_sys )
    {
Rémi Duraffort's avatar
Rémi Duraffort committed
181
        free( psz_name );
182 183 184
        return VLC_ENOMEM;
    }
    p_sys = p_intf->p_sys;
185
    p_sys->psz_filename = FindFile( p_this, psz_name );
186 187 188 189
    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
190
        free( psz_name );
Rémi Duraffort's avatar
Rémi Duraffort committed
191
        free( p_sys );
192 193 194 195 196 197 198 199
        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
200
        free( p_sys->psz_filename );
Rémi Duraffort's avatar
Rémi Duraffort committed
201
        free( psz_name );
202 203 204 205
        free( p_sys );
        return VLC_EGENERIC;
    }

206
    luaL_openlibs( L );
207 208 209

    /* register our functions */
    luaL_register( L, "vlc", p_reg );
210 211

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

215
    /* register submodules */
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    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 );
234
    luaopen_gettext( L );
235

236 237 238 239 240 241 242 243 244 245 246
    /* 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,
247
                  "package.path = [[%s"DIR_SEP"modules"DIR_SEP"?.lua;]]..package.path",
248
                  p_sys->psz_filename ) < 0 )
Rémi Duraffort's avatar
Rémi Duraffort committed
249
    {
Rémi Duraffort's avatar
Rémi Duraffort committed
250
        free( p_sys->psz_filename );
Rémi Duraffort's avatar
Rémi Duraffort committed
251 252
        free( psz_name );
        free( p_sys );
253
        return VLC_EGENERIC;
Rémi Duraffort's avatar
Rémi Duraffort committed
254
    }
255 256
    *psz_char = DIR_SEP_CHAR;
    if( luaL_dostring( L, psz_command ) )
Rémi Duraffort's avatar
Rémi Duraffort committed
257
    {
Rémi Duraffort's avatar
Rémi Duraffort committed
258 259
        free( psz_command );
        free( p_sys->psz_filename );
Rémi Duraffort's avatar
Rémi Duraffort committed
260 261
        free( psz_name );
        free( p_sys );
262 263
        return VLC_EGENERIC;
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
264
    free( psz_command );
Rémi Duraffort's avatar
Rémi Duraffort committed
265
    }
266 267
    /* </gruik> */

268
    psz_config = var_CreateGetString( p_intf, "lua-config" );
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    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" );
285
                    b_config_set = true;
286 287 288 289
                }
            }
        }
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
290 291
    free( psz_config );

292
    if( b_config_set == false )
293 294 295 296 297 298 299
    {
        lua_newtable( L );
        lua_setglobal( L, "config" );
    }

    p_sys->L = L;

300 301 302 303 304 305 306 307 308 309 310 311 312
    p_intf->psz_header = psz_name;
    /* ^^ Do I need to clean that up myself in Close_LuaIntf? */

    vlc_mutex_init( &p_sys->lock );
    vlc_cond_init( &p_sys->wait );
    p_sys->exiting = false;

    if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
    {
        p_sys->exiting = true;
        Close_LuaIntf( p_this );
        return VLC_ENOMEM;
    }
313 314 315 316

    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
317
void Close_LuaIntf( vlc_object_t *p_this )
318 319
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
320 321
    intf_sys_t *p_sys = p_intf->p_sys;

322 323
    vlc_cancel( p_sys->thread );

324 325 326 327 328 329 330 331 332 333
    if( !p_sys->exiting ) /* <- Read-only here and in thread: no locking */
    {
        vlc_mutex_lock( &p_sys->lock );
        p_sys->exiting = true;
        vlc_cond_signal( &p_sys->wait );
        vlc_mutex_unlock( &p_sys->lock );
        vlc_join( p_sys->thread, NULL );
    }
    vlc_cond_destroy( &p_sys->wait );
    vlc_mutex_destroy( &p_sys->lock );
334

335
    lua_close( p_sys->L );
Rémi Duraffort's avatar
Rémi Duraffort committed
336 337

    free( p_sys->psz_filename );
338
    free( p_sys );
339 340
}

341
static void *Run( void *data )
342
{
343 344 345
    intf_thread_t *p_intf = data;
    intf_sys_t *p_sys = p_intf->p_sys;
    lua_State *L = p_sys->L;
346

347
    if( luaL_dofile( L, p_sys->psz_filename ) )
348
    {
349
        msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
350 351 352
                 lua_tostring( L, lua_gettop( L ) ) );
        lua_pop( L, 1 );
    }
353
    return NULL;
354
}