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