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