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