interface.c 12.3 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
 *****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
6
 * Copyright (C) 1998-2004 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 11 12 13 14
 *
 * 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.
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
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21 22 23
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
 *****************************************************************************/
35
#include <stdlib.h>                                      /* free(), strtol() */
36
#include <stdio.h>                                                   /* FILE */
37
#include <string.h>                                            /* strerror() */
Christophe Massiot's avatar
Christophe Massiot committed
38

39
#include <vlc/vlc.h>
Laurent Aimar's avatar
Laurent Aimar committed
40
#include <vlc/input.h>
41

Renaud Dartus's avatar
 
Renaud Dartus committed
42 43
#include "audio_output.h"

44
#include "vlc_interface.h"
45

46
#include "vlc_video.h"
Vincent Seguin's avatar
Vincent Seguin committed
47
#include "video_output.h"
Michel Kaempf's avatar
Michel Kaempf committed
48

49
/*****************************************************************************
50
 * Local prototypes
51
 *****************************************************************************/
52
static void Manager( intf_thread_t *p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
53 54 55 56 57 58
static void RunInterface( intf_thread_t *p_intf );

static int SwitchIntfCallback( vlc_object_t *, char const *,
                               vlc_value_t , vlc_value_t , void * );
static int AddIntfCallback( vlc_object_t *, char const *,
                            vlc_value_t , vlc_value_t , void * );
59

60
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
61
 * intf_Create: prepare interface before main loop
62
 *****************************************************************************
63 64
 * This function opens output devices and creates specific interfaces. It sends
 * its own error messages.
65
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
66 67
/**
 * Create the interface, and prepare it for main loop.
Clément Stenac's avatar
Clément Stenac committed
68
 *
Clément Stenac's avatar
Clément Stenac committed
69 70 71 72
 * \param p_this the calling vlc_object_t
 * \param psz_module a prefered interface module
 * \return a pointer to the created interface thread, NULL on error
 */
73
intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
74
{
75
    intf_thread_t * p_intf;
Michel Kaempf's avatar
Michel Kaempf committed
76

Vincent Seguin's avatar
Vincent Seguin committed
77
    /* Allocate structure */
78
    p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
Vincent Seguin's avatar
Vincent Seguin committed
79 80
    if( !p_intf )
    {
81 82
        msg_Err( p_this, "out of memory" );
        return NULL;
83
    }
Gildas Bazin's avatar
Gildas Bazin committed
84 85
    p_intf->pf_request_window = NULL;
    p_intf->pf_release_window = NULL;
86
    p_intf->pf_control_window = NULL;
87
    p_intf->b_play = VLC_FALSE;
88

Sam Hocevar's avatar
 
Sam Hocevar committed
89
    /* Choose the best module */
Gildas Bazin's avatar
 
Gildas Bazin committed
90
    p_intf->p_module = module_Need( p_intf, "interface", psz_module, 0 );
91

Sam Hocevar's avatar
 
Sam Hocevar committed
92
    if( p_intf->p_module == NULL )
93
    {
94 95 96
        msg_Err( p_intf, "no suitable intf module" );
        vlc_object_destroy( p_intf );
        return NULL;
97
    }
98

99
    /* Initialize structure */
Sam Hocevar's avatar
Sam Hocevar committed
100 101
    p_intf->b_menu        = VLC_FALSE;
    p_intf->b_menu_change = VLC_FALSE;
102

Sam Hocevar's avatar
 
Sam Hocevar committed
103
    /* Initialize mutexes */
104 105 106
    vlc_mutex_init( p_intf, &p_intf->change_lock );

    msg_Dbg( p_intf, "interface initialized" );
Sam Hocevar's avatar
 
Sam Hocevar committed
107

Sam Hocevar's avatar
Sam Hocevar committed
108 109
    /* Attach interface to its parent object */
    vlc_object_attach( p_intf, p_this );
110 111

    return p_intf;
112 113 114
}

/*****************************************************************************
115
 * intf_RunThread: launch the interface thread
116
 *****************************************************************************
117 118
 * This function either creates a new thread and runs the interface in it,
 * or runs the interface in the current thread, depending on b_block.
119
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
120 121 122
/**
 * Run the interface thread.
 *
Clément Stenac's avatar
Clément Stenac committed
123
 * If b_block is not set, runs the interface in the thread, else,
Clément Stenac's avatar
Clément Stenac committed
124 125 126 127
 * creates a new thread and runs the interface.
 * \param p_intf the interface thread
 * \return VLC_SUCCESS on success, an error number else
 */
Sam Hocevar's avatar
Sam Hocevar committed
128
int intf_RunThread( intf_thread_t *p_intf )
129
{
130
    if( p_intf->b_block )
Sam Hocevar's avatar
 
Sam Hocevar committed
131
    {
132
        /* Run a manager thread, launch the interface, kill the manager */
133 134
        if( vlc_thread_create( p_intf, "manager", Manager,
                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
135
        {
136 137
            msg_Err( p_intf, "cannot spawn manager thread" );
            return VLC_EGENERIC;
Sam Hocevar's avatar
 
Sam Hocevar committed
138
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
139

Gildas Bazin's avatar
 
Gildas Bazin committed
140
        RunInterface( p_intf );
141

Sam Hocevar's avatar
Sam Hocevar committed
142
        p_intf->b_die = VLC_TRUE;
143 144

        /* Do not join the thread... intf_StopThread will do it for us */
Sam Hocevar's avatar
 
Sam Hocevar committed
145 146
    }
    else
147
    {
148
        /* Run the interface in a separate thread */
Gildas Bazin's avatar
 
Gildas Bazin committed
149
        if( vlc_thread_create( p_intf, "interface", RunInterface,
150
                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
151
        {
152 153
            msg_Err( p_intf, "cannot spawn interface thread" );
            return VLC_EGENERIC;
Sam Hocevar's avatar
 
Sam Hocevar committed
154
        }
155
    }
Stéphane Borel's avatar
Stéphane Borel committed
156

157 158 159
    return VLC_SUCCESS;
}

Clément Stenac's avatar
Clément Stenac committed
160 161 162 163 164 165 166
/**
 * Stops the interface thread
 *
 * This function asks the interface thread to stop
 * \param p_intf the interface thread
 * \return nothing
 */
167 168 169 170 171
void intf_StopThread( intf_thread_t *p_intf )
{
    /* Tell the interface to die */
    if( !p_intf->b_block )
    {
Sam Hocevar's avatar
Sam Hocevar committed
172
        p_intf->b_die = VLC_TRUE;
173
    }
174 175 176

    /* Wait for the thread to exit */
    vlc_thread_join( p_intf );
177
}
178

Clément Stenac's avatar
Clément Stenac committed
179 180
/**
 * \brief Destroy the interface after the main loop endeed.
Clément Stenac's avatar
Clément Stenac committed
181 182
 *
 * Destroys interfaces and closes output devices
Clément Stenac's avatar
Clément Stenac committed
183 184 185
 * \param p_intf the interface thread
 * \return nothing
 */
186 187
void intf_Destroy( intf_thread_t *p_intf )
{
Sam Hocevar's avatar
Sam Hocevar committed
188 189 190 191 192
    /* Unlock module if present (a switch may have failed) */
    if( p_intf->p_module )
    {
        module_Unneed( p_intf, p_intf->p_module );
    }
193

Sam Hocevar's avatar
 
Sam Hocevar committed
194
    vlc_mutex_destroy( &p_intf->change_lock );
195

Sam Hocevar's avatar
 
Sam Hocevar committed
196
    /* Free structure */
197 198 199
    vlc_object_destroy( p_intf );
}

Clément Stenac's avatar
Clément Stenac committed
200

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

203 204 205 206 207 208 209
/*****************************************************************************
 * Manager: helper thread for blocking interfaces
 *****************************************************************************
 * If the interface is launched in the main thread, it will not listen to
 * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
 * This thread takes care of the matter.
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
210 211 212 213 214 215 216 217 218 219 220 221
/**
 * \brief Helper thread for blocking interfaces.
 * \ingroup vlc_interface
 *
 * This is a local function
 * If the interface is launched in the main thread, it will not listen to
 * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
 * This thread takes care of the matter.
 * \see intf_RunThread
 * \param p_intf an interface thread
 * \return nothing
 */
222 223 224 225 226 227 228 229
static void Manager( intf_thread_t *p_intf )
{
    while( !p_intf->b_die )
    {
        msleep( INTF_IDLE_SLEEP );

        if( p_intf->p_vlc->b_die )
        {
Sam Hocevar's avatar
Sam Hocevar committed
230
            p_intf->b_die = VLC_TRUE;
231 232 233
            return;
        }
    }
234
}
235

Gildas Bazin's avatar
 
Gildas Bazin committed
236 237 238 239 240
/*****************************************************************************
 * RunInterface: setups necessary data and give control to the interface
 *****************************************************************************/
static void RunInterface( intf_thread_t *p_intf )
{
Sam Hocevar's avatar
Sam Hocevar committed
241 242 243 244 245 246 247 248 249 250 251
    static char *ppsz_interfaces[] =
    {
        "skins", "Skins",
        "skins2", "Skins 2",
        "wxwindows", "wxWindows",
        NULL, NULL
    };
    char **ppsz_parser;

    vlc_list_t *p_list;
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
252
    vlc_value_t val, text;
Sam Hocevar's avatar
Sam Hocevar committed
253
    char *psz_intf;
Gildas Bazin's avatar
 
Gildas Bazin committed
254 255 256 257 258 259 260 261

    /* Variable used for interface switching */
    p_intf->psz_switch_intf = NULL;
    var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
                VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
    text.psz_string = _("Switch interface");
    var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );

Sam Hocevar's avatar
Sam Hocevar committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    /* Only fill the list with available modules */
    p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
    {
        for( i = 0; i < p_list->i_count; i++ )
        {
            module_t *p_module = (module_t *)p_list->p_values[i].p_object;
            if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
            {
                val.psz_string = ppsz_parser[0];
                text.psz_string = ppsz_parser[1];
                var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
                            &val, &text );
                break;
            }
        }
    }
    vlc_list_release( p_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
280 281 282 283 284 285

    var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );

    /* Variable used for interface spawning */
    var_Create( p_intf, "intf-add", VLC_VAR_STRING |
                VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
286
    text.psz_string = _("Add Interface");
Gildas Bazin's avatar
 
Gildas Bazin committed
287 288 289 290
    var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );

    val.psz_string = "rc"; text.psz_string = "Console";
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
291 292 293
    val.psz_string = "telnet"; text.psz_string = "Telnet Interface";
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
    val.psz_string = "http"; text.psz_string = "Web Interface";
Gildas Bazin's avatar
 
Gildas Bazin committed
294
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
295
    val.psz_string = "logger"; text.psz_string = "Debug logging";
Gildas Bazin's avatar
 
Gildas Bazin committed
296
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
297
    val.psz_string = "sap"; text.psz_string = "SAP Playlist";
298
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
299
    val.psz_string = "gestures"; text.psz_string = "Mouse Gestures";
Gildas Bazin's avatar
 
Gildas Bazin committed
300
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
Gildas Bazin's avatar
 
Gildas Bazin committed
301 302 303

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

Sam Hocevar's avatar
Sam Hocevar committed
304 305 306 307
    do
    {
        /* Give control to the interface */
        p_intf->pf_run( p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
308

Sam Hocevar's avatar
Sam Hocevar committed
309 310
        /* Reset play on start status */
        p_intf->b_play = VLC_FALSE;
311

Sam Hocevar's avatar
Sam Hocevar committed
312 313 314 315 316
        if( !p_intf->psz_switch_intf )
        {
            break;
        }

317 318 319
        /* Make sure the old interface is completely uninitialized */
        module_Unneed( p_intf, p_intf->p_module );

Sam Hocevar's avatar
Sam Hocevar committed
320 321
        /* Provide ability to switch the main interface on the fly */
        psz_intf = p_intf->psz_switch_intf;
Gildas Bazin's avatar
 
Gildas Bazin committed
322 323
        p_intf->psz_switch_intf = NULL;

324 325 326 327 328
        vlc_mutex_lock( &p_intf->object_lock );
        p_intf->b_die = VLC_FALSE;
        p_intf->b_dead = VLC_FALSE;
        vlc_mutex_unlock( &p_intf->object_lock );

Gildas Bazin's avatar
 
Gildas Bazin committed
329
        p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
330 331
        free( psz_intf );
    }
Sam Hocevar's avatar
Sam Hocevar committed
332
    while( p_intf->p_module );
Gildas Bazin's avatar
 
Gildas Bazin committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
}

static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    p_intf->psz_switch_intf =
        malloc( strlen(newval.psz_string) + sizeof(",none") );
    sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
    p_intf->b_die = VLC_TRUE;

    return VLC_SUCCESS;
}

static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    intf_thread_t *p_intf;
    char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );

    /* Try to create the interface */
    sprintf( psz_intf, "%s,none", newval.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
356
    p_intf = intf_Create( p_this->p_vlc, psz_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    free( psz_intf );
    if( p_intf == NULL )
    {
        msg_Err( p_this, "interface \"%s\" initialization failed",
                 newval.psz_string );
        return VLC_EGENERIC;
    }

    /* Try to run the interface */
    p_intf->b_block = VLC_FALSE;
    if( intf_RunThread( p_intf ) != VLC_SUCCESS )
    {
        vlc_object_detach( p_intf );
        intf_Destroy( p_intf );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}