]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
129d545fb3d98c249186b796d36de015e43e3266
[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
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        UpdatePosition();
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