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