]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Qt4 - Try to improve quitting of the application... Ref #1397
[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-2007 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 "qt4.hpp"
25 #include "input_manager.hpp"
26 #include "dialogs_provider.hpp"
27
28 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
29                         vlc_value_t n, void *param );
30 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
31                         vlc_value_t n, void *param );
32 static int ItemChanged( vlc_object_t *, const char *,
33                         vlc_value_t, vlc_value_t, void * );
34 static int InterfaceChanged( vlc_object_t *, const char *,
35                             vlc_value_t, vlc_value_t, void * );
36 static int ItemStateChanged( vlc_object_t *, const char *,
37                         vlc_value_t, vlc_value_t, void * );
38 static int ItemRateChanged( vlc_object_t *, const char *,
39                         vlc_value_t, vlc_value_t, void * );
40 static int ItemTitleChanged( vlc_object_t *, const char *,
41                         vlc_value_t, vlc_value_t, void * );
42 static int VolumeChanged( vlc_object_t *, const char *,
43                         vlc_value_t, vlc_value_t, void * );
44
45 /**********************************************************************
46  * InputManager implementation
47  **********************************************************************/
48
49 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
50                            QObject( parent ), p_intf( _p_intf )
51 {
52     i_old_playing_status = END_S;
53     old_name = "";
54     p_input = NULL;
55     i_rate = 0;
56     var_AddCallback( THEPL, "playlist-current", ItemChanged, this );
57     var_AddCallback( THEPL, "intf-change", ItemChanged, this );
58 }
59
60 InputManager::~InputManager()
61 {
62     delInput();
63     var_DelCallback( THEPL, "playlist-current", ItemChanged, this );
64     var_DelCallback( THEPL, "intf-change", ItemChanged, this );
65 }
66
67 void InputManager::setInput( input_thread_t *_p_input )
68 {
69     delInput();
70     p_input = _p_input;
71     b_had_audio = b_had_video = b_has_audio = b_has_video = false;
72     if( p_input )
73     {
74         vlc_object_yield( p_input );
75         vlc_value_t val;
76         var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
77         b_has_video = val.i_int > 0;
78         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
79         b_has_audio = val.i_int > 0;
80         emit statusChanged( PLAYING_S );
81         addCallbacks();
82     }
83 }
84
85 void InputManager::delInput()
86 {
87     if( p_input )
88     {
89         delCallbacks();
90         vlc_object_release( p_input );
91         p_input = NULL;
92     }
93     i_old_playing_status = END_S;
94     old_name=qfu("");
95     artUrl = qfu("");
96     emit positionUpdated( 0.0, 0 ,0 );
97     emit statusChanged( END_S );
98     emit nameChanged( "" );
99     emit artChanged( "" );
100 }
101
102 void InputManager::delCallbacks( void )
103 {
104     var_DelCallback( p_input, "audio-es", ChangeAudio, this );
105     var_DelCallback( p_input, "video-es", ChangeVideo, this );
106     var_DelCallback( THEPL, "state", ItemStateChanged, this );
107     var_DelCallback( p_input, "rate", ItemRateChanged, this );
108     var_DelCallback( p_input, "title", ItemTitleChanged, this );
109     var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
110     var_DelCallback( THEPL, "item-change", ItemChanged, this );
111 }
112
113 void InputManager::addCallbacks( void )
114 {
115     var_AddCallback( p_input, "audio-es", ChangeAudio, this );
116     var_AddCallback( p_input, "video-es", ChangeVideo, this );
117     /* src/playlist/control.c */
118     var_AddCallback( THEPL, "state", ItemStateChanged, this );
119     /* src/input/input.c:1765 */
120     var_AddCallback( p_input, "rate", ItemRateChanged, this );
121     /* src/input/input.c:2003 */
122     var_AddCallback( p_input, "title", ItemTitleChanged, this );
123     /* src/input/input.c:734 for timers update*/
124     var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
125     /* src/input/input.c:2076*/
126     var_AddCallback( THEPL, "item-change", ItemChanged, this );
127 }
128
129 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
130                             vlc_value_t oldval, vlc_value_t newval, void *param )
131 {
132     static int counter = 0;
133     InputManager *im = (InputManager*)param;
134
135     counter = counter++ % 4;
136     if(!counter)
137         return VLC_SUCCESS;
138     IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
139     QApplication::postEvent( im, static_cast<QEvent*>(event) );
140     return VLC_SUCCESS;
141 }
142
143 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
144                             vlc_value_t oldval, vlc_value_t newval, void *param )
145 {
146     InputManager *im = (InputManager*)param;
147
148     IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
149     QApplication::postEvent( im, static_cast<QEvent*>(event) );
150     return VLC_SUCCESS;
151 }
152
153 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
154                             vlc_value_t oldval, vlc_value_t newval, void *param )
155 {
156     InputManager *im = (InputManager*)param;
157
158     IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
159     QApplication::postEvent( im, static_cast<QEvent*>(event) );
160     return VLC_SUCCESS;
161 }
162
163 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
164                             vlc_value_t oldval, vlc_value_t newval, void *param )
165 {
166     InputManager *im = (InputManager*)param;
167
168     IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
169     QApplication::postEvent( im, static_cast<QEvent*>(event) );
170     return VLC_SUCCESS;
171 }
172
173 static int InputChanged( vlc_object_t *p_this, const char *psz_var,
174                         vlc_value_t oldval, vlc_value_t newval, void *param )
175 {
176     MainInputManager *im = (MainInputManager*)param;
177
178     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
179     QApplication::postEvent( im, static_cast<QEvent*>(event) );
180     return VLC_SUCCESS;
181 }
182
183 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
184                         vlc_value_t oldval, vlc_value_t newval, void *param )
185 {
186     MainInputManager *im = (MainInputManager*)param;
187
188     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
189     QApplication::postEvent( im, static_cast<QEvent*>(event) );
190     return VLC_SUCCESS;
191 }
192
193 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
194                         vlc_value_t oldval, vlc_value_t newval, void *param )
195 {
196     InputManager *im = (InputManager*)param;
197
198     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
199     QApplication::postEvent( im, static_cast<QEvent*>(event) );
200     return VLC_SUCCESS;
201 }
202
203 void InputManager::customEvent( QEvent *event )
204 {
205     int type = event->type();
206     if ( type != PositionUpdate_Type &&  type != ItemChanged_Type &&
207          type != ItemRateChanged_Type && type != ItemTitleChanged_Type &&
208          type != ItemStateChanged_Type )
209         return;
210
211     if(!p_input || p_input->b_dead || p_input->b_die )
212     {
213          delInput();
214          return;
215     }
216
217     IMEvent *ime = static_cast<IMEvent *>(event);
218
219     if ( type == PositionUpdate_Type )
220     {
221         UpdatePosition();
222     }
223     else if ( type == ItemChanged_Type )
224     {
225         UpdateMeta();
226     }
227     else if ( type == ItemRateChanged_Type )
228     {
229        UpdatePosition();
230     }
231     else if ( type == ItemTitleChanged_Type )
232     {
233        UpdateTitle();
234     }
235     else if (type == ItemStateChanged_Type )
236     {
237        UpdateStatus();
238     }
239 }
240
241 void InputManager::UpdatePosition( void )
242 {
243      /* Update position */
244      int i_length, i_time; /* Int is enough, since we store seconds */
245      float f_pos;
246      i_length = var_GetTime(  p_input , "length" ) / 1000000;
247      i_time = var_GetTime(  p_input , "time") / 1000000;
248      f_pos = var_GetFloat(  p_input , "position" );
249      emit positionUpdated( f_pos, i_time, i_length );
250 }
251
252 void InputManager::UpdateTitle( void )
253 {
254      /* Update navigation status */
255      vlc_value_t val; val.i_int = 0;
256      var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
257      if( val.i_int > 0 )
258      {
259          val.i_int = 0;
260          var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
261          emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
262      }
263      else
264      {
265          emit navigationChanged( 0 );
266      }
267 }
268
269 void InputManager::UpdateStatus( void )
270 {
271      /* Update playing status */
272      vlc_value_t val; val.i_int = 0;
273      var_Get( p_input, "state", &val );
274      if( i_old_playing_status != val.i_int )
275      {
276          i_old_playing_status = val.i_int;
277          emit statusChanged( val.i_int );
278      }
279 }
280
281 void InputManager::UpdateRate( void )
282 {
283      /* Update Rate */
284      int i_new_rate = var_GetInteger( p_input, "rate");
285      if( i_new_rate != i_rate )
286      {
287          i_rate = i_new_rate;
288          /* Update rate */
289          emit rateChanged( i_rate );
290      }
291 }
292
293 void InputManager::UpdateMeta( void )
294 {
295      /* Update text */
296      QString text;
297      char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
298      char *psz_nowplaying =
299          input_item_GetNowPlaying( input_GetItem( p_input ) );
300      char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
301      if( EMPTY_STR( psz_name ) )
302      {
303          free( psz_name );
304          psz_name = input_item_GetName( input_GetItem( p_input ) );
305      }
306      if( !EMPTY_STR( psz_nowplaying ) )
307      {
308          text.sprintf( "%s - %s", psz_nowplaying, psz_name );
309      }
310      else if( !EMPTY_STR( psz_artist ) )
311      {
312          text.sprintf( "%s - %s", psz_artist, psz_name );
313      }
314      else
315      {
316          text.sprintf( "%s", psz_name );
317      }
318      free( psz_name );
319      free( psz_nowplaying );
320      free( psz_artist );
321      if( old_name != text )
322      {
323          emit nameChanged( text );
324          old_name=text;
325      }
326
327      QString url;
328      char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
329      url.sprintf("%s", psz_art );
330      free( psz_art );
331      if( artUrl != url )
332      {
333          artUrl = url.replace( "file://",QString("" ) );
334          emit artChanged( artUrl );
335      }
336 #ifdef ZVBI_COMPILED
337      /* Update teletext status*/
338      emit teletextEnabled( true );/* FIXME */
339 #endif
340
341 }
342
343 void InputManager::sliderUpdate( float new_pos )
344 {
345     if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
346 }
347
348 void InputManager::togglePlayPause()
349 {
350     vlc_value_t state;
351     var_Get( p_input, "state", &state );
352     state.i_int = ( ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S );
353     msg_Dbg( p_input, "state : %d", state.i_int );
354     /*{
355         /* A stream is being played, pause it */
356        /* state.i_int = PAUSE_S;
357     }
358     else
359     {
360         /* Stream is paused, resume it */
361         /*state.i_int = PLAYING_S;
362     }*/
363     var_Set( p_input, "state", state );
364     emit statusChanged( state.i_int );
365 }
366
367 void InputManager::sectionPrev()
368 {
369     if( hasInput() )
370     {
371         int i_type = var_Type( p_input, "next-chapter" );
372         vlc_value_t val; val.b_bool = VLC_TRUE;
373         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
374                             "prev-chapter":"prev-title", val );
375     }
376 }
377
378 void InputManager::sectionNext()
379 {
380     if( hasInput() )
381     {
382         int i_type = var_Type( p_input, "next-chapter" );
383         vlc_value_t val; val.b_bool = VLC_TRUE;
384         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
385                             "next-chapter":"next-title", val );
386     }
387 }
388
389 void InputManager::sectionMenu()
390 {
391     if( hasInput() )
392         var_SetInteger( p_input, "title 0", 2 );
393 }
394
395 #ifdef ZVBI_COMPILED
396 void InputManager::telexGotoPage( int page )
397 {
398     if( hasInput() )
399     {
400         vlc_object_t *p_vbi;
401         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
402                     "zvbi", FIND_ANYWHERE );
403         if( p_vbi )
404         {
405             var_SetInteger( p_vbi, "vbi-page", page );
406             vlc_object_release( p_vbi );
407         }
408     }
409 }
410
411 void InputManager::telexToggle( bool b_enabled )
412 {
413     int i_page = 0;
414
415     if( b_enabled )
416         i_page = 100;
417     telexGotoPage( i_page );
418 }
419
420 void InputManager::telexSetTransparency( bool b_transp )
421 {
422     if( hasInput() )
423     {
424         vlc_object_t *p_vbi;
425         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
426                     "zvbi", FIND_ANYWHERE );
427         if( p_vbi )
428         {
429             var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
430             vlc_object_release( p_vbi );
431         }
432     }
433 }
434 #endif
435
436 void InputManager::slower()
437 {
438     if( hasInput() )
439         var_SetVoid( p_input, "rate-slower" );
440 }
441
442 void InputManager::faster()
443 {
444     if( hasInput() )
445         var_SetVoid( p_input, "rate-faster" );
446 }
447
448 void InputManager::normalRate()
449 {
450     if( hasInput() )
451         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
452 }
453
454 void InputManager::setRate( int new_rate )
455 {
456     if( hasInput() )
457         var_SetInteger( p_input, "rate", new_rate );
458 }
459
460 /**********************************************************************
461  * MainInputManager implementation. Wrap an input manager and
462  * take care of updating the main playlist input
463  **********************************************************************/
464 MainInputManager * MainInputManager::instance = NULL;
465
466 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
467                                                 p_intf( _p_intf )
468 {
469     p_input = NULL;
470     im = new InputManager( this, p_intf );
471     var_AddCallback( THEPL, "playlist-current", InputChanged, this );
472     var_AddCallback( THEPL, "activity", InputChanged, this );
473     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
474     /* Warn our embedded IM about input changes */
475     CONNECT( this, inputChanged( input_thread_t * ),
476              im,   setInput( input_thread_t * ) );
477 }
478
479 MainInputManager::~MainInputManager()
480 {
481     if( p_input )
482     {
483        vlc_object_release( p_input );
484        emit inputChanged( NULL );
485     }
486     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
487     var_DelCallback( THEPL, "playlist-current", InputChanged, this );
488     var_DelCallback( THEPL, "activity", InputChanged, this );
489 }
490
491 void MainInputManager::customEvent( QEvent *event )
492 {
493     int type = event->type();
494     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
495         return;
496
497     if( type == VolumeChanged_Type )
498     {
499         emit volumeChanged();
500         return;
501     }
502
503     if( VLC_OBJECT_INTF == p_intf->i_object_type )
504     {
505         vlc_mutex_lock( &p_intf->change_lock );
506         if( p_input && ( p_input->b_dead || p_input->b_die ) )
507         {
508             var_DelCallback( p_input, "state", InputChanged, this );
509             vlc_object_release( p_input );
510             p_input = NULL;
511             emit inputChanged( NULL );
512         }
513
514         if( !p_input )
515         {
516             QPL_LOCK;
517             p_input = THEPL->p_input;
518             if( p_input && !( p_input->b_die || p_input->b_dead) )
519             {
520                 vlc_object_yield( p_input );
521                 var_AddCallback( p_input, "state", InputChanged, this );
522                 emit inputChanged( p_input );
523             }
524             else
525                 p_input = NULL;
526             QPL_UNLOCK;
527         }
528         vlc_mutex_unlock( &p_intf->change_lock );
529     }
530     else {
531         /* we are working as a dialogs provider */
532         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
533                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
534         if( p_playlist )
535         {
536             p_input = p_playlist->p_input;
537             vlc_object_yield( p_input );
538             emit inputChanged( p_input );
539         }
540     }
541 }
542
543 void MainInputManager::stop()
544 {
545    playlist_Stop( THEPL );
546 }
547
548 void MainInputManager::next()
549 {
550    playlist_Next( THEPL );
551 }
552
553 void MainInputManager::prev()
554 {
555    playlist_Prev( THEPL );
556 }
557
558 void MainInputManager::togglePlayPause()
559 {
560     if( p_input == NULL )
561     {
562         playlist_Play( THEPL );
563         return;
564     }
565     getIM()->togglePlayPause();
566 }
567
568
569
570 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
571                         vlc_value_t n, void *param )
572 {
573     InputManager *im = (InputManager*)param;
574     im->b_has_audio = true;
575     return VLC_SUCCESS;
576 }
577
578 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
579                         vlc_value_t n, void *param )
580 {
581     InputManager *im = (InputManager*)param;
582     im->b_has_video = true;
583     return VLC_SUCCESS;
584 }
585