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