Fixed playlist behaviour (draging a file replaces the playlist, and
[vlc.git] / modules / gui / beos / InterfaceWindow.cpp
1 /*****************************************************************************
2  * InterfaceWindow.cpp: beos interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: InterfaceWindow.cpp,v 1.17 2003/01/14 22:03:38 titer Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Tony Castley <tony@castley.net>
10  *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11  *          Stephan Aßmus <stippi@yellowbites.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /* System headers */
29 #include <kernel/OS.h>
30 #include <InterfaceKit.h>
31 #include <AppKit.h>
32 #include <StorageKit.h>
33 #include <SupportKit.h>
34 #include <malloc.h>
35 #include <scsi.h>
36 #include <scsiprobe_driver.h>
37 #include <fs_info.h>
38 #include <string.h>
39
40 /* VLC headers */
41 #include <vlc/vlc.h>
42 #include <vlc/aout.h>
43 #include <vlc/intf.h>
44
45 /* BeOS interface headers */
46 #include "VlcWrapper.h"
47 #include "MsgVals.h"
48 #include "MediaControlView.h"
49 #include "PlayListWindow.h"
50 #include "PreferencesWindow.h"
51 #include "InterfaceWindow.h"
52
53 #define INTERFACE_UPDATE_TIMEOUT 80000 // 2 frames if at 25 fps
54
55
56 /*****************************************************************************
57  * InterfaceWindow
58  *****************************************************************************/
59
60 InterfaceWindow::InterfaceWindow( BRect frame, const char *name,
61                                                                   intf_thread_t  *p_interface )
62         : BWindow( frame, name, B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
63                            B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK | B_ASYNCHRONOUS_CONTROLS ),
64           p_intf( p_interface ),
65           fFilePanel( NULL ),
66           fSubtitlesPanel( NULL ),
67           fLastUpdateTime( system_time() ),
68           fSettings( new BMessage( 'sett' ) )
69 {
70     p_intf = p_interface;
71     p_wrapper = p_intf->p_sys->p_wrapper;
72     
73     fPlaylistIsEmpty = ( p_wrapper->PlaylistSize() < 0 );
74     
75     fPlaylistWindow = new PlayListWindow( BRect( 100.0, 100.0, 400.0, 350.0 ),
76                                                                                   "Playlist",
77                                                                                   this,
78                                                                                   p_intf );
79     BScreen *p_screen = new BScreen();
80     BRect screen_rect = p_screen->Frame();
81     delete p_screen;
82     BRect window_rect;
83     window_rect.Set( ( screen_rect.right - PREFS_WINDOW_WIDTH ) / 2,
84                      ( screen_rect.bottom - PREFS_WINDOW_HEIGHT ) / 2,
85                      ( screen_rect.right + PREFS_WINDOW_WIDTH ) / 2,
86                      ( screen_rect.bottom + PREFS_WINDOW_HEIGHT ) / 2 );
87         fPreferencesWindow = new PreferencesWindow( window_rect,
88                                                     "Preferences",
89                                                     p_intf );
90     
91         // set the title bar
92         SetName( "interface" );
93         SetTitle( VOUT_TITLE );
94
95         // the media control view
96         p_mediaControl = new MediaControlView( BRect( 0.0, 0.0, 250.0, 50.0 ),
97                                                p_intf );
98         p_mediaControl->SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );
99         p_mediaControl->SetEnabled( !fPlaylistIsEmpty );
100
101         float width, height;
102         p_mediaControl->GetPreferredSize( &width, &height );
103
104         // set up the main menu
105         fMenuBar = new BMenuBar( BRect(0.0, 0.0, width, 15.0), "main menu",
106                                                          B_FOLLOW_NONE, B_ITEMS_IN_ROW, false );
107
108         // make menu bar resize to correct height
109         float menuWidth, menuHeight;
110         fMenuBar->GetPreferredSize( &menuWidth, &menuHeight );
111         fMenuBar->ResizeTo( width, menuHeight );        // don't change! it's a workarround!
112         // take care of proper size for ourself
113         height += fMenuBar->Bounds().Height();
114         ResizeTo( width, height );
115
116         p_mediaControl->MoveTo( fMenuBar->Bounds().LeftBottom() + BPoint(0.0, 1.0) );
117         AddChild( fMenuBar );
118         AddChild( p_mediaControl );
119
120         // Add the file Menu
121         BMenu* fileMenu = new BMenu( "File" );
122         fMenuBar->AddItem( fileMenu );
123         fileMenu->AddItem( new BMenuItem( "Open File" B_UTF8_ELLIPSIS,
124                                                                           new BMessage( OPEN_FILE ), 'O') );
125         
126         fileMenu->AddItem( new CDMenu( "Open Disc" ) );
127
128         fileMenu->AddItem( new BMenuItem( "Load a subtitle file" B_UTF8_ELLIPSIS,
129                                                                           new BMessage( LOAD_SUBFILE ) ) );
130         
131         fileMenu->AddSeparatorItem();
132         fileMenu->AddItem( new BMenuItem( "Play List" B_UTF8_ELLIPSIS,
133                                                                           new BMessage( OPEN_PLAYLIST ), 'P') );
134         
135         fileMenu->AddSeparatorItem();
136         BMenuItem* item = new BMenuItem( "About" B_UTF8_ELLIPSIS,
137                                                                          new BMessage( B_ABOUT_REQUESTED ), 'A');
138         item->SetTarget( be_app );
139         fileMenu->AddItem( item );
140         fileMenu->AddItem( new BMenuItem( "Quit", new BMessage( B_QUIT_REQUESTED ), 'Q') );
141
142         fLanguageMenu = new LanguageMenu("Language", AUDIO_ES, p_wrapper);
143         fSubtitlesMenu = new LanguageMenu("Subtitles", SPU_ES, p_wrapper);
144
145         /* Add the Audio menu */
146         fAudioMenu = new BMenu( "Audio" );
147         fMenuBar->AddItem ( fAudioMenu );
148         fAudioMenu->AddItem( fLanguageMenu );
149         fAudioMenu->AddItem( fSubtitlesMenu );
150
151         fPrevTitleMI = new BMenuItem( "Prev Title", new BMessage( PREV_TITLE ) );
152         fNextTitleMI = new BMenuItem( "Next Title", new BMessage( NEXT_TITLE ) );
153         fPrevChapterMI = new BMenuItem( "Prev Chapter", new BMessage( PREV_CHAPTER ) );
154         fNextChapterMI = new BMenuItem( "Next Chapter", new BMessage( NEXT_CHAPTER ) );
155
156         /* Add the Navigation menu */
157         fNavigationMenu = new BMenu( "Navigation" );
158         fMenuBar->AddItem( fNavigationMenu );
159         fNavigationMenu->AddItem( fPrevTitleMI );
160         fNavigationMenu->AddItem( fNextTitleMI );
161         fNavigationMenu->AddItem( fTitleMenu = new TitleMenu( "Go to Title", p_intf ) );
162         fNavigationMenu->AddSeparatorItem();
163         fNavigationMenu->AddItem( fPrevChapterMI );
164         fNavigationMenu->AddItem( fNextChapterMI );
165         fNavigationMenu->AddItem( fChapterMenu = new ChapterMenu( "Go to Chapter", p_intf ) );
166
167         /* Add the Speed menu */
168         fSpeedMenu = new BMenu( "Speed" );
169         fSpeedMenu->SetRadioMode( true );
170         fSpeedMenu->AddItem( fSlowerMI = new BMenuItem( "Slower", new BMessage( SLOWER_PLAY ) ) );
171         fNormalMI = new BMenuItem( "Normal", new BMessage( NORMAL_PLAY ) );
172         fNormalMI->SetMarked(true); // default to normal speed
173         fSpeedMenu->AddItem( fNormalMI );
174         fSpeedMenu->AddItem( fFasterMI = new BMenuItem( "Faster", new BMessage( FASTER_PLAY) ) );
175         fSpeedMenu->SetTargetForItems( this );
176         fMenuBar->AddItem( fSpeedMenu );
177
178     /* Add the Settings menu */
179     fSettingsMenu = new BMenu( "Settings" );
180     fSettingsMenu->AddItem( fPreferencesMI =
181         new BMenuItem( "Preferences", new BMessage( OPEN_PREFERENCES ) ) );
182         fMenuBar->AddItem( fSettingsMenu );                                                     
183
184         // prepare fow showing
185         _SetMenusEnabled( false );
186         p_mediaControl->SetEnabled( false );
187
188         _RestoreSettings();
189
190         Show();
191 }
192
193 InterfaceWindow::~InterfaceWindow()
194 {
195         if (fPlaylistWindow)
196                 fPlaylistWindow->ReallyQuit();
197         delete fSettings;
198 }
199
200 /*****************************************************************************
201  * InterfaceWindow::FrameResized
202  *****************************************************************************/
203 void
204 InterfaceWindow::FrameResized(float width, float height)
205 {
206         BRect r(Bounds());
207         fMenuBar->MoveTo(r.LeftTop());
208         fMenuBar->ResizeTo(r.Width(), fMenuBar->Bounds().Height());
209         r.top += fMenuBar->Bounds().Height() + 1.0;
210         p_mediaControl->MoveTo(r.LeftTop());
211         p_mediaControl->ResizeTo(r.Width(), r.Height());
212 }
213
214 /*****************************************************************************
215  * InterfaceWindow::MessageReceived
216  *****************************************************************************/
217 void InterfaceWindow::MessageReceived( BMessage * p_message )
218 {
219         int playback_status;      // remember playback state
220         playback_status = p_wrapper->InputStatus();
221
222         switch( p_message->what )
223         {
224                 case B_ABOUT_REQUESTED:
225                 {
226                         BAlert* alert = new BAlert( VOUT_TITLE,
227                                                                                 "BeOS " VOUT_TITLE "\n\n<www.videolan.org>", "Ok");
228                         alert->Go();
229                         break;
230                 }
231                 case TOGGLE_ON_TOP:
232                         break;
233                         
234                 case OPEN_FILE:
235                         if( fFilePanel )
236                         {
237                                 fFilePanel->Show();
238                                 break;
239                         }
240                         fFilePanel = new BFilePanel();
241                         fFilePanel->SetTarget( this );
242                         fFilePanel->Show();
243                         break;
244         
245                 case OPEN_PLAYLIST:
246                         if (fPlaylistWindow->Lock())
247                         {
248                                 if (fPlaylistWindow->IsHidden())
249                                         fPlaylistWindow->Show();
250                                 else
251                                         fPlaylistWindow->Activate();
252                                 fPlaylistWindow->Unlock();
253                         }
254                         break;
255                 case OPEN_DVD:
256                         {
257                                 const char *psz_device;
258                                 BString type( "dvd" );
259                                 if( p_message->FindString( "device", &psz_device ) == B_OK )
260                                 {
261                                         BString device( psz_device );
262                                         p_wrapper->openDisc( type, device, 0, 0 );
263                                 }
264                                 _UpdatePlaylist();
265                         }
266                         break;
267                 
268                 case LOAD_SUBFILE:
269                         if( fSubtitlesPanel )
270                         {
271                                 fSubtitlesPanel->Show();
272                                 break;
273                         }
274                         fSubtitlesPanel = new BFilePanel();
275                         fSubtitlesPanel->SetTarget( this );
276                         fSubtitlesPanel->SetMessage( new BMessage( SUBFILE_RECEIVED ) );
277                         fSubtitlesPanel->Show();
278                         break;
279
280                 case SUBFILE_RECEIVED:
281                 {
282                         entry_ref ref;
283                         if( p_message->FindRef( "refs", 0, &ref ) == B_OK )
284                         {
285                                 BPath path( &ref );
286                                 if ( path.InitCheck() == B_OK )
287                                         p_wrapper->LoadSubFile( (char*)path.Path() );
288                         }
289                         break;
290                 }
291         
292                 case STOP_PLAYBACK:
293                         // this currently stops playback not nicely
294                         if (playback_status > UNDEF_S)
295                         {
296                                 snooze( 400000 );
297                                 p_wrapper->PlaylistStop();
298                                 p_mediaControl->SetStatus(NOT_STARTED_S, DEFAULT_RATE);
299                         }
300                         break;
301         
302                 case START_PLAYBACK:
303                         /*  starts playing in normal mode */
304         
305                 case PAUSE_PLAYBACK:
306                         /* toggle between pause and play */
307                         if (playback_status > UNDEF_S)
308                         {
309                                 /* pause if currently playing */
310                                 if ( playback_status == PLAYING_S )
311                                 {
312                                         p_wrapper->PlaylistPause();
313                                 }
314                                 else
315                                 {
316                                         p_wrapper->PlaylistPlay();
317                                 }
318                         }
319                         else
320                         {
321                                 /* Play a new file */
322                                 p_wrapper->PlaylistPlay();
323                         }       
324                         break;
325         
326                 case FASTER_PLAY:
327                         /* cycle the fast playback modes */
328                         if (playback_status > UNDEF_S)
329                         {
330                                 p_wrapper->InputFaster();
331                         }
332                         break;
333         
334                 case SLOWER_PLAY:
335                         /*  cycle the slow playback modes */
336                         if (playback_status > UNDEF_S)
337                         {
338                                 p_wrapper->InputSlower();
339                         }
340                         break;
341         
342                 case NORMAL_PLAY:
343                         /*  restore speed to normal if already playing */
344                         if (playback_status > UNDEF_S)
345                         {
346                                 p_wrapper->PlaylistPlay();
347                         }
348                         break;
349         
350                 case SEEK_PLAYBACK:
351                         /* handled by semaphores */
352                         break;
353                 // volume related messages
354                 case VOLUME_CHG:
355                         /* adjust the volume */
356                         if (playback_status > UNDEF_S)
357                         {
358                                 p_wrapper->SetVolume( p_mediaControl->GetVolume() );
359                                 p_mediaControl->SetMuted( p_wrapper->IsMuted() );
360                         }
361                         break;
362         
363                 case VOLUME_MUTE:
364                         // toggle muting
365                         if( p_wrapper->IsMuted() )
366                             p_wrapper->VolumeRestore();
367                         else
368                             p_wrapper->VolumeMute();
369                         p_mediaControl->SetMuted( p_wrapper->IsMuted() );
370                         break;
371         
372                 case SELECT_CHANNEL:
373                         if ( playback_status > UNDEF_S )
374                         {
375                                 int32 channel;
376                                 if ( p_message->FindInt32( "channel", &channel ) == B_OK )
377                                 {
378                                         p_wrapper->ToggleLanguage( channel );
379                                 }
380                         }
381                         break;
382         
383                 case SELECT_SUBTITLE:
384                         if ( playback_status > UNDEF_S )
385                         {
386                                 int32 subtitle;
387                                 if ( p_message->FindInt32( "subtitle", &subtitle ) == B_OK )
388                                          p_wrapper->ToggleSubtitle( subtitle );
389                         }
390                         break;
391         
392                 // specific navigation messages
393                 case PREV_TITLE:
394                 {
395                         p_wrapper->PrevTitle();
396                         break;
397                 }
398                 case NEXT_TITLE:
399                 {
400             p_wrapper->NextTitle();
401                         break;
402                 }
403                 case TOGGLE_TITLE:
404                         if ( playback_status > UNDEF_S )
405                         {
406                                 int32 index;
407                                 if ( p_message->FindInt32( "index", &index ) == B_OK )
408                                         p_wrapper->toggleTitle( index );
409                         }
410                         break;
411                 case PREV_CHAPTER:
412                 {
413             p_wrapper->PrevChapter();
414                         break;
415                 }
416                 case NEXT_CHAPTER:
417                 {
418             p_wrapper->NextChapter();
419                         break;
420                 }
421                 case TOGGLE_CHAPTER:
422                         if ( playback_status > UNDEF_S )
423                         {
424                                 int32 index;
425                                 if ( p_message->FindInt32( "index", &index ) == B_OK )
426                      p_wrapper->toggleChapter( index );
427                         }
428                         break;
429                 case PREV_FILE:
430                         p_wrapper->PlaylistPrev();
431                         break;
432                 case NEXT_FILE:
433                         p_wrapper->PlaylistNext();
434                         break;
435                 // general next/prev functionality (skips to whatever makes most sense)
436                 case NAVIGATE_PREV:
437                         p_wrapper->navigatePrev();
438                         break;
439                 case NAVIGATE_NEXT:
440                         p_wrapper->navigateNext();
441                         break;
442                 // drag'n'drop and system messages
443                 case B_REFS_RECEIVED:
444                 case B_SIMPLE_DATA:
445                         {
446                                 /* file(s) opened by the File menu -> append to the playlist;
447                                  * file(s) opened by drag & drop -> replace playlist;
448                                  * file(s) opened by 'shift' + drag & drop -> append */
449                                 bool replace = false;
450                                 if ( p_message->WasDropped() )
451                                         replace = !( modifiers() & B_SHIFT_KEY );
452                                         
453                                 // build list of files to be played from message contents
454                                 entry_ref ref;
455                                 BList files;
456                                 for ( int i = 0; p_message->FindRef( "refs", i, &ref ) == B_OK; i++ )
457                                 {
458                                         BPath path( &ref );
459                                         if ( path.InitCheck() == B_OK )
460                                                 files.AddItem( new BString( (char*)path.Path() ) );
461                                 }
462                                 // give the list to VLC
463                                 p_wrapper->openFiles(&files, replace);
464                                 _UpdatePlaylist();
465                         }
466                         break;
467
468                 case OPEN_PREFERENCES:
469                     if( fPreferencesWindow->Lock() )
470                     {
471                             if (fPreferencesWindow->IsHidden())
472                                     fPreferencesWindow->Show();
473                             else
474                                     fPreferencesWindow->Activate();
475                                 fPreferencesWindow->Unlock();
476                         }
477                         break;
478                                 
479                 default:
480                         BWindow::MessageReceived( p_message );
481                         break;
482         }
483
484 }
485
486 /*****************************************************************************
487  * InterfaceWindow::QuitRequested
488  *****************************************************************************/
489 bool InterfaceWindow::QuitRequested()
490 {
491         p_wrapper->PlaylistStop();
492         p_mediaControl->SetStatus(NOT_STARTED_S, DEFAULT_RATE);
493         
494         p_intf->b_die = 1;
495
496         _StoreSettings();
497
498         return( true );
499 }
500
501 /*****************************************************************************
502  * InterfaceWindow::updateInterface
503  *****************************************************************************/
504 void InterfaceWindow::updateInterface()
505 {
506     if( p_wrapper->HasInput() )
507     {
508                 if ( acquire_sem( p_mediaControl->fScrubSem ) == B_OK )
509                 {
510                     p_wrapper->setTimeAsFloat(p_mediaControl->GetSeekTo());
511                 }
512                 else if ( Lock() )
513                 {
514 //                      p_mediaControl->SetEnabled( true );
515                         bool hasTitles = p_wrapper->HasTitles();
516                         bool hasChapters = p_wrapper->HasChapters();
517                         p_mediaControl->SetStatus( p_wrapper->InputStatus(), 
518                                                                            p_wrapper->InputRate() );
519                         p_mediaControl->SetProgress( p_wrapper->getTimeAsFloat() );
520                         _SetMenusEnabled( true, hasChapters, hasTitles );
521
522                         _UpdateSpeedMenu( p_wrapper->InputRate() );
523
524                         // enable/disable skip buttons
525                         bool canSkipPrev;
526                         bool canSkipNext;
527                         p_wrapper->getNavCapabilities( &canSkipPrev, &canSkipNext );
528                         p_mediaControl->SetSkippable( canSkipPrev, canSkipNext );
529
530                         if ( p_wrapper->HasAudio() )
531                         {
532                                 p_mediaControl->SetAudioEnabled( true );
533                                 p_mediaControl->SetMuted( p_wrapper->IsMuted() );
534                         } else
535                                 p_mediaControl->SetAudioEnabled( false );
536
537                         Unlock();
538                 }
539                 // update playlist as well
540                 if ( fPlaylistWindow->Lock() )
541                 {
542                         fPlaylistWindow->UpdatePlaylist();
543                         fPlaylistWindow->Unlock();
544                 }
545         }
546     else
547     {
548         _SetMenusEnabled( false );
549 //              p_mediaControl->SetEnabled( false );
550     }
551
552     /* always force the user-specified volume */
553     /* FIXME : I'm quite sure there is a cleaner way to do this */
554     if( p_wrapper->GetVolume() != p_mediaControl->GetVolume() )
555     {
556         p_wrapper->SetVolume( p_mediaControl->GetVolume() );
557     }
558
559         fLastUpdateTime = system_time();
560 }
561
562 /*****************************************************************************
563  * InterfaceWindow::IsStopped
564  *****************************************************************************/
565 bool
566 InterfaceWindow::IsStopped() const
567 {
568         return (system_time() - fLastUpdateTime > INTERFACE_UPDATE_TIMEOUT);
569 }
570
571 /*****************************************************************************
572  * InterfaceWindow::_UpdatePlaylist
573  *****************************************************************************/
574 void
575 InterfaceWindow::_UpdatePlaylist()
576 {
577         if ( fPlaylistWindow->Lock() )
578         {
579                 fPlaylistWindow->UpdatePlaylist( true );
580                 fPlaylistWindow->Unlock();
581                 p_mediaControl->SetEnabled( p_wrapper->PlaylistSize() );
582         }
583 }
584
585 /*****************************************************************************
586  * InterfaceWindow::_SetMenusEnabled
587  *****************************************************************************/
588 void
589 InterfaceWindow::_SetMenusEnabled(bool hasFile, bool hasChapters, bool hasTitles)
590 {
591         if (!hasFile)
592         {
593                 hasChapters = false;
594                 hasTitles = false;
595         }
596         if (Lock())
597         {
598                 if (fNextChapterMI->IsEnabled() != hasChapters)
599                         fNextChapterMI->SetEnabled(hasChapters);
600                 if (fPrevChapterMI->IsEnabled() != hasChapters)
601                         fPrevChapterMI->SetEnabled(hasChapters);
602                 if (fChapterMenu->IsEnabled() != hasChapters)
603                         fChapterMenu->SetEnabled(hasChapters);
604                 if (fNextTitleMI->IsEnabled() != hasTitles)
605                         fNextTitleMI->SetEnabled(hasTitles);
606                 if (fPrevTitleMI->IsEnabled() != hasTitles)
607                         fPrevTitleMI->SetEnabled(hasTitles);
608                 if (fTitleMenu->IsEnabled() != hasTitles)
609                         fTitleMenu->SetEnabled(hasTitles);
610                 if (fAudioMenu->IsEnabled() != hasFile)
611                         fAudioMenu->SetEnabled(hasFile);
612                 if (fNavigationMenu->IsEnabled() != hasFile)
613                         fNavigationMenu->SetEnabled(hasFile);
614                 if (fLanguageMenu->IsEnabled() != hasFile)
615                         fLanguageMenu->SetEnabled(hasFile);
616                 if (fSubtitlesMenu->IsEnabled() != hasFile)
617                         fSubtitlesMenu->SetEnabled(hasFile);
618                 if (fSpeedMenu->IsEnabled() != hasFile)
619                         fSpeedMenu->SetEnabled(hasFile);
620                 Unlock();
621         }
622 }
623
624 /*****************************************************************************
625  * InterfaceWindow::_UpdateSpeedMenu
626  *****************************************************************************/
627 void
628 InterfaceWindow::_UpdateSpeedMenu( int rate )
629 {
630         if ( rate == DEFAULT_RATE )
631         {
632                 if ( !fNormalMI->IsMarked() )
633                         fNormalMI->SetMarked( true );
634         }
635         else if ( rate < DEFAULT_RATE )
636         {
637                 if ( !fFasterMI->IsMarked() )
638                         fFasterMI->SetMarked( true );
639         }
640         else
641         {
642                 if ( !fSlowerMI->IsMarked() )
643                         fSlowerMI->SetMarked( true );
644         }
645 }
646
647 /*****************************************************************************
648  * InterfaceWindow::_InputStreamChanged
649  *****************************************************************************/
650 void
651 InterfaceWindow::_InputStreamChanged()
652 {
653 //printf("InterfaceWindow::_InputStreamChanged()\n");
654         // TODO: move more stuff from updateInterface() here!
655         snooze( 400000 );
656         p_wrapper->SetVolume( p_mediaControl->GetVolume() );
657 }
658
659 /*****************************************************************************
660  * InterfaceWindow::_LoadSettings
661  *****************************************************************************/
662 status_t
663 InterfaceWindow::_LoadSettings( BMessage* message, const char* fileName, const char* folder )
664 {
665         status_t ret = B_BAD_VALUE;
666         if ( message )
667         {
668                 BPath path;
669                 if ( ( ret = find_directory( B_USER_SETTINGS_DIRECTORY, &path ) ) == B_OK )
670                 {
671                         // passing folder is optional
672                         if ( folder )
673                                 ret = path.Append( folder );
674                         if ( ret == B_OK && ( ret = path.Append( fileName ) ) == B_OK )
675                         {
676                                 BFile file( path.Path(), B_READ_ONLY );
677                                 if ( ( ret = file.InitCheck() ) == B_OK )
678                                 {
679                                         ret = message->Unflatten( &file );
680                                         file.Unset();
681                                 }
682                         }
683                 }
684         }
685         return ret;
686 }
687
688 /*****************************************************************************
689  * InterfaceWindow::_SaveSettings
690  *****************************************************************************/
691 status_t
692 InterfaceWindow::_SaveSettings( BMessage* message, const char* fileName, const char* folder )
693 {
694         status_t ret = B_BAD_VALUE;
695         if ( message )
696         {
697                 BPath path;
698                 if ( ( ret = find_directory( B_USER_SETTINGS_DIRECTORY, &path ) ) == B_OK )
699                 {
700                         // passing folder is optional
701                         if ( folder && ( ret = path.Append( folder ) ) == B_OK )
702                                 ret = create_directory( path.Path(), 0777 );
703                         if ( ret == B_OK && ( ret = path.Append( fileName ) ) == B_OK )
704                         {
705                                 BFile file( path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE );
706                                 if ( ( ret = file.InitCheck() ) == B_OK )
707                                 {
708                                         ret = message->Flatten( &file );
709                                         file.Unset();
710                                 }
711                         }
712                 }
713         }
714         return ret;
715 }
716
717 /*****************************************************************************
718  * InterfaceWindow::_RestoreSettings
719  *****************************************************************************/
720 bool
721 make_sure_frame_is_on_screen( BRect& frame )
722 {
723         BScreen screen( B_MAIN_SCREEN_ID );
724         if (frame.IsValid() && screen.IsValid()) {
725                 if (!screen.Frame().Contains(frame)) {
726                         // make sure frame fits in the screen
727                         if (frame.Width() > screen.Frame().Width())
728                                 frame.right -= frame.Width() - screen.Frame().Width() + 10.0;
729                         if (frame.Height() > screen.Frame().Height())
730                                 frame.bottom -= frame.Height() - screen.Frame().Height() + 30.0;
731                         // frame is now at the most the size of the screen
732                         if (frame.right > screen.Frame().right)
733                                 frame.OffsetBy(-(frame.right - screen.Frame().right), 0.0);
734                         if (frame.bottom > screen.Frame().bottom)
735                                 frame.OffsetBy(0.0, -(frame.bottom - screen.Frame().bottom));
736                         if (frame.left < screen.Frame().left)
737                                 frame.OffsetBy((screen.Frame().left - frame.left), 0.0);
738                         if (frame.top < screen.Frame().top)
739                                 frame.OffsetBy(0.0, (screen.Frame().top - frame.top));
740                 }
741                 return true;
742         }
743         return false;
744 }
745
746 void
747 make_sure_frame_is_within_limits( BRect& frame, float minWidth, float minHeight,
748                                                                   float maxWidth, float maxHeight )
749 {
750         if ( frame.Width() < minWidth )
751                 frame.right = frame.left + minWidth;
752         if ( frame.Height() < minHeight )
753                 frame.bottom = frame.top + minHeight;
754         if ( frame.Width() > maxWidth )
755                 frame.right = frame.left + maxWidth;
756         if ( frame.Height() > maxHeight )
757                 frame.bottom = frame.top + maxHeight;
758 }
759
760 /*****************************************************************************
761  * InterfaceWindow::_RestoreSettings
762  *****************************************************************************/
763 void
764 InterfaceWindow::_RestoreSettings()
765 {
766         if ( _LoadSettings( fSettings, "interface_settings", "VideoLAN Client" ) == B_OK )
767         {
768                 BRect mainFrame;
769                 if ( fSettings->FindRect( "main frame", &mainFrame ) == B_OK )
770                 {
771                         // sanity checks: make sure window is not too big/small
772                         // and that it's not off-screen
773                         float minWidth, maxWidth, minHeight, maxHeight;
774                         GetSizeLimits( &minWidth, &maxWidth, &minHeight, &maxHeight );
775
776                         make_sure_frame_is_within_limits( mainFrame,
777                                                                                           minWidth, minHeight, maxWidth, maxHeight );
778                         make_sure_frame_is_on_screen( mainFrame );
779
780
781                         MoveTo( mainFrame.LeftTop() );
782                         ResizeTo( mainFrame.Width(), mainFrame.Height() );
783                 }
784                 if ( fPlaylistWindow->Lock() )
785                 {
786                         BRect playlistFrame;
787                         if (fSettings->FindRect( "playlist frame", &playlistFrame ) == B_OK )
788                         {
789                                 // sanity checks: make sure window is not too big/small
790                                 // and that it's not off-screen
791                                 float minWidth, maxWidth, minHeight, maxHeight;
792                                 fPlaylistWindow->GetSizeLimits( &minWidth, &maxWidth, &minHeight, &maxHeight );
793
794                                 make_sure_frame_is_within_limits( playlistFrame,
795                                                                                                   minWidth, minHeight, maxWidth, maxHeight );
796                                 make_sure_frame_is_on_screen( playlistFrame );
797
798                                 fPlaylistWindow->MoveTo( playlistFrame.LeftTop() );
799                                 fPlaylistWindow->ResizeTo( playlistFrame.Width(), playlistFrame.Height() );
800                         }
801                         
802                         bool showing;
803                         if ( fSettings->FindBool( "playlist showing", &showing ) == B_OK )
804                         {
805                                 if ( showing )
806                                 {
807                                         if ( fPlaylistWindow->IsHidden() )
808                                                 fPlaylistWindow->Show();
809                                 }
810                                 else
811                                 {
812                                         if ( !fPlaylistWindow->IsHidden() )
813                                                 fPlaylistWindow->Hide();
814                                 }
815                         }
816
817                         fPlaylistWindow->Unlock();
818                 }
819         }
820 }
821
822 /*****************************************************************************
823  * InterfaceWindow::_StoreSettings
824  *****************************************************************************/
825 void
826 InterfaceWindow::_StoreSettings()
827 {
828         if ( fSettings->ReplaceRect( "main frame", Frame() ) != B_OK )
829                 fSettings->AddRect( "main frame", Frame() );
830         if ( fPlaylistWindow->Lock() )
831         {
832                 if (fSettings->ReplaceRect( "playlist frame", fPlaylistWindow->Frame() ) != B_OK)
833                         fSettings->AddRect( "playlist frame", fPlaylistWindow->Frame() );
834                 if (fSettings->ReplaceBool( "playlist showing", !fPlaylistWindow->IsHidden() ) != B_OK)
835                         fSettings->AddBool( "playlist showing", !fPlaylistWindow->IsHidden() );
836                 fPlaylistWindow->Unlock();
837         }
838         _SaveSettings( fSettings, "interface_settings", "VideoLAN Client" );
839 }
840
841 /*****************************************************************************
842  * CDMenu::CDMenu
843  *****************************************************************************/
844 CDMenu::CDMenu(const char *name)
845           : BMenu(name)
846 {
847 }
848
849 /*****************************************************************************
850  * CDMenu::~CDMenu
851  *****************************************************************************/
852 CDMenu::~CDMenu()
853 {
854 }
855
856 /*****************************************************************************
857  * CDMenu::AttachedToWindow
858  *****************************************************************************/
859 void CDMenu::AttachedToWindow(void)
860 {
861         // remove all items
862         while (BMenuItem* item = RemoveItem(0L))
863                 delete item;
864         GetCD("/dev/disk");
865         BMenu::AttachedToWindow();
866 }
867
868 /*****************************************************************************
869  * CDMenu::GetCD
870  *****************************************************************************/
871 int CDMenu::GetCD( const char *directory )
872 {
873         BVolumeRoster *volRoster;
874         BVolume    *vol;
875         BDirectory      *dir;
876         int                status;
877         int                mounted;   
878         char              name[B_FILE_NAME_LENGTH]; 
879         fs_info    info;
880         dev_t            dev;
881         
882         volRoster = new BVolumeRoster();
883         vol = new BVolume();
884         dir = new BDirectory();
885         status = volRoster->GetNextVolume(vol);
886         status = vol->GetRootDirectory(dir);
887         while (status ==  B_NO_ERROR)
888         {
889                 mounted = vol->GetName(name);   
890                 if ((mounted == B_OK) && /* Disk is currently Mounted */
891                         (vol->IsReadOnly()) ) /* Disk is read-only */
892                 {
893                         dev = vol->Device();
894                         fs_stat_dev(dev, &info);
895                         
896                         device_geometry g;
897                         int i_dev;
898                         i_dev = open( info.device_name, O_RDONLY );
899                    
900                         if( i_dev >= 0 )
901                         {
902                                 if( ioctl(i_dev, B_GET_GEOMETRY, &g, sizeof(g)) >= 0 )
903                                 {
904                                         if( g.device_type == B_CD ) //ensure the drive is a CD-ROM
905                                         {
906                                                 BMessage *msg;
907                                                 msg = new BMessage( OPEN_DVD );
908                                                 msg->AddString( "device", info.device_name );
909                                                 BMenuItem *menu_item;
910                                                 menu_item = new BMenuItem( name, msg );
911                                                 AddItem( menu_item );
912                                         }
913                                         close(i_dev);
914                                 }
915                         }
916                 }
917                 vol->Unset();
918                 status = volRoster->GetNextVolume(vol);
919         }
920         return 0;
921 }
922
923 /*****************************************************************************
924  * LanguageMenu::LanguageMenu
925  *****************************************************************************/
926 LanguageMenu::LanguageMenu( const char *name, int menu_kind, 
927                                                         VlcWrapper *p_wrapper )
928         :BMenu(name)
929 {
930         kind = menu_kind;
931         this->p_wrapper = p_wrapper;
932 }
933
934 /*****************************************************************************
935  * LanguageMenu::~LanguageMenu
936  *****************************************************************************/
937 LanguageMenu::~LanguageMenu()
938 {
939 }
940
941 /*****************************************************************************
942  * LanguageMenu::AttachedToWindow
943  *****************************************************************************/
944 void LanguageMenu::AttachedToWindow()
945 {
946         // remove all items
947         while ( BMenuItem* item = RemoveItem( 0L ) )
948                 delete item;
949
950         SetRadioMode( true );
951         _GetChannels();
952         BMenu::AttachedToWindow();
953 }
954
955 /*****************************************************************************
956  * LanguageMenu::_GetChannels
957  *****************************************************************************/
958 void LanguageMenu::_GetChannels()
959 {
960     BMenuItem *item;
961     BList *list;
962     
963     if( ( list = p_wrapper->InputGetChannels( kind ) ) == NULL )
964         return;
965     
966     for( int i = 0; i < list->CountItems(); i++ )
967     {
968         item = (BMenuItem*)list->ItemAt( i );
969         AddItem( item );
970     }
971     
972     if( list->CountItems() > 1 )
973         AddItem( new BSeparatorItem(), 1 );
974 }
975
976
977 /*****************************************************************************
978  * TitleMenu::TitleMenu
979  *****************************************************************************/
980 TitleMenu::TitleMenu( const char *name, intf_thread_t  *p_interface )
981         : BMenu(name),
982         p_intf( p_interface )
983 {
984 }
985
986 /*****************************************************************************
987  * TitleMenu::~TitleMenu
988  *****************************************************************************/
989 TitleMenu::~TitleMenu()
990 {
991 }
992
993 /*****************************************************************************
994  * TitleMenu::AttachedToWindow
995  *****************************************************************************/
996 void TitleMenu::AttachedToWindow()
997 {
998         // make title menu empty
999         while ( BMenuItem* item = RemoveItem( 0L ) )
1000                 delete item;
1001
1002 #if 0
1003         input_thread_t* input = p_intf->p_sys->p_input;
1004         if ( input )
1005         {
1006                 // lock stream access
1007                 vlc_mutex_lock( &input->stream.stream_lock );
1008                 // populate menu according to current stream
1009                 int32 numTitles = input->stream.i_area_nb;
1010                 if ( numTitles > 1 )
1011                 {
1012                         // disallow title 0!
1013                         for ( int32 i = 1; i < numTitles; i++ )
1014                         {
1015                                 BMessage* message = new BMessage( TOGGLE_TITLE );
1016                                 message->AddInt32( "index", i );
1017                                 BString helper( "" );
1018                                 helper << i;
1019                                 BMenuItem* item = new BMenuItem( helper.String(), message );
1020                                 item->SetMarked( input->stream.p_selected_area->i_id == i );
1021                                 AddItem( item );
1022                         }
1023                 }
1024                 // done messing with stream
1025                 vlc_mutex_unlock( &input->stream.stream_lock );
1026         }
1027 #endif
1028         BMenu::AttachedToWindow();
1029 }
1030
1031
1032 /*****************************************************************************
1033  * ChapterMenu::ChapterMenu
1034  *****************************************************************************/
1035 ChapterMenu::ChapterMenu( const char *name, intf_thread_t  *p_interface )
1036         : BMenu(name),
1037         p_intf( p_interface )
1038 {
1039 }
1040
1041 /*****************************************************************************
1042  * ChapterMenu::~ChapterMenu
1043  *****************************************************************************/
1044 ChapterMenu::~ChapterMenu()
1045 {
1046 }
1047
1048 /*****************************************************************************
1049  * ChapterMenu::AttachedToWindow
1050  *****************************************************************************/
1051 void ChapterMenu::AttachedToWindow()
1052 {
1053         // make title menu empty
1054         while ( BMenuItem* item = RemoveItem( 0L ) )
1055                 delete item;
1056
1057 #if 0
1058         input_thread_t* input = p_intf->p_sys->p_input;
1059         if ( input )
1060         {
1061                 // lock stream access
1062                 vlc_mutex_lock( &input->stream.stream_lock );
1063                 // populate menu according to current stream
1064                 int32 numChapters = input->stream.p_selected_area->i_part_nb;
1065                 if ( numChapters > 1 )
1066                 {
1067                         for ( int32 i = 0; i < numChapters; i++ )
1068                         {
1069                                 BMessage* message = new BMessage( TOGGLE_CHAPTER );
1070                                 message->AddInt32( "index", i );
1071                                 BString helper( "" );
1072                                 helper << i + 1;
1073                                 BMenuItem* item = new BMenuItem( helper.String(), message );
1074                                 item->SetMarked( input->stream.p_selected_area->i_part == i );
1075                                 AddItem( item );
1076                         }
1077                 }
1078                 // done messing with stream
1079                 vlc_mutex_unlock( &input->stream.stream_lock );
1080         }
1081         BMenu::AttachedToWindow();
1082 #endif
1083 }
1084