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