1 /*****************************************************************************
2 * main_inteface.cpp : Main interface
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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. *****************************************************************************/
24 #include "main_interface.hpp"
25 #include "input_manager.hpp"
26 #include "util/input_slider.hpp"
27 #include "util/qvlcframe.hpp"
28 #include "dialogs_provider.hpp"
29 #include "components/interface_widgets.hpp"
30 #include "dialogs/playlist.hpp"
33 #include <QCloseEvent>
34 #include <QPushButton>
50 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
53 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
54 int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
56 return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
58 static void DoRelease( intf_thread_t *p_intf, void *p_win )
60 return p_intf->p_sys->p_mi->releaseVideo( p_win );
62 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
64 return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
67 bool embeddedPlaylistWasActive;
70 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
72 settings = new QSettings( "VideoLAN", "VLC" );
73 settings->beginGroup( "MainWindow" );
75 need_components_update = false;
76 bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
77 embeddedPlaylistWasActive = false;
80 setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
81 handleMainUi( settings );
83 QVLCMenu::createMenuBar( menuBar(), p_intf );
85 timeLabel = new QLabel( 0 );
86 nameLabel = new QLabel( 0 );
87 statusBar()->addWidget( nameLabel, 4 );
88 statusBar()->addPermanentWidget( timeLabel, 1 );
90 setFocusPolicy( Qt::StrongFocus );
92 /* Init input manager */
93 MainInputManager::getInstance( p_intf );
94 ON_TIMEOUT( updateOnTimer() );
97 CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
98 /* Connect the input manager to the GUI elements it manages */
99 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
100 slider, setPosition( float,int, int ) );
101 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
102 this, setDisplay( float, int, int ) );
103 CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
104 CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
105 CONNECT( slider, sliderDragged( float ),
106 THEMIM->getIM(), sliderUpdate( float ) );
109 BUTTONACT( ui.playButton, play() );
110 BUTTONACT( ui.stopButton, stop() );
111 BUTTONACT( ui.nextButton, next() );
112 BUTTONACT( ui.prevButton, prev() );
113 BUTTONACT( ui.playlistButton, playlist() );
115 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
116 var_AddCallback( p_intf, "interaction", InteractCallback, this );
117 p_intf->b_interaction = VLC_TRUE;
120 MainInterface::~MainInterface()
122 /// \todo Save everything
123 p_intf->b_interaction = VLC_FALSE;
124 var_DelCallback( p_intf, "interaction", InteractCallback, this );
126 p_intf->pf_request_window = NULL;
127 p_intf->pf_release_window = NULL;
128 p_intf->pf_control_window = NULL;
131 void MainInterface::handleMainUi( QSettings *settings )
133 QWidget *main = new QWidget( this );
134 setCentralWidget( main );
135 ui.setupUi( centralWidget() );
137 slider = new InputSlider( Qt::Horizontal, NULL );
138 ui.hboxLayout->insertWidget( 0, slider );
140 ui.prevButton->setText( "" );
141 ui.nextButton->setText( "" );
142 ui.playButton->setText( "" );
143 ui.stopButton->setText( "" );
144 ui.prevButton->setIcon( QIcon( ":/pixmaps/previous.png" ) );
145 ui.nextButton->setIcon( QIcon( ":/pixmaps/next.png" ) );
146 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
147 ui.stopButton->setIcon( QIcon( ":/pixmaps/stop.png" ) );
150 ui.volLowLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
151 ui.volHighLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
152 ui.volumeSlider->setMaximum( 100 );
154 VolumeClickHandler *h = new VolumeClickHandler( this );
155 ui.volLowLabel->installEventFilter(h);
156 ui.volHighLabel->installEventFilter(h);
157 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
159 ui.playlistButton->setText( "" );
160 ui.playlistButton->setIcon( QIcon( ":/pixmaps/volume-low.png" ) );
162 /* Fetch configuration from settings and vlc config */
163 videoEmbeddedFlag = false;
164 if( config_GetInt( p_intf, "embedded-video" ) )
165 videoEmbeddedFlag = true;
167 playlistEmbeddedFlag = true;
168 /// \todo fetch playlist settings
170 resize ( PREF_W, PREF_H );
172 if( videoEmbeddedFlag )
174 videoWidget = new VideoWidget( p_intf );
175 videoWidget->widgetSize = QSize( 1, 1 );
176 videoWidget->resize( videoWidget->widgetSize );
177 ui.vboxLayout->insertWidget( 0, videoWidget );
179 p_intf->pf_request_window = ::DoRequest;
180 p_intf->pf_release_window = ::DoRelease;
181 p_intf->pf_control_window = ::DoControl;
183 if( config_GetInt( p_intf, "qt-always-video" ))
185 // bgWidget = new BackgroundWidget( p_intf );
186 // bgWidget->widgetSize = settings->value( "backgroundSize",
187 // QSize( 200, 200 ) ).toSize();
188 // ui.vboxLayout->insertWidget( 0, bgWidget );
193 // Size for fixed elements
194 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
196 calculateInterfaceSize();
198 /// \bug still needed ?
201 setMinimumSize( PREF_W, addSize.height() );
204 /**********************************************************************
205 * Handling of the components
206 **********************************************************************/
207 void MainInterface::calculateInterfaceSize()
209 int width = 0, height = 0;
210 fprintf( stderr, "Calculating size\n" );
211 if( bgWidget && bgWidget->isVisible() )
213 width = bgWidget->widgetSize.width();
214 height = bgWidget->widgetSize.height();
215 assert( !playlistWidget->isVisible() );
217 else if( playlistWidget && playlistWidget->isVisible() )
219 width = playlistWidget->widgetSize.width();
220 height = playlistWidget->widgetSize.height();
221 fprintf( stderr, "Have %ix%i playlist\n", width, height );
223 else if( videoIsActive )
225 width = videoWidget->widgetSize.width() ;
226 height = videoWidget->widgetSize.height();
227 fprintf( stderr, "Video Size %ix%i\n", videoWidget->widgetSize.width(), videoWidget->widgetSize.height() );
229 mainSize.setWidth( width + addSize.width() );
230 mainSize.setHeight( height + addSize.height() );
233 void MainInterface::resizeEvent( QResizeEvent *e )
235 fprintf( stderr, "Resize event to %ix%i\n", e->size().width(), e->size().height() );
236 /* Width : Always passed to all children. We are guaranteed it will
237 * not go below PREF_W
238 * Height : Only passed to the only visible child
242 videoWidget->widgetSize.setWidth( e->size().width() -
246 videoWidget->widgetSize.setHeight( e->size().height() -
248 videoWidget->updateGeometry();
250 fprintf( stderr, "Video set to %ix%\ni", videoWidget->widgetSize.width(), videoWidget->widgetSize.height() );
254 playlistWidget->widgetSize.setWidth( e->size().width() -
256 if( playlistWidget->isVisible() )
258 playlistWidget->widgetSize.setHeight( e->size().height() -
260 playlistWidget->updateGeometry();
262 fprintf( stderr, "Playlist set to %ix%i\n",playlistWidget->widgetSize.width(), playlistWidget->widgetSize.height() );
264 // videoWidget->widgetSize.setHeight( e->size().height() - addSize.height() );
265 // videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
266 // videoWidget->updateGeometry() ;
269 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
270 int *pi_y, unsigned int *pi_width,
271 unsigned int *pi_height )
273 void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
276 videoIsActive = true;
277 if( playlistWidget && playlistWidget->isVisible() )
279 embeddedPlaylistWasActive = true;
280 playlistWidget->hide();
282 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
283 videoWidget->updateGeometry(); /// FIXME: Needed ?
284 need_components_update = true;
289 void MainInterface::releaseVideo( void *p_win )
291 videoWidget->release( p_win );
292 videoWidget->widgetSize = QSize( 1, 1 );
293 videoWidget->updateGeometry();
294 if( embeddedPlaylistWasActive )
296 playlistWidget->show();
298 videoIsActive = false;
299 need_components_update = true;
302 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
304 int i_ret = VLC_EGENERIC;
309 unsigned int *pi_width = va_arg( args, unsigned int * );
310 unsigned int *pi_height = va_arg( args, unsigned int * );
311 *pi_width = videoWidget->widgetSize.width();
312 *pi_height = videoWidget->widgetSize.height();
318 unsigned int i_width = va_arg( args, unsigned int );
319 unsigned int i_height = va_arg( args, unsigned int );
320 // if( !i_width && p_vout ) i_width = p_vout->i_window_width;
321 // if( !i_height && p_vout ) i_height = p_vout->i_window_height;
322 videoWidget->widgetSize = QSize( i_width, i_height );
323 videoWidget->updateGeometry();
324 need_components_update = true;
328 case VOUT_SET_STAY_ON_TOP:
330 msg_Warn( p_intf, "unsupported control query" );
336 void MainInterface::playlist()
338 // Toggle the playlist dialog
339 if( !playlistEmbeddedFlag )
345 THEDP->playlistDialog();
349 if( !playlistWidget )
351 PlaylistDialog::killInstance();
352 playlistWidget = new PlaylistWidget( p_intf );
353 ui.vboxLayout->insertWidget( 0, playlistWidget );
354 fprintf( stderr, "BUG ! Do not set size if it has already been changed\n" );
355 playlistWidget->widgetSize = settings->value( "playlistSize",
356 QSize( 600, 300 ) ).toSize();
357 playlistWidget->hide();
359 /// Todo, reset its size ?
360 if( playlistWidget->isVisible() )
362 fprintf( stderr, "hiding playlist\n" );
363 playlistWidget->hide();
371 fprintf( stderr, "showing playlist\n" );
372 playlistWidget->show();
377 calculateInterfaceSize();
381 void MainInterface::doComponentsUpdate()
383 calculateInterfaceSize();
387 /************************************************************************
389 ************************************************************************/
390 void MainInterface::keyPressEvent( QKeyEvent *e )
393 /* Handle modifiers */
394 if( e->modifiers()& Qt::ShiftModifier ) i_vlck |= KEY_MODIFIER_SHIFT;
395 if( e->modifiers()& Qt::AltModifier ) i_vlck |= KEY_MODIFIER_ALT;
396 if( e->modifiers()& Qt::ControlModifier ) i_vlck |= KEY_MODIFIER_CTRL;
397 if( e->modifiers()& Qt::MetaModifier ) i_vlck |= KEY_MODIFIER_META;
400 /* Look for some special keys */
401 #define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break
404 HANDLE( Key_Left, KEY_LEFT );
405 HANDLE( Key_Right, KEY_RIGHT );
406 HANDLE( Key_Up, KEY_UP );
407 HANDLE( Key_Down, KEY_DOWN );
408 HANDLE( Key_Space, KEY_SPACE );
409 HANDLE( Key_Escape, KEY_ESC );
410 HANDLE( Key_Enter, KEY_ENTER );
411 HANDLE( Key_F1, KEY_F1 );
412 HANDLE( Key_F2, KEY_F2 );
413 HANDLE( Key_F3, KEY_F3 );
414 HANDLE( Key_F4, KEY_F4 );
415 HANDLE( Key_F5, KEY_F5 );
416 HANDLE( Key_F6, KEY_F6 );
417 HANDLE( Key_F7, KEY_F7 );
418 HANDLE( Key_F8, KEY_F8 );
419 HANDLE( Key_F9, KEY_F9 );
420 HANDLE( Key_F10, KEY_F10 );
421 HANDLE( Key_F11, KEY_F11 );
422 HANDLE( Key_F12, KEY_F12 );
423 HANDLE( Key_PageUp, KEY_PAGEUP );
424 HANDLE( Key_PageDown, KEY_PAGEDOWN );
425 HANDLE( Key_Home, KEY_HOME );
426 HANDLE( Key_End, KEY_END );
427 HANDLE( Key_Insert, KEY_INSERT );
428 HANDLE( Key_Delete, KEY_DELETE );
433 /* Force lowercase */
434 if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
435 i_vlck += e->key() + 32;
436 /* Rest of the ascii range */
437 else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
442 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
449 void MainInterface::stop()
451 playlist_Stop( THEPL );
453 void MainInterface::play()
455 if( !THEPL->i_size || !THEPL->i_enabled )
457 /* The playlist is empty, open a file requester */
462 THEMIM->togglePlayPause();
464 void MainInterface::prev()
466 playlist_Prev( THEPL );
468 void MainInterface::next()
470 playlist_Next( THEPL );
473 void MainInterface::setDisplay( float pos, int time, int length )
475 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
476 secstotimestr( psz_length, length );
477 secstotimestr( psz_time, time );
479 title.sprintf( "%s/%s", psz_time, psz_length );
480 timeLabel->setText( " "+title+" " );
483 void MainInterface::setName( QString name )
485 nameLabel->setText( " " + name+" " );
488 void MainInterface::setStatus( int status )
490 if( status == 1 ) // Playing
491 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
493 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
496 static bool b_my_volume;
498 void MainInterface::updateOnTimer()
502 QApplication::closeAllWindows();
503 DialogsProvider::killInstance();
504 QApplication::quit();
506 if( need_components_update )
508 doComponentsUpdate();
509 need_components_update = false;
512 audio_volume_t i_volume;
513 aout_VolumeGet( p_intf, &i_volume );
514 i_volume = (i_volume * 200 )/ AOUT_VOLUME_MAX ;
515 int i_gauge = ui.volumeSlider->value();
517 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
520 ui.volumeSlider->setValue( i_volume );
525 void MainInterface::closeEvent( QCloseEvent *e )
528 p_intf->b_die = VLC_TRUE;
531 void MainInterface::updateVolume( int sliderVolume )
535 int i_res = sliderVolume * AOUT_VOLUME_MAX /
536 (2*ui.volumeSlider->maximum() );
537 aout_VolumeSet( p_intf, i_res );
541 static int InteractCallback( vlc_object_t *p_this,
542 const char *psz_var, vlc_value_t old_val,
543 vlc_value_t new_val, void *param )
545 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
546 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
548 MainInterface *p_interface = (MainInterface*)param;
549 DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
550 QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );