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