1 /*****************************************************************************
2 * intf_qt.cpp: Qt interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_qt.cpp,v 1.6 2001/05/07 03:14:09 stef 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( 1, "intf warning: "
131 "unimplemented function" ); };
134 intf_thread_t *p_intf;
136 IntfSlider *p_slider;
144 # include "BUILTIN_intf_qt.moc"
146 # include "intf_qt.moc"
149 #define SLIDER_MIN 0x00000
150 #define SLIDER_MAX 0x10000
151 #define SLIDER_STEP (SLIDER_MAX >> 4)
153 /*****************************************************************************
154 * intf_sys_t: description and status of Qt interface
155 *****************************************************************************/
156 typedef struct intf_sys_s
159 IntfWindow *p_window;
163 /*****************************************************************************
165 *****************************************************************************/
166 static int intf_Probe ( probedata_t *p_data );
167 static int intf_Open ( intf_thread_t *p_intf );
168 static void intf_Close ( intf_thread_t *p_intf );
169 static void intf_Run ( intf_thread_t *p_intf );
171 /*****************************************************************************
172 * Functions exported as capabilities. They are declared as static so that
173 * we don't pollute the namespace too much.
174 *****************************************************************************/
178 void _M( intf_getfunctions )( function_list_t * p_function_list )
180 p_function_list->pf_probe = intf_Probe;
181 p_function_list->functions.intf.pf_open = intf_Open;
182 p_function_list->functions.intf.pf_close = intf_Close;
183 p_function_list->functions.intf.pf_run = intf_Run;
188 /*****************************************************************************
189 * intf_Probe: probe the interface and return a score
190 *****************************************************************************
191 * This function tries to initialize Qt and returns a score to the
192 * plugin manager so that it can select the best plugin.
193 *****************************************************************************/
194 static int intf_Probe( probedata_t *p_data )
196 if( TestMethod( INTF_METHOD_VAR, "qt" ) )
201 if( TestProgram( "qvlc" ) )
209 /*****************************************************************************
210 * intf_Open: initialize and create window
211 *****************************************************************************/
212 static int intf_Open( intf_thread_t *p_intf )
214 char *pp_argv[] = { "" };
217 /* Allocate instance and initialize some members */
218 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
219 if( p_intf->p_sys == NULL )
221 intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
225 /* Create the C++ objects */
226 p_intf->p_sys->p_app = new QApplication( i_argc, pp_argv );
227 p_intf->p_sys->p_window = new IntfWindow( p_intf );
229 /* Tell the world we are here */
230 p_intf->p_sys->p_window->setCaption( VOUT_TITLE " (Qt interface)" );
235 /*****************************************************************************
236 * intf_Close: destroy interface window
237 *****************************************************************************/
238 static void intf_Close( intf_thread_t *p_intf )
240 /* Get rid of the C++ objects */
241 delete p_intf->p_sys->p_window;
242 delete p_intf->p_sys->p_app;
244 /* Destroy structure */
245 free( p_intf->p_sys );
248 /*****************************************************************************
249 * intf_Run: Qt thread
250 *****************************************************************************
251 * This part of the interface is in a separate thread so that we can call
252 * exec() from within it without annoying the rest of the program.
253 *****************************************************************************/
254 static void intf_Run( intf_thread_t *p_intf )
256 p_intf->p_sys->p_window->show();
258 p_intf->p_sys->p_app->exec();
261 /* following functions are local */
263 /*****************************************************************************
264 * IntfWindow: interface window creator
265 *****************************************************************************
266 * This function creates the interface window, and populates it with a
267 * menu bar, a toolbar and a slider.
268 *****************************************************************************/
269 IntfWindow::IntfWindow( intf_thread_t *p_intf )
272 setUsesTextLabel( TRUE );
274 this->p_intf = p_intf;
280 p_toolbar = new QToolBar( this, "toolbar" );
281 p_toolbar->setHorizontalStretchable( TRUE );
283 QIconSet * set = new QIconSet();
284 QPixmap pixmap = set->pixmap( QIconSet::Automatic, QIconSet::Normal );
286 #define addbut( l, t, s ) new QToolButton( pixmap, l, t, this, s, p_toolbar );
287 addbut( "Open", "Open a File", SLOT(FileOpen()) );
288 addbut( "Disc", "Open a DVD or VCD", SLOT(Unimplemented()) );
289 addbut( "Net", "Select a Network Stream", SLOT(Unimplemented()) );
290 p_toolbar->addSeparator();
291 addbut( "Back", "Rewind Stream", SLOT(Unimplemented()) );
292 addbut( "Stop", "Stop Stream", SLOT(Unimplemented()) );
293 addbut( "Play", "Play Stream", SLOT(PlaybackPlay()) );
294 addbut( "Pause", "Pause Stream", SLOT(PlaybackPause()) );
295 addbut( "Slow", "Play Slower", SLOT(PlaybackSlow()) );
296 addbut( "Fast", "Play Faster", SLOT(PlaybackFast()) );
297 p_toolbar->addSeparator();
298 addbut( "Playlist", "Open Playlist", SLOT(Unimplemented()) );
299 addbut( "Prev", "Previous File", SLOT(PlaylistPrev()) );
300 addbut( "Next", "Next File", SLOT(PlaylistNext()) );
307 QPopupMenu * p_tmpmenu = new QPopupMenu( this );
309 #define instmp( x, y... ) p_tmpmenu->insertItem( x, this, ## y )
310 menuBar()->insertItem( "&File", p_tmpmenu );
311 instmp( "&Open File...", SLOT(FileOpen()), Key_F3 );
312 instmp( "Open &Disc...", SLOT(Unimplemented()), Key_F4 );
313 instmp( "&Network Stream...", SLOT(Unimplemented()), Key_F5 );
314 p_tmpmenu->insertSeparator();
315 instmp( "&Exit", SLOT(FileQuit()), CTRL+Key_Q );
317 p_tmpmenu = new QPopupMenu( this );
318 menuBar()->insertItem( "&View", p_tmpmenu );
319 instmp( "&Playlist...", SLOT(Unimplemented()) );
320 instmp( "&Modules...", SLOT(Unimplemented()) );
322 p_tmpmenu = new QPopupMenu( this );
323 menuBar()->insertItem( "&Settings", p_tmpmenu );
324 instmp( "&Preferences...", SLOT(Unimplemented()) );
326 p_tmpmenu = new QPopupMenu( this );
327 menuBar()->insertItem( "&Help", p_tmpmenu );
328 instmp( "&About...", SLOT(About()) );
332 * Create the popup menu
335 p_popup = new QPopupMenu( /* floating menu */ );
337 #define inspop( x, y... ) p_popup->insertItem( x, this, ## y )
338 inspop( "&Play", SLOT(PlaybackPlay()) );
339 inspop( "Pause", SLOT(PlaybackPause()) );
340 inspop( "&Slow", SLOT(PlaybackSlow()) );
341 inspop( "&Fast", SLOT(PlaybackFast()) );
342 p_popup->insertSeparator();
343 inspop( "&Open File...", SLOT(FileOpen()), Key_F3 );
344 inspop( "Open &Disc...", SLOT(Unimplemented()), Key_F4 );
345 inspop( "&Network Stream...", SLOT(Unimplemented()), Key_F5 );
346 p_popup->insertSeparator();
347 inspop( "&About...", SLOT(About()) );
348 inspop( "&Exit", SLOT(FileQuit()) );
351 /* Activate the statusbar */
354 /* Add the vertical box */
355 QVBox * p_vbox = new QVBox( this );
356 setCentralWidget( p_vbox );
358 /* The horizontal box */
359 QHBox * p_hbox = new QHBox( p_vbox );
362 p_date = new QLabel( p_hbox );
363 p_date->setAlignment( AlignHCenter | AlignVCenter );
364 p_date->setText( "-:--:--" );
366 /* The status label */
367 QLabel *p_label = new QLabel( p_hbox );
368 p_label->setAlignment( AlignHCenter | AlignVCenter );
369 p_label->setText( "Status: foo" );
372 p_label = new QLabel( p_hbox );
373 p_label->setAlignment( AlignHCenter | AlignVCenter );
374 p_label->setText( "Bar: baz quux" );
376 /* Create the slider and connect it to the date label */
377 p_slider = new IntfSlider( p_intf, p_vbox );
379 connect( p_slider, SIGNAL(valueChanged(int)),
380 this, SLOT(DateDisplay(int)) );
383 QTimer *p_timer = new QTimer( this );
384 connect( p_timer, SIGNAL(timeout()), this, SLOT(Manage()) );
385 p_timer->start( INTF_IDLE_SLEEP / 1000 );
387 /* Everything worked fine */
391 /*****************************************************************************
392 * ~IntfWindow: interface window destructor
393 *****************************************************************************
394 * This function is called when the interface window is destroyed.
395 *****************************************************************************/
396 IntfWindow::~IntfWindow( void )
398 /* FIXME: remove everything cleanly */
401 /*****************************************************************************
402 * DateDisplay: display date
403 *****************************************************************************
404 * This function displays the current date in the date label.
405 *****************************************************************************/
406 void IntfWindow::DateDisplay( int i_range )
408 if( p_intf->p_input != NULL )
410 char psz_time[ OFFSETTOTIME_MAX_SIZE ];
412 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
413 p_date->setText( input_OffsetToTime( p_intf->p_input, psz_time,
414 ( p_intf->p_input->stream.p_selected_area->i_size * i_range )
416 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
420 /*****************************************************************************
421 * FileOpen: open a file
422 *****************************************************************************
423 * This function opens a file requester and adds the selected file to
425 *****************************************************************************/
426 void IntfWindow::FileOpen( void )
428 QString file = QFileDialog::getOpenFileName( QString::null,
429 QString::null, this );
433 statusBar()->message( "No file loaded", 2000 );
437 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, file.latin1() );
441 /*****************************************************************************
442 * About: display the "about" box
443 *****************************************************************************
444 * This function displays a simple "about" box with copyright information.
445 *****************************************************************************/
446 void IntfWindow::About( void )
448 QMessageBox::about( this, "About",
450 "(C) 1996, 1997, 1998, 1999, 2000, 2001 - the VideoLAN Team\n"
452 "This is the VideoLAN client, a DVD and MPEG player.\n"
453 "It can play MPEG and MPEG 2 files from a file "
454 "or from a network source.\n"
456 "More information: http://www.videolan.org/" );
459 /*****************************************************************************
460 * Manage: manage main thread messages
461 *****************************************************************************
462 * In this function, called approx. 10 times a second, we check what the
463 * main program wanted to tell us.
464 *****************************************************************************/
465 void IntfWindow::Manage( void )
467 /* Manage the slider */
468 if( p_intf->p_input != NULL && p_intf->p_input->stream.b_seekable )
470 int i_value = p_slider->value();
472 #define p_area p_intf->p_input->stream.p_selected_area
473 /* If the user hasn't touched the slider since the last time,
474 * then the input can safely change it */
475 if( i_value == p_slider->oldvalue() )
477 i_value = ( SLIDER_MAX * p_area->i_tell ) / p_area->i_size;
479 p_slider->setValue( i_value );
480 p_slider->setOldValue( i_value );
482 /* Otherwise, send message to the input if the user has
483 * finished dragging the slider */
484 else if( p_slider->b_free )
486 off_t i_seek = ( i_value * p_area->i_size ) / SLIDER_MAX;
488 input_Seek( p_intf->p_input, i_seek );
490 /* Update the old value */
491 p_slider->setOldValue( i_value );
496 /* If the "display popup" flag has changed, popup the context menu */
497 if( p_intf->b_menu_change )
499 p_popup->popup( QCursor::pos() );
500 p_intf->b_menu_change = 0;
503 /* Manage core vlc functions through the callback */
504 p_intf->pf_manage( p_intf );
508 /* Prepare to die, young Skywalker */
516 /*****************************************************************************
518 *****************************************************************************/
519 void IntfWindow::PlaybackPlay( void )
521 if( p_intf->p_input != NULL )
523 input_SetStatus( p_intf->p_input, INPUT_STATUS_PLAY );
527 /*****************************************************************************
528 * PlaybackPause: pause
529 *****************************************************************************/
530 void IntfWindow::PlaybackPause( void )
532 if( p_intf->p_input != NULL )
534 input_SetStatus( p_intf->p_input, INPUT_STATUS_PAUSE );
538 /*****************************************************************************
540 *****************************************************************************/
541 void IntfWindow::PlaybackSlow( void )
543 if( p_intf->p_input != NULL )
545 input_SetStatus( p_intf->p_input, INPUT_STATUS_SLOWER );
549 /*****************************************************************************
551 *****************************************************************************/
552 void IntfWindow::PlaybackFast( void )
554 if( p_intf->p_input != NULL )
556 input_SetStatus( p_intf->p_input, INPUT_STATUS_FASTER );
560 /*****************************************************************************
561 * PlaylistPrev: previous playlist entry
562 *****************************************************************************/
563 void IntfWindow::PlaylistPrev( void )
565 if( p_intf->p_input != NULL )
567 /* FIXME: temporary hack */
568 intf_PlaylistPrev( p_main->p_playlist );
569 intf_PlaylistPrev( p_main->p_playlist );
570 p_intf->p_input->b_eof = 1;
574 /*****************************************************************************
575 * PlaylistNext: next playlist entry
576 *****************************************************************************/
577 void IntfWindow::PlaylistNext( void )
579 if( p_intf->p_input != NULL )
581 /* FIXME: temporary hack */
582 p_intf->p_input->b_eof = 1;
586 /*****************************************************************************
587 * IntfSlider: slider creator
588 *****************************************************************************
589 * This function creates the slider, sets its default values, and connects
590 * the interesting signals.
591 *****************************************************************************/
592 IntfSlider::IntfSlider( intf_thread_t *p_intf, QWidget *p_parent )
593 :QSlider( Horizontal, p_parent )
595 this->p_intf = p_intf;
597 setRange( SLIDER_MIN, SLIDER_MAX );
598 setPageStep( SLIDER_STEP );
600 setValue( SLIDER_MIN );
601 setOldValue( SLIDER_MIN );
606 connect( this, SIGNAL(sliderMoved(int)), this, SLOT(SlideStart()) );
607 connect( this, SIGNAL(sliderPressed()), this, SLOT(SlideStart()) );
608 connect( this, SIGNAL(sliderReleased()), this, SLOT(SlideStop()) );
611 /*****************************************************************************
612 * ~IntfSlider: slider destructor
613 *****************************************************************************
614 * This function is called when the interface slider is destroyed.
615 *****************************************************************************/
616 IntfSlider::~IntfSlider( void )
618 /* We don't need to remove anything */