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