interface.c 6.17 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2 3 4
 * interface.c: interface access for other threads
 * This library provides basic functions for threads to interact with user
 * interface, such as command line.
5
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
6
 * Copyright (C) 1998-2007 VLC authors and VideoLAN
Gildas Bazin's avatar
Gildas Bazin committed
7
 * $Id$
8
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
9
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
14
 * (at your option) any later version.
Clément Stenac's avatar
Clément Stenac committed
15
 *
16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * You should have received a copy of the GNU Lesser 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.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

Clément Stenac's avatar
Clément Stenac committed
26 27 28 29 30 31
/**
 *   \file
 *   This file contains functions related to interface management
 */


32
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
33
 * Preamble
34
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
35

36 37 38 39
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

40
#include <assert.h>
41 42 43 44
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

45
#include <vlc_common.h>
46 47
#include <vlc_modules.h>
#include <vlc_interface.h>
48
#include "libvlc.h"
Michel Kaempf's avatar
Michel Kaempf committed
49

50 51 52 53
#ifdef __APPLE__
#include "../lib/libvlc_internal.h"
#endif

54
/*****************************************************************************
55
 * Local prototypes
56
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
57
static void* RunInterface( void * );
Gildas Bazin's avatar
 
Gildas Bazin committed
58 59
static int AddIntfCallback( vlc_object_t *, char const *,
                            vlc_value_t , vlc_value_t , void * );
60

61 62
static vlc_mutex_t lock = VLC_STATIC_MUTEX;

63
#undef intf_Create
Clément Stenac's avatar
Clément Stenac committed
64
/**
65
 * Create and start an interface.
Clément Stenac's avatar
Clément Stenac committed
66
 *
Rémi Duraffort's avatar
Rémi Duraffort committed
67
 * @param p_this the calling vlc_object_t
68
 * @param chain configuration chain string
69
 * @return VLC_SUCCESS or an error code
Clément Stenac's avatar
Clément Stenac committed
70
 */
71
int intf_Create( vlc_object_t *p_this, const char *chain )
72
{
73
    libvlc_int_t *p_libvlc = p_this->p_libvlc;
74
    intf_thread_t * p_intf;
Michel Kaempf's avatar
Michel Kaempf committed
75

Vincent Seguin's avatar
Vincent Seguin committed
76
    /* Allocate structure */
77
    p_intf = vlc_custom_create( p_libvlc, sizeof( *p_intf ), "interface" );
Vincent Seguin's avatar
Vincent Seguin committed
78
    if( !p_intf )
79 80
        return VLC_ENOMEM;

81 82 83 84 85 86
    /* Variable used for interface spawning */
    vlc_value_t val, text;
    var_Create( p_intf, "intf-add", VLC_VAR_STRING |
                VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
    text.psz_string = _("Add Interface");
    var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
87
#if !defined(WIN32) && defined(HAVE_ISATTY)
88
    if( isatty( 0 ) )
89
#endif
90
    {
91
        val.psz_string = (char *)"rc";
92 93 94
        text.psz_string = (char *)_("Console");
        var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
    }
95
    val.psz_string = (char *)"telnet";
96
    text.psz_string = (char *)_("Telnet");
97 98
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
    val.psz_string = (char *)"http";
99
    text.psz_string = (char *)_("Web");
100 101 102 103 104 105 106 107 108 109
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
    val.psz_string = (char *)"logger";
    text.psz_string = (char *)_("Debug logging");
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
    val.psz_string = (char *)"gestures";
    text.psz_string = (char *)_("Mouse Gestures");
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );

    var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );

110 111
    /* Attach interface to LibVLC */

Sam Hocevar's avatar
 
Sam Hocevar committed
112
    /* Choose the best module */
113
    p_intf->p_cfg = NULL;
114 115 116 117 118
    char *psz_parser = *chain == '$'
                     ? var_CreateGetString(p_intf, chain+1)
                     : strdup( chain );
    char *module;
    char *psz_tmp = config_ChainCreate( &module, &p_intf->p_cfg,
119 120 121
                                        psz_parser );
    free( psz_tmp );
    free( psz_parser );
122
    p_intf->p_module = module_need( p_intf, "interface", module, true );
Rafaël Carré's avatar
Rafaël Carré committed
123
    free(module);
Sam Hocevar's avatar
 
Sam Hocevar committed
124
    if( p_intf->p_module == NULL )
125
    {
126
        msg_Err( p_intf, "no suitable interface module" );
127
        goto error;
128
    }
129

130
    vlc_mutex_lock( &lock );
131 132 133 134
    p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
    libvlc_priv( p_libvlc )->p_intf = p_intf;
    vlc_mutex_unlock( &lock );

135
    return VLC_SUCCESS;
136 137 138 139 140 141 142

error:
    if( p_intf->p_module )
        module_unneed( p_intf, p_intf->p_module );
    config_ChainDestroy( p_intf->p_cfg );
    vlc_object_release( p_intf );
    return VLC_EGENERIC;
143 144
}

Rémi Duraffort's avatar
Rémi Duraffort committed
145

Clément Stenac's avatar
Clément Stenac committed
146
/**
147 148
 * Stops and destroys all interfaces
 * @param p_libvlc the LibVLC instance
Clément Stenac's avatar
Clément Stenac committed
149
 */
150
void intf_DestroyAll( libvlc_int_t *p_libvlc )
151
{
152 153 154 155 156 157 158 159
    intf_thread_t *p_first;

    vlc_mutex_lock( &lock );
    p_first = libvlc_priv( p_libvlc )->p_intf;
#ifndef NDEBUG
    libvlc_priv( p_libvlc )->p_intf = NULL;
#endif
    vlc_mutex_unlock( &lock );
160 161

    /* Tell the interfaces to die */
162 163
    for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
        vlc_object_kill( p_intf );
164 165

    /* Cleanup the interfaces */
166
    for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
167
    {
168
        intf_thread_t *p_next = p_intf->p_next;
169 170 171

        module_unneed( p_intf, p_intf->p_module );
        config_ChainDestroy( p_intf->p_cfg );
172
        vlc_object_release( p_intf );
173

174 175
        p_intf = p_next;
    }
176
}
177

Gildas Bazin's avatar
 
Gildas Bazin committed
178
/* Following functions are local */
Clément Stenac's avatar
Clément Stenac committed
179

Gildas Bazin's avatar
 
Gildas Bazin committed
180 181 182
static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
183
    (void)psz_cmd; (void)oldval; (void)p_data;
Rémi Duraffort's avatar
Rémi Duraffort committed
184
    char* psz_intf;
185

Gildas Bazin's avatar
 
Gildas Bazin committed
186
    /* Try to create the interface */
Rémi Duraffort's avatar
Rémi Duraffort committed
187 188 189
    if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
        return VLC_ENOMEM;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
190
    int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
191
    free( psz_intf );
192
    if( ret )
Gildas Bazin's avatar
 
Gildas Bazin committed
193 194
        msg_Err( p_this, "interface \"%s\" initialization failed",
                 newval.psz_string );
195
    return ret;
Gildas Bazin's avatar
 
Gildas Bazin committed
196
}