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