]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Fix play/pause icons
[vlc] / modules / gui / qt4 / input_manager.cpp
1 /*****************************************************************************
2  * input_manager.cpp : Manage an input and interact with its GUI elements
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <assert.h>
25
26 #include "input_manager.hpp"
27 #include "dialogs_provider.hpp"
28 #include "qt4.hpp"
29
30 /**********************************************************************
31  * InputManager implementation
32  **********************************************************************/
33
34 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
35                            QObject( parent ), p_intf( _p_intf )
36 {
37     i_old_playing_status = END_S;
38     p_input = NULL;
39     /* Subscribe to updates */
40     connect( THEDP->fixed_timer, SIGNAL( timeout() ), this, SLOT( update() ) );
41 }
42
43 InputManager::~InputManager()
44 {
45 }
46
47 void InputManager::setInput( input_thread_t *_p_input )
48 {
49     p_input = _p_input;
50     emit positionUpdated( 0.0,0,0 );
51 }
52
53 void InputManager::update()
54 {
55     /// \todo Emit the signals only if it changed
56     if( !p_input  ) return;
57
58     if( p_input->b_dead || p_input->b_die )
59     {
60         emit positionUpdated( 0.0, 0, 0 );
61         emit navigationChanged( 0 );
62         emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
63     }
64
65     /* Update position */
66     mtime_t i_length, i_time;
67     float f_pos;
68     i_length = var_GetTime( p_input, "length" ) / 1000000;
69     i_time = var_GetTime( p_input, "time") / 1000000;
70     f_pos = var_GetFloat( p_input, "position" );
71     emit positionUpdated( f_pos, i_time, i_length );
72
73     /* Update disc status */
74     vlc_value_t val;
75     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
76     if( val.i_int > 0 )
77     {
78         vlc_value_t val;
79         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
80     if( val.i_int > 0 )
81         emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 3 = NO
82     else
83         emit navigationChanged( 2 );
84     }
85     else
86     {
87         emit navigationChanged( 0 );
88     }
89
90     /* Update text */
91     QString text;
92     if( p_input->input.p_item->p_meta &&
93         p_input->input.p_item->p_meta->psz_nowplaying &&
94         *p_input->input.p_item->p_meta->psz_nowplaying )
95     {
96         text.sprintf( "%s - %s",
97                   p_input->input.p_item->p_meta->psz_nowplaying,
98                   p_input->input.p_item->psz_name );
99     }
100     else
101     {
102         text.sprintf( "%s", p_input->input.p_item->psz_name );
103     }
104     emit nameChanged( text );
105
106     /* Update playing status */
107     var_Get( p_input, "state", &val );
108     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
109     if( i_old_playing_status != val.i_int )
110     {
111         i_old_playing_status = val.i_int;
112         emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
113     }
114 }
115
116 void InputManager::sliderUpdate( float new_pos )
117 {
118    if( p_input && !p_input->b_die && !p_input->b_dead )
119         var_SetFloat( p_input, "position", new_pos );
120 }
121
122 void InputManager::togglePlayPause()
123 {
124     vlc_value_t state;
125     var_Get( p_input, "state", &state );
126     if( state.i_int != PAUSE_S )
127     {
128         /* A stream is being played, pause it */
129         state.i_int = PAUSE_S;
130     }
131     else
132     {
133         /* Stream is paused, resume it */
134         state.i_int = PLAYING_S;
135     }
136     var_Set( p_input, "state", state );
137     emit statusChanged( state.i_int );
138 }
139
140 /**********************************************************************
141  * MainInputManager implementation. Wrap an input manager and
142  * take care of updating the main playlist input
143  **********************************************************************/
144 MainInputManager * MainInputManager::instance = NULL;
145
146 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
147                                                 p_intf( _p_intf )
148 {
149     p_input = NULL;
150     im = new InputManager( this, p_intf );
151     /* Get timer updates */
152     connect( DialogsProvider::getInstance(p_intf)->fixed_timer,
153              SIGNAL(timeout() ), this, SLOT( updateInput() ) );
154     /* Warn our embedded IM about input changes */
155     connect( this, SIGNAL( inputChanged( input_thread_t * ) ),
156              im, SLOT( setInput( input_thread_t * ) ) );
157 }
158
159 MainInputManager::~MainInputManager()
160 {
161     if( p_input ) vlc_object_release( p_input );
162 }
163
164 void MainInputManager::updateInput()
165 {
166     vlc_mutex_lock( &p_intf->change_lock );
167     if( p_input && p_input->b_dead )
168     {
169         vlc_object_release( p_input );
170         p_input = NULL;
171         emit inputChanged( NULL );
172     }
173
174     if( !p_input )
175     {
176         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
177                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
178         assert( p_playlist );
179         PL_LOCK;
180         p_input = p_playlist->p_input;
181         if( p_input )
182         {
183             vlc_object_yield( p_input );
184             emit inputChanged( p_input );
185         }
186         PL_UNLOCK;
187         vlc_object_release( p_playlist );
188     }
189     vlc_mutex_unlock( &p_intf->change_lock );
190 }
191
192 void MainInputManager::togglePlayPause()
193 {
194     if( p_input == NULL )
195     {
196         playlist_Play( THEPL );
197         return;
198     }
199     getIM()->togglePlayPause();
200 }