d0bce6bd29e0996f2b01b588eb38a306b4266145
[vlc.git] / modules / gui / beos / InterfaceWindow.cpp
1 /*****************************************************************************
2  * InterfaceWindow.cpp: beos interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: InterfaceWindow.cpp,v 1.42 2003/06/22 00:40:18 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     fPlaylistIsEmpty = !( p_wrapper->PlaylistSize() > 0 );
193     
194     BScreen screen;
195     BRect screen_rect = screen.Frame();
196     BRect window_rect;
197     window_rect.Set( ( screen_rect.right - PREFS_WINDOW_WIDTH ) / 2,
198                      ( screen_rect.bottom - PREFS_WINDOW_HEIGHT ) / 2,
199                      ( screen_rect.right + PREFS_WINDOW_WIDTH ) / 2,
200                      ( screen_rect.bottom + PREFS_WINDOW_HEIGHT ) / 2 );
201     fPreferencesWindow = new PreferencesWindow( p_intf, window_rect, _("Preferences") );
202     window_rect.Set( screen_rect.right - 500,
203                      screen_rect.top + 50,
204                      screen_rect.right - 150,
205                      screen_rect.top + 250 );
206     fPlaylistWindow = new PlayListWindow( window_rect, _("Playlist"), this, p_intf );
207     window_rect.Set( screen_rect.right - 550,
208                      screen_rect.top + 300,
209                      screen_rect.right - 150,
210                      screen_rect.top + 500 );
211     fMessagesWindow = new MessagesWindow( p_intf, window_rect, _("Messages") );
212
213     // the media control view
214     p_mediaControl = new MediaControlView( BRect( 0.0, 0.0, 250.0, 50.0 ),
215                                            p_intf );
216     p_mediaControl->SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );
217
218     float width, height;
219     p_mediaControl->GetPreferredSize( &width, &height );
220
221     // set up the main menu
222     fMenuBar = new BMenuBar( BRect(0.0, 0.0, width, 15.0), "main menu",
223                              B_FOLLOW_NONE, B_ITEMS_IN_ROW, false );
224
225     // make menu bar resize to correct height
226     float menuWidth, menuHeight;
227     fMenuBar->GetPreferredSize( &menuWidth, &menuHeight );
228     fMenuBar->ResizeTo( width, menuHeight );    // don't change! it's a workarround!
229     // take care of proper size for ourself
230     height += fMenuBar->Bounds().Height();
231     ResizeTo( width, height );
232
233     p_mediaControl->MoveTo( fMenuBar->Bounds().LeftBottom() + BPoint(0.0, 1.0) );
234     AddChild( fMenuBar );
235     AddChild( p_mediaControl );
236
237     // Add the file Menu
238     BMenu* fileMenu = new BMenu( _("File") );
239     fMenuBar->AddItem( fileMenu );
240     fileMenu->AddItem( new BMenuItem( _AddEllipsis(_("Open File")),
241                                       new BMessage( OPEN_FILE ), 'O') );
242     
243     fileMenu->AddItem( new CDMenu( _("Open Disc") ) );
244
245     fileMenu->AddItem( new BMenuItem( _AddEllipsis(_("Open Subtitles")),
246                                       new BMessage( LOAD_SUBFILE ) ) );
247     
248     fileMenu->AddSeparatorItem();
249     BMenuItem* item = new BMenuItem( _AddEllipsis(_("About")),
250                                      new BMessage( B_ABOUT_REQUESTED ), 'A');
251     item->SetTarget( be_app );
252     fileMenu->AddItem( item );
253     fileMenu->AddItem( new BMenuItem( _("Quit"), new BMessage( B_QUIT_REQUESTED ), 'Q') );
254
255     fLanguageMenu = new LanguageMenu( _("Language"), AUDIO_ES, p_wrapper);
256     fSubtitlesMenu = new LanguageMenu( _("Subtitles"), SPU_ES, p_wrapper);
257
258     /* Add the Audio menu */
259     fAudioMenu = new BMenu( _("Audio") );
260     fMenuBar->AddItem ( fAudioMenu );
261     fAudioMenu->AddItem( fLanguageMenu );
262     fAudioMenu->AddItem( fSubtitlesMenu );
263
264     fPrevTitleMI = new BMenuItem( _("Prev Title"), new BMessage( PREV_TITLE ) );
265     fNextTitleMI = new BMenuItem( _("Next Title"), new BMessage( NEXT_TITLE ) );
266     fPrevChapterMI = new BMenuItem( _("Prev Chapter"), new BMessage( PREV_CHAPTER ) );
267     fNextChapterMI = new BMenuItem( _("Next Chapter"), new BMessage( NEXT_CHAPTER ) );
268     fGotoMenuMI = new BMenuItem( _("Goto Menu"), new BMessage( NAVIGATE_MENU ) );
269
270     /* Add the Navigation menu */
271     fNavigationMenu = new BMenu( _("Navigation") );
272     fMenuBar->AddItem( fNavigationMenu );
273     fNavigationMenu->AddItem( fGotoMenuMI );
274     fNavigationMenu->AddSeparatorItem();
275     fNavigationMenu->AddItem( fPrevTitleMI );
276     fNavigationMenu->AddItem( fNextTitleMI );
277     fNavigationMenu->AddItem( fTitleMenu = new TitleMenu( _("Go to Title"), p_intf ) );
278     fNavigationMenu->AddSeparatorItem();
279     fNavigationMenu->AddItem( fPrevChapterMI );
280     fNavigationMenu->AddItem( fNextChapterMI );
281     fNavigationMenu->AddItem( fChapterMenu = new ChapterMenu( _("Go to Chapter"), p_intf ) );
282
283     /* Add the Speed menu */
284     fSpeedMenu = new BMenu( _("Speed") );
285     fSpeedMenu->SetRadioMode( true );
286     fSpeedMenu->AddItem(
287         fHeighthMI = new BMenuItem( "1/8x", new BMessage( HEIGHTH_PLAY ) ) );
288     fSpeedMenu->AddItem(
289         fQuarterMI = new BMenuItem( "1/4x", new BMessage( QUARTER_PLAY ) ) );
290     fSpeedMenu->AddItem(
291         fHalfMI = new BMenuItem( "1/2x", new BMessage( HALF_PLAY ) ) );
292     fSpeedMenu->AddItem(
293         fNormalMI = new BMenuItem( "1x", new BMessage( NORMAL_PLAY ) ) );
294     fSpeedMenu->AddItem(
295         fTwiceMI = new BMenuItem( "2x", new BMessage( TWICE_PLAY ) ) );
296     fSpeedMenu->AddItem(
297         fFourMI = new BMenuItem( "4x", new BMessage( FOUR_PLAY ) ) );
298     fSpeedMenu->AddItem(
299         fHeightMI = new BMenuItem( "8x", new BMessage( HEIGHT_PLAY ) ) );
300     fMenuBar->AddItem( fSpeedMenu );
301
302     /* Add the Show menu */
303     fShowMenu = new BMenu( _("Window") );
304     fShowMenu->AddItem( new BMenuItem( _AddEllipsis(_("Play List")),
305                                        new BMessage( OPEN_PLAYLIST ), 'P') );
306     fShowMenu->AddItem( new BMenuItem( _AddEllipsis(_("Messages")),
307                                        new BMessage( OPEN_MESSAGES ), 'M' ) );
308     fShowMenu->AddItem( new BMenuItem( _AddEllipsis(_("Preferences")),
309                                        new BMessage( OPEN_PREFERENCES ), 'S' ) );
310     fMenuBar->AddItem( fShowMenu );                            
311
312     /* Prepare fow showing */
313     _SetMenusEnabled( false );
314     p_mediaControl->SetEnabled( false );
315
316         _RestoreSettings();    
317
318     Show();
319 }
320
321 InterfaceWindow::~InterfaceWindow()
322 {
323     if( fPlaylistWindow )
324         fPlaylistWindow->ReallyQuit();
325     fPlaylistWindow = NULL;
326     if( fMessagesWindow )
327         fMessagesWindow->ReallyQuit();
328     fMessagesWindow = NULL;
329     if( fPreferencesWindow )
330         fPreferencesWindow->ReallyQuit();
331     fPreferencesWindow = NULL;
332         delete fFilePanel;
333         delete fSettings;
334 }
335
336 /*****************************************************************************
337  * InterfaceWindow::FrameResized
338  *****************************************************************************/
339 void
340 InterfaceWindow::FrameResized(float width, float height)
341 {
342     BRect r(Bounds());
343     fMenuBar->MoveTo(r.LeftTop());
344     fMenuBar->ResizeTo(r.Width(), fMenuBar->Bounds().Height());
345     r.top += fMenuBar->Bounds().Height() + 1.0;
346     p_mediaControl->MoveTo(r.LeftTop());
347     p_mediaControl->ResizeTo(r.Width(), r.Height());
348 }
349
350 /*****************************************************************************
351  * InterfaceWindow::MessageReceived
352  *****************************************************************************/
353 void InterfaceWindow::MessageReceived( BMessage * p_message )
354 {
355     int playback_status;      // remember playback state
356     playback_status = p_wrapper->InputStatus();
357
358     switch( p_message->what )
359     {
360         case B_ABOUT_REQUESTED:
361         {
362             BAlert* alert = new BAlert( "VLC " PACKAGE_VERSION,
363                                         "VLC " PACKAGE_VERSION " for BeOS"
364                                         "\n\n<www.videolan.org>", _("OK"));
365             alert->Go();
366             break;
367         }
368         case TOGGLE_ON_TOP:
369             break;
370             
371         case OPEN_FILE:
372                 _ShowFilePanel( B_REFS_RECEIVED, _("VideoLAN Client: Open Media Files") );
373             break;
374
375         case LOAD_SUBFILE:
376                 _ShowFilePanel( SUBFILE_RECEIVED, _("VideoLAN Client: Open Subtitle File") );
377             break;
378
379         case OPEN_PLAYLIST:
380             if (fPlaylistWindow->Lock())
381             {
382                 if (fPlaylistWindow->IsHidden())
383                     fPlaylistWindow->Show();
384                 else
385                     fPlaylistWindow->Activate();
386                 fPlaylistWindow->Unlock();
387             }
388             break;
389         case OPEN_DVD:
390             {
391                 const char *psz_device;
392                 BString type( "dvd" );
393                 if( p_message->FindString( "device", &psz_device ) == B_OK )
394                 {
395                     BString device( psz_device );
396                     p_wrapper->OpenDisc( type, device, 0, 0 );
397                 }
398                 _UpdatePlaylist();
399             }
400             break;
401         
402         case SUBFILE_RECEIVED:
403         {
404             entry_ref ref;
405             if( p_message->FindRef( "refs", 0, &ref ) == B_OK )
406             {
407                 BPath path( &ref );
408                 if ( path.InitCheck() == B_OK )
409                     p_wrapper->LoadSubFile( path.Path() );
410             }
411             break;
412         }
413     
414         case STOP_PLAYBACK:
415             // this currently stops playback not nicely
416             if (playback_status > UNDEF_S)
417             {
418                 p_wrapper->PlaylistStop();
419                 p_mediaControl->SetStatus(NOT_STARTED_S, DEFAULT_RATE);
420             }
421             break;
422     
423         case START_PLAYBACK:
424             /*  starts playing in normal mode */
425     
426         case PAUSE_PLAYBACK:
427             /* toggle between pause and play */
428             if (playback_status > UNDEF_S)
429             {
430                 /* pause if currently playing */
431                 if ( playback_status == PLAYING_S )
432                 {
433                     p_wrapper->PlaylistPause();
434                 }
435                 else
436                 {
437                     p_wrapper->PlaylistPlay();
438                 }
439             }
440             else
441             {
442                 /* Play a new file */
443                 p_wrapper->PlaylistPlay();
444             }    
445             break;
446     
447         case HEIGHTH_PLAY:
448             p_wrapper->InputSetRate( DEFAULT_RATE * 8 );
449             break;
450
451         case QUARTER_PLAY:
452             p_wrapper->InputSetRate( DEFAULT_RATE * 4 );
453             break;
454
455         case HALF_PLAY:
456             p_wrapper->InputSetRate( DEFAULT_RATE * 2 );
457             break;
458
459         case NORMAL_PLAY:
460             p_wrapper->InputSetRate( DEFAULT_RATE );
461             break;
462
463         case TWICE_PLAY:
464             p_wrapper->InputSetRate( DEFAULT_RATE / 2 );
465             break;
466
467         case FOUR_PLAY:
468             p_wrapper->InputSetRate( DEFAULT_RATE / 4 );
469             break;
470
471         case HEIGHT_PLAY:
472             p_wrapper->InputSetRate( DEFAULT_RATE / 8 );
473             break;
474
475         case SEEK_PLAYBACK:
476             /* handled by semaphores */
477             break;
478         // volume related messages
479         case VOLUME_CHG:
480             /* adjust the volume */
481             if (playback_status > UNDEF_S)
482             {
483                 p_wrapper->SetVolume( p_mediaControl->GetVolume() );
484                 p_mediaControl->SetMuted( p_wrapper->IsMuted() );
485             }
486             break;
487     
488         case VOLUME_MUTE:
489             // toggle muting
490             if( p_wrapper->IsMuted() )
491                 p_wrapper->VolumeRestore();
492             else
493                 p_wrapper->VolumeMute();
494             p_mediaControl->SetMuted( p_wrapper->IsMuted() );
495             break;
496     
497         case SELECT_CHANNEL:
498             if ( playback_status > UNDEF_S )
499             {
500                 int32 channel;
501                 if ( p_message->FindInt32( "channel", &channel ) == B_OK )
502                 {
503                     p_wrapper->ToggleLanguage( channel );
504                 }
505             }
506             break;
507     
508         case SELECT_SUBTITLE:
509             if ( playback_status > UNDEF_S )
510             {
511                 int32 subtitle;
512                 if ( p_message->FindInt32( "subtitle", &subtitle ) == B_OK )
513                      p_wrapper->ToggleSubtitle( subtitle );
514             }
515             break;
516     
517         // specific navigation messages
518         case PREV_TITLE:
519         {
520             p_wrapper->PrevTitle();
521             break;
522         }
523         case NEXT_TITLE:
524         {
525             p_wrapper->NextTitle();
526             break;
527         }
528         case NAVIGATE_MENU:
529                 p_wrapper->ToggleTitle( 0 );
530                 break;
531         case TOGGLE_TITLE:
532             if ( playback_status > UNDEF_S )
533             {
534                 int32 index;
535                 if( p_message->FindInt32( "index", &index ) == B_OK )
536                     p_wrapper->ToggleTitle( index );
537             }
538             break;
539         case PREV_CHAPTER:
540         {
541             p_wrapper->PrevChapter();
542             break;
543         }
544         case NEXT_CHAPTER:
545         {
546             p_wrapper->NextChapter();
547             break;
548         }
549         case TOGGLE_CHAPTER:
550             if ( playback_status > UNDEF_S )
551             {
552                 int32 index;
553                 if( p_message->FindInt32( "index", &index ) == B_OK )
554                     p_wrapper->ToggleChapter( index );
555             }
556             break;
557         case PREV_FILE:
558             p_wrapper->PlaylistPrev();
559             break;
560         case NEXT_FILE:
561             p_wrapper->PlaylistNext();
562             break;
563         // general next/prev functionality (skips to whatever makes most sense)
564         case NAVIGATE_PREV:
565             p_wrapper->NavigatePrev();
566             break;
567         case NAVIGATE_NEXT:
568             p_wrapper->NavigateNext();
569             break;
570         // drag'n'drop and system messages
571         case MSG_SOUNDPLAY:
572                 // convert soundplay drag'n'drop message (containing paths)
573                 // to normal message (containing refs)
574                 {
575                         const char* path;
576                         for ( int32 i = 0; p_message->FindString( "path", i, &path ) == B_OK; i++ )
577                         {
578                                 entry_ref ref;
579                                 if ( get_ref_for_path( path, &ref ) == B_OK )
580                                         p_message->AddRef( "refs", &ref );
581                         }
582                 }
583                 // fall through
584         case B_REFS_RECEIVED:
585         case B_SIMPLE_DATA:
586             {
587                 /* file(s) opened by the File menu -> append to the playlist;
588                  * file(s) opened by drag & drop -> replace playlist;
589                  * file(s) opened by 'shift' + drag & drop -> append */
590                 bool replace = false;
591                 bool reverse = false;
592                 if ( p_message->WasDropped() )
593                 {
594                     replace = !( modifiers() & B_SHIFT_KEY );
595                     reverse = true;
596                 }
597                     
598                 // build list of files to be played from message contents
599                 entry_ref ref;
600                 BList files;
601                 
602                 // if we should parse sub-folders as well
603                         bool askedAlready = false;
604                         bool parseSubFolders = askedAlready;
605                         // traverse refs in reverse order
606                         int32 count;
607                         type_code dummy;
608                         if ( p_message->GetInfo( "refs", &dummy, &count ) == B_OK && count > 0 )
609                         {
610                                 int32 i = reverse ? count - 1 : 0;
611                                 int32 increment = reverse ? -1 : 1;
612                         for ( ; p_message->FindRef( "refs", i, &ref ) == B_OK; i += increment )
613                         {
614                             BPath path( &ref );
615                             if ( path.InitCheck() == B_OK )
616                             {
617                                 bool add = true;
618                                 // has the user dropped a folder?
619                                 BDirectory dir( &ref );
620                                 if ( dir.InitCheck() == B_OK)
621                                 {
622                                         // has the user dropped a dvd disk icon?
623                                                                 if ( dir.IsRootDirectory() )
624                                                                 {
625                                                                         BVolumeRoster volRoster;
626                                                                         BVolume vol;
627                                                                         BDirectory volumeRoot;
628                                                                         status_t status = volRoster.GetNextVolume( &vol );
629                                                                         while ( status == B_NO_ERROR )
630                                                                         {
631                                                                                 if ( vol.GetRootDirectory( &volumeRoot ) == B_OK
632                                                                                          && dir == volumeRoot )
633                                                                                 {
634                                                                                         BString volumeName;
635                                                                                         BString deviceName;
636                                                                                         bool isCDROM;
637                                                                                         if ( get_volume_info( vol, volumeName, isCDROM, deviceName )
638                                                                                                  && isCDROM )
639                                                                                         {
640                                                                                                 BMessage msg( OPEN_DVD );
641                                                                                                 msg.AddString( "device", deviceName.String() );
642                                                                                                 PostMessage( &msg );
643                                                                                                 add = false;
644                                                                                         }
645                                                                                         break;
646                                                                                 }
647                                                                                 else
648                                                                                 {
649                                                                                         vol.Unset();
650                                                                                         status = volRoster.GetNextVolume( &vol );
651                                                                                 }
652                                                                         }
653                                                                 }
654                                         if ( add )
655                                         {
656                                                 add = false;
657                                                 dir.Rewind();   // defensive programming
658                                                 BEntry entry;
659                                                                         collect_folder_contents( dir, files,
660                                                                                                                          parseSubFolders,
661                                                                                                                          askedAlready,
662                                                                                                                          entry );
663                                         }
664                                 }
665                                 if ( add )
666                                 {
667                                         BString* string = new BString( path.Path() );
668                                         if ( !files.AddItem( string, 0 ) )
669                                                 delete string;  // at least don't leak
670                                 }
671                             }
672                         }
673                         // give the list to VLC
674                         // BString objects allocated here will be deleted there
675                         int32 index;
676                         if ( p_message->FindInt32("drop index", &index) != B_OK )
677                                 index = -1;
678                         p_wrapper->OpenFiles( &files, replace, index );
679                         _UpdatePlaylist();
680                         }
681             }
682             break;
683
684         case OPEN_PREFERENCES:
685         {
686             if( fPreferencesWindow->Lock() )
687             {
688                 if (fPreferencesWindow->IsHidden())
689                     fPreferencesWindow->Show();
690                 else
691                     fPreferencesWindow->Activate();
692                 fPreferencesWindow->Unlock();
693             }
694             break;
695         }
696
697         case OPEN_MESSAGES:
698         {
699             if( fMessagesWindow->Lock() )
700             {
701                 if (fMessagesWindow->IsHidden())
702                     fMessagesWindow->Show();
703                 else
704                     fMessagesWindow->Activate();
705                 fMessagesWindow->Unlock();
706             }
707             break;
708         }
709         case MSG_UPDATE:
710                 UpdateInterface();
711                 break;
712         default:
713             BWindow::MessageReceived( p_message );
714             break;
715     }
716
717 }
718
719 /*****************************************************************************
720  * InterfaceWindow::QuitRequested
721  *****************************************************************************/
722 bool InterfaceWindow::QuitRequested()
723 {
724     p_wrapper->PlaylistStop();
725     p_mediaControl->SetStatus(NOT_STARTED_S, DEFAULT_RATE);
726
727         _StoreSettings();
728    
729     p_intf->b_die = 1;
730
731     return( true );
732 }
733
734 /*****************************************************************************
735  * InterfaceWindow::UpdateInterface
736  *****************************************************************************/
737 void InterfaceWindow::UpdateInterface()
738 {
739     if( p_wrapper->HasInput() )
740     {
741         if ( acquire_sem( p_mediaControl->fScrubSem ) == B_OK )
742         {
743             p_wrapper->SetTimeAsFloat( p_mediaControl->GetSeekTo() );
744         }
745         else if ( LockWithTimeout( INTERFACE_LOCKING_TIMEOUT ) == B_OK )
746         {
747             p_mediaControl->SetEnabled( true );
748             bool hasTitles = p_wrapper->HasTitles();
749             bool hasChapters = p_wrapper->HasChapters();
750             p_mediaControl->SetStatus( p_wrapper->InputStatus(), 
751                                        p_wrapper->InputRate() );
752             p_mediaControl->SetProgress( p_wrapper->GetTimeAsFloat() );
753             _SetMenusEnabled( true, hasChapters, hasTitles );
754
755             _UpdateSpeedMenu( p_wrapper->InputRate() );
756
757             // enable/disable skip buttons
758             bool canSkipPrev;
759             bool canSkipNext;
760             p_wrapper->GetNavCapabilities( &canSkipPrev, &canSkipNext );
761             p_mediaControl->SetSkippable( canSkipPrev, canSkipNext );
762
763             if ( p_wrapper->HasInput() )
764             {
765                 p_mediaControl->SetAudioEnabled( true );
766                 p_mediaControl->SetMuted( p_wrapper->IsMuted() );
767             } else
768                 p_mediaControl->SetAudioEnabled( false );
769
770             Unlock();
771         }
772         // update playlist as well
773         if ( fPlaylistWindow->LockWithTimeout( INTERFACE_LOCKING_TIMEOUT ) == B_OK )
774         {
775             fPlaylistWindow->UpdatePlaylist();
776             fPlaylistWindow->Unlock();
777         }
778     }
779     else
780     {
781                 if ( LockWithTimeout(INTERFACE_LOCKING_TIMEOUT) == B_OK )
782                 {
783                 _SetMenusEnabled( false );
784                 if( !( p_wrapper->PlaylistSize() > 0 ) )
785                     p_mediaControl->SetEnabled( false );
786                 else
787                 {
788                     p_mediaControl->SetProgress( 0 );
789                     // enable/disable skip buttons
790                     bool canSkipPrev;
791                     bool canSkipNext;
792                     p_wrapper->GetNavCapabilities( &canSkipPrev, &canSkipNext );
793                     p_mediaControl->SetSkippable( canSkipPrev, canSkipNext );
794                         }
795             Unlock();
796         }
797     }
798
799     fLastUpdateTime = system_time();
800 }
801
802 /*****************************************************************************
803  * InterfaceWindow::IsStopped
804  *****************************************************************************/
805 bool
806 InterfaceWindow::IsStopped() const
807 {
808     return (system_time() - fLastUpdateTime > INTERFACE_UPDATE_TIMEOUT);
809 }
810
811 /*****************************************************************************
812  * InterfaceWindow::_UpdatePlaylist
813  *****************************************************************************/
814 void
815 InterfaceWindow::_UpdatePlaylist()
816 {
817     if ( fPlaylistWindow->Lock() )
818     {
819         fPlaylistWindow->UpdatePlaylist( true );
820         fPlaylistWindow->Unlock();
821         p_mediaControl->SetEnabled( p_wrapper->PlaylistSize() );
822     }
823 }
824
825 /*****************************************************************************
826  * InterfaceWindow::_SetMenusEnabled
827  *****************************************************************************/
828 void
829 InterfaceWindow::_SetMenusEnabled(bool hasFile, bool hasChapters, bool hasTitles)
830 {
831     if (!hasFile)
832     {
833         hasChapters = false;
834         hasTitles = false;
835     }
836     if ( LockWithTimeout( INTERFACE_LOCKING_TIMEOUT ) == B_OK)
837     {
838         if ( fNextChapterMI->IsEnabled() != hasChapters )
839              fNextChapterMI->SetEnabled( hasChapters );
840         if ( fPrevChapterMI->IsEnabled() != hasChapters )
841              fPrevChapterMI->SetEnabled( hasChapters );
842         if ( fChapterMenu->IsEnabled() != hasChapters )
843              fChapterMenu->SetEnabled( hasChapters );
844         if ( fNextTitleMI->IsEnabled() != hasTitles )
845              fNextTitleMI->SetEnabled( hasTitles );
846         if ( fPrevTitleMI->IsEnabled() != hasTitles )
847              fPrevTitleMI->SetEnabled( hasTitles );
848         if ( fTitleMenu->IsEnabled() != hasTitles )
849              fTitleMenu->SetEnabled( hasTitles );
850         if ( fAudioMenu->IsEnabled() != hasFile )
851              fAudioMenu->SetEnabled( hasFile );
852         if ( fNavigationMenu->IsEnabled() != hasFile )
853              fNavigationMenu->SetEnabled( hasFile );
854         if ( fLanguageMenu->IsEnabled() != hasFile )
855              fLanguageMenu->SetEnabled( hasFile );
856         if ( fSubtitlesMenu->IsEnabled() != hasFile )
857              fSubtitlesMenu->SetEnabled( hasFile );
858         if ( fSpeedMenu->IsEnabled() != hasFile )
859              fSpeedMenu->SetEnabled( hasFile );
860         // "goto menu" menu item
861         bool hasMenu = p_wrapper->IsUsingMenus();
862         if ( fGotoMenuMI->IsEnabled() != hasMenu )
863              fGotoMenuMI->SetEnabled( hasMenu );
864         Unlock();
865     }
866 }
867
868 /*****************************************************************************
869  * InterfaceWindow::_UpdateSpeedMenu
870  *****************************************************************************/
871 void
872 InterfaceWindow::_UpdateSpeedMenu( int rate )
873 {
874     BMenuItem * toMark = NULL;
875     
876     switch( rate )
877     {
878         case ( DEFAULT_RATE * 8 ):
879             toMark = fHeighthMI;
880             break;
881             
882         case ( DEFAULT_RATE * 4 ):
883             toMark = fQuarterMI;
884             break;
885             
886         case ( DEFAULT_RATE * 2 ):
887             toMark = fHalfMI;
888             break;
889             
890         case ( DEFAULT_RATE ):
891             toMark = fNormalMI;
892             break;
893             
894         case ( DEFAULT_RATE / 2 ):
895             toMark = fTwiceMI;
896             break;
897             
898         case ( DEFAULT_RATE / 4 ):
899             toMark = fFourMI;
900             break;
901             
902         case ( DEFAULT_RATE / 8 ):
903             toMark = fHeightMI;
904             break;
905     }
906
907     if ( !toMark->IsMarked() )
908         toMark->SetMarked( true );
909 }
910
911 /*****************************************************************************
912  * InterfaceWindow::_ShowFilePanel
913  *****************************************************************************/
914 void
915 InterfaceWindow::_ShowFilePanel( uint32 command, const char* windowTitle )
916 {
917         if( !fFilePanel )
918         {
919                 fFilePanel = new BFilePanel( B_OPEN_PANEL, NULL, NULL,
920                                                                          B_FILE_NODE | B_DIRECTORY_NODE );
921                 fFilePanel->SetTarget( this );
922         }
923         fFilePanel->Window()->SetTitle( windowTitle );
924         BMessage message( command );
925         fFilePanel->SetMessage( &message );
926         if ( !fFilePanel->IsShowing() )
927         {
928                 fFilePanel->Refresh();
929                 fFilePanel->Show();
930         }
931 }
932
933 // set_window_pos
934 void
935 set_window_pos( BWindow* window, BRect frame )
936 {
937         // sanity checks: make sure window is not too big/small
938         // and that it's not off-screen
939         float minWidth, maxWidth, minHeight, maxHeight;
940         window->GetSizeLimits( &minWidth, &maxWidth, &minHeight, &maxHeight );
941
942         make_sure_frame_is_within_limits( frame,
943                                                                           minWidth, minHeight, maxWidth, maxHeight );
944         if ( make_sure_frame_is_on_screen( frame ) )
945         {
946                 window->MoveTo( frame.LeftTop() );
947                 window->ResizeTo( frame.Width(), frame.Height() );
948         }
949 }
950
951 // set_window_pos
952 void
953 launch_window( BWindow* window, bool showing )
954 {
955         if ( window->Lock() )
956         {
957                 if ( showing )
958                 {
959                         if ( window->IsHidden() )
960                                 window->Show();
961                 }
962                 else
963                 {
964                         if ( !window->IsHidden() )
965                                 window->Hide();
966                 }
967                 window->Unlock();
968         }
969 }
970
971 /*****************************************************************************
972  * InterfaceWindow::_RestoreSettings
973  *****************************************************************************/
974 void
975 InterfaceWindow::_RestoreSettings()
976 {
977         if ( load_settings( fSettings, "interface_settings", "VideoLAN Client" ) == B_OK )
978         {
979                 BRect frame;
980                 if ( fSettings->FindRect( "main frame", &frame ) == B_OK )
981                         set_window_pos( this, frame );
982                 if (fSettings->FindRect( "playlist frame", &frame ) == B_OK )
983                         set_window_pos( fPlaylistWindow, frame );
984                 if (fSettings->FindRect( "messages frame", &frame ) == B_OK )
985                         set_window_pos( fMessagesWindow, frame );
986                 if (fSettings->FindRect( "settings frame", &frame ) == B_OK )
987                 {
988                     /* FIXME: Preferences resizing doesn't work correctly yet */
989                     frame.right = frame.left + fPreferencesWindow->Frame().Width();
990                     frame.bottom = frame.top + fPreferencesWindow->Frame().Height();
991                         set_window_pos( fPreferencesWindow, frame );
992                 }
993                 
994                 bool showing;
995                 if ( fSettings->FindBool( "playlist showing", &showing ) == B_OK )
996                         launch_window( fPlaylistWindow, showing );
997                 if ( fSettings->FindBool( "messages showing", &showing ) == B_OK )
998                         launch_window( fMessagesWindow, showing );
999                 if ( fSettings->FindBool( "settings showing", &showing ) == B_OK )
1000                         launch_window( fPreferencesWindow, showing );
1001
1002                 uint32 displayMode;
1003                 if ( fSettings->FindInt32( "playlist display mode", (int32*)&displayMode ) == B_OK )
1004                         fPlaylistWindow->SetDisplayMode( displayMode );
1005         }
1006 }
1007
1008 /*****************************************************************************
1009  * InterfaceWindow::_StoreSettings
1010  *****************************************************************************/
1011 void
1012 InterfaceWindow::_StoreSettings()
1013 {
1014     /* Save the volume */
1015     config_PutInt( p_intf, "volume", p_mediaControl->GetVolume() );
1016     config_SaveConfigFile( p_intf, "main" );
1017
1018     /* Save the windows positions */
1019         if ( fSettings->ReplaceRect( "main frame", Frame() ) != B_OK )
1020                 fSettings->AddRect( "main frame", Frame() );
1021         if ( fPlaylistWindow->Lock() )
1022         {
1023                 if (fSettings->ReplaceRect( "playlist frame", fPlaylistWindow->Frame() ) != B_OK)
1024                         fSettings->AddRect( "playlist frame", fPlaylistWindow->Frame() );
1025                 if (fSettings->ReplaceBool( "playlist showing", !fPlaylistWindow->IsHidden() ) != B_OK)
1026                         fSettings->AddBool( "playlist showing", !fPlaylistWindow->IsHidden() );
1027                 fPlaylistWindow->Unlock();
1028         }
1029         if ( fMessagesWindow->Lock() )
1030         {
1031                 if (fSettings->ReplaceRect( "messages frame", fMessagesWindow->Frame() ) != B_OK)
1032                         fSettings->AddRect( "messages frame", fMessagesWindow->Frame() );
1033                 if (fSettings->ReplaceBool( "messages showing", !fMessagesWindow->IsHidden() ) != B_OK)
1034                         fSettings->AddBool( "messages showing", !fMessagesWindow->IsHidden() );
1035                 fMessagesWindow->Unlock();
1036         }
1037         if ( fPreferencesWindow->Lock() )
1038         {
1039                 if (fSettings->ReplaceRect( "settings frame", fPreferencesWindow->Frame() ) != B_OK)
1040                         fSettings->AddRect( "settings frame", fPreferencesWindow->Frame() );
1041                 if (fSettings->ReplaceBool( "settings showing", !fPreferencesWindow->IsHidden() ) != B_OK)
1042                         fSettings->AddBool( "settings showing", !fPreferencesWindow->IsHidden() );
1043                 fPreferencesWindow->Unlock();
1044         }
1045         uint32 displayMode = fPlaylistWindow->DisplayMode();
1046         if (fSettings->ReplaceInt32( "playlist display mode", displayMode ) != B_OK )
1047                 fSettings->AddInt32( "playlist display mode", displayMode );
1048
1049         save_settings( fSettings, "interface_settings", "VideoLAN Client" );
1050 }
1051
1052
1053 /*****************************************************************************
1054  * CDMenu::CDMenu
1055  *****************************************************************************/
1056 CDMenu::CDMenu(const char *name)
1057       : BMenu(name)
1058 {
1059 }
1060
1061 /*****************************************************************************
1062  * CDMenu::~CDMenu
1063  *****************************************************************************/
1064 CDMenu::~CDMenu()
1065 {
1066 }
1067
1068 /*****************************************************************************
1069  * CDMenu::AttachedToWindow
1070  *****************************************************************************/
1071 void CDMenu::AttachedToWindow(void)
1072 {
1073     // remove all items
1074     while ( BMenuItem* item = RemoveItem( 0L ) )
1075         delete item;
1076     GetCD( "/dev/disk" );
1077     BMenu::AttachedToWindow();
1078 }
1079
1080 /*****************************************************************************
1081  * CDMenu::GetCD
1082  *****************************************************************************/
1083 int CDMenu::GetCD( const char *directory )
1084 {
1085         BVolumeRoster volRoster;
1086         BVolume vol;
1087         BDirectory dir;
1088         status_t status = volRoster.GetNextVolume( &vol );
1089         while ( status ==  B_NO_ERROR )
1090         {
1091                 BString deviceName;
1092                 BString volumeName;
1093                 bool isCDROM;
1094                 if ( get_volume_info( vol, volumeName, isCDROM, deviceName )
1095                          && isCDROM )
1096                 {
1097                         BMessage* msg = new BMessage( OPEN_DVD );
1098                         msg->AddString( "device", deviceName.String() );
1099                         BMenuItem* item = new BMenuItem( volumeName.String(), msg );
1100                         AddItem( item );
1101                 }
1102                 vol.Unset();
1103                 status = volRoster.GetNextVolume( &vol );
1104         }
1105         return 0;
1106 }
1107
1108 /*****************************************************************************
1109  * LanguageMenu::LanguageMenu
1110  *****************************************************************************/
1111 LanguageMenu::LanguageMenu( const char *name, int menu_kind, 
1112                             VlcWrapper *p_wrapper )
1113     :BMenu(name)
1114 {
1115     kind = menu_kind;
1116     this->p_wrapper = p_wrapper;
1117 }
1118
1119 /*****************************************************************************
1120  * LanguageMenu::~LanguageMenu
1121  *****************************************************************************/
1122 LanguageMenu::~LanguageMenu()
1123 {
1124 }
1125
1126 /*****************************************************************************
1127  * LanguageMenu::AttachedToWindow
1128  *****************************************************************************/
1129 void LanguageMenu::AttachedToWindow()
1130 {
1131     // remove all items
1132     while ( BMenuItem* item = RemoveItem( 0L ) )
1133         delete item;
1134
1135     SetRadioMode( true );
1136         if ( BList *list = p_wrapper->GetChannels( kind ) )
1137         {
1138             for ( int32 i = 0; BMenuItem* item = (BMenuItem*)list->ItemAt( i ); i++ )
1139                 AddItem( item );
1140             
1141             if ( list->CountItems() > 1 )
1142                 AddItem( new BSeparatorItem(), 1 );
1143         }
1144     BMenu::AttachedToWindow();
1145 }
1146
1147 /*****************************************************************************
1148  * TitleMenu::TitleMenu
1149  *****************************************************************************/
1150 TitleMenu::TitleMenu( const char *name, intf_thread_t  *p_interface )
1151     : BMenu(name),
1152     p_intf( p_interface )
1153 {
1154 }
1155
1156 /*****************************************************************************
1157  * TitleMenu::~TitleMenu
1158  *****************************************************************************/
1159 TitleMenu::~TitleMenu()
1160 {
1161 }
1162
1163 /*****************************************************************************
1164  * TitleMenu::AttachedToWindow
1165  *****************************************************************************/
1166 void TitleMenu::AttachedToWindow()
1167 {
1168     while( BMenuItem* item = RemoveItem( 0L ) )
1169         delete item;
1170
1171     if ( BList *list = p_intf->p_sys->p_wrapper->GetTitles() )
1172         {    
1173                 for( int i = 0; BMenuItem* item = (BMenuItem*)list->ItemAt( i ); i++ )
1174                 AddItem( item );
1175         }
1176     BMenu::AttachedToWindow();
1177 }
1178
1179
1180 /*****************************************************************************
1181  * ChapterMenu::ChapterMenu
1182  *****************************************************************************/
1183 ChapterMenu::ChapterMenu( const char *name, intf_thread_t  *p_interface )
1184     : BMenu(name),
1185     p_intf( p_interface )
1186 {
1187 }
1188
1189 /*****************************************************************************
1190  * ChapterMenu::~ChapterMenu
1191  *****************************************************************************/
1192 ChapterMenu::~ChapterMenu()
1193 {
1194 }
1195
1196 /*****************************************************************************
1197  * ChapterMenu::AttachedToWindow
1198  *****************************************************************************/
1199 void ChapterMenu::AttachedToWindow()
1200 {
1201     while( BMenuItem* item = RemoveItem( 0L ) )
1202         delete item;
1203
1204     if ( BList* list = p_intf->p_sys->p_wrapper->GetChapters() )
1205         {    
1206             for( int i = 0; BMenuItem* item = (BMenuItem*)list->ItemAt( i ); i++ )
1207                 AddItem( item );
1208         }
1209     
1210     BMenu::AttachedToWindow();
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221 /*****************************************************************************
1222  * load_settings
1223  *****************************************************************************/
1224 status_t
1225 load_settings( BMessage* message, const char* fileName, const char* folder )
1226 {
1227         status_t ret = B_BAD_VALUE;
1228         if ( message )
1229         {
1230                 BPath path;
1231                 if ( ( ret = find_directory( B_USER_SETTINGS_DIRECTORY, &path ) ) == B_OK )
1232                 {
1233                         // passing folder is optional
1234                         if ( folder )
1235                                 ret = path.Append( folder );
1236                         if ( ret == B_OK && ( ret = path.Append( fileName ) ) == B_OK )
1237                         {
1238                                 BFile file( path.Path(), B_READ_ONLY );
1239                                 if ( ( ret = file.InitCheck() ) == B_OK )
1240                                 {
1241                                         ret = message->Unflatten( &file );
1242                                         file.Unset();
1243                                 }
1244                         }
1245                 }
1246         }
1247         return ret;
1248 }
1249
1250 /*****************************************************************************
1251  * save_settings
1252  *****************************************************************************/
1253 status_t
1254 save_settings( BMessage* message, const char* fileName, const char* folder )
1255 {
1256         status_t ret = B_BAD_VALUE;
1257         if ( message )
1258         {
1259                 BPath path;
1260                 if ( ( ret = find_directory( B_USER_SETTINGS_DIRECTORY, &path ) ) == B_OK )
1261                 {
1262                         // passing folder is optional
1263                         if ( folder && ( ret = path.Append( folder ) ) == B_OK )
1264                                 ret = create_directory( path.Path(), 0777 );
1265                         if ( ret == B_OK && ( ret = path.Append( fileName ) ) == B_OK )
1266                         {
1267                                 BFile file( path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE );
1268                                 if ( ( ret = file.InitCheck() ) == B_OK )
1269                                 {
1270                                         ret = message->Flatten( &file );
1271                                         file.Unset();
1272                                 }
1273                         }
1274                 }
1275         }
1276         return ret;
1277 }