input_manager.cpp 24.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * input_manager.cpp : Manage an input and interact with its GUI elements
 ****************************************************************************
4
 * Copyright (C) 2006-2008 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Ilkka Ollakka  <ileoo@videolan.org>
9
 *          Jean-Baptiste <jb@videolan.org>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
 *****************************************************************************/
25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
28

Clément Stenac's avatar
Clément Stenac committed
29
#include "qt4.hpp"
30 31 32
#include "input_manager.hpp"
#include "dialogs_provider.hpp"

33 34
static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
                      vlc_value_t n, void *param );
35 36 37 38

static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
                           vlc_value_t n, void *param );

39 40
static int ItemChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
41
static int PLItemChanged( vlc_object_t *, const char *,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
42
                        vlc_value_t, vlc_value_t, void * );
43 44
static int InterfaceChanged( vlc_object_t *, const char *,
                            vlc_value_t, vlc_value_t, void * );
45 46
static int InterfaceVoutChanged( vlc_object_t *, const char *,
                                 vlc_value_t, vlc_value_t, void * );
47 48 49 50 51 52
static int ItemStateChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
static int ItemRateChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
static int ItemTitleChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
53 54
static int VolumeChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
55

56 57
/**********************************************************************
 * InputManager implementation
58 59 60
 **********************************************************************
 * The Input Manager can be the main one around the playlist
 * But can also be used for VLM dialog or similar
61 62
 **********************************************************************/

63 64 65
InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
                           QObject( parent ), p_intf( _p_intf )
{
66
    i_old_playing_status = END_S;
67 68 69 70 71
    old_name     = "";
    artUrl       = "";
    p_input      = NULL;
    i_rate       = 0;
    i_input_id   = 0;
72
    b_video      = false;
73
    b_transparentTelextext = false;
74 75 76 77
}

InputManager::~InputManager()
{
78
    delInput();
79 80
}

81 82 83
/* Define the Input used.
   Add the callbacks on input
   p_input is yield once here */
84 85
void InputManager::setInput( input_thread_t *_p_input )
{
86
    delInput();
87
    p_input = _p_input;
88
    if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
89
    {
90
        vlc_object_hold( p_input );
91
        emit statusChanged( PLAYING_S );
92 93
        UpdateMeta();
        UpdateArt();
94
        UpdateSPU();
95
        UpdateTeletext();
96
        UpdateNavigation();
97
        UpdateVout();
98
        addCallbacks();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
99 100 101 102
        i_input_id = input_GetItem( p_input )->i_id;
    }
    else
    {
103 104
        p_input = NULL;
        i_input_id = 0;
105
        emit rateChanged( INPUT_RATE_DEFAULT );
106 107
    }
}
108

109 110 111
/* delete Input if it ever existed.
   Delete the callbacls on input
   p_input is released once here */
112 113 114 115
void InputManager::delInput()
{
    if( p_input )
    {
116
        delCallbacks();
117
        i_old_playing_status = END_S;
118
        i_input_id = 0;
119 120
        old_name   = "";
        artUrl     = "";
121
        b_video    = false;
122
        emit positionUpdated( -1.0, 0 ,0 );
123 124
        emit statusChanged( END_S );
        emit nameChanged( "" );
125
        emit artChanged( NULL );
126
        emit rateChanged( INPUT_RATE_DEFAULT );
127
        emit voutChanged( false );
Rafaël Carré's avatar
Rafaël Carré committed
128 129
        vlc_object_release( p_input );
        p_input = NULL;
130
        UpdateSPU();
131
        UpdateTeletext();
132
    }
133 134
}

135
/* Add the callbacks on Input. Self explanatory */
136
void InputManager::addCallbacks()
137
{
138 139 140 141 142 143 144 145
    /* We don't care about:
       - chapter
       - programs
       - audio-delay
       - spu-delay
       - bookmark
       - position, time, length, because they are included in intf-change
     */
146 147
    /* src/input/input.c:1629 */
    var_AddCallback( p_input, "state", ItemStateChanged, this );
148 149
    /* src/input/es-out.c:552 */
    var_AddCallback( p_input, "spu-es", ChangeSPU, this );
150 151 152
    /* emit UpdateStatus so that main_interface updates controls 
     * if there is new videotracks (mpeg-ts)*/
    var_AddCallback( p_input, "video-es", ItemStateChanged, this );
153 154
    /* src/input/es-out.c: */
    var_AddCallback( p_input, "teletext-es", ChangeTeletext, this );
155
    /* src/input/input.c:1765 */
156
    var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
157 158 159 160
    /* src/input/input.c:2003 */
    var_AddCallback( p_input, "title", ItemTitleChanged, this );
    /* src/input/input.c:734 for timers update*/
    var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
161 162
    /* src/input/input.c for vout creation/destruction */
    var_AddCallback( p_input, "intf-change-vout", InterfaceVoutChanged, this );
163 164
}

165
/* Delete the callbacks on Input. Self explanatory */
166
void InputManager::delCallbacks()
167
{
168
    var_DelCallback( p_input, "spu-es", ChangeSPU, this );
169
    var_DelCallback( p_input, "video-es", ItemStateChanged, this );
170
    var_DelCallback( p_input, "teletext-es", ChangeTeletext, this );
171
    var_DelCallback( p_input, "state", ItemStateChanged, this );
172
    var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
173 174
    var_DelCallback( p_input, "title", ItemTitleChanged, this );
    var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
175
    var_DelCallback( p_input, "intf-change-vout", InterfaceVoutChanged, this );
176 177
}

178
/* Convert the event from the callbacks in actions */
179 180 181
void InputManager::customEvent( QEvent *event )
{
    int type = event->type();
182
    IMEvent *ple = static_cast<IMEvent *>(event);
183

184 185 186 187
    if ( type != PositionUpdate_Type &&
         type != ItemChanged_Type &&
         type != ItemRateChanged_Type &&
         type != ItemTitleChanged_Type &&
188
         type != ItemSpuChanged_Type &&
189
         type != ItemTeletextChanged_Type &&
190 191
         type != ItemStateChanged_Type &&
         type != InterfaceVoutUpdate_Type )
192
        return;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
193

194 195 196
    if( type == ItemStateChanged_Type )
    {
        UpdateNavigation();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
197
        UpdateTeletext();
198 199
    }

200 201
    if( !hasInput() ) return;

202 203
    if( ( type != PositionUpdate_Type  &&
          type != ItemRateChanged_Type &&
204
          type != ItemSpuChanged_Type &&
205
          type != ItemTeletextChanged_Type &&
206 207
          type != ItemStateChanged_Type &&
          type != InterfaceVoutUpdate_Type
208 209
        )
        && ( i_input_id != ple->i_id ) )
210
        return;
211

212 213 214
    if( type != PositionUpdate_Type )
        msg_Dbg( p_intf, "New Event: type %i", type );

215
    /* Actions */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
216
    switch( type )
217
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
218
    case PositionUpdate_Type:
219
        UpdatePosition();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
220 221
        break;
    case ItemChanged_Type:
222
        UpdateMeta();
223
        UpdateStatus();
224
        UpdateArt();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
225
        break;
226 227 228 229
    case ItemStateChanged_Type:
        UpdateStatus();
        UpdateNavigation();
        UpdateMeta();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
230 231
        break;
    case ItemTitleChanged_Type:
232
        UpdateNavigation();
233
        UpdateMeta();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
234
        break;
235 236 237
    case ItemRateChanged_Type:
        UpdateRate();
        break;
238 239
    case ItemSpuChanged_Type:
        UpdateSPU();
240
        break;
241 242 243
    case ItemTeletextChanged_Type:
        UpdateTeletext();
        break;
244 245 246
    case InterfaceVoutUpdate_Type:
        UpdateVout();
        break;
247
    }
248
}
249

250
void InputManager::UpdatePosition()
251
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
252 253 254 255 256 257 258
    /* Update position */
    int i_length, i_time; /* Int is enough, since we store seconds */
    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 );
259 260
}

261
void InputManager::UpdateNavigation()
262
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
263 264
    /* Update navigation status */
    vlc_value_t val; val.i_int = 0;
265 266
    if( hasInput() )
        var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
267 268 269 270 271 272 273 274 275 276
    if( val.i_int > 0 )
    {
        val.i_int = 0;
        var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
        emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
    }
    else
    {
        emit navigationChanged( 0 );
    }
277 278
}

279
void InputManager::UpdateStatus()
280
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
281 282 283 284 285 286 287 288
    /* Update playing status */
    vlc_value_t val; val.i_int = 0;
    var_Get( p_input, "state", &val );
    if( i_old_playing_status != val.i_int )
    {
        i_old_playing_status = val.i_int;
        emit statusChanged( val.i_int );
    }
289 290
}

291
void InputManager::UpdateRate()
292
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
293 294 295 296 297 298 299 300
    /* Update Rate */
    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 );
    }
301 302
}

303
void InputManager::UpdateMeta()
304
{
305 306
    /* Update text, name and nowplaying */
    QString text;
307

308
    /* Try to get the Title, then the Name */
309 310 311 312 313 314 315
    char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
    if( EMPTY_STR( psz_name ) )
    {
        free( psz_name );
        psz_name = input_item_GetName( input_GetItem( p_input ) );
    }

316
    /* Try to get the nowplaying */
317 318 319 320 321 322
    char *psz_nowplaying =
        input_item_GetNowPlaying( input_GetItem( p_input ) );
    if( !EMPTY_STR( psz_nowplaying ) )
    {
        text.sprintf( "%s - %s", psz_nowplaying, psz_name );
    }
323
    else  /* Do it ourself */
324 325
    {
        char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
326

327 328
        if( !EMPTY_STR( psz_artist ) )
            text.sprintf( "%s - %s", psz_artist, psz_name );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
329 330
        else
            text.sprintf( "%s", psz_name );
331

332
        free( psz_artist );
333
    }
334
    /* Free everything */
335 336 337
    free( psz_name );
    free( psz_nowplaying );

338 339 340 341 342 343 344 345 346
    /* If we have Nothing */
    if( text.isEmpty() )
    {
        psz_name = input_item_GetURI( input_GetItem( p_input ) );
        text.sprintf( "%s", psz_name );
        text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
        free( psz_name );
    }

347 348 349 350 351
    if( old_name != text )
    {
        emit nameChanged( text );
        old_name=text;
    }
352
}
353

354
bool InputManager::hasAudio()
355
{
356 357 358 359 360 361 362 363 364 365
    if( hasInput() )
    {
        vlc_value_t val;
        var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
        return val.i_int > 0;
    }
    return false;
}

void InputManager::UpdateSPU()
366 367 368 369 370
{
    UpdateTeletext();
}

void InputManager::UpdateTeletext()
371 372
{
    if( hasInput() )
373
        telexToggle( var_GetInteger( p_input, "teletext-es" ) >= 0 );
374 375
    else
        telexToggle( false );
376 377
}

378 379 380 381
void InputManager::UpdateVout()
{
    if( hasInput() )
    {
382 383
        bool b_old_video = b_video;

384
        vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
385
        b_video = p_vout != NULL;
386 387
        if( p_vout )
            vlc_object_release( p_vout );
388 389
        if( !!b_old_video != !!b_video )
            emit voutChanged( b_video );
390 391 392
    }
}

Jean-Baptiste Kempf's avatar
 
Jean-Baptiste Kempf committed
393
void InputManager::UpdateArt()
394 395
{
    /* Update Art meta */
396
    emit artChanged( input_GetItem( p_input ) );
397 398
}

399
/* User update of the slider */
Clément Stenac's avatar
Clément Stenac committed
400 401
void InputManager::sliderUpdate( float new_pos )
{
402 403
    if( hasInput() )
        var_SetFloat( p_input, "position", new_pos );
Clément Stenac's avatar
Clément Stenac committed
404
}
405

406
/* User togglePlayPause */
407 408 409 410
void InputManager::togglePlayPause()
{
    vlc_value_t state;
    var_Get( p_input, "state", &state );
411
    state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
412 413 414 415
    var_Set( p_input, "state", state );
    emit statusChanged( state.i_int );
}

416 417 418 419
void InputManager::sectionPrev()
{
    if( hasInput() )
    {
420
        int i_type = var_Type( p_input, "next-chapter" );
421
        vlc_value_t val; val.b_bool = true;
422 423 424 425 426 427 428 429 430
        var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
                            "prev-chapter":"prev-title", val );
    }
}

void InputManager::sectionNext()
{
    if( hasInput() )
    {
431
        int i_type = var_Type( p_input, "next-chapter" );
432
        vlc_value_t val; val.b_bool = true;
433 434 435 436 437 438 439 440
        var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
                            "next-chapter":"next-title", val );
    }
}

void InputManager::sectionMenu()
{
    if( hasInput() )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
441
    {
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
        vlc_value_t val, text;
        vlc_value_t root;

        if( var_Change( p_input, "title  0", VLC_VAR_GETLIST, &val, &text ) < 0 )
            return;

        /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
        root.i_int = 0;
        for( int i = 0; i < val.p_list->i_count; i++ )
        {
            if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
                root.i_int = i;
        }
        var_Change( p_input, "title  0", VLC_VAR_FREELIST, &val, &text );

        var_Set( p_input, "title  0", root );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
458
    }
459 460
}

461 462
void InputManager::telexGotoPage( int page )
{
463
    if( hasInput() )
464
    {
465 466 467 468
        const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
        const int i_spu_es = var_GetInteger( p_input, "spu-es" );

        if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
469
        {
470 471 472 473 474 475 476 477
            vlc_object_t *p_vbi;
            p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
                        "zvbi", FIND_ANYWHERE );
            if( p_vbi )
            {
                var_SetInteger( p_vbi, "vbi-page", page );
                vlc_object_release( p_vbi );
            }
478 479
        }
    }
Lukas Durfina's avatar
Lukas Durfina committed
480
    emit setNewTelexPage( page );
481 482 483 484
}

void InputManager::telexToggle( bool b_enabled )
{
485 486
    if( hasInput() )
    {
487 488 489
        const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
        const int i_spu_es = var_GetInteger( p_input, "spu-es" );

490 491 492
        b_enabled = (i_teletext_es >= 0);
        emit teletextEnabled( b_enabled );
        if( b_enabled && (i_teletext_es == i_spu_es) )
493
        {
494
            vlc_object_t *p_vbi;
495
            int i_page = 100;
496 497 498 499 500 501 502 503 504
            p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
                        "zvbi", FIND_ANYWHERE );
            if( p_vbi )
            {
                i_page = var_GetInteger( p_vbi, "vbi-page" );
                vlc_object_release( p_vbi );
                i_page = b_enabled ? i_page : 0;
                telexGotoPage( i_page );
            }
505 506
        }
    }
507
    else emit teletextEnabled( b_enabled );
Lukas Durfina's avatar
Lukas Durfina committed
508 509 510 511
}

void InputManager::telexToggleButtons()
{
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    if( hasInput() )
    {
        const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
        if( i_teletext_es >= 0 )
        {
            const int i_spu_es = var_GetInteger( p_input, "spu-es" );

            if( i_teletext_es == i_spu_es )
                var_SetInteger( p_input, "spu-es", -1 );
            else
                var_SetInteger( p_input, "spu-es", i_teletext_es );

            emit toggleTelexButtons();
        }
    }
527 528
}

529
void InputManager::telexSetTransparency()
530
{
531
    if( hasInput() )
532 533 534 535 536 537
    {
        vlc_object_t *p_vbi;
        p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
                    "zvbi", FIND_ANYWHERE );
        if( p_vbi )
        {
538 539
            var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
            b_transparentTelextext = !b_transparentTelextext;
540 541 542
            vlc_object_release( p_vbi );
        }
    }
Lukas Durfina's avatar
Lukas Durfina committed
543
    emit toggleTelexTransparency();
544 545
}

Clément Stenac's avatar
Clément Stenac committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
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 );
}

564 565 566 567 568 569
void InputManager::setRate( int new_rate )
{
    if( hasInput() )
        var_SetInteger( p_input, "rate", new_rate );
}

570 571
/**********************************************************************
 * MainInputManager implementation. Wrap an input manager and
572 573
 * take care of updating the main playlist input.
 * Used in the main playlist Dialog
574 575 576
 **********************************************************************/
MainInputManager * MainInputManager::instance = NULL;

577 578
MainInputManager::MainInputManager( intf_thread_t *_p_intf )
                 : QObject(NULL), p_intf( _p_intf )
579 580 581
{
    p_input = NULL;
    im = new InputManager( this, p_intf );
582

583
//    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
584
    var_AddCallback( THEPL, "item-change", ItemChanged, im );
585 586
    var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
    var_AddCallback( THEPL, "activity", PLItemChanged, this );
587

588
    var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
589

590
    /* Warn our embedded IM about input changes */
Clément Stenac's avatar
Clément Stenac committed
591
    CONNECT( this, inputChanged( input_thread_t * ),
592
             im, setInput( input_thread_t * ) );
593 594 595 596 597 598 599

    /* emit check if playlist has allready started playing */
    vlc_value_t val;
    var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
    IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
    QApplication::postEvent( this, static_cast<QEvent*>(event) );

600 601
}

Clément Stenac's avatar
Clément Stenac committed
602 603
MainInputManager::~MainInputManager()
{
604 605
    if( p_input )
    {
606 607
       var_DelCallback( p_input, "state", PLItemChanged, this );
       vlc_object_release( p_input );
608 609
       emit inputChanged( NULL );
    }
610

611
    var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
612 613

    var_DelCallback( THEPL, "activity", PLItemChanged, this );
614
    var_DelCallback( THEPL, "item-change", ItemChanged, im );
615
//    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
616

617
    var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
Clément Stenac's avatar
Clément Stenac committed
618 619
}

620
void MainInputManager::customEvent( QEvent *event )
621
{
622
    int type = event->type();
623 624 625
    if ( type != ItemChanged_Type && type != VolumeChanged_Type )
        return;

626
    // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
627 628 629
    if( type == VolumeChanged_Type )
    {
        emit volumeChanged();
630
        return;
631
    }
632

633
    /* Should be PLItemChanged Event */
634
    if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
635
    {
636
        vlc_mutex_lock( &p_intf->change_lock );
637
        if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
638
        {
639 640
            var_DelCallback( p_input, "state", PLItemChanged, this );
            vlc_object_release( p_input );
641
            emit inputChanged( NULL );
642
            p_input = NULL;
643 644
            vlc_mutex_unlock( &p_intf->change_lock );
            return;
645
        }
646

647 648 649 650
        if( !p_input )
        {
            QPL_LOCK;
            p_input = THEPL->p_input;
651
            if( p_input && !( !vlc_object_alive (p_input) || p_input->b_dead) )
652
            {
653
                vlc_object_hold( p_input );
654
                var_AddCallback( p_input, "state", PLItemChanged, this );
655 656
                emit inputChanged( p_input );
            }
657 658
            else
                p_input = NULL;
659 660 661 662
            QPL_UNLOCK;
        }
        vlc_mutex_unlock( &p_intf->change_lock );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
663 664
    else
    {
665
        /* we are working as a dialogs provider */
666
        playlist_t *p_playlist = pl_Yield( p_intf );
667
        p_input = playlist_CurrentInput( p_playlist );
668 669 670 671 672
        if( p_input )
        {
            emit inputChanged( p_input );
            vlc_object_release( p_input );
        }
673
        pl_Release( p_intf );
674 675
    }
}
676

677
/* Playlist Control functions */
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
void MainInputManager::stop()
{
   playlist_Stop( THEPL );
}

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

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

693 694 695 696 697 698 699 700 701
void MainInputManager::togglePlayPause()
{
    if( p_input == NULL )
    {
        playlist_Play( THEPL );
        return;
    }
    getIM()->togglePlayPause();
}
702

703 704 705 706 707
bool MainInputManager::teletextState()
{
    im = getIM();
    if( im->hasInput() )
    {
708 709 710 711
        const int i_teletext_es = var_GetInteger( getInput(), "teletext-es" );
        const int i_spu_es = var_GetInteger( getInput(), "spu-es" );

        return i_teletext_es >= 0 && i_teletext_es == i_spu_es;
712 713 714 715
    }
    return false;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
716 717 718
/* Static callbacks */

/* IM */
719 720 721
static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
                           vlc_value_t oldval, vlc_value_t newval, void *param )
{
722
    /* FIXME remove that static variable */
723 724 725
    static int counter = 0;
    InputManager *im = (InputManager*)param;

726
    counter = ++counter % 4;
727 728 729 730 731 732 733
    if(!counter)
        return VLC_SUCCESS;
    IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

734 735 736 737 738 739 740 741 742 743
static int InterfaceVoutChanged( vlc_object_t *p_this, const char *psz_var,
                                 vlc_value_t oldval, vlc_value_t newval, void *param )
{
    InputManager *im = (InputManager*)param;

    IMEvent *event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

744
static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
745
                           vlc_value_t oldval, vlc_value_t newval, void *param )
746 747 748 749 750 751 752 753 754
{
    InputManager *im = (InputManager*)param;

    IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
755
                           vlc_value_t oldval, vlc_value_t newval, void *param )
756 757
{
    InputManager *im = (InputManager*)param;
758

759 760 761 762 763 764
    IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
765
                           vlc_value_t oldval, vlc_value_t newval, void *param )
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
{
    InputManager *im = (InputManager*)param;

    IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}

static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *param )
{
    InputManager *im = (InputManager*)param;

    IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
}
783

784 785 786 787
static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
                        vlc_value_t n, void *param )
{
    InputManager *im = (InputManager*)param;
788 789
    IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
790
    return VLC_SUCCESS;
791 792 793 794 795
}

static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
                           vlc_value_t n, void *param )
{
796

797 798 799 800
    InputManager *im = (InputManager*)param;
    IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
    QApplication::postEvent( im, static_cast<QEvent*>(event) );
    return VLC_SUCCESS;
801 802
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
803
/* MIM */
804
static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
805 806
                        vlc_value_t oldval, vlc_value_t newval, void *param )
{
807
    MainInputManager *mim = (MainInputManager*)param;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
808 809

    IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
810
    QApplication::postEvent( mim, static_cast<QEvent*>(event) );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
811 812 813 814 815 816
    return VLC_SUCCESS;
}

static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *param )
{
817
    MainInputManager *mim = (MainInputManager*)param;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
818 819

    IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
820
    QApplication::postEvent( mim, static_cast<QEvent*>(event) );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
821 822 823
    return VLC_SUCCESS;
}