input_manager.cpp 9.79 KB
Newer Older
1 2 3
/*****************************************************************************
 * input_manager.cpp : Manage an input and interact with its GUI elements
 ****************************************************************************
4
 * Copyright (C) 2006-2007 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Clément Stenac <zorglub@videolan.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.
 *****************************************************************************/

Clément Stenac's avatar
Clément Stenac committed
24
#include "qt4.hpp"
25 26 27
#include "input_manager.hpp"
#include "dialogs_provider.hpp"

28 29 30 31 32
static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
                        vlc_value_t n, void *param );
static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
                        vlc_value_t n, void *param );

33 34 35 36
/**********************************************************************
 * InputManager implementation
 **********************************************************************/

37 38 39
InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
                           QObject( parent ), p_intf( _p_intf )
{
40
    i_old_playing_status = END_S;
41
    old_name="";
42
    p_input = NULL;
43
    i_rate = 0;
Clément Stenac's avatar
Clément Stenac committed
44
    ON_TIMEOUT( update() );
45 46 47 48
}

InputManager::~InputManager()
{
49
    delInput();
50 51 52 53
}

void InputManager::setInput( input_thread_t *_p_input )
{
54
    delInput();
55
    p_input = _p_input;
Clément Stenac's avatar
Cleanup  
Clément Stenac committed
56
    emit positionUpdated( 0.0,0,0 );
57 58 59
    b_had_audio = b_had_video = b_has_audio = b_has_video = false;
    if( p_input )
    {
60
        vlc_object_yield( p_input );
Clément Stenac's avatar
Clément Stenac committed
61 62 63 64 65
        vlc_value_t val;
        var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
        b_has_video = val.i_int > 0;
        var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
        b_has_audio = val.i_int > 0;
66 67 68 69
        var_AddCallback( p_input, "audio-es", ChangeAudio, this );
        var_AddCallback( p_input, "video-es", ChangeVideo, this );
    }
}
70

71 72 73 74 75 76
void InputManager::delInput()
{
    if( p_input )
    {
        var_DelCallback( p_input, "audio-es", ChangeAudio, this );
        var_DelCallback( p_input, "video-es", ChangeVideo, this );
77 78
        vlc_object_release( p_input );
        p_input = NULL;
79
    }
80 81
}

82
//FIXME break that
83 84
void InputManager::update()
{
85
    /// \todo Emit the signals only if it changed
86 87 88 89 90
    if( !p_input )
    {
        emit nameChanged( "" );
        return;
    }
91

Clément Stenac's avatar
Clément Stenac committed
92
    if( p_input->b_dead || p_input->b_die )
93
    {
Clément Stenac's avatar
Cleanup  
Clément Stenac committed
94
        emit positionUpdated( 0.0, 0, 0 );
Clément Stenac's avatar
Clément Stenac committed
95
        emit navigationChanged( 0 );
96
        i_old_playing_status = 0;
97
        emit statusChanged( END_S ); // see vlc_input.h, input_state_e enum
98 99
        delInput();
        return;
100
    }
101 102

    /* Update position */
103
    int i_length, i_time; /* Int is enough, since we store seconds */
104 105 106 107 108
    float f_pos;
    i_length = var_GetTime( p_input, "length" ) / 1000000;
    i_time = var_GetTime( p_input, "time") / 1000000;
    f_pos = var_GetFloat( p_input, "position" );
    emit positionUpdated( f_pos, i_time, i_length );
109

110
    /* Update Rate */
111 112 113 114 115 116 117
    int i_new_rate = var_GetInteger( p_input, "rate");
    if( i_new_rate != i_rate )
    {
        i_rate = i_new_rate;
        /* Update rate */
        emit rateChanged( i_rate );
    }
118

119
    /* Update navigation status */
120
    vlc_value_t val; val.i_int = 0;
121 122 123
    var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
    if( val.i_int > 0 )
    {
124
        val.i_int = 0;
125
        var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
126 127
        emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
        /*if( val.i_int > 0 )
128
        {
129
            emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
130
        }
131
        else
132
        {
133
            emit navigationChanged( 2 );
134
        }*/
135 136 137
    }
    else
    {
Clément Stenac's avatar
Clément Stenac committed
138
        emit navigationChanged( 0 );
139 140 141 142
    }

    /* Update text */
    QString text;
143
    char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
144 145
    char *psz_nowplaying =
        input_item_GetNowPlaying( input_GetItem( p_input ) );
146
    char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
147 148
    if( EMPTY_STR( psz_name ) )
    {
149
        free( psz_name );
150 151
        psz_name = input_item_GetName( input_GetItem( p_input ) );
    }
152
    if( !EMPTY_STR( psz_nowplaying ) )
153
    {
154
        text.sprintf( "%s - %s", psz_nowplaying, psz_name );
155
    }
156
    else if( !EMPTY_STR( psz_artist ) )
157
    {
158
        text.sprintf( "%s - %s", psz_artist, psz_name );
159
    }
160 161
    else
    {
162
        text.sprintf( "%s", psz_name );
163
    }
164 165 166
    free( psz_name );
    free( psz_nowplaying );
    free( psz_artist );
167 168 169 170 171
    if( old_name != text )
    {
        emit nameChanged( text );
        old_name=text;
    }
172

173 174 175 176 177 178 179 180
    /* Update playing status */
    var_Get( p_input, "state", &val );
    val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
    if( i_old_playing_status != val.i_int )
    {
        i_old_playing_status = val.i_int;
        emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
    }
181
}
Clément Stenac's avatar
Clément Stenac committed
182 183 184

void InputManager::sliderUpdate( float new_pos )
{
185
    if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
Clément Stenac's avatar
Clément Stenac committed
186
}
187

188 189 190 191
void InputManager::togglePlayPause()
{
    vlc_value_t state;
    var_Get( p_input, "state", &state );
192 193
    state.i_int = ( ( state.i_int != PAUSE_S ) ? PAUSE_S : PLAYING_S );
    /*{
194
        /* A stream is being played, pause it */
195
       /* state.i_int = PAUSE_S;
196 197 198 199
    }
    else
    {
        /* Stream is paused, resume it */
200 201
        /*state.i_int = PLAYING_S;
    }*/
202 203 204 205
    var_Set( p_input, "state", state );
    emit statusChanged( state.i_int );
}

206 207 208 209
void InputManager::sectionPrev()
{
    if( hasInput() )
    {
210
        int i_type = var_Type( p_input, "next-chapter" );
211 212 213 214 215 216 217 218 219 220
        vlc_value_t val; val.b_bool = VLC_TRUE;
        var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
                            "prev-chapter":"prev-title", val );
    }
}

void InputManager::sectionNext()
{
    if( hasInput() )
    {
221
        int i_type = var_Type( p_input, "next-chapter" );
222 223 224 225 226 227 228 229 230
        vlc_value_t val; val.b_bool = VLC_TRUE;
        var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
                            "next-chapter":"next-title", val );
    }
}

void InputManager::sectionMenu()
{
    if( hasInput() )
231
        var_SetInteger( p_input, "title 0", 2 );
232 233
}

Clément Stenac's avatar
Clément Stenac committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
void InputManager::slower()
{
    if( hasInput() )
        var_SetVoid( p_input, "rate-slower" );
}

void InputManager::faster()
{
    if( hasInput() )
        var_SetVoid( p_input, "rate-faster" );
}

void InputManager::normalRate()
{
    if( hasInput() )
        var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
}

252 253 254 255 256 257
void InputManager::setRate( int new_rate )
{
    if( hasInput() )
        var_SetInteger( p_input, "rate", new_rate );
}

258 259 260 261 262 263 264 265 266 267 268
/**********************************************************************
 * MainInputManager implementation. Wrap an input manager and
 * take care of updating the main playlist input
 **********************************************************************/
MainInputManager * MainInputManager::instance = NULL;

MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
                                                p_intf( _p_intf )
{
    p_input = NULL;
    im = new InputManager( this, p_intf );
Clément Stenac's avatar
Clément Stenac committed
269
    ON_TIMEOUT( updateInput() );
270
    /* Warn our embedded IM about input changes */
Clément Stenac's avatar
Clément Stenac committed
271 272
    CONNECT( this, inputChanged( input_thread_t * ),
             im,   setInput( input_thread_t * ) );
273 274
}

Clément Stenac's avatar
Clément Stenac committed
275 276 277 278 279
MainInputManager::~MainInputManager()
{
    if( p_input ) vlc_object_release( p_input );
}

280 281
void MainInputManager::updateInput()
{
282
    if( VLC_OBJECT_INTF == p_intf->i_object_type )
283
    {
284 285 286 287 288 289 290 291
        vlc_mutex_lock( &p_intf->change_lock );
        if( p_input && p_input->b_dead )
        {
            vlc_object_release( p_input );
            getIM()->delInput();
            p_input = NULL;
            emit inputChanged( NULL );
        }
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        if( !p_input )
        {
            QPL_LOCK;
            p_input = THEPL->p_input;
            if( p_input )
            {
                vlc_object_yield( p_input );
                emit inputChanged( p_input );
            }
            QPL_UNLOCK;
        }
        vlc_mutex_unlock( &p_intf->change_lock );
    }
    else {
        /* we are working as a dialogs provider */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
308 309
        playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
                                       VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
310
        if( p_playlist )
311
        {
312
            p_input = p_playlist->p_input;
313 314 315 316
            emit inputChanged( p_input );
        }
    }
}
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
void MainInputManager::stop()
{
   playlist_Stop( THEPL );
}

void MainInputManager::next()
{
   playlist_Next( THEPL );
}

void MainInputManager::prev()
{
   playlist_Prev( THEPL );
}

333 334 335 336 337 338 339 340 341
void MainInputManager::togglePlayPause()
{
    if( p_input == NULL )
    {
        playlist_Play( THEPL );
        return;
    }
    getIM()->togglePlayPause();
}
342 343 344 345 346 347

static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
                        vlc_value_t n, void *param )
{
    InputManager *im = (InputManager*)param;
    im->b_has_audio = true;
348
    return VLC_SUCCESS;
349
}
Clément Stenac's avatar
Clément Stenac committed
350

351 352 353 354 355
static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
                        vlc_value_t n, void *param )
{
    InputManager *im = (InputManager*)param;
    im->b_has_video = true;
356
    return VLC_SUCCESS;
357
}