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