]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
* Split apart gui and input management
[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) 2000-2005 the VideoLAN team
5  * $Id: wxwidgets.cpp 15731 2006-05-25 14:43:53Z zorglub $
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 "input_manager.hpp"
25 #include "dialogs_provider.hpp"
26 #include "qt4.hpp"
27
28 /**********************************************************************
29  * InputManager implementation
30  **********************************************************************/
31
32 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
33                            QObject( parent ), p_intf( _p_intf )
34 {
35     p_input = NULL;
36     /* Subscribe to updates */
37     connect( DialogsProvider::getInstance( p_intf )->fixed_timer,
38              SIGNAL( timeout() ), this, SLOT( update() ) );
39 }
40
41 InputManager::~InputManager()
42 {
43 }
44
45 void InputManager::setInput( input_thread_t *_p_input )
46 {
47     p_input = _p_input;
48     emit positionUpdated( 0.0,0,0 );
49 }
50
51 void InputManager::update()
52 {
53     /// \todo Emit the signals only if it changed
54     if( !p_input || p_input->b_die ) return;
55
56     if( p_input->b_dead )
57     {
58         emit positionUpdated( 0.0, 0, 0 );
59         emit navigationChanged( 0 );
60         emit statusChanged( 0 ); // 0 = STOPPED, 1 = PAUSE, 2 = PLAY
61     }
62
63     /* Update position */
64     mtime_t i_length, i_time;
65     float f_pos;
66     i_length = var_GetTime( p_input, "length" ) / 1000000;
67     i_time = var_GetTime( p_input, "time") / 1000000;
68     f_pos = var_GetFloat( p_input, "position" );
69     emit positionUpdated( f_pos, i_time, i_length );
70
71     /* Update disc status */
72     vlc_value_t val;
73     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
74     if( val.i_int > 0 )
75     {
76         vlc_value_t val;
77         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
78     if( val.i_int > 0 )
79         emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 3 = NO
80     else
81         emit navigationChanged( 2 );
82     }
83     else
84     {
85         emit navigationChanged( 0 );
86     }
87
88     /* Update text */
89     QString text;
90     if( p_input->input.p_item->p_meta &&
91         p_input->input.p_item->p_meta->psz_nowplaying &&
92         *p_input->input.p_item->p_meta->psz_nowplaying )
93     {
94         text.sprintf( "%s - %s",
95                   p_input->input.p_item->p_meta->psz_nowplaying,
96                   p_input->input.p_item->psz_name );
97     }
98     else
99     {
100         text.sprintf( "%s", p_input->input.p_item->psz_name );
101     }
102     emit nameChanged( text );
103
104 }
105
106 void InputManager::sliderUpdate( float new_pos )
107 {
108    if( p_input && !p_input->b_die && !p_input->b_dead )
109         var_SetFloat( p_input, "position", new_pos );
110 }
111
112 /**********************************************************************
113  * MainInputManager implementation. Wrap an input manager and
114  * take care of updating the main playlist input
115  **********************************************************************/
116 MainInputManager * MainInputManager::instance = NULL;
117
118 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
119                                                 p_intf( _p_intf )
120 {
121     p_input = NULL;
122     im = new InputManager( this, p_intf );
123     /* Get timer updates */
124     connect( DialogsProvider::getInstance(p_intf)->fixed_timer,
125              SIGNAL(timeout() ), this, SLOT( updateInput() ) );
126     /* Warn our embedded IM about input changes */
127     connect( this, SIGNAL( inputChanged( input_thread_t * ) ),
128              im, SLOT( setInput( input_thread_t * ) ) );
129 }
130
131 void MainInputManager::updateInput()
132 {
133     vlc_mutex_lock( &p_intf->change_lock );
134     if( p_input && p_input->b_dead )
135     {
136         vlc_object_release( p_input );
137         p_input = NULL;
138         emit inputChanged( NULL );
139     }
140
141     if( !p_input )
142     {
143         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
144                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
145         assert( p_playlist );
146         PL_LOCK;
147         p_input = p_playlist->p_input;
148         if( p_input )
149         {
150             vlc_object_yield( p_input );
151             emit inputChanged( p_input );
152         }
153         PL_UNLOCK;
154         vlc_object_release( p_playlist );
155     }
156     vlc_mutex_unlock( &p_intf->change_lock );
157 }