]> git.sesse.net Git - vlc/blob - modules/gui/beos/VideoOutput.cpp
* Makefile.am: fixed 'make package-beos'
[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.17 2003/04/22 16:36:16 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     fprintf( stderr, "%x\n", '&' );
304     AddShortcut( '&', 0, new BMessage( RESIZE_50 ) );
305     AddShortcut( 'é', 0, new BMessage( RESIZE_100 ) );
306         // FIXME - this one doesn't work because 'é' is a multi-byte character
307     AddShortcut( '"', 0, new BMessage( RESIZE_200 ) );
308     
309     _SetToSettings();
310 }
311
312 VideoWindow::~VideoWindow()
313 {
314     int32 result;
315
316     teardownwindow = true;
317     wait_for_thread(fDrawThreadID, &result);
318     _FreeBuffers();
319         delete fSettings;
320 }
321
322 /*****************************************************************************
323  * VideoWindow::MessageReceived
324  *****************************************************************************/
325 void
326 VideoWindow::MessageReceived( BMessage *p_message )
327 {
328         switch( p_message->what )
329         {
330                 case TOGGLE_FULL_SCREEN:
331                         BWindow::Zoom();
332                         break;
333                 case RESIZE_50:
334                 case RESIZE_100:
335                 case RESIZE_200:
336                         if (IsFullScreen())
337                                 BWindow::Zoom();
338                         _SetVideoSize(p_message->what);
339                         break;
340                 case VERT_SYNC:
341                         SetSyncToRetrace(!IsSyncedToRetrace());
342                         break;
343                 case WINDOW_FEEL:
344                         {
345                                 window_feel winFeel;
346                                 if (p_message->FindInt32("WinFeel", (int32*)&winFeel) == B_OK)
347                                 {
348                                         SetFeel(winFeel);
349                                         fCachedFeel = winFeel;
350                                         if (winFeel == B_FLOATING_ALL_WINDOW_FEEL)
351                                                 fSettings->AddFlags(VideoSettings::FLAG_ON_TOP_ALL);
352                                         else
353                                                 fSettings->ClearFlags(VideoSettings::FLAG_ON_TOP_ALL);
354                                 }
355                         }
356                         break;
357                 case ASPECT_CORRECT:
358                         SetCorrectAspectRatio(!CorrectAspectRatio());
359                         break;
360                 case SCREEN_SHOT:
361                         // save a screen shot
362                         if ( BBitmap* current = bitmap[i_buffer] )
363                         {
364 // the following line might be tempting, but does not work for some overlay bitmaps!!!
365 //                              BBitmap* temp = new BBitmap( current );
366 // so we clone the bitmap ourselves
367 // however, we need to take care of potentially different padding!
368 // memcpy() is slow when reading from grafix memory, but what the heck...
369                                 BBitmap* temp = new BBitmap( current->Bounds(), current->ColorSpace() );
370                                 if ( temp && temp->IsValid() )
371                                 {
372                                         int32 height = (int32)current->Bounds().Height();
373                                         uint8* dst = (uint8*)temp->Bits();
374                                         uint8* src = (uint8*)current->Bits();
375                                         int32 dstBpr = temp->BytesPerRow();
376                                         int32 srcBpr = current->BytesPerRow();
377                                         int32 validBytes = dstBpr > srcBpr ? srcBpr : dstBpr;
378                                         for ( int32 y = 0; y < height; y++ )
379                                         {
380                                                 memcpy( dst, src, validBytes );
381                                                 dst += dstBpr;
382                                                 src += srcBpr;
383                                         }
384                                         char* path = config_GetPsz( p_vout, "beos-screenshot-path" );
385                                         if ( !path )
386                                                 path = strdup( DEFAULT_SCREEN_SHOT_PATH );
387                                         int32 format = config_GetInt( p_vout, "beos-screenshot-format" );
388                                         _SaveScreenShot( temp, path, format );
389                                 }
390                                 else
391                                 {
392                                         delete temp;
393                                 }
394                         }
395                         break;
396                 default:
397                         BWindow::MessageReceived( p_message );
398                         break;
399         }
400 }
401
402 /*****************************************************************************
403  * VideoWindow::Zoom
404  *****************************************************************************/
405 void
406 VideoWindow::Zoom(BPoint origin, float width, float height )
407 {
408         ToggleFullScreen();
409 }
410
411 /*****************************************************************************
412  * VideoWindow::FrameMoved
413  *****************************************************************************/
414 void
415 VideoWindow::FrameMoved(BPoint origin) 
416 {
417         if (IsFullScreen())
418                 return ;
419         winSize = Frame();
420 }
421
422 /*****************************************************************************
423  * VideoWindow::FrameResized
424  *****************************************************************************/
425 void
426 VideoWindow::FrameResized( float width, float height )
427 {
428         int32 useWidth = CorrectAspectRatio() ? i_width : fTrueWidth;
429         int32 useHeight = CorrectAspectRatio() ? i_height : fTrueHeight;
430     float out_width, out_height;
431     float out_left, out_top;
432     float width_scale = width / useWidth;
433     float height_scale = height / useHeight;
434
435     if (width_scale <= height_scale)
436     {
437         out_width = (useWidth * width_scale);
438         out_height = (useHeight * width_scale);
439         out_left = 0; 
440         out_top = (height - out_height) / 2;
441     }
442     else   /* if the height is proportionally smaller */
443     {
444         out_width = (useWidth * height_scale);
445         out_height = (useHeight * height_scale);
446         out_top = 0;
447         out_left = (width - out_width) / 2;
448     }
449     view->MoveTo(out_left,out_top);
450     view->ResizeTo(out_width, out_height);
451
452         if (!IsFullScreen())
453         winSize = Frame();
454 }
455
456 /*****************************************************************************
457  * VideoWindow::ScreenChanged
458  *****************************************************************************/
459 void
460 VideoWindow::ScreenChanged(BRect frame, color_space format)
461 {
462         BScreen screen(this);
463         display_mode mode; 
464         screen.GetMode(&mode); 
465         float refresh = (mode.timing.pixel_clock * 1000)
466                                         / ((mode.timing.h_total) * (mode.timing.v_total)); 
467         SetSyncToRetrace(refresh < MIN_AUTO_VSYNC_REFRESH);
468 }
469
470 /*****************************************************************************
471  * VideoWindow::Activate
472  *****************************************************************************/
473 void
474 VideoWindow::WindowActivated(bool active)
475 {
476 }
477
478 /*****************************************************************************
479  * VideoWindow::drawBuffer
480  *****************************************************************************/
481 void
482 VideoWindow::drawBuffer(int bufferIndex)
483 {
484     i_buffer = bufferIndex;
485
486     // sync to the screen if required
487     if (IsSyncedToRetrace())
488     {
489         BScreen screen(this);
490         screen.WaitForRetrace(22000);
491     }
492     if (fInitStatus >= B_OK && LockLooper())
493     {
494        // switch the overlay bitmap
495        if (mode == OVERLAY)
496        {
497           rgb_color key;
498           view->SetViewOverlay(bitmap[i_buffer], 
499                             bitmap[i_buffer]->Bounds() ,
500                             view->Bounds(),
501                             &key, B_FOLLOW_ALL,
502                                                         B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|
503                                     B_OVERLAY_TRANSFER_CHANNEL);
504                    view->SetViewColor(key);
505            }
506        else
507        {
508          // switch the bitmap
509          view->DrawBitmap(bitmap[i_buffer], view->Bounds() );
510        }
511        UnlockLooper();
512     }
513 }
514
515 /*****************************************************************************
516  * VideoWindow::SetInterfaceShowing
517  *****************************************************************************/
518 void
519 VideoWindow::ToggleInterfaceShowing()
520 {
521         SetInterfaceShowing(!fInterfaceShowing);
522 }
523
524 /*****************************************************************************
525  * VideoWindow::SetInterfaceShowing
526  *****************************************************************************/
527 void
528 VideoWindow::SetInterfaceShowing(bool showIt)
529 {
530         BWindow* window = get_interface_window();
531         if (window)
532         {
533                 if (showIt)
534                 {
535                         if (fCachedFeel != B_NORMAL_WINDOW_FEEL)
536                                 SetFeel(B_NORMAL_WINDOW_FEEL);
537                         window->Activate(true);
538                         SendBehind(window);
539                 }
540                 else
541                 {
542                         SetFeel(fCachedFeel);
543                         Activate(true);
544                         window->SendBehind(this);
545                 }
546                 fInterfaceShowing = showIt;
547         }
548 }
549
550 /*****************************************************************************
551  * VideoWindow::SetCorrectAspectRatio
552  *****************************************************************************/
553 void
554 VideoWindow::SetCorrectAspectRatio(bool doIt)
555 {
556         if (CorrectAspectRatio() != doIt)
557         {
558                 if (doIt)
559                         fSettings->AddFlags(VideoSettings::FLAG_CORRECT_RATIO);
560                 else
561                         fSettings->ClearFlags(VideoSettings::FLAG_CORRECT_RATIO);
562                 FrameResized(Bounds().Width(), Bounds().Height());
563         }
564 }
565
566 /*****************************************************************************
567  * VideoWindow::CorrectAspectRatio
568  *****************************************************************************/
569 bool
570 VideoWindow::CorrectAspectRatio() const
571 {
572         return fSettings->HasFlags(VideoSettings::FLAG_CORRECT_RATIO);
573 }
574
575 /*****************************************************************************
576  * VideoWindow::ToggleFullScreen
577  *****************************************************************************/
578 void
579 VideoWindow::ToggleFullScreen()
580 {
581         SetFullScreen(!IsFullScreen());
582 }
583
584 /*****************************************************************************
585  * VideoWindow::SetFullScreen
586  *****************************************************************************/
587 void
588 VideoWindow::SetFullScreen(bool doIt)
589 {
590         if (doIt)
591         {
592                 BScreen screen(this);
593                 BRect rect = screen.Frame();
594                 Activate();
595                 MoveTo(0.0, 0.0);
596                 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
597                 be_app->ObscureCursor();
598                 fInterfaceShowing = false;
599                 fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
600         }
601         else
602         {
603                 MoveTo(winSize.left, winSize.top);
604                 ResizeTo(winSize.IntegerWidth(), winSize.IntegerHeight());
605                 be_app->ShowCursor();
606                 fInterfaceShowing = true;
607                 fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
608         }
609 }
610
611 /*****************************************************************************
612  * VideoWindow::IsFullScreen
613  *****************************************************************************/
614 bool
615 VideoWindow::IsFullScreen() const
616 {
617         return fSettings->HasFlags(VideoSettings::FLAG_FULL_SCREEN);
618 }
619
620 /*****************************************************************************
621  * VideoWindow::SetSyncToRetrace
622  *****************************************************************************/
623 void
624 VideoWindow::SetSyncToRetrace(bool doIt)
625 {
626         if (doIt)
627                 fSettings->AddFlags(VideoSettings::FLAG_SYNC_RETRACE);
628         else
629                 fSettings->ClearFlags(VideoSettings::FLAG_SYNC_RETRACE);
630 }
631
632 /*****************************************************************************
633  * VideoWindow::IsSyncedToRetrace
634  *****************************************************************************/
635 bool
636 VideoWindow::IsSyncedToRetrace() const
637 {
638         return fSettings->HasFlags(VideoSettings::FLAG_SYNC_RETRACE);
639 }
640
641
642 /*****************************************************************************
643  * VideoWindow::_AllocateBuffers
644  *****************************************************************************/
645 status_t
646 VideoWindow::_AllocateBuffers(int width, int height, int* mode)
647 {
648         // clear any old buffers
649         _FreeBuffers();
650         // set default mode
651         *mode = BITMAP;
652
653         BRect bitmapFrame( 0, 0, width, height );
654         // read from config, if we are supposed to use overlay at all
655     int noOverlay = !config_GetInt( p_vout, "overlay" );
656         // test for overlay capability
657     for (int i = 0; i < COLOR_COUNT; i++)
658     {
659         if (noOverlay) break;
660         bitmap[0] = new BBitmap ( bitmapFrame, 
661                                   B_BITMAP_WILL_OVERLAY |
662                                   B_BITMAP_RESERVE_OVERLAY_CHANNEL,
663                                   colspace[i].colspace);
664
665         if(bitmap[0] && bitmap[0]->InitCheck() == B_OK) 
666         {
667             colspace_index = i;
668
669             bitmap[1] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
670                                      colspace[colspace_index].colspace);
671             bitmap[2] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
672                                      colspace[colspace_index].colspace);
673             if ( (bitmap[2] && bitmap[2]->InitCheck() == B_OK) )
674             {
675                *mode = OVERLAY;
676                rgb_color key;
677                view->SetViewOverlay(bitmap[0], 
678                                     bitmap[0]->Bounds() ,
679                                     view->Bounds(),
680                                     &key, B_FOLLOW_ALL,
681                                             B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
682                        view->SetViewColor(key);
683                SetTitle("VLC " PACKAGE_VERSION " (Overlay)");
684                break;
685             }
686             else
687             {
688                _FreeBuffers();
689                *mode = BITMAP; // might want to try again with normal bitmaps
690             }
691         }
692         else
693             delete bitmap[0];
694         }
695
696     if (*mode == BITMAP)
697         {
698         // fallback to RGB
699         colspace_index = DEFAULT_COL;   // B_RGB32
700         SetTitle( "VLC " PACKAGE_VERSION " (Bitmap)" );
701         bitmap[0] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
702         bitmap[1] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
703         bitmap[2] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
704     }
705     // see if everything went well
706     status_t status = B_ERROR;
707     for (int32 i = 0; i < 3; i++)
708     {
709         if (bitmap[i])
710                 status = bitmap[i]->InitCheck();
711                 if (status < B_OK)
712                         break;
713     }
714     if (status >= B_OK)
715     {
716             // clear bitmaps to black
717             for (int32 i = 0; i < 3; i++)
718                 _BlankBitmap(bitmap[i]);
719     }
720     return status;
721 }
722
723 /*****************************************************************************
724  * VideoWindow::_FreeBuffers
725  *****************************************************************************/
726 void
727 VideoWindow::_FreeBuffers()
728 {
729         delete bitmap[0];
730         bitmap[0] = NULL;
731         delete bitmap[1];
732         bitmap[1] = NULL;
733         delete bitmap[2];
734         bitmap[2] = NULL;
735         fInitStatus = B_ERROR;
736 }
737
738 /*****************************************************************************
739  * VideoWindow::_BlankBitmap
740  *****************************************************************************/
741 void
742 VideoWindow::_BlankBitmap(BBitmap* bitmap) const
743 {
744         // no error checking (we do that earlier on and since it's a private function...
745
746         // YCbCr: 
747         // Loss/Saturation points are Y 16-235 (absoulte); Cb/Cr 16-240 (center 128)
748
749         // YUV: 
750         // Extrema points are Y 0 - 207 (absolute) U -91 - 91 (offset 128) V -127 - 127 (offset 128)
751
752         // we only handle weird colorspaces with special care
753         switch (bitmap->ColorSpace()) {
754                 case B_YCbCr422: {
755                         // Y0[7:0]  Cb0[7:0]  Y1[7:0]  Cr0[7:0]  Y2[7:0]  Cb2[7:0]  Y3[7:0]  Cr2[7:0]
756                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
757                         uint8* bits = (uint8*)bitmap->Bits();
758                         int32 bpr = bitmap->BytesPerRow();
759                         for (int32 y = 0; y < height; y++) {
760                                 // handle 2 bytes at a time
761                                 for (int32 i = 0; i < bpr; i += 2) {
762                                         // offset into line
763                                         bits[i] = 16;
764                                         bits[i + 1] = 128;
765                                 }
766                                 // next line
767                                 bits += bpr;
768                         }
769                         break;
770                 }
771                 case B_YCbCr420: {
772 // TODO: untested!!
773                         // Non-interlaced only, Cb0  Y0  Y1  Cb2 Y2  Y3  on even scan lines ...
774                         // Cr0  Y0  Y1  Cr2 Y2  Y3  on odd scan lines
775                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
776                         uint8* bits = (uint8*)bitmap->Bits();
777                         int32 bpr = bitmap->BytesPerRow();
778                         for (int32 y = 0; y < height; y += 1) {
779                                 // handle 3 bytes at a time
780                                 for (int32 i = 0; i < bpr; i += 3) {
781                                         // offset into line
782                                         bits[i] = 128;
783                                         bits[i + 1] = 16;
784                                         bits[i + 2] = 16;
785                                 }
786                                 // next line
787                                 bits += bpr;
788                         }
789                         break;
790                 }
791                 case B_YUV422: {
792 // TODO: untested!!
793                         // U0[7:0]  Y0[7:0]   V0[7:0]  Y1[7:0]  U2[7:0]  Y2[7:0]   V2[7:0]  Y3[7:0]
794                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
795                         uint8* bits = (uint8*)bitmap->Bits();
796                         int32 bpr = bitmap->BytesPerRow();
797                         for (int32 y = 0; y < height; y += 1) {
798                                 // handle 2 bytes at a time
799                                 for (int32 i = 0; i < bpr; i += 2) {
800                                         // offset into line
801                                         bits[i] = 128;
802                                         bits[i + 1] = 0;
803                                 }
804                                 // next line
805                                 bits += bpr;
806                         }
807                         break;
808                 }
809                 default:
810                         memset(bitmap->Bits(), 0, bitmap->BitsLength());
811                         break;
812         }
813 }
814
815 /*****************************************************************************
816  * VideoWindow::_SetVideoSize
817  *****************************************************************************/
818 void
819 VideoWindow::_SetVideoSize(uint32 mode)
820 {
821         // let size depend on aspect correction
822         int32 width = CorrectAspectRatio() ? i_width : fTrueWidth;
823         int32 height = CorrectAspectRatio() ? i_height : fTrueHeight;
824         switch (mode)
825         {
826                 case RESIZE_50:
827                         width /= 2;
828                         height /= 2;
829                         break;
830                 case RESIZE_200:
831                         width *= 2;
832                         height *= 2;
833                         break;
834                 case RESIZE_100:
835                 default:
836                 break;
837         }
838         fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
839         ResizeTo(width, height);
840 }
841
842 /*****************************************************************************
843  * VideoWindow::_SetToSettings
844  *****************************************************************************/
845 void
846 VideoWindow::_SetToSettings()
847 {
848         // adjust dimensions
849         uint32 mode = RESIZE_100;
850         switch (fSettings->VideoSize())
851         {
852                 case VideoSettings::SIZE_50:
853                         mode = RESIZE_50;
854                         break;
855                 case VideoSettings::SIZE_200:
856                         mode = RESIZE_200;
857                         break;
858                 case VideoSettings::SIZE_100:
859                 case VideoSettings::SIZE_OTHER:
860                 default:
861                         break;
862         }
863         bool fullscreen = IsFullScreen();       // remember settings
864         _SetVideoSize(mode);                            // because this will reset settings
865         // the fullscreen status is reflected in the settings,
866         // but not yet in the windows state
867         if (fullscreen)
868                 SetFullScreen(true);
869         if (fSettings->HasFlags(VideoSettings::FLAG_ON_TOP_ALL))
870                 fCachedFeel = B_FLOATING_ALL_WINDOW_FEEL;
871         else
872                 fCachedFeel = B_NORMAL_WINDOW_FEEL;
873         SetFeel(fCachedFeel);
874 }
875
876 /*****************************************************************************
877  * VideoWindow::_SaveScreenShot
878  *****************************************************************************/
879 void
880 VideoWindow::_SaveScreenShot( BBitmap* bitmap, char* path,
881                                                   uint32 translatorID ) const
882 {
883         // make the info object from the parameters
884         screen_shot_info* info = new screen_shot_info;
885         info->bitmap = bitmap;
886         info->path = path;
887         info->translatorID = translatorID;
888         info->width = CorrectAspectRatio() ? i_width : fTrueWidth;
889         info->height = CorrectAspectRatio() ? i_height : fTrueHeight;
890         // spawn a new thread to take care of the actual saving to disk
891         thread_id thread = spawn_thread( _save_screen_shot,
892                                                                          "screen shot saver",
893                                                                          B_LOW_PRIORITY, (void*)info );
894         // start thread or do the job ourself if something went wrong
895         if ( thread < B_OK || resume_thread( thread ) < B_OK )
896                 _save_screen_shot( (void*)info );
897 }
898
899 /*****************************************************************************
900  * VideoWindow::_save_screen_shot
901  *****************************************************************************/
902 int32
903 VideoWindow::_save_screen_shot( void* cookie )
904 {
905         screen_shot_info* info = (screen_shot_info*)cookie;
906         if ( info && info->bitmap && info->bitmap->IsValid() && info->path )
907         {
908                 // try to be as quick as possible creating the file (the user might have
909                 // taken the next screen shot already!)
910                 // make sure we have a unique name for the screen shot
911                 BString path( info->path );
912                 // create the folder if it doesn't exist
913                 BString folder( info->path );
914                 int32 pos = folder.FindLast("/");
915                 if ( pos > 0 )
916                 {
917                         pos++; // leave the last '/' in the string
918                         if ( pos == path.Length() )
919                                 path << "vlc screenshot";
920                         else
921                         {
922                                 int32 removeChars = folder.Length() - pos;      
923                                 folder.Remove( pos, removeChars );
924                         }
925                         create_directory( folder.String(), 0777 );
926                 }
927                 BEntry entry( path.String() );
928                 int32 appendedNumber = 0;
929                 if ( entry.Exists() && !entry.IsSymLink() )
930                 {
931                         // we would clobber an existing entry
932                         bool foundUniqueName = false;
933                         appendedNumber = 1;
934                         while ( !foundUniqueName ) {
935                                 BString newName( path.String() );
936                                 newName << " " << appendedNumber;
937                                 BEntry possiblyClobberedEntry( newName.String() );
938                                 if ( possiblyClobberedEntry.Exists()
939                                         && !possiblyClobberedEntry.IsSymLink() )
940                                         appendedNumber++;
941                                 else
942                                         foundUniqueName = true;
943                         }
944                 }
945                 if ( appendedNumber > 0 )
946                         path << " " << appendedNumber;
947                 // there is still a slight chance to clobber an existing
948                 // file (if it was created in the "meantime"), but we take it...
949                 BFile outFile( path.String(),
950                                            B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE );
951
952                 // make colorspace converted copy of bitmap
953                 BBitmap* converted = new BBitmap( BRect( 0.0, 0.0, info->width, info->height ),
954                                                                                   B_RGB32 );
955                 status_t status = convert_bitmap( info->bitmap, converted );
956                 if ( status == B_OK )
957                 {
958                         BTranslatorRoster* roster = BTranslatorRoster::Default();
959                         uint32 imageFormat = 0;
960                         translator_id translator = 0;
961                         bool found = false;
962
963                         // find suitable translator
964                         translator_id* ids = NULL;
965                         int32 count = 0;
966                 
967                         status = roster->GetAllTranslators( &ids, &count );
968                         if ( status >= B_OK )
969                         {
970                                 for ( int tix = 0; tix < count; tix++ )
971                                 { 
972                                         const translation_format *formats = NULL; 
973                                         int32 num_formats = 0; 
974                                         bool ok = false; 
975                                         status = roster->GetInputFormats( ids[tix],
976                                                                                                           &formats, &num_formats );
977                                         if (status >= B_OK)
978                                         {
979                                                 for ( int iix = 0; iix < num_formats; iix++ )
980                                                 { 
981                                                         if ( formats[iix].type == B_TRANSLATOR_BITMAP )
982                                                         { 
983                                                                 ok = true; 
984                                                                 break; 
985                                                         }
986                                                 }
987                                         }
988                                         if ( !ok )
989                                                 continue; 
990                                         status = roster->GetOutputFormats( ids[tix],
991                                                                                                            &formats, &num_formats); 
992                                         if ( status >= B_OK )
993                                         {
994                                                 for ( int32 oix = 0; oix < num_formats; oix++ )
995                                                 {
996                                                         if ( formats[oix].type != B_TRANSLATOR_BITMAP )
997                                                         {
998                                                                 if ( formats[oix].type == info->translatorID )
999                                                                 {
1000                                                                         found = true;
1001                                                                         imageFormat = formats[oix].type;
1002                                                                         translator = ids[tix];
1003                                                                         break;
1004                                                                 }
1005                                                         }
1006                                                 }
1007                                         }
1008                                 }
1009                         }
1010                         delete[] ids;
1011                         if ( found )
1012                         {
1013                                 // make bitmap stream
1014                                 BBitmapStream outStream( converted );
1015
1016                                 status = outFile.InitCheck();
1017                                 if (status == B_OK) {
1018                                         status = roster->Translate( &outStream, NULL, NULL,
1019                                                                                                 &outFile, imageFormat );
1020                                         if ( status == B_OK )
1021                                         {
1022                                                 BNodeInfo nodeInfo( &outFile );
1023                                                 if ( nodeInfo.InitCheck() == B_OK )
1024                                                 {
1025                                                         translation_format* formats; 
1026                                                         int32 count;
1027                                                         status = roster->GetOutputFormats( translator,
1028                                                                                                                            (const translation_format **) &formats,
1029                                                                                                                            &count);
1030                                                         if ( status >= B_OK )
1031                                                         {
1032                                                                 const char * mime = NULL; 
1033                                                                 for ( int ix = 0; ix < count; ix++ ) {
1034                                                                         if ( formats[ix].type == imageFormat ) {
1035                                                                                 mime = formats[ix].MIME;
1036                                                                                 break;
1037                                                                         }
1038                                                                 } 
1039                                                                 if ( mime )
1040                                                                         nodeInfo.SetType( mime );
1041                                                         }
1042                                                 }
1043                                         }
1044                                 }
1045                                 outStream.DetachBitmap( &converted );
1046                                 outFile.Unset();
1047                         }
1048                 }
1049                 delete converted;
1050         }
1051         if ( info )
1052         {
1053                 delete info->bitmap;
1054                 free( info->path );
1055         }
1056         delete info;
1057         return B_OK;
1058 }
1059
1060
1061 /*****************************************************************************
1062  * VLCView::VLCView
1063  *****************************************************************************/
1064 VLCView::VLCView(BRect bounds, vout_thread_t *p_vout_instance )
1065         : BView(bounds, "video view", B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED),
1066           fLastMouseMovedTime(system_time()),
1067           fCursorHidden(false),
1068           fCursorInside(false),
1069           fIgnoreDoubleClick(false)
1070 {
1071     p_vout = p_vout_instance;
1072     SetViewColor(B_TRANSPARENT_32_BIT);
1073 }
1074
1075 /*****************************************************************************
1076  * VLCView::~VLCView
1077  *****************************************************************************/
1078 VLCView::~VLCView()
1079 {
1080 }
1081
1082 /*****************************************************************************
1083  * VLCVIew::AttachedToWindow
1084  *****************************************************************************/
1085 void
1086 VLCView::AttachedToWindow()
1087 {
1088         // in order to get keyboard events
1089         MakeFocus(true);
1090         // periodically check if we want to hide the pointer
1091         Window()->SetPulseRate(1000000);
1092 }
1093
1094 /*****************************************************************************
1095  * VLCVIew::MouseDown
1096  *****************************************************************************/
1097 void
1098 VLCView::MouseDown(BPoint where)
1099 {
1100         VideoWindow* videoWindow = dynamic_cast<VideoWindow*>(Window());
1101         BMessage* msg = Window()->CurrentMessage();
1102         int32 clicks;
1103         uint32 buttons;
1104         msg->FindInt32("clicks", &clicks);
1105         msg->FindInt32("buttons", (int32*)&buttons);
1106
1107         if (videoWindow)
1108         {
1109                 if (buttons & B_PRIMARY_MOUSE_BUTTON)
1110                 {
1111                         if (clicks == 2 && !fIgnoreDoubleClick)
1112                                 Window()->Zoom();
1113                         /* else
1114                                 videoWindow->ToggleInterfaceShowing(); */
1115                         fIgnoreDoubleClick = false;
1116                 }
1117             else
1118             {
1119                         if (buttons & B_SECONDARY_MOUSE_BUTTON) 
1120                         {
1121                                 // clicks will be 2 next time (if interval short enough)
1122                                 // even if the first click and the second
1123                                 // have not been made with the same mouse button
1124                                 fIgnoreDoubleClick = true;
1125                                 // launch popup menu
1126                                 BPopUpMenu *menu = new BPopUpMenu("context menu");
1127                                 menu->SetRadioMode(false);
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 }