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