1 /*****************************************************************************
2 * intf_qt.cpp: Qt interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_qt.cpp,v 1.2 2001/03/18 00:01:13 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #define MODULE_NAME qt
28 #include "modules_inner.h"
30 /*****************************************************************************
32 *****************************************************************************/
35 #include <errno.h> /* ENOMEM */
36 #include <stdlib.h> /* free() */
37 #include <string.h> /* strerror() */
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
51 #include "intf_playlist.h"
52 #include "interface.h"
58 #include <qapplication.h>
59 #include <qmainwindow.h>
61 #include <qtoolbutton.h>
62 #include <qwhatsthis.h>
63 #include <qpushbutton.h>
64 #include <qfiledialog.h>
66 #include <qlcdnumber.h>
68 #include <qstatusbar.h>
69 #include <qmessagebox.h>
77 /*****************************************************************************
78 * Local Qt slider class
79 *****************************************************************************/
80 class IntfSlider : public QSlider
85 IntfSlider( intf_thread_t *, QWidget * ); /* Constructor and destructor */
88 bool b_free; /* Is the slider free ? */
90 int oldvalue ( void ) { return i_oldvalue; };
91 void setOldValue( int i_value ) { i_oldvalue = i_value; };
94 void SlideStart ( void ) { b_free = FALSE; };
95 void SlideStop ( void ) { b_free = TRUE; };
98 intf_thread_t *p_intf;
102 /*****************************************************************************
103 * Local Qt interface window class
104 *****************************************************************************/
105 class IntfWindow : public QMainWindow
110 IntfWindow( intf_thread_t * );
116 void FileOpen ( void );
117 void FileQuit ( void ) { p_intf->b_die = 1; };
119 void PlaybackPlay ( void );
120 void PlaybackPause ( void );
121 void PlaybackSlow ( void );
122 void PlaybackFast ( void );
124 void PlaylistPrev ( void );
125 void PlaylistNext ( void );
127 void DateDisplay ( int );
130 void Unimplemented( void ) { intf_WarnMsg( 3, "intf warning: "
131 "unimplemented function" ); };
134 intf_thread_t *p_intf;
136 IntfSlider *p_slider;
143 #include "intf_qt.moc"
145 #define SLIDER_MIN 0x00000
146 #define SLIDER_MAX 0x10000
147 #define SLIDER_STEP (SLIDER_MAX >> 4)
149 /*****************************************************************************
150 * intf_sys_t: description and status of Qt interface
151 *****************************************************************************/
152 typedef struct intf_sys_s
155 IntfWindow *p_window;
159 /*****************************************************************************
161 *****************************************************************************/
162 static int intf_Probe ( probedata_t *p_data );
163 static int intf_Open ( intf_thread_t *p_intf );
164 static void intf_Close ( intf_thread_t *p_intf );
165 static void intf_Run ( intf_thread_t *p_intf );
167 /*****************************************************************************
168 * Functions exported as capabilities. They are declared as static so that
169 * we don't pollute the namespace too much.
170 *****************************************************************************/
174 void _M( intf_getfunctions )( function_list_t * p_function_list )
176 p_function_list->pf_probe = intf_Probe;
177 p_function_list->functions.intf.pf_open = intf_Open;
178 p_function_list->functions.intf.pf_close = intf_Close;
179 p_function_list->functions.intf.pf_run = intf_Run;
184 /*****************************************************************************
185 * intf_Probe: probe the interface and return a score
186 *****************************************************************************
187 * This function tries to initialize Qt and returns a score to the
188 * plugin manager so that it can select the best plugin.
189 *****************************************************************************/
190 static int intf_Probe( probedata_t *p_data )
192 if( TestMethod( INTF_METHOD_VAR, "qt" ) )
197 if( TestProgram( "qvlc" ) )
205 /*****************************************************************************
206 * intf_Open: initialize and create window
207 *****************************************************************************/
208 static int intf_Open( intf_thread_t *p_intf )
210 char *pp_argv[] = { "" };
213 /* Allocate instance and initialize some members */
214 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
215 if( p_intf->p_sys == NULL )
217 intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
221 /* Create the C++ objects */
222 p_intf->p_sys->p_app = new QApplication( i_argc, pp_argv );
223 p_intf->p_sys->p_window = new IntfWindow( p_intf );
225 /* Tell the world we are here */
226 p_intf->p_sys->p_window->setCaption( VOUT_TITLE " (Qt interface)" );
231 /*****************************************************************************
232 * intf_Close: destroy interface window
233 *****************************************************************************/
234 static void intf_Close( intf_thread_t *p_intf )
236 /* Get rid of the C++ objects */
237 delete p_intf->p_sys->p_window;
238 delete p_intf->p_sys->p_app;
240 /* Destroy structure */
241 free( p_intf->p_sys );
244 /*****************************************************************************
245 * intf_Run: Qt thread
246 *****************************************************************************
247 * This part of the interface is in a separate thread so that we can call
248 * exec() from within it without annoying the rest of the program.
249 *****************************************************************************/
250 static void intf_Run( intf_thread_t *p_intf )
252 p_intf->p_sys->p_window->show();
254 p_intf->p_sys->p_app->exec();
257 /* following functions are local */
259 /*****************************************************************************
260 * IntfWindow: interface window creator
261 *****************************************************************************
262 * This function creates the interface window, and populates it with a
263 * menu bar, a toolbar and a slider.
264 *****************************************************************************/
265 IntfWindow::IntfWindow( intf_thread_t *p_intf )
268 setUsesTextLabel( TRUE );
270 this->p_intf = p_intf;
276 p_toolbar = new QToolBar( this, "toolbar" );
277 p_toolbar->setHorizontalStretchable( TRUE );
279 QIconSet * set = new QIconSet();
280 QPixmap pixmap = set->pixmap( QIconSet::Automatic, QIconSet::Normal );
282 #define addbut( l, t, s ) new QToolButton( pixmap, l, t, this, s, p_toolbar );
283 addbut( "Open", "Open a File", SLOT(FileOpen()) );
284 addbut( "Disc", "Open a DVD or VCD", SLOT(Unimplemented()) );
285 addbut( "Net", "Select a Network Stream", SLOT(Unimplemented()) );
286 p_toolbar->addSeparator();
287 addbut( "Back", "Rewind Stream", SLOT(Unimplemented()) );
288 addbut( "Stop", "Stop Stream", SLOT(Unimplemented()) );
289 addbut( "Play", "Play Stream", SLOT(PlaybackPlay()) );
290 addbut( "Pause", "Pause Stream", SLOT(PlaybackPause()) );
291 addbut( "Slow", "Play Slower", SLOT(PlaybackSlow()) );
292 addbut( "Fast", "Play Faster", SLOT(PlaybackFast()) );
293 p_toolbar->addSeparator();
294 addbut( "Playlist", "Open Playlist", SLOT(Unimplemented()) );
295 addbut( "Prev", "Previous File", SLOT(PlaylistPrev()) );
296 addbut( "Next", "Next File", SLOT(PlaylistNext()) );
303 QPopupMenu * p_tmpmenu = new QPopupMenu( this );
305 #define instmp( x, y... ) p_tmpmenu->insertItem( x, this, ## y )
306 menuBar()->insertItem( "&File", p_tmpmenu );
307 instmp( "&Open File...", SLOT(FileOpen()), Key_F3 );
308 instmp( "Open &Disc...", SLOT(Unimplemented()), Key_F4 );
309 instmp( "&Network Stream...", SLOT(Unimplemented()), Key_F5 );
310 p_tmpmenu->insertSeparator();
311 instmp( "&Exit", SLOT(FileQuit()), CTRL+Key_Q );
313 p_tmpmenu = new QPopupMenu( this );
314 menuBar()->insertItem( "&View", p_tmpmenu );
315 instmp( "&Playlist...", SLOT(Unimplemented()) );
316 instmp( "&Modules...", SLOT(Unimplemented()) );
318 p_tmpmenu = new QPopupMenu( this );
319 menuBar()->insertItem( "&Settings", p_tmpmenu );
320 instmp( "&Preferences...", SLOT(Unimplemented()) );
322 p_tmpmenu = new QPopupMenu( this );
323 menuBar()->insertItem( "&Help", p_tmpmenu );
324 instmp( "&About...", SLOT(About()) );
328 * Create the popup menu
331 p_popup = new QPopupMenu( /* floating menu */ );
333 #define inspop( x, y... ) p_popup->insertItem( x, this, ## y )
334 inspop( "&Play", SLOT(PlaybackPlay()) );
335 inspop( "Pause", SLOT(PlaybackPause()) );
336 inspop( "&Slow", SLOT(PlaybackSlow()) );
337 inspop( "&Fast", SLOT(PlaybackFast()) );
338 p_popup->insertSeparator();
339 inspop( "&Open File...", SLOT(FileOpen()), Key_F3 );
340 inspop( "Open &Disc...", SLOT(Unimplemented()), Key_F4 );
341 inspop( "&Network Stream...", SLOT(Unimplemented()), Key_F5 );
342 p_popup->insertSeparator();
343 inspop( "&About...", SLOT(About()) );
344 inspop( "&Exit", SLOT(FileQuit()) );
347 /* Activate the statusbar */
350 /* Add the vertical box */
351 QVBox * p_vbox = new QVBox( this );
352 setCentralWidget( p_vbox );
354 /* The horizontal box */
355 QHBox * p_hbox = new QHBox( p_vbox );
358 p_date = new QLabel( p_hbox );
359 p_date->setAlignment( AlignHCenter | AlignVCenter );
360 p_date->setText( "-:--:--" );
362 /* The status label */
363 QLabel *p_label = new QLabel( p_hbox );
364 p_label->setAlignment( AlignHCenter | AlignVCenter );
365 p_label->setText( "Status: foo" );
368 p_label = new QLabel( p_hbox );
369 p_label->setAlignment( AlignHCenter | AlignVCenter );
370 p_label->setText( "Bar: baz quux" );
372 /* Create the slider and connect it to the date label */
373 p_slider = new IntfSlider( p_intf, p_vbox );
375 connect( p_slider, SIGNAL(valueChanged(int)),
376 this, SLOT(DateDisplay(int)) );
379 QTimer *p_timer = new QTimer( this );
380 connect( p_timer, SIGNAL(timeout()), this, SLOT(Manage()) );
381 p_timer->start( INTF_IDLE_SLEEP / 1000 );
383 /* Everything worked fine */
387 /*****************************************************************************
388 * ~IntfWindow: interface window destructor
389 *****************************************************************************
390 * This function is called when the interface window is destroyed.
391 *****************************************************************************/
392 IntfWindow::~IntfWindow( void )
394 /* FIXME: remove everything cleanly */
397 /*****************************************************************************
398 * DateDisplay: display date
399 *****************************************************************************
400 * This function displays the current date in the date label.
401 *****************************************************************************/
402 void IntfWindow::DateDisplay( int i_range )
404 if( p_intf->p_input != NULL )
406 char psz_time[ OFFSETTOTIME_MAX_SIZE ];
408 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
409 p_date->setText( input_OffsetToTime( p_intf->p_input, psz_time,
410 ( p_intf->p_input->stream.p_selected_area->i_size * i_range )
412 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
416 /*****************************************************************************
417 * FileOpen: open a file
418 *****************************************************************************
419 * This function opens a file requester and adds the selected file to
421 *****************************************************************************/
422 void IntfWindow::FileOpen( void )
424 QString file = QFileDialog::getOpenFileName( QString::null,
425 QString::null, this );
429 statusBar()->message( "No file loaded", 2000 );
433 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, file.latin1() );
437 /*****************************************************************************
438 * About: display the "about" box
439 *****************************************************************************
440 * This function displays a simple "about" box with copyright information.
441 *****************************************************************************/
442 void IntfWindow::About( void )
444 QMessageBox::about( this, "About",
446 "(C) 1996, 1997, 1998, 1999, 2000, 2001 - the VideoLAN Team\n"
448 "This is the VideoLAN client, a DVD and MPEG player.\n"
449 "It can play MPEG and MPEG 2 files from a file "
450 "or from a network source.\n"
452 "More information: http://www.videolan.org/" );
455 /*****************************************************************************
456 * Manage: manage main thread messages
457 *****************************************************************************
458 * In this function, called approx. 10 times a second, we check what the
459 * main program wanted to tell us.
460 *****************************************************************************/
461 void IntfWindow::Manage( void )
463 /* Manage the slider */
464 if( p_intf->p_input != NULL )
466 int i_value = p_slider->value();
468 #define p_area p_intf->p_input->stream.p_selected_area
469 /* If the user hasn't touched the slider since the last time,
470 * then the input can safely change it */
471 if( i_value == p_slider->oldvalue() )
473 i_value = ( SLIDER_MAX * p_area->i_tell ) / p_area->i_size;
475 p_slider->setValue( i_value );
476 p_slider->setOldValue( i_value );
478 /* Otherwise, send message to the input if the user has
479 * finished dragging the slider */
480 else if( p_slider->b_free )
482 off_t i_seek = ( i_value * p_area->i_size ) / SLIDER_MAX;
484 input_Seek( p_intf->p_input, i_seek );
486 /* Update the old value */
487 p_slider->setOldValue( i_value );
492 /* If the "display popup" flag has changed, popup the context menu */
493 if( p_intf->b_menu_change )
495 p_popup->popup( QCursor::pos() );
496 p_intf->b_menu_change = 0;
499 /* Manage core vlc functions through the callback */
500 p_intf->pf_manage( p_intf );
504 /* Prepare to die, young Skywalker */
512 /*****************************************************************************
514 *****************************************************************************/
515 void IntfWindow::PlaybackPlay( void )
517 if( p_intf->p_input != NULL )
519 input_SetStatus( p_intf->p_input, INPUT_STATUS_PLAY );
523 /*****************************************************************************
524 * PlaybackPause: pause
525 *****************************************************************************/
526 void IntfWindow::PlaybackPause( void )
528 if( p_intf->p_input != NULL )
530 input_SetStatus( p_intf->p_input, INPUT_STATUS_PAUSE );
534 /*****************************************************************************
536 *****************************************************************************/
537 void IntfWindow::PlaybackSlow( void )
539 if( p_intf->p_input != NULL )
541 input_SetStatus( p_intf->p_input, INPUT_STATUS_SLOWER );
545 /*****************************************************************************
547 *****************************************************************************/
548 void IntfWindow::PlaybackFast( void )
550 if( p_intf->p_input != NULL )
552 input_SetStatus( p_intf->p_input, INPUT_STATUS_FASTER );
556 /*****************************************************************************
557 * PlaylistPrev: previous playlist entry
558 *****************************************************************************/
559 void IntfWindow::PlaylistPrev( void )
561 if( p_intf->p_input != NULL )
563 /* FIXME: temporary hack */
564 intf_PlaylistPrev( p_main->p_playlist );
565 intf_PlaylistPrev( p_main->p_playlist );
566 p_intf->p_input->b_eof = 1;
570 /*****************************************************************************
571 * PlaylistNext: next playlist entry
572 *****************************************************************************/
573 void IntfWindow::PlaylistNext( void )
575 if( p_intf->p_input != NULL )
577 /* FIXME: temporary hack */
578 p_intf->p_input->b_eof = 1;
582 /*****************************************************************************
583 * IntfSlider: slider creator
584 *****************************************************************************
585 * This function creates the slider, sets its default values, and connects
586 * the interesting signals.
587 *****************************************************************************/
588 IntfSlider::IntfSlider( intf_thread_t *p_intf, QWidget *p_parent )
589 :QSlider( Horizontal, p_parent )
591 this->p_intf = p_intf;
593 setRange( SLIDER_MIN, SLIDER_MAX );
594 setPageStep( SLIDER_STEP );
596 setValue( SLIDER_MIN );
597 setOldValue( SLIDER_MIN );
602 connect( this, SIGNAL(sliderMoved(int)), this, SLOT(SlideStart()) );
603 connect( this, SIGNAL(sliderPressed()), this, SLOT(SlideStart()) );
604 connect( this, SIGNAL(sliderReleased()), this, SLOT(SlideStop()) );
607 /*****************************************************************************
608 * ~IntfSlider: slider destructor
609 *****************************************************************************
610 * This function is called when the interface slider is destroyed.
611 *****************************************************************************/
612 IntfSlider::~IntfSlider( void )
614 /* We don't need to remove anything */