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