input_manager.cpp 8.25 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
/*****************************************************************************
 * input_manager.cpp : Manage an input and interact with its GUI elements
 ****************************************************************************
4
5
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
zorglub's avatar
zorglub committed
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.
 *****************************************************************************/

24
25
#include <assert.h>

zorglub's avatar
zorglub committed
26
#include "qt4.hpp"
zorglub's avatar
zorglub committed
27
28
29
#include "input_manager.hpp"
#include "dialogs_provider.hpp"

30
31
32
33
34
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 );

35
36
37
38
/**********************************************************************
 * InputManager implementation
 **********************************************************************/

zorglub's avatar
zorglub committed
39
40
41
InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
                           QObject( parent ), p_intf( _p_intf )
{
42
    i_old_playing_status = END_S;
zorglub's avatar
zorglub committed
43
    p_input = NULL;
zorglub's avatar
zorglub committed
44
    ON_TIMEOUT( update() );
zorglub's avatar
zorglub committed
45
46
47
48
49
50
51
52
53
}

InputManager::~InputManager()
{
}

void InputManager::setInput( input_thread_t *_p_input )
{
    p_input = _p_input;
zorglub's avatar
Cleanup    
zorglub committed
54
    emit positionUpdated( 0.0,0,0 );
55
56
57
    b_had_audio = b_had_video = b_has_audio = b_has_video = false;
    if( p_input )
    {
zorglub's avatar
zorglub committed
58
59
60
61
62
        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;
63
64
65
66
67
68
69
70
71
72
73
74
        var_AddCallback( p_input, "audio-es", ChangeAudio, this );
        var_AddCallback( p_input, "video-es", ChangeVideo, this );
    }

}
void InputManager::delInput()
{
    if( p_input )
    {
        var_DelCallback( p_input, "audio-es", ChangeAudio, this );
        var_DelCallback( p_input, "video-es", ChangeVideo, this );
    }
zorglub's avatar
zorglub committed
75
76
77
78
}

void InputManager::update()
{
79
    /// \todo Emit the signals only if it changed
zorglub's avatar
zorglub committed
80
    if( !p_input ) return;
zorglub's avatar
zorglub committed
81

zorglub's avatar
zorglub committed
82
    if( p_input->b_dead || p_input->b_die )
zorglub's avatar
zorglub committed
83
    {
zorglub's avatar
Cleanup    
zorglub committed
84
        emit positionUpdated( 0.0, 0, 0 );
zorglub's avatar
zorglub committed
85
        msg_Dbg( p_intf, "*********** NAV 0");
zorglub's avatar
zorglub committed
86
        emit navigationChanged( 0 );
zorglub's avatar
zorglub committed
87
        emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
zorglub's avatar
zorglub committed
88
    }
89

90
91
92
93
94
    if( !b_had_audio && b_has_audio )
        emit audioStarted();
    if( !b_had_video && b_has_video )
        emit videoStarted();

95
    /* Update position */
zorglub's avatar
zorglub committed
96
97
98
99
100
101
    mtime_t i_length, i_time;
    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 );
102
103

    /* Update disc status */
zorglub's avatar
zorglub committed
104
    vlc_value_t val; val.i_int = 0;
105
106
107
    var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
    if( val.i_int > 0 )
    {
zorglub's avatar
zorglub committed
108
        val.i_int = 0;
109
        var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
110
        if( val.i_int > 0 )
111
        {
zorglub's avatar
zorglub committed
112
            msg_Dbg( p_intf, "******* CHAPTER");
113
            emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
114
        }
115
        else
zorglub's avatar
zorglub committed
116
117
        {
            msg_Dbg( p_intf, "******* TITLE");
118
            emit navigationChanged( 2 );
zorglub's avatar
zorglub committed
119
        }
120
121
122
    }
    else
    {
zorglub's avatar
zorglub committed
123
        emit navigationChanged( 0 );
124
125
126
127
    }

    /* Update text */
    QString text;
zorglub's avatar
zorglub committed
128
129
130
    if( input_GetItem(p_input)->p_meta &&
        input_GetItem(p_input)->p_meta->psz_nowplaying &&
        *input_GetItem(p_input)->p_meta->psz_nowplaying )
131
    {
zorglub's avatar
zorglub committed
132
        text.sprintf( "%s - %s",
zorglub's avatar
zorglub committed
133
134
                  input_GetItem(p_input)->p_meta->psz_nowplaying,
                  input_GetItem(p_input)->psz_name );
135
136
137
    }
    else
    {
zorglub's avatar
zorglub committed
138
        text.sprintf( "%s", input_GetItem(p_input)->psz_name );
139
140
141
    }
    emit nameChanged( text );

142
143
144
145
146
147
148
149
    /* 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 );
    }
zorglub's avatar
zorglub committed
150
}
zorglub's avatar
zorglub committed
151
152
153

void InputManager::sliderUpdate( float new_pos )
{
zorglub's avatar
zorglub committed
154
    if( hasInput() )
zorglub's avatar
zorglub committed
155
        var_SetFloat( p_input, "position", new_pos );
zorglub's avatar
zorglub committed
156
}
157

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
void InputManager::togglePlayPause()
{
    vlc_value_t state;
    var_Get( p_input, "state", &state );
    if( state.i_int != PAUSE_S )
    {
        /* A stream is being played, pause it */
        state.i_int = PAUSE_S;
    }
    else
    {
        /* Stream is paused, resume it */
        state.i_int = PLAYING_S;
    }
    var_Set( p_input, "state", state );
    emit statusChanged( state.i_int );
}

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
void InputManager::sectionPrev()
{
    if( hasInput() )
    {
        int i_type = var_Type( p_input, "prev-chapter" );
        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() )
    {
        int i_type = var_Type( p_input, "prev-chapter" );
        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() )
        var_SetInteger( p_input, "title 0", 2);
}

zorglub's avatar
zorglub committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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 );
}

222
223
224
225
226
227
228
229
230
231
232
/**********************************************************************
 * 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 );
zorglub's avatar
zorglub committed
233
    ON_TIMEOUT( updateInput() );
234
    /* Warn our embedded IM about input changes */
zorglub's avatar
zorglub committed
235
236
    CONNECT( this, inputChanged( input_thread_t * ),
             im,   setInput( input_thread_t * ) );
237
238
}

zorglub's avatar
zorglub committed
239
240
241
242
243
MainInputManager::~MainInputManager()
{
    if( p_input ) vlc_object_release( p_input );
}

244
245
246
247
248
249
void MainInputManager::updateInput()
{
    vlc_mutex_lock( &p_intf->change_lock );
    if( p_input && p_input->b_dead )
    {
        vlc_object_release( p_input );
250
        getIM()->delInput();
251
252
253
254
255
256
        p_input = NULL;
        emit inputChanged( NULL );
    }

    if( !p_input )
    {
zorglub's avatar
zorglub committed
257
258
        QPL_LOCK;
        p_input = THEPL->p_input;
259
260
261
262
263
        if( p_input )
        {
            vlc_object_yield( p_input );
            emit inputChanged( p_input );
        }
zorglub's avatar
zorglub committed
264
        QPL_UNLOCK;
265
266
267
    }
    vlc_mutex_unlock( &p_intf->change_lock );
}
268
269
270
271
272
273
274
275
276
277

void MainInputManager::togglePlayPause()
{
    if( p_input == NULL )
    {
        playlist_Play( THEPL );
        return;
    }
    getIM()->togglePlayPause();
}
278
279
280
281
282
283
284


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;
zorglub's avatar
zorglub committed
285
    return 0;
286
}
zorglub's avatar
zorglub committed
287

288
289
290
291
292
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;
zorglub's avatar
zorglub committed
293
    return 0;
294
}