]> git.sesse.net Git - vlc/blob - modules/gui/beos/VideoOutput.cpp
* modules/gui/beos/AudioOutput.cpp: more cleaning
[vlc] / modules / gui / beos / VideoOutput.cpp
1 /*****************************************************************************
2  * vout_beos.cpp: beos video output display method
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: VideoOutput.cpp,v 1.19 2003/05/08 10:40:31 titer Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Tony Castley <tcastley@mail.powerup.com.au>
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 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                                /* free() */
33 #include <stdio.h>
34 #include <string.h>                                            /* strerror() */
35
36 #include <Application.h>
37 #include <BitmapStream.h>
38 #include <Bitmap.h>
39 #include <Directory.h>
40 #include <DirectWindow.h>
41 #include <File.h>
42 #include <InterfaceKit.h>
43 #include <NodeInfo.h>
44 #include <String.h>
45 #include <TranslatorRoster.h>
46 #include <WindowScreen.h>
47
48 /* VLC headers */
49 #include <vlc/vlc.h>
50 #include <vlc/intf.h>
51 #include <vlc/vout.h>
52
53 #include "InterfaceWindow.h"    // for load/save_settings()
54 #include "DrawingTidbits.h"
55 #include "MsgVals.h"
56
57 #include "VideoWindow.h"
58
59 /*****************************************************************************
60  * vout_sys_t: BeOS video output method descriptor
61  *****************************************************************************
62  * This structure is part of the video output thread descriptor.
63  * It describes the BeOS specific properties of an output thread.
64  *****************************************************************************/
65 struct vout_sys_t
66 {
67     VideoWindow *  p_window;
68
69     s32 i_width;
70     s32 i_height;
71
72 //    u8 *pp_buffer[3];
73     u32 source_chroma;
74     int i_index;
75
76 };
77
78 #define MOUSE_IDLE_TIMEOUT 2000000      // two seconds
79 #define MIN_AUTO_VSYNC_REFRESH 61       // Hz
80 #define DEFAULT_SCREEN_SHOT_FORMAT 'PNG '
81 #define DEFAULT_SCREEN_SHOT_PATH "/boot/home/vlc screenshot"
82
83 /*****************************************************************************
84  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
85  *****************************************************************************/
86 BWindow*
87 beos_GetAppWindow(char *name)
88 {
89     int32       index;
90     BWindow     *window;
91     
92     for (index = 0 ; ; index++)
93     {
94         window = be_app->WindowAt(index);
95         if (window == NULL)
96             break;
97         if (window->LockWithTimeout(20000) == B_OK)
98         {
99             if (strcmp(window->Name(), name) == 0)
100             {
101                 window->Unlock();
102                 break;
103             }
104             window->Unlock();
105         }
106     }
107     return window; 
108 }
109
110 /*****************************************************************************
111  * get_interface_window
112  *****************************************************************************/
113 BWindow*
114 get_interface_window()
115 {
116         return beos_GetAppWindow( "VLC " PACKAGE_VERSION );
117 }
118
119 class BackgroundView : public BView
120 {
121  public:
122                                                         BackgroundView(BRect frame, VLCView* view)
123                                                         : BView(frame, "background",
124                                                                         B_FOLLOW_ALL, B_FULL_UPDATE_ON_RESIZE),
125                                                           fVideoView(view)
126                                                         {
127                                                                 SetViewColor(kBlack);
128                                                         }
129         virtual                                 ~BackgroundView() {}
130
131         virtual void                    MouseDown(BPoint where)
132                                                         {
133                                                                 // convert coordinates
134                                                                 where = fVideoView->ConvertFromParent(where);
135                                                                 // let him handle it
136                                                                 fVideoView->MouseDown(where);
137                                                         }
138         virtual void                    MouseMoved(BPoint where, uint32 transit,
139                                                                            const BMessage* dragMessage)
140                                                         {
141                                                                 // convert coordinates
142                                                                 where = fVideoView->ConvertFromParent(where);
143                                                                 // let him handle it
144                                                                 fVideoView->MouseMoved(where, transit, dragMessage);
145                                                                 // notice: It might look like transit should be
146                                                                 // B_OUTSIDE_VIEW regardless, but leave it like this,
147                                                                 // otherwise, unwanted things will happen!
148                                                         }
149
150  private:
151         VLCView*                                fVideoView;
152 };
153
154
155 /*****************************************************************************
156  * VideoSettings constructor and destructor
157  *****************************************************************************/
158 VideoSettings::VideoSettings()
159         : fVideoSize( SIZE_100 ),
160           fFlags( FLAG_CORRECT_RATIO ),
161           fSettings( new BMessage( 'sett' ) )
162 {
163         // read settings from disk
164         status_t ret = load_settings( fSettings, "video_settings", "VideoLAN Client" );
165         if ( ret == B_OK )
166         {
167                 uint32 flags;
168                 if ( fSettings->FindInt32( "flags", (int32*)&flags ) == B_OK )
169                         SetFlags( flags );
170                 uint32 size;
171                 if ( fSettings->FindInt32( "video size", (int32*)&size ) == B_OK )
172                         SetVideoSize( size );
173         }
174         else
175         {
176                 fprintf( stderr, "error loading video settings: %s\n", strerror( ret ) );
177                 
178                 // figure out if we should use vertical sync by default
179                 BScreen screen(B_MAIN_SCREEN_ID);
180                 if (screen.IsValid())
181                 {
182                         display_mode mode; 
183                         screen.GetMode(&mode); 
184                         float refresh = (mode.timing.pixel_clock * 1000)
185                                                         / ((mode.timing.h_total)* (mode.timing.v_total)); 
186                         if (refresh < MIN_AUTO_VSYNC_REFRESH)
187                                 AddFlags(FLAG_SYNC_RETRACE);
188                 }
189         }
190 }
191
192 VideoSettings::VideoSettings( const VideoSettings& clone )
193         : fVideoSize( clone.VideoSize() ),
194           fFlags( clone.Flags() ),
195           fSettings( NULL )
196 {
197 }
198
199
200 VideoSettings::~VideoSettings()
201 {
202         if ( fSettings )
203         {
204                 // we are the default settings
205                 // and write our settings to disk
206                 if (fSettings->ReplaceInt32( "video size", VideoSize() ) != B_OK)
207                         fSettings->AddInt32( "video size", VideoSize() );
208                 if (fSettings->ReplaceInt32( "flags", Flags() ) != B_OK)
209                         fSettings->AddInt32( "flags", Flags() );
210
211                 status_t ret = save_settings( fSettings, "video_settings", "VideoLAN Client" );
212                 if ( ret != B_OK )
213                         fprintf( stderr, "error saving video settings: %s\n", strerror( ret ) );
214                 delete fSettings;
215         }
216         else
217         {
218                 // we are just a clone of the default settings
219                 fDefaultSettings.SetVideoSize( VideoSize() );
220                 fDefaultSettings.SetFlags( Flags() );
221         }
222 }
223
224 /*****************************************************************************
225  * VideoSettings::DefaultSettings
226  *****************************************************************************/
227 VideoSettings*
228 VideoSettings::DefaultSettings()
229 {
230         return &fDefaultSettings;
231 }
232
233 /*****************************************************************************
234  * VideoSettings::SetVideoSize
235  *****************************************************************************/
236 void
237 VideoSettings::SetVideoSize( uint32 mode )
238 {
239         fVideoSize = mode;
240 }
241
242 // static variable initialization
243 VideoSettings
244 VideoSettings::fDefaultSettings;
245
246
247 /*****************************************************************************
248  * VideoWindow constructor and destructor
249  *****************************************************************************/
250 VideoWindow::VideoWindow(int v_width, int v_height, BRect frame,
251                          vout_thread_t *p_videoout)
252         : BWindow(frame, NULL, B_TITLED_WINDOW, B_NOT_CLOSABLE | B_NOT_MINIMIZABLE),
253           i_width(frame.IntegerWidth()),
254           i_height(frame.IntegerHeight()),
255           winSize(frame),
256           i_buffer(0),
257           teardownwindow(false),
258           fTrueWidth(v_width),
259           fTrueHeight(v_height),
260           fCachedFeel(B_NORMAL_WINDOW_FEEL),
261           fInterfaceShowing(false),
262           fInitStatus(B_ERROR),
263           fSettings(new VideoSettings(*VideoSettings::DefaultSettings()))
264 {
265     p_vout = p_videoout;
266     
267     // create the view to do the display
268     view = new VLCView( Bounds(), p_vout );
269
270         // create background view
271     BView *mainView =  new BackgroundView( Bounds(), view );
272     AddChild(mainView);
273     mainView->AddChild(view);
274
275         // allocate bitmap buffers
276         for (int32 i = 0; i < 3; i++)
277                 bitmap[i] = NULL;
278         fInitStatus = _AllocateBuffers(v_width, v_height, &mode);
279
280         // make sure we layout the view correctly
281     FrameResized(i_width, i_height);
282
283     if (fInitStatus >= B_OK && mode == OVERLAY)
284     {
285        overlay_restrictions r;
286
287        bitmap[1]->GetOverlayRestrictions(&r);
288        SetSizeLimits((i_width * r.min_width_scale), i_width * r.max_width_scale,
289                      (i_height * r.min_height_scale), i_height * r.max_height_scale);
290     }
291     
292         // vlc settings override settings from disk
293         if (config_GetInt(p_vout, "fullscreen"))
294                 fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
295
296     // add a few useful shortcuts
297     AddShortcut( 'f', 0, new BMessage( TOGGLE_FULL_SCREEN ) );
298     AddShortcut( '1', 0, new BMessage( RESIZE_50 ) );
299     AddShortcut( '2', 0, new BMessage( RESIZE_100 ) );
300     AddShortcut( '3', 0, new BMessage( RESIZE_200 ) );
301     
302     // workaround for french keyboards
303     AddShortcut( '&', 0, new BMessage( RESIZE_50 ) );
304     AddShortcut( 'é', 0, new BMessage( RESIZE_100 ) );
305         // FIXME - this one doesn't work because 'é' is a multi-byte character
306     AddShortcut( '"', 0, new BMessage( RESIZE_200 ) );
307     
308     _SetToSettings();
309 }
310
311 VideoWindow::~VideoWindow()
312 {
313     int32 result;
314
315     teardownwindow = true;
316     wait_for_thread(fDrawThreadID, &result);
317     _FreeBuffers();
318         delete fSettings;
319 }
320
321 /*****************************************************************************
322  * VideoWindow::MessageReceived
323  *****************************************************************************/
324 void
325 VideoWindow::MessageReceived( BMessage *p_message )
326 {
327         switch( p_message->what )
328         {
329             case SHOW_INTERFACE:
330                 SetInterfaceShowing( true );
331                 break;
332                 case TOGGLE_FULL_SCREEN:
333                         BWindow::Zoom();
334                         break;
335                 case RESIZE_50:
336                 case RESIZE_100:
337                 case RESIZE_200:
338                         if (IsFullScreen())
339                                 BWindow::Zoom();
340                         _SetVideoSize(p_message->what);
341                         break;
342                 case VERT_SYNC:
343                         SetSyncToRetrace(!IsSyncedToRetrace());
344                         break;
345                 case WINDOW_FEEL:
346                         {
347                                 window_feel winFeel;
348                                 if (p_message->FindInt32("WinFeel", (int32*)&winFeel) == B_OK)
349                                 {
350                                         SetFeel(winFeel);
351                                         fCachedFeel = winFeel;
352                                         if (winFeel == B_FLOATING_ALL_WINDOW_FEEL)
353                                                 fSettings->AddFlags(VideoSettings::FLAG_ON_TOP_ALL);
354                                         else
355                                                 fSettings->ClearFlags(VideoSettings::FLAG_ON_TOP_ALL);
356                                 }
357                         }
358                         break;
359                 case ASPECT_CORRECT:
360                         SetCorrectAspectRatio(!CorrectAspectRatio());
361                         break;
362                 case SCREEN_SHOT:
363                         // save a screen shot
364                         if ( BBitmap* current = bitmap[i_buffer] )
365                         {
366 // the following line might be tempting, but does not work for some overlay bitmaps!!!
367 //                              BBitmap* temp = new BBitmap( current );
368 // so we clone the bitmap ourselves
369 // however, we need to take care of potentially different padding!
370 // memcpy() is slow when reading from grafix memory, but what the heck...
371                                 BBitmap* temp = new BBitmap( current->Bounds(), current->ColorSpace() );
372                                 if ( temp && temp->IsValid() )
373                                 {
374                                         int32 height = (int32)current->Bounds().Height();
375                                         uint8* dst = (uint8*)temp->Bits();
376                                         uint8* src = (uint8*)current->Bits();
377                                         int32 dstBpr = temp->BytesPerRow();
378                                         int32 srcBpr = current->BytesPerRow();
379                                         int32 validBytes = dstBpr > srcBpr ? srcBpr : dstBpr;
380                                         for ( int32 y = 0; y < height; y++ )
381                                         {
382                                                 memcpy( dst, src, validBytes );
383                                                 dst += dstBpr;
384                                                 src += srcBpr;
385                                         }
386                                         char* path = config_GetPsz( p_vout, "beos-screenshotpath" );
387                                         if ( !path )
388                                                 path = strdup( DEFAULT_SCREEN_SHOT_PATH );
389                                         /* TODO: handle the format */
390                                         /* config_GetPsz( p_vout, "beos-screenshotformat" ); */
391                                         int32 format = DEFAULT_SCREEN_SHOT_FORMAT;
392                                         _SaveScreenShot( temp, path, format );
393                                 }
394                                 else
395                                 {
396                                         delete temp;
397                                 }
398                         }
399                         break;
400                 default:
401                         BWindow::MessageReceived( p_message );
402                         break;
403         }
404 }
405
406 /*****************************************************************************
407  * VideoWindow::Zoom
408  *****************************************************************************/
409 void
410 VideoWindow::Zoom(BPoint origin, float width, float height )
411 {
412         ToggleFullScreen();
413 }
414
415 /*****************************************************************************
416  * VideoWindow::FrameMoved
417  *****************************************************************************/
418 void
419 VideoWindow::FrameMoved(BPoint origin) 
420 {
421         if (IsFullScreen())
422                 return ;
423         winSize = Frame();
424 }
425
426 /*****************************************************************************
427  * VideoWindow::FrameResized
428  *****************************************************************************/
429 void
430 VideoWindow::FrameResized( float width, float height )
431 {
432         int32 useWidth = CorrectAspectRatio() ? i_width : fTrueWidth;
433         int32 useHeight = CorrectAspectRatio() ? i_height : fTrueHeight;
434     float out_width, out_height;
435     float out_left, out_top;
436     float width_scale = width / useWidth;
437     float height_scale = height / useHeight;
438
439     if (width_scale <= height_scale)
440     {
441         out_width = (useWidth * width_scale);
442         out_height = (useHeight * width_scale);
443         out_left = 0; 
444         out_top = (height - out_height) / 2;
445     }
446     else   /* if the height is proportionally smaller */
447     {
448         out_width = (useWidth * height_scale);
449         out_height = (useHeight * height_scale);
450         out_top = 0;
451         out_left = (width - out_width) / 2;
452     }
453     view->MoveTo(out_left,out_top);
454     view->ResizeTo(out_width, out_height);
455
456         if (!IsFullScreen())
457         winSize = Frame();
458 }
459
460 /*****************************************************************************
461  * VideoWindow::ScreenChanged
462  *****************************************************************************/
463 void
464 VideoWindow::ScreenChanged(BRect frame, color_space format)
465 {
466         BScreen screen(this);
467         display_mode mode; 
468         screen.GetMode(&mode); 
469         float refresh = (mode.timing.pixel_clock * 1000)
470                                         / ((mode.timing.h_total) * (mode.timing.v_total)); 
471         SetSyncToRetrace(refresh < MIN_AUTO_VSYNC_REFRESH);
472 }
473
474 /*****************************************************************************
475  * VideoWindow::Activate
476  *****************************************************************************/
477 void
478 VideoWindow::WindowActivated(bool active)
479 {
480 }
481
482 /*****************************************************************************
483  * VideoWindow::drawBuffer
484  *****************************************************************************/
485 void
486 VideoWindow::drawBuffer(int bufferIndex)
487 {
488     i_buffer = bufferIndex;
489
490     // sync to the screen if required
491     if (IsSyncedToRetrace())
492     {
493         BScreen screen(this);
494         screen.WaitForRetrace(22000);
495     }
496     if (fInitStatus >= B_OK && LockLooper())
497     {
498        // switch the overlay bitmap
499        if (mode == OVERLAY)
500        {
501           rgb_color key;
502           view->SetViewOverlay(bitmap[i_buffer], 
503                             bitmap[i_buffer]->Bounds() ,
504                             view->Bounds(),
505                             &key, B_FOLLOW_ALL,
506                                                         B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|
507                                     B_OVERLAY_TRANSFER_CHANNEL);
508                    view->SetViewColor(key);
509            }
510        else
511        {
512          // switch the bitmap
513          view->DrawBitmap(bitmap[i_buffer], view->Bounds() );
514        }
515        UnlockLooper();
516     }
517 }
518
519 /*****************************************************************************
520  * VideoWindow::SetInterfaceShowing
521  *****************************************************************************/
522 void
523 VideoWindow::ToggleInterfaceShowing()
524 {
525         SetInterfaceShowing(!fInterfaceShowing);
526 }
527
528 /*****************************************************************************
529  * VideoWindow::SetInterfaceShowing
530  *****************************************************************************/
531 void
532 VideoWindow::SetInterfaceShowing(bool showIt)
533 {
534         BWindow* window = get_interface_window();
535         if (window)
536         {
537                 if (showIt)
538                 {
539                         if (fCachedFeel != B_NORMAL_WINDOW_FEEL)
540                                 SetFeel(B_NORMAL_WINDOW_FEEL);
541                         window->Activate(true);
542                         SendBehind(window);
543                 }
544                 else
545                 {
546                         SetFeel(fCachedFeel);
547                         Activate(true);
548                         window->SendBehind(this);
549                 }
550                 fInterfaceShowing = showIt;
551         }
552 }
553
554 /*****************************************************************************
555  * VideoWindow::SetCorrectAspectRatio
556  *****************************************************************************/
557 void
558 VideoWindow::SetCorrectAspectRatio(bool doIt)
559 {
560         if (CorrectAspectRatio() != doIt)
561         {
562                 if (doIt)
563                         fSettings->AddFlags(VideoSettings::FLAG_CORRECT_RATIO);
564                 else
565                         fSettings->ClearFlags(VideoSettings::FLAG_CORRECT_RATIO);
566                 FrameResized(Bounds().Width(), Bounds().Height());
567         }
568 }
569
570 /*****************************************************************************
571  * VideoWindow::CorrectAspectRatio
572  *****************************************************************************/
573 bool
574 VideoWindow::CorrectAspectRatio() const
575 {
576         return fSettings->HasFlags(VideoSettings::FLAG_CORRECT_RATIO);
577 }
578
579 /*****************************************************************************
580  * VideoWindow::ToggleFullScreen
581  *****************************************************************************/
582 void
583 VideoWindow::ToggleFullScreen()
584 {
585         SetFullScreen(!IsFullScreen());
586 }
587
588 /*****************************************************************************
589  * VideoWindow::SetFullScreen
590  *****************************************************************************/
591 void
592 VideoWindow::SetFullScreen(bool doIt)
593 {
594         if (doIt)
595         {
596                 BScreen screen(this);
597                 BRect rect = screen.Frame();
598                 Activate();
599                 MoveTo(0.0, 0.0);
600                 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
601                 be_app->ObscureCursor();
602                 fInterfaceShowing = false;
603                 fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
604         }
605         else
606         {
607                 MoveTo(winSize.left, winSize.top);
608                 ResizeTo(winSize.IntegerWidth(), winSize.IntegerHeight());
609                 be_app->ShowCursor();
610                 fInterfaceShowing = true;
611                 fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
612         }
613 }
614
615 /*****************************************************************************
616  * VideoWindow::IsFullScreen
617  *****************************************************************************/
618 bool
619 VideoWindow::IsFullScreen() const
620 {
621         return fSettings->HasFlags(VideoSettings::FLAG_FULL_SCREEN);
622 }
623
624 /*****************************************************************************
625  * VideoWindow::SetSyncToRetrace
626  *****************************************************************************/
627 void
628 VideoWindow::SetSyncToRetrace(bool doIt)
629 {
630         if (doIt)
631                 fSettings->AddFlags(VideoSettings::FLAG_SYNC_RETRACE);
632         else
633                 fSettings->ClearFlags(VideoSettings::FLAG_SYNC_RETRACE);
634 }
635
636 /*****************************************************************************
637  * VideoWindow::IsSyncedToRetrace
638  *****************************************************************************/
639 bool
640 VideoWindow::IsSyncedToRetrace() const
641 {
642         return fSettings->HasFlags(VideoSettings::FLAG_SYNC_RETRACE);
643 }
644
645
646 /*****************************************************************************
647  * VideoWindow::_AllocateBuffers
648  *****************************************************************************/
649 status_t
650 VideoWindow::_AllocateBuffers(int width, int height, int* mode)
651 {
652         // clear any old buffers
653         _FreeBuffers();
654         // set default mode
655         *mode = BITMAP;
656
657         BRect bitmapFrame( 0, 0, width, height );
658         // read from config, if we are supposed to use overlay at all
659     int noOverlay = !config_GetInt( p_vout, "overlay" );
660         // test for overlay capability
661     for (int i = 0; i < COLOR_COUNT; i++)
662     {
663         if (noOverlay) break;
664         bitmap[0] = new BBitmap ( bitmapFrame, 
665                                   B_BITMAP_WILL_OVERLAY |
666                                   B_BITMAP_RESERVE_OVERLAY_CHANNEL,
667                                   colspace[i].colspace);
668
669         if(bitmap[0] && bitmap[0]->InitCheck() == B_OK) 
670         {
671             colspace_index = i;
672
673             bitmap[1] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
674                                      colspace[colspace_index].colspace);
675             bitmap[2] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
676                                      colspace[colspace_index].colspace);
677             if ( (bitmap[2] && bitmap[2]->InitCheck() == B_OK) )
678             {
679                *mode = OVERLAY;
680                rgb_color key;
681                view->SetViewOverlay(bitmap[0], 
682                                     bitmap[0]->Bounds() ,
683                                     view->Bounds(),
684                                     &key, B_FOLLOW_ALL,
685                                             B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
686                        view->SetViewColor(key);
687                SetTitle("VLC " PACKAGE_VERSION " (Overlay)");
688                break;
689             }
690             else
691             {
692                _FreeBuffers();
693                *mode = BITMAP; // might want to try again with normal bitmaps
694             }
695         }
696         else
697             delete bitmap[0];
698         }
699
700     if (*mode == BITMAP)
701         {
702         // fallback to RGB
703         colspace_index = DEFAULT_COL;   // B_RGB32
704         SetTitle( "VLC " PACKAGE_VERSION " (Bitmap)" );
705         bitmap[0] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
706         bitmap[1] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
707         bitmap[2] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
708     }
709     // see if everything went well
710     status_t status = B_ERROR;
711     for (int32 i = 0; i < 3; i++)
712     {
713         if (bitmap[i])
714                 status = bitmap[i]->InitCheck();
715                 if (status < B_OK)
716                         break;
717     }
718     if (status >= B_OK)
719     {
720             // clear bitmaps to black
721             for (int32 i = 0; i < 3; i++)
722                 _BlankBitmap(bitmap[i]);
723     }
724     return status;
725 }
726
727 /*****************************************************************************
728  * VideoWindow::_FreeBuffers
729  *****************************************************************************/
730 void
731 VideoWindow::_FreeBuffers()
732 {
733         delete bitmap[0];
734         bitmap[0] = NULL;
735         delete bitmap[1];
736         bitmap[1] = NULL;
737         delete bitmap[2];
738         bitmap[2] = NULL;
739         fInitStatus = B_ERROR;
740 }
741
742 /*****************************************************************************
743  * VideoWindow::_BlankBitmap
744  *****************************************************************************/
745 void
746 VideoWindow::_BlankBitmap(BBitmap* bitmap) const
747 {
748         // no error checking (we do that earlier on and since it's a private function...
749
750         // YCbCr: 
751         // Loss/Saturation points are Y 16-235 (absoulte); Cb/Cr 16-240 (center 128)
752
753         // YUV: 
754         // Extrema points are Y 0 - 207 (absolute) U -91 - 91 (offset 128) V -127 - 127 (offset 128)
755
756         // we only handle weird colorspaces with special care
757         switch (bitmap->ColorSpace()) {
758                 case B_YCbCr422: {
759                         // Y0[7:0]  Cb0[7:0]  Y1[7:0]  Cr0[7:0]  Y2[7:0]  Cb2[7:0]  Y3[7:0]  Cr2[7:0]
760                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
761                         uint8* bits = (uint8*)bitmap->Bits();
762                         int32 bpr = bitmap->BytesPerRow();
763                         for (int32 y = 0; y < height; y++) {
764                                 // handle 2 bytes at a time
765                                 for (int32 i = 0; i < bpr; i += 2) {
766                                         // offset into line
767                                         bits[i] = 16;
768                                         bits[i + 1] = 128;
769                                 }
770                                 // next line
771                                 bits += bpr;
772                         }
773                         break;
774                 }
775                 case B_YCbCr420: {
776 // TODO: untested!!
777                         // Non-interlaced only, Cb0  Y0  Y1  Cb2 Y2  Y3  on even scan lines ...
778                         // Cr0  Y0  Y1  Cr2 Y2  Y3  on odd scan lines
779                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
780                         uint8* bits = (uint8*)bitmap->Bits();
781                         int32 bpr = bitmap->BytesPerRow();
782                         for (int32 y = 0; y < height; y += 1) {
783                                 // handle 3 bytes at a time
784                                 for (int32 i = 0; i < bpr; i += 3) {
785                                         // offset into line
786                                         bits[i] = 128;
787                                         bits[i + 1] = 16;
788                                         bits[i + 2] = 16;
789                                 }
790                                 // next line
791                                 bits += bpr;
792                         }
793                         break;
794                 }
795                 case B_YUV422: {
796 // TODO: untested!!
797                         // U0[7:0]  Y0[7:0]   V0[7:0]  Y1[7:0]  U2[7:0]  Y2[7:0]   V2[7:0]  Y3[7:0]
798                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
799                         uint8* bits = (uint8*)bitmap->Bits();
800                         int32 bpr = bitmap->BytesPerRow();
801                         for (int32 y = 0; y < height; y += 1) {
802                                 // handle 2 bytes at a time
803                                 for (int32 i = 0; i < bpr; i += 2) {
804                                         // offset into line
805                                         bits[i] = 128;
806                                         bits[i + 1] = 0;
807                                 }
808                                 // next line
809                                 bits += bpr;
810                         }
811                         break;
812                 }
813                 default:
814                         memset(bitmap->Bits(), 0, bitmap->BitsLength());
815                         break;
816         }
817 }
818
819 /*****************************************************************************
820  * VideoWindow::_SetVideoSize
821  *****************************************************************************/
822 void
823 VideoWindow::_SetVideoSize(uint32 mode)
824 {
825         // let size depend on aspect correction
826         int32 width = CorrectAspectRatio() ? i_width : fTrueWidth;
827         int32 height = CorrectAspectRatio() ? i_height : fTrueHeight;
828         switch (mode)
829         {
830                 case RESIZE_50:
831                         width /= 2;
832                         height /= 2;
833                         break;
834                 case RESIZE_200:
835                         width *= 2;
836                         height *= 2;
837                         break;
838                 case RESIZE_100:
839                 default:
840                 break;
841         }
842         fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
843         ResizeTo(width, height);
844 }
845
846 /*****************************************************************************
847  * VideoWindow::_SetToSettings
848  *****************************************************************************/
849 void
850 VideoWindow::_SetToSettings()
851 {
852         // adjust dimensions
853         uint32 mode = RESIZE_100;
854         switch (fSettings->VideoSize())
855         {
856                 case VideoSettings::SIZE_50:
857                         mode = RESIZE_50;
858                         break;
859                 case VideoSettings::SIZE_200:
860                         mode = RESIZE_200;
861                         break;
862                 case VideoSettings::SIZE_100:
863                 case VideoSettings::SIZE_OTHER:
864                 default:
865                         break;
866         }
867         bool fullscreen = IsFullScreen();       // remember settings
868         _SetVideoSize(mode);                            // because this will reset settings
869         // the fullscreen status is reflected in the settings,
870         // but not yet in the windows state
871         if (fullscreen)
872                 SetFullScreen(true);
873         if (fSettings->HasFlags(VideoSettings::FLAG_ON_TOP_ALL))
874                 fCachedFeel = B_FLOATING_ALL_WINDOW_FEEL;
875         else
876                 fCachedFeel = B_NORMAL_WINDOW_FEEL;
877         SetFeel(fCachedFeel);
878 }
879
880 /*****************************************************************************
881  * VideoWindow::_SaveScreenShot
882  *****************************************************************************/
883 void
884 VideoWindow::_SaveScreenShot( BBitmap* bitmap, char* path,
885                                                   uint32 translatorID ) const
886 {
887         // make the info object from the parameters
888         screen_shot_info* info = new screen_shot_info;
889         info->bitmap = bitmap;
890         info->path = path;
891         info->translatorID = translatorID;
892         info->width = CorrectAspectRatio() ? i_width : fTrueWidth;
893         info->height = CorrectAspectRatio() ? i_height : fTrueHeight;
894         // spawn a new thread to take care of the actual saving to disk
895         thread_id thread = spawn_thread( _save_screen_shot,
896                                                                          "screen shot saver",
897                                                                          B_LOW_PRIORITY, (void*)info );
898         // start thread or do the job ourself if something went wrong
899         if ( thread < B_OK || resume_thread( thread ) < B_OK )
900                 _save_screen_shot( (void*)info );
901 }
902
903 /*****************************************************************************
904  * VideoWindow::_save_screen_shot
905  *****************************************************************************/
906 int32
907 VideoWindow::_save_screen_shot( void* cookie )
908 {
909         screen_shot_info* info = (screen_shot_info*)cookie;
910         if ( info && info->bitmap && info->bitmap->IsValid() && info->path )
911         {
912                 // try to be as quick as possible creating the file (the user might have
913                 // taken the next screen shot already!)
914                 // make sure we have a unique name for the screen shot
915                 BString path( info->path );
916                 // create the folder if it doesn't exist
917                 BString folder( info->path );
918                 create_directory( folder.String(), 0777 );
919                 path << "/vlc screenshot";
920                 BEntry entry( path.String() );
921                 int32 appendedNumber = 0;
922                 if ( entry.Exists() && !entry.IsSymLink() )
923                 {
924                         // we would clobber an existing entry
925                         bool foundUniqueName = false;
926                         appendedNumber = 1;
927                         while ( !foundUniqueName ) {
928                                 BString newName( path.String() );
929                                 newName << " " << appendedNumber;
930                                 BEntry possiblyClobberedEntry( newName.String() );
931                                 if ( possiblyClobberedEntry.Exists()
932                                         && !possiblyClobberedEntry.IsSymLink() )
933                                         appendedNumber++;
934                                 else
935                                         foundUniqueName = true;
936                         }
937                 }
938                 if ( appendedNumber > 0 )
939                         path << " " << appendedNumber;
940                 // there is still a slight chance to clobber an existing
941                 // file (if it was created in the "meantime"), but we take it...
942                 BFile outFile( path.String(),
943                                            B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE );
944
945                 // make colorspace converted copy of bitmap
946                 BBitmap* converted = new BBitmap( BRect( 0.0, 0.0, info->width, info->height ),
947                                                                                   B_RGB32 );
948                 status_t status = convert_bitmap( info->bitmap, converted );
949                 if ( status == B_OK )
950                 {
951                         BTranslatorRoster* roster = BTranslatorRoster::Default();
952                         uint32 imageFormat = 0;
953                         translator_id translator = 0;
954                         bool found = false;
955
956                         // find suitable translator
957                         translator_id* ids = NULL;
958                         int32 count = 0;
959                 
960                         status = roster->GetAllTranslators( &ids, &count );
961                         if ( status >= B_OK )
962                         {
963                                 for ( int tix = 0; tix < count; tix++ )
964                                 { 
965                                         const translation_format *formats = NULL; 
966                                         int32 num_formats = 0; 
967                                         bool ok = false; 
968                                         status = roster->GetInputFormats( ids[tix],
969                                                                                                           &formats, &num_formats );
970                                         if (status >= B_OK)
971                                         {
972                                                 for ( int iix = 0; iix < num_formats; iix++ )
973                                                 { 
974                                                         if ( formats[iix].type == B_TRANSLATOR_BITMAP )
975                                                         { 
976                                                                 ok = true; 
977                                                                 break; 
978                                                         }
979                                                 }
980                                         }
981                                         if ( !ok )
982                                                 continue; 
983                                         status = roster->GetOutputFormats( ids[tix],
984                                                                                                            &formats, &num_formats); 
985                                         if ( status >= B_OK )
986                                         {
987                                                 for ( int32 oix = 0; oix < num_formats; oix++ )
988                                                 {
989                                                         if ( formats[oix].type != B_TRANSLATOR_BITMAP )
990                                                         {
991                                                                 if ( formats[oix].type == info->translatorID )
992                                                                 {
993                                                                         found = true;
994                                                                         imageFormat = formats[oix].type;
995                                                                         translator = ids[tix];
996                                                                         break;
997                                                                 }
998                                                         }
999                                                 }
1000                                         }
1001                                 }
1002                         }
1003                         delete[] ids;
1004                         if ( found )
1005                         {
1006                                 // make bitmap stream
1007                                 BBitmapStream outStream( converted );
1008
1009                                 status = outFile.InitCheck();
1010                                 if (status == B_OK) {
1011                                         status = roster->Translate( &outStream, NULL, NULL,
1012                                                                                                 &outFile, imageFormat );
1013                                         if ( status == B_OK )
1014                                         {
1015                                                 BNodeInfo nodeInfo( &outFile );
1016                                                 if ( nodeInfo.InitCheck() == B_OK )
1017                                                 {
1018                                                         translation_format* formats; 
1019                                                         int32 count;
1020                                                         status = roster->GetOutputFormats( translator,
1021                                                                                                                            (const translation_format **) &formats,
1022                                                                                                                            &count);
1023                                                         if ( status >= B_OK )
1024                                                         {
1025                                                                 const char * mime = NULL; 
1026                                                                 for ( int ix = 0; ix < count; ix++ ) {
1027                                                                         if ( formats[ix].type == imageFormat ) {
1028                                                                                 mime = formats[ix].MIME;
1029                                                                                 break;
1030                                                                         }
1031                                                                 } 
1032                                                                 if ( mime )
1033                                                                         nodeInfo.SetType( mime );
1034                                                         }
1035                                                 }
1036                                         }
1037                                 }
1038                                 outStream.DetachBitmap( &converted );
1039                                 outFile.Unset();
1040                         }
1041                 }
1042                 delete converted;
1043         }
1044         if ( info )
1045         {
1046                 delete info->bitmap;
1047                 free( info->path );
1048         }
1049         delete info;
1050         return B_OK;
1051 }
1052
1053
1054 /*****************************************************************************
1055  * VLCView::VLCView
1056  *****************************************************************************/
1057 VLCView::VLCView(BRect bounds, vout_thread_t *p_vout_instance )
1058         : BView(bounds, "video view", B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED),
1059           fLastMouseMovedTime(system_time()),
1060           fCursorHidden(false),
1061           fCursorInside(false),
1062           fIgnoreDoubleClick(false)
1063 {
1064     p_vout = p_vout_instance;
1065     SetViewColor(B_TRANSPARENT_32_BIT);
1066 }
1067
1068 /*****************************************************************************
1069  * VLCView::~VLCView
1070  *****************************************************************************/
1071 VLCView::~VLCView()
1072 {
1073 }
1074
1075 /*****************************************************************************
1076  * VLCVIew::AttachedToWindow
1077  *****************************************************************************/
1078 void
1079 VLCView::AttachedToWindow()
1080 {
1081         // in order to get keyboard events
1082         MakeFocus(true);
1083         // periodically check if we want to hide the pointer
1084         Window()->SetPulseRate(1000000);
1085 }
1086
1087 /*****************************************************************************
1088  * VLCVIew::MouseDown
1089  *****************************************************************************/
1090 void
1091 VLCView::MouseDown(BPoint where)
1092 {
1093         VideoWindow* videoWindow = dynamic_cast<VideoWindow*>(Window());
1094         BMessage* msg = Window()->CurrentMessage();
1095         int32 clicks;
1096         uint32 buttons;
1097         msg->FindInt32("clicks", &clicks);
1098         msg->FindInt32("buttons", (int32*)&buttons);
1099
1100         if (videoWindow)
1101         {
1102                 if (buttons & B_PRIMARY_MOUSE_BUTTON)
1103                 {
1104                         if (clicks == 2 && !fIgnoreDoubleClick)
1105                                 Window()->Zoom();
1106                         /* else
1107                                 videoWindow->ToggleInterfaceShowing(); */
1108                         fIgnoreDoubleClick = false;
1109                 }
1110             else
1111             {
1112                         if (buttons & B_SECONDARY_MOUSE_BUTTON) 
1113                         {
1114                                 // clicks will be 2 next time (if interval short enough)
1115                                 // even if the first click and the second
1116                                 // have not been made with the same mouse button
1117                                 fIgnoreDoubleClick = true;
1118                                 // launch popup menu
1119                                 BPopUpMenu *menu = new BPopUpMenu("context menu");
1120                                 menu->SetRadioMode(false);
1121                                 // In full screen, add an item to show/hide the interface
1122                                 if( videoWindow->IsFullScreen() )
1123                                 {
1124                                     BMenuItem *intfItem =
1125                                         new BMenuItem( _("Show Interface"), new BMessage(SHOW_INTERFACE) );
1126                                     menu->AddItem( intfItem );
1127                                 }
1128                                 // Resize to 50%
1129                                 BMenuItem *halfItem = new BMenuItem(_("50%"), new BMessage(RESIZE_50));
1130                                 menu->AddItem(halfItem);
1131                                 // Resize to 100%
1132                                 BMenuItem *origItem = new BMenuItem(_("100%"), new BMessage(RESIZE_100));
1133                                 menu->AddItem(origItem);
1134                                 // Resize to 200%
1135                                 BMenuItem *doubleItem = new BMenuItem(_("200%"), new BMessage(RESIZE_200));
1136                                 menu->AddItem(doubleItem);
1137                                 // Toggle FullScreen
1138                                 BMenuItem *zoomItem = new BMenuItem(_("Fullscreen"), new BMessage(TOGGLE_FULL_SCREEN));
1139                                 zoomItem->SetMarked(videoWindow->IsFullScreen());
1140                                 menu->AddItem(zoomItem);
1141         
1142                                 menu->AddSeparatorItem();
1143         
1144                                 // Toggle vSync
1145                                 BMenuItem *vsyncItem = new BMenuItem(_("Vertical Sync"), new BMessage(VERT_SYNC));
1146                                 vsyncItem->SetMarked(videoWindow->IsSyncedToRetrace());
1147                                 menu->AddItem(vsyncItem);
1148                                 // Correct Aspect Ratio
1149                                 BMenuItem *aspectItem = new BMenuItem(_("Correct Aspect Ratio"), new BMessage(ASPECT_CORRECT));
1150                                 aspectItem->SetMarked(videoWindow->CorrectAspectRatio());
1151                                 menu->AddItem(aspectItem);
1152         
1153                                 menu->AddSeparatorItem();
1154         
1155                                 // Windwo Feel Items
1156 /*                              BMessage *winNormFeel = new BMessage(WINDOW_FEEL);
1157                                 winNormFeel->AddInt32("WinFeel", (int32)B_NORMAL_WINDOW_FEEL);
1158                                 BMenuItem *normWindItem = new BMenuItem("Normal Window", winNormFeel);
1159                                 normWindItem->SetMarked(videoWindow->Feel() == B_NORMAL_WINDOW_FEEL);
1160                                 menu->AddItem(normWindItem);
1161                                 
1162                                 BMessage *winFloatFeel = new BMessage(WINDOW_FEEL);
1163                                 winFloatFeel->AddInt32("WinFeel", (int32)B_FLOATING_APP_WINDOW_FEEL);
1164                                 BMenuItem *onTopWindItem = new BMenuItem("App Top", winFloatFeel);
1165                                 onTopWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_APP_WINDOW_FEEL);
1166                                 menu->AddItem(onTopWindItem);
1167                                 
1168                                 BMessage *winAllFeel = new BMessage(WINDOW_FEEL);
1169                                 winAllFeel->AddInt32("WinFeel", (int32)B_FLOATING_ALL_WINDOW_FEEL);
1170                                 BMenuItem *allSpacesWindItem = new BMenuItem("On Top All Workspaces", winAllFeel);
1171                                 allSpacesWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL);
1172                                 menu->AddItem(allSpacesWindItem);*/
1173
1174                                 BMessage *windowFeelMsg = new BMessage( WINDOW_FEEL );
1175                                 bool onTop = videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL;
1176                                 window_feel feel = onTop ? B_NORMAL_WINDOW_FEEL : B_FLOATING_ALL_WINDOW_FEEL;
1177                                 windowFeelMsg->AddInt32( "WinFeel", (int32)feel );
1178                                 BMenuItem *windowFeelItem = new BMenuItem( _("Stay On Top"), windowFeelMsg );
1179                                 windowFeelItem->SetMarked( onTop );
1180                                 menu->AddItem( windowFeelItem );
1181
1182                                 menu->AddSeparatorItem();
1183
1184                                 BMenuItem* screenShotItem = new BMenuItem( _("Take Screen Shot"),
1185                                                                                                                    new BMessage( SCREEN_SHOT ) );
1186                                 menu->AddItem( screenShotItem );
1187
1188                                 menu->SetTargetForItems( this );
1189                                 ConvertToScreen( &where );
1190                                 menu->Go( where, true, false, true );
1191                 }
1192                 }
1193         }
1194         fLastMouseMovedTime = system_time();
1195         fCursorHidden = false;
1196 }
1197
1198 /*****************************************************************************
1199  * VLCVIew::MouseUp
1200  *****************************************************************************/
1201 void
1202 VLCView::MouseUp( BPoint where )
1203 {
1204     vlc_value_t val;
1205     val.b_bool = VLC_TRUE;
1206     var_Set( p_vout, "mouse-clicked", val );
1207 }
1208
1209 /*****************************************************************************
1210  * VLCVIew::MouseMoved
1211  *****************************************************************************/
1212 void
1213 VLCView::MouseMoved(BPoint point, uint32 transit, const BMessage* dragMessage)
1214 {
1215         fLastMouseMovedTime = system_time();
1216         fCursorHidden = false;
1217         fCursorInside = (transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW);
1218         /* DVD navigation */
1219         unsigned int i_width, i_height, i_x, i_y;
1220     vout_PlacePicture( p_vout, (unsigned int)Bounds().Width(),
1221                        (unsigned int)Bounds().Height(),
1222                        &i_x, &i_y, &i_width, &i_height );
1223         vlc_value_t val;
1224         val.i_int = ( (int)point.x - i_x ) * p_vout->render.i_width / i_width;
1225         var_Set( p_vout, "mouse-x", val );
1226         val.i_int = ( (int)point.y - i_y ) * p_vout->render.i_height / i_height;
1227         var_Set( p_vout, "mouse-y", val );
1228         val.b_bool = VLC_TRUE;
1229     var_Set( p_vout, "mouse-moved", val );
1230 }
1231
1232 /*****************************************************************************
1233  * VLCVIew::Pulse
1234  *****************************************************************************/
1235 void 
1236 VLCView::Pulse()
1237 {
1238         // We are getting the pulse messages no matter if the mouse is over
1239         // this view. If we are in full screen mode, we want to hide the cursor
1240         // even if it is not.
1241         VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
1242         if (!fCursorHidden)
1243         {
1244                 if (fCursorInside
1245                         && system_time() - fLastMouseMovedTime > MOUSE_IDLE_TIMEOUT)
1246                 {
1247                         be_app->ObscureCursor();
1248                         fCursorHidden = true;
1249                         
1250                         // hide the interface window as well if full screen
1251                         if (videoWindow && videoWindow->IsFullScreen())
1252                                 videoWindow->SetInterfaceShowing(false);
1253                 }
1254         }
1255
1256     // Workaround to disable the screensaver in full screen:
1257     // we simulate an activity every 29 seconds 
1258         if( videoWindow && videoWindow->IsFullScreen() &&
1259             system_time() - fLastMouseMovedTime > 29000000 )
1260         {
1261             BPoint where;
1262                 uint32 buttons;
1263                 GetMouse(&where, &buttons, false);
1264                 ConvertToScreen(&where);
1265                 set_mouse_position((int32) where.x, (int32) where.y);
1266         }
1267 }
1268
1269 /*****************************************************************************
1270  * VLCVIew::KeyDown
1271  *****************************************************************************/
1272 void
1273 VLCView::KeyDown(const char *bytes, int32 numBytes)
1274 {
1275     VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
1276     BWindow* interfaceWindow = get_interface_window();
1277         if (videoWindow && numBytes > 0) {
1278                 uint32 mods = modifiers();
1279                 switch (*bytes) {
1280                         case B_TAB:
1281                         case 'f':
1282                         case 'F':
1283                                 // toggle window and full screen mode
1284                                 // not passing on the tab key to the default KeyDown()
1285                                 // implementation also avoids loosing the keyboard focus
1286                                 videoWindow->PostMessage(TOGGLE_FULL_SCREEN);
1287                                 break;
1288                         case B_ESCAPE:
1289                                 // go back to window mode
1290                                 if (videoWindow->IsFullScreen())
1291                                         videoWindow->PostMessage(TOGGLE_FULL_SCREEN);
1292                                 break;
1293                         case B_SPACE:
1294                                 // toggle playback
1295                                 if (interfaceWindow)
1296                                         interfaceWindow->PostMessage(PAUSE_PLAYBACK);
1297                                 break;
1298                         case B_RIGHT_ARROW:
1299                                 if (interfaceWindow)
1300                                 {
1301                                         if (mods & B_SHIFT_KEY)
1302                                                 // next title
1303                                                 interfaceWindow->PostMessage(NEXT_TITLE);
1304                                         else
1305                                                 // next chapter
1306                                                 interfaceWindow->PostMessage(NEXT_CHAPTER);
1307                                 }
1308                                 break;
1309                         case B_LEFT_ARROW:
1310                                 if (interfaceWindow)
1311                                 {
1312                                         if (mods & B_SHIFT_KEY)
1313                                                 // previous title
1314                                                 interfaceWindow->PostMessage(PREV_TITLE);
1315                                         else
1316                                                 // previous chapter
1317                                                 interfaceWindow->PostMessage(PREV_CHAPTER);
1318                                 }
1319                                 break;
1320                         case B_UP_ARROW:
1321                                 // previous file in playlist
1322                                 interfaceWindow->PostMessage(PREV_FILE);
1323                                 break;
1324                         case B_DOWN_ARROW:
1325                                 // next file in playlist
1326                                 interfaceWindow->PostMessage(NEXT_FILE);
1327                                 break;
1328                         case B_PRINT_KEY:
1329                         case 's':
1330                         case 'S':
1331                                 videoWindow->PostMessage( SCREEN_SHOT );
1332                                 break;
1333                         default:
1334                                 BView::KeyDown(bytes, numBytes);
1335                                 break;
1336                 }
1337         }
1338 }
1339
1340 /*****************************************************************************
1341  * VLCVIew::Draw
1342  *****************************************************************************/
1343 void
1344 VLCView::Draw(BRect updateRect) 
1345 {
1346         VideoWindow* window = dynamic_cast<VideoWindow*>( Window() );
1347         if ( window && window->mode == BITMAP )
1348                 FillRect( updateRect );
1349 }
1350
1351 /*****************************************************************************
1352  * Local prototypes
1353  *****************************************************************************/
1354 static int  Init       ( vout_thread_t * );
1355 static void End        ( vout_thread_t * );
1356 // static int  Manage     ( vout_thread_t * );
1357 static void Display    ( vout_thread_t *, picture_t * );
1358
1359 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
1360 static void BeosCloseDisplay( vout_thread_t *p_vout );
1361
1362 /*****************************************************************************
1363  * OpenVideo: allocates BeOS video thread output method
1364  *****************************************************************************
1365  * This function allocates and initializes a BeOS vout method.
1366  *****************************************************************************/
1367 int E_(OpenVideo) ( vlc_object_t *p_this )
1368 {
1369     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1370
1371     /* Allocate structure */
1372     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
1373     if( p_vout->p_sys == NULL )
1374     {
1375         msg_Err( p_vout, "out of memory" );
1376         return( 1 );
1377     }
1378     p_vout->p_sys->i_width = p_vout->render.i_width;
1379     p_vout->p_sys->i_height = p_vout->render.i_height;
1380     p_vout->p_sys->source_chroma = p_vout->render.i_chroma;
1381
1382     p_vout->pf_init = Init;
1383     p_vout->pf_end = End;
1384     p_vout->pf_manage = NULL;
1385     p_vout->pf_render = NULL;
1386     p_vout->pf_display = Display;
1387
1388     return( 0 );
1389 }
1390
1391 /*****************************************************************************
1392  * Init: initialize BeOS video thread output method
1393  *****************************************************************************/
1394 int Init( vout_thread_t *p_vout )
1395 {
1396     int i_index;
1397     picture_t *p_pic;
1398
1399     I_OUTPUTPICTURES = 0;
1400
1401     /* Open and initialize device */
1402     if( BeosOpenDisplay( p_vout ) )
1403     {
1404         msg_Err(p_vout, "vout error: can't open display");
1405         return 0;
1406     }
1407     p_vout->output.i_width  = p_vout->render.i_width;
1408     p_vout->output.i_height = p_vout->render.i_height;
1409
1410     /* Assume we have square pixels */
1411     p_vout->output.i_aspect = p_vout->p_sys->i_width
1412                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
1413     p_vout->output.i_chroma = colspace[p_vout->p_sys->p_window->colspace_index].chroma;
1414     p_vout->p_sys->i_index = 0;
1415
1416     p_vout->b_direct = 1;
1417
1418     p_vout->output.i_rmask  = 0x00ff0000;
1419     p_vout->output.i_gmask  = 0x0000ff00;
1420     p_vout->output.i_bmask  = 0x000000ff;
1421
1422     for (int buffer_index = 0 ; buffer_index < 3; buffer_index++)
1423     {
1424        p_pic = NULL;
1425        /* Find an empty picture slot */
1426        for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
1427        {
1428            p_pic = NULL;
1429            if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
1430            {
1431                p_pic = p_vout->p_picture + i_index;
1432                break;
1433            }
1434        }
1435
1436        if( p_pic == NULL )
1437        {
1438            return 0;
1439        }
1440        p_pic->p->p_pixels = (u8*)p_vout->p_sys->p_window->bitmap[buffer_index]->Bits();
1441        p_pic->p->i_lines = p_vout->p_sys->i_height;
1442
1443        p_pic->p->i_pixel_pitch = colspace[p_vout->p_sys->p_window->colspace_index].pixel_bytes;
1444        p_pic->i_planes = colspace[p_vout->p_sys->p_window->colspace_index].planes;
1445        p_pic->p->i_pitch = p_vout->p_sys->p_window->bitmap[buffer_index]->BytesPerRow(); 
1446        p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch * ( p_vout->p_sys->p_window->bitmap[buffer_index]->Bounds().IntegerWidth() + 1 );
1447
1448        p_pic->i_status = DESTROYED_PICTURE;
1449        p_pic->i_type   = DIRECT_PICTURE;
1450  
1451        PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
1452
1453        I_OUTPUTPICTURES++;
1454     }
1455
1456     return( 0 );
1457 }
1458
1459 /*****************************************************************************
1460  * End: terminate BeOS video thread output method
1461  *****************************************************************************/
1462 void End( vout_thread_t *p_vout )
1463 {
1464     BeosCloseDisplay( p_vout );
1465 }
1466
1467 /*****************************************************************************
1468  * CloseVideo: destroy BeOS video thread output method
1469  *****************************************************************************
1470  * Terminate an output method created by DummyCreateOutputMethod
1471  *****************************************************************************/
1472 void E_(CloseVideo) ( vlc_object_t *p_this )
1473 {
1474     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1475
1476     free( p_vout->p_sys );
1477 }
1478
1479 /*****************************************************************************
1480  * Display: displays previously rendered output
1481  *****************************************************************************
1482  * This function send the currently rendered image to BeOS image, waits until
1483  * it is displayed and switch the two rendering buffers, preparing next frame.
1484  *****************************************************************************/
1485 void Display( vout_thread_t *p_vout, picture_t *p_pic )
1486 {
1487     VideoWindow * p_win = p_vout->p_sys->p_window;
1488
1489     /* draw buffer if required */    
1490     if (!p_win->teardownwindow)
1491     { 
1492        p_win->drawBuffer(p_vout->p_sys->i_index);
1493     }
1494     /* change buffer */
1495     p_vout->p_sys->i_index = ++p_vout->p_sys->i_index % 3;
1496     p_pic->p->p_pixels = (u8*)p_vout->p_sys->p_window->bitmap[p_vout->p_sys->i_index]->Bits();
1497 }
1498
1499 /* following functions are local */
1500
1501 /*****************************************************************************
1502  * BeosOpenDisplay: open and initialize BeOS device
1503  *****************************************************************************/
1504 static int BeosOpenDisplay( vout_thread_t *p_vout )
1505
1506
1507     p_vout->p_sys->p_window = new VideoWindow( p_vout->p_sys->i_width - 1,
1508                                                p_vout->p_sys->i_height - 1,
1509                                                BRect( 20, 50,
1510                                                       20 + p_vout->i_window_width - 1, 
1511                                                       50 + p_vout->i_window_height - 1 ),
1512                                                p_vout );
1513     if( p_vout->p_sys->p_window == NULL )
1514     {
1515         msg_Err( p_vout, "cannot allocate VideoWindow" );
1516         return( 1 );
1517     }
1518     else
1519     {
1520         p_vout->p_sys->p_window->Show();
1521     }
1522     
1523     return( 0 );
1524 }
1525
1526 /*****************************************************************************
1527  * BeosDisplay: close and reset BeOS device
1528  *****************************************************************************
1529  * Returns all resources allocated by BeosOpenDisplay and restore the original
1530  * state of the device.
1531  *****************************************************************************/
1532 static void BeosCloseDisplay( vout_thread_t *p_vout )
1533 {    
1534     VideoWindow * p_win = p_vout->p_sys->p_window;
1535     /* Destroy the video window */
1536     if( p_win != NULL && !p_win->teardownwindow)
1537     {
1538         p_win->Lock();
1539         p_win->teardownwindow = true;
1540         p_win->Hide();
1541         p_win->Quit();
1542     }
1543     p_win = NULL;
1544 }