]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
a4f844cfd3bd6cfd30318de3be7d497460d6f459
[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         UpdateTitle();
228     }
229     else if ( type == ItemRateChanged_Type )
230     {
231        UpdateRate();
232     }
233     else if ( type == ItemTitleChanged_Type )
234     {
235        UpdateTitle();
236     }
237     else if (type == ItemStateChanged_Type )
238     {
239        UpdateStatus();
240     }
241 }
242
243 void InputManager::UpdatePosition( void )
244 {
245      /* Update position */
246      int i_length, i_time; /* Int is enough, since we store seconds */
247      float f_pos;
248      i_length = var_GetTime(  p_input , "length" ) / 1000000;
249      i_time = var_GetTime(  p_input , "time") / 1000000;
250      f_pos = var_GetFloat(  p_input , "position" );
251      emit positionUpdated( f_pos, i_time, i_length );
252 }
253
254 void InputManager::UpdateTitle( void )
255 {
256      /* Update navigation status */
257      vlc_value_t val; val.i_int = 0;
258      var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
259      msg_Dbg( p_intf, "updateTitle called" );
260      if( val.i_int > 0 )
261      {
262          val.i_int = 0;
263          var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
264          emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
265      }
266      else
267      {
268          emit navigationChanged( 0 );
269      }
270 }
271
272 void InputManager::UpdateStatus( void )
273 {
274      /* Update playing status */
275      vlc_value_t val; val.i_int = 0;
276      var_Get( p_input, "state", &val );
277      if( i_old_playing_status != val.i_int )
278      {
279          i_old_playing_status = val.i_int;
280          emit statusChanged( val.i_int );
281      }
282 }
283
284 void InputManager::UpdateRate( void )
285 {
286      /* Update Rate */
287      int i_new_rate = var_GetInteger( p_input, "rate");
288      if( i_new_rate != i_rate )
289      {
290          i_rate = i_new_rate;
291          /* Update rate */
292          emit rateChanged( i_rate );
293      }
294 }
295
296 void InputManager::UpdateMeta( void )
297 {
298      /* Update text */
299      QString text;
300      char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
301      char *psz_nowplaying =
302          input_item_GetNowPlaying( input_GetItem( p_input ) );
303      char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
304      if( EMPTY_STR( psz_name ) )
305      {
306          free( psz_name );
307          psz_name = input_item_GetName( input_GetItem( p_input ) );
308      }
309      if( !EMPTY_STR( psz_nowplaying ) )
310      {
311          text.sprintf( "%s - %s", psz_nowplaying, psz_name );
312      }
313      else if( !EMPTY_STR( psz_artist ) )
314      {
315          text.sprintf( "%s - %s", psz_artist, psz_name );
316      }
317      else
318      {
319          text.sprintf( "%s", psz_name );
320      }
321      free( psz_name );
322      free( psz_nowplaying );
323      free( psz_artist );
324      if( old_name != text )
325      {
326          emit nameChanged( text );
327          old_name=text;
328      }
329
330      QString url;
331      char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
332      url.sprintf("%s", psz_art );
333      free( psz_art );
334      if( artUrl != url )
335      {
336          artUrl = url.replace( "file://",QString("" ) );
337          emit artChanged( artUrl );
338      }
339 #ifdef ZVBI_COMPILED
340      /* Update teletext status*/
341      emit teletextEnabled( true );/* FIXME */
342 #endif
343
344 }
345
346 void InputManager::sliderUpdate( float new_pos )
347 {
348     if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
349 }
350
351 void InputManager::togglePlayPause()
352 {
353     vlc_value_t state;
354     var_Get( p_input, "state", &state );
355     state.i_int = ( ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S );
356     msg_Dbg( p_input, "state : %d", state.i_int );
357     /*{
358         /* A stream is being played, pause it */
359        /* state.i_int = PAUSE_S;
360     }
361     else
362     {
363         /* Stream is paused, resume it */
364         /*state.i_int = PLAYING_S;
365     }*/
366     var_Set( p_input, "state", state );
367     emit statusChanged( state.i_int );
368 }
369
370 void InputManager::sectionPrev()
371 {
372     if( hasInput() )
373     {
374         int i_type = var_Type( p_input, "next-chapter" );
375         vlc_value_t val; val.b_bool = VLC_TRUE;
376         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
377                             "prev-chapter":"prev-title", val );
378     }
379 }
380
381 void InputManager::sectionNext()
382 {
383     if( hasInput() )
384     {
385         int i_type = var_Type( p_input, "next-chapter" );
386         vlc_value_t val; val.b_bool = VLC_TRUE;
387         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
388                             "next-chapter":"next-title", val );
389     }
390 }
391
392 void InputManager::sectionMenu()
393 {
394     if( hasInput() )
395         var_SetInteger( p_input, "title 0", 2 );
396 }
397
398 #ifdef ZVBI_COMPILED
399 void InputManager::telexGotoPage( int page )
400 {
401     if( hasInput() )
402     {
403         vlc_object_t *p_vbi;
404         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
405                     "zvbi", FIND_ANYWHERE );
406         if( p_vbi )
407         {
408             var_SetInteger( p_vbi, "vbi-page", page );
409             vlc_object_release( p_vbi );
410         }
411     }
412 }
413
414 void InputManager::telexToggle( bool b_enabled )
415 {
416     int i_page = 0;
417
418     if( b_enabled )
419         i_page = 100;
420     telexGotoPage( i_page );
421 }
422
423 void InputManager::telexSetTransparency( bool b_transp )
424 {
425     if( hasInput() )
426     {
427         vlc_object_t *p_vbi;
428         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
429                     "zvbi", FIND_ANYWHERE );
430         if( p_vbi )
431         {
432             var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
433             vlc_object_release( p_vbi );
434         }
435     }
436 }
437 #endif
438
439 void InputManager::slower()
440 {
441     if( hasInput() )
442         var_SetVoid( p_input, "rate-slower" );
443 }
444
445 void InputManager::faster()
446 {
447     if( hasInput() )
448         var_SetVoid( p_input, "rate-faster" );
449 }
450
451 void InputManager::normalRate()
452 {
453     if( hasInput() )
454         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
455 }
456
457 void InputManager::setRate( int new_rate )
458 {
459     if( hasInput() )
460         var_SetInteger( p_input, "rate", new_rate );
461 }
462
463 /**********************************************************************
464  * MainInputManager implementation. Wrap an input manager and
465  * take care of updating the main playlist input
466  **********************************************************************/
467 MainInputManager * MainInputManager::instance = NULL;
468
469 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
470                                                 p_intf( _p_intf )
471 {
472     p_input = NULL;
473     im = new InputManager( this, p_intf );
474     var_AddCallback( THEPL, "playlist-current", InputChanged, this );
475     var_AddCallback( THEPL, "activity", InputChanged, this );
476     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
477     /* Warn our embedded IM about input changes */
478     CONNECT( this, inputChanged( input_thread_t * ),
479              im,   setInput( input_thread_t * ) );
480 }
481
482 MainInputManager::~MainInputManager()
483 {
484     if( p_input )
485     {
486        vlc_object_release( p_input );
487        emit inputChanged( NULL );
488     }
489     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
490     var_DelCallback( THEPL, "playlist-current", InputChanged, this );
491     var_DelCallback( THEPL, "activity", InputChanged, this );
492 }
493
494 void MainInputManager::customEvent( QEvent *event )
495 {
496     int type = event->type();
497     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
498         return;
499
500     if( type == VolumeChanged_Type )
501     {
502         emit volumeChanged();
503         return;
504     }
505
506     if( VLC_OBJECT_INTF == p_intf->i_object_type )
507     {
508         vlc_mutex_lock( &p_intf->change_lock );
509         if( p_input && ( p_input->b_dead || p_input->b_die ) )
510         {
511             var_DelCallback( p_input, "state", InputChanged, this );
512             vlc_object_release( p_input );
513             p_input = NULL;
514             emit inputChanged( NULL );
515         }
516
517         if( !p_input )
518         {
519             QPL_LOCK;
520             p_input = THEPL->p_input;
521             if( p_input && !( p_input->b_die || p_input->b_dead) )
522             {
523                 vlc_object_yield( p_input );
524                 var_AddCallback( p_input, "state", InputChanged, this );
525                 emit inputChanged( p_input );
526             }
527             else
528                 p_input = NULL;
529             QPL_UNLOCK;
530         }
531         vlc_mutex_unlock( &p_intf->change_lock );
532     }
533     else {
534         /* we are working as a dialogs provider */
535         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
536                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
537         if( p_playlist )
538         {
539             p_input = p_playlist->p_input;
540             vlc_object_yield( p_input );
541             emit inputChanged( p_input );
542         }
543     }
544 }
545
546 void MainInputManager::stop()
547 {
548    playlist_Stop( THEPL );
549 }
550
551 void MainInputManager::next()
552 {
553    playlist_Next( THEPL );
554 }
555
556 void MainInputManager::prev()
557 {
558    playlist_Prev( THEPL );
559 }
560
561 void MainInputManager::togglePlayPause()
562 {
563     if( p_input == NULL )
564     {
565         playlist_Play( THEPL );
566         return;
567     }
568     getIM()->togglePlayPause();
569 }
570
571
572
573 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
574                         vlc_value_t n, void *param )
575 {
576     InputManager *im = (InputManager*)param;
577     im->b_has_audio = true;
578     return VLC_SUCCESS;
579 }
580
581 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
582                         vlc_value_t n, void *param )
583 {
584     InputManager *im = (InputManager*)param;
585     im->b_has_video = true;
586     return VLC_SUCCESS;
587 }
588