]> git.sesse.net Git - vlc/blob - modules/gui/beos/VideoOutput.cpp
modules/gui/beos/*: fixed a few things I broke recently (dvd menus,
[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.18 2003/05/07 14:49:19 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-screenshotpath" );
385                                         if ( !path )
386                                                 path = strdup( DEFAULT_SCREEN_SHOT_PATH );
387                                         /* config_GetPsz( p_vout, "beos-screenshotformat" ); */
388                                         int32 format = DEFAULT_SCREEN_SHOT_FORMAT;
389                                         _SaveScreenShot( temp, path, format );
390                                 }
391                                 else
392                                 {
393                                         delete temp;
394                                 }
395                         }
396                         break;
397                 default:
398                         BWindow::MessageReceived( p_message );
399                         break;
400         }
401 }
402
403 /*****************************************************************************
404  * VideoWindow::Zoom
405  *****************************************************************************/
406 void
407 VideoWindow::Zoom(BPoint origin, float width, float height )
408 {
409         ToggleFullScreen();
410 }
411
412 /*****************************************************************************
413  * VideoWindow::FrameMoved
414  *****************************************************************************/
415 void
416 VideoWindow::FrameMoved(BPoint origin) 
417 {
418         if (IsFullScreen())
419                 return ;
420         winSize = Frame();
421 }
422
423 /*****************************************************************************
424  * VideoWindow::FrameResized
425  *****************************************************************************/
426 void
427 VideoWindow::FrameResized( float width, float height )
428 {
429         int32 useWidth = CorrectAspectRatio() ? i_width : fTrueWidth;
430         int32 useHeight = CorrectAspectRatio() ? i_height : fTrueHeight;
431     float out_width, out_height;
432     float out_left, out_top;
433     float width_scale = width / useWidth;
434     float height_scale = height / useHeight;
435
436     if (width_scale <= height_scale)
437     {
438         out_width = (useWidth * width_scale);
439         out_height = (useHeight * width_scale);
440         out_left = 0; 
441         out_top = (height - out_height) / 2;
442     }
443     else   /* if the height is proportionally smaller */
444     {
445         out_width = (useWidth * height_scale);
446         out_height = (useHeight * height_scale);
447         out_top = 0;
448         out_left = (width - out_width) / 2;
449     }
450     view->MoveTo(out_left,out_top);
451     view->ResizeTo(out_width, out_height);
452
453         if (!IsFullScreen())
454         winSize = Frame();
455 }
456
457 /*****************************************************************************
458  * VideoWindow::ScreenChanged
459  *****************************************************************************/
460 void
461 VideoWindow::ScreenChanged(BRect frame, color_space format)
462 {
463         BScreen screen(this);
464         display_mode mode; 
465         screen.GetMode(&mode); 
466         float refresh = (mode.timing.pixel_clock * 1000)
467                                         / ((mode.timing.h_total) * (mode.timing.v_total)); 
468         SetSyncToRetrace(refresh < MIN_AUTO_VSYNC_REFRESH);
469 }
470
471 /*****************************************************************************
472  * VideoWindow::Activate
473  *****************************************************************************/
474 void
475 VideoWindow::WindowActivated(bool active)
476 {
477 }
478
479 /*****************************************************************************
480  * VideoWindow::drawBuffer
481  *****************************************************************************/
482 void
483 VideoWindow::drawBuffer(int bufferIndex)
484 {
485     i_buffer = bufferIndex;
486
487     // sync to the screen if required
488     if (IsSyncedToRetrace())
489     {
490         BScreen screen(this);
491         screen.WaitForRetrace(22000);
492     }
493     if (fInitStatus >= B_OK && LockLooper())
494     {
495        // switch the overlay bitmap
496        if (mode == OVERLAY)
497        {
498           rgb_color key;
499           view->SetViewOverlay(bitmap[i_buffer], 
500                             bitmap[i_buffer]->Bounds() ,
501                             view->Bounds(),
502                             &key, B_FOLLOW_ALL,
503                                                         B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|
504                                     B_OVERLAY_TRANSFER_CHANNEL);
505                    view->SetViewColor(key);
506            }
507        else
508        {
509          // switch the bitmap
510          view->DrawBitmap(bitmap[i_buffer], view->Bounds() );
511        }
512        UnlockLooper();
513     }
514 }
515
516 /*****************************************************************************
517  * VideoWindow::SetInterfaceShowing
518  *****************************************************************************/
519 void
520 VideoWindow::ToggleInterfaceShowing()
521 {
522         SetInterfaceShowing(!fInterfaceShowing);
523 }
524
525 /*****************************************************************************
526  * VideoWindow::SetInterfaceShowing
527  *****************************************************************************/
528 void
529 VideoWindow::SetInterfaceShowing(bool showIt)
530 {
531         BWindow* window = get_interface_window();
532         if (window)
533         {
534                 if (showIt)
535                 {
536                         if (fCachedFeel != B_NORMAL_WINDOW_FEEL)
537                                 SetFeel(B_NORMAL_WINDOW_FEEL);
538                         window->Activate(true);
539                         SendBehind(window);
540                 }
541                 else
542                 {
543                         SetFeel(fCachedFeel);
544                         Activate(true);
545                         window->SendBehind(this);
546                 }
547                 fInterfaceShowing = showIt;
548         }
549 }
550
551 /*****************************************************************************
552  * VideoWindow::SetCorrectAspectRatio
553  *****************************************************************************/
554 void
555 VideoWindow::SetCorrectAspectRatio(bool doIt)
556 {
557         if (CorrectAspectRatio() != doIt)
558         {
559                 if (doIt)
560                         fSettings->AddFlags(VideoSettings::FLAG_CORRECT_RATIO);
561                 else
562                         fSettings->ClearFlags(VideoSettings::FLAG_CORRECT_RATIO);
563                 FrameResized(Bounds().Width(), Bounds().Height());
564         }
565 }
566
567 /*****************************************************************************
568  * VideoWindow::CorrectAspectRatio
569  *****************************************************************************/
570 bool
571 VideoWindow::CorrectAspectRatio() const
572 {
573         return fSettings->HasFlags(VideoSettings::FLAG_CORRECT_RATIO);
574 }
575
576 /*****************************************************************************
577  * VideoWindow::ToggleFullScreen
578  *****************************************************************************/
579 void
580 VideoWindow::ToggleFullScreen()
581 {
582         SetFullScreen(!IsFullScreen());
583 }
584
585 /*****************************************************************************
586  * VideoWindow::SetFullScreen
587  *****************************************************************************/
588 void
589 VideoWindow::SetFullScreen(bool doIt)
590 {
591         if (doIt)
592         {
593                 BScreen screen(this);
594                 BRect rect = screen.Frame();
595                 Activate();
596                 MoveTo(0.0, 0.0);
597                 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
598                 be_app->ObscureCursor();
599                 fInterfaceShowing = false;
600                 fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
601         }
602         else
603         {
604                 MoveTo(winSize.left, winSize.top);
605                 ResizeTo(winSize.IntegerWidth(), winSize.IntegerHeight());
606                 be_app->ShowCursor();
607                 fInterfaceShowing = true;
608                 fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
609         }
610 }
611
612 /*****************************************************************************
613  * VideoWindow::IsFullScreen
614  *****************************************************************************/
615 bool
616 VideoWindow::IsFullScreen() const
617 {
618         return fSettings->HasFlags(VideoSettings::FLAG_FULL_SCREEN);
619 }
620
621 /*****************************************************************************
622  * VideoWindow::SetSyncToRetrace
623  *****************************************************************************/
624 void
625 VideoWindow::SetSyncToRetrace(bool doIt)
626 {
627         if (doIt)
628                 fSettings->AddFlags(VideoSettings::FLAG_SYNC_RETRACE);
629         else
630                 fSettings->ClearFlags(VideoSettings::FLAG_SYNC_RETRACE);
631 }
632
633 /*****************************************************************************
634  * VideoWindow::IsSyncedToRetrace
635  *****************************************************************************/
636 bool
637 VideoWindow::IsSyncedToRetrace() const
638 {
639         return fSettings->HasFlags(VideoSettings::FLAG_SYNC_RETRACE);
640 }
641
642
643 /*****************************************************************************
644  * VideoWindow::_AllocateBuffers
645  *****************************************************************************/
646 status_t
647 VideoWindow::_AllocateBuffers(int width, int height, int* mode)
648 {
649         // clear any old buffers
650         _FreeBuffers();
651         // set default mode
652         *mode = BITMAP;
653
654         BRect bitmapFrame( 0, 0, width, height );
655         // read from config, if we are supposed to use overlay at all
656     int noOverlay = !config_GetInt( p_vout, "overlay" );
657         // test for overlay capability
658     for (int i = 0; i < COLOR_COUNT; i++)
659     {
660         if (noOverlay) break;
661         bitmap[0] = new BBitmap ( bitmapFrame, 
662                                   B_BITMAP_WILL_OVERLAY |
663                                   B_BITMAP_RESERVE_OVERLAY_CHANNEL,
664                                   colspace[i].colspace);
665
666         if(bitmap[0] && bitmap[0]->InitCheck() == B_OK) 
667         {
668             colspace_index = i;
669
670             bitmap[1] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
671                                      colspace[colspace_index].colspace);
672             bitmap[2] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
673                                      colspace[colspace_index].colspace);
674             if ( (bitmap[2] && bitmap[2]->InitCheck() == B_OK) )
675             {
676                *mode = OVERLAY;
677                rgb_color key;
678                view->SetViewOverlay(bitmap[0], 
679                                     bitmap[0]->Bounds() ,
680                                     view->Bounds(),
681                                     &key, B_FOLLOW_ALL,
682                                             B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
683                        view->SetViewColor(key);
684                SetTitle("VLC " PACKAGE_VERSION " (Overlay)");
685                break;
686             }
687             else
688             {
689                _FreeBuffers();
690                *mode = BITMAP; // might want to try again with normal bitmaps
691             }
692         }
693         else
694             delete bitmap[0];
695         }
696
697     if (*mode == BITMAP)
698         {
699         // fallback to RGB
700         colspace_index = DEFAULT_COL;   // B_RGB32
701         SetTitle( "VLC " PACKAGE_VERSION " (Bitmap)" );
702         bitmap[0] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
703         bitmap[1] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
704         bitmap[2] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
705     }
706     // see if everything went well
707     status_t status = B_ERROR;
708     for (int32 i = 0; i < 3; i++)
709     {
710         if (bitmap[i])
711                 status = bitmap[i]->InitCheck();
712                 if (status < B_OK)
713                         break;
714     }
715     if (status >= B_OK)
716     {
717             // clear bitmaps to black
718             for (int32 i = 0; i < 3; i++)
719                 _BlankBitmap(bitmap[i]);
720     }
721     return status;
722 }
723
724 /*****************************************************************************
725  * VideoWindow::_FreeBuffers
726  *****************************************************************************/
727 void
728 VideoWindow::_FreeBuffers()
729 {
730         delete bitmap[0];
731         bitmap[0] = NULL;
732         delete bitmap[1];
733         bitmap[1] = NULL;
734         delete bitmap[2];
735         bitmap[2] = NULL;
736         fInitStatus = B_ERROR;
737 }
738
739 /*****************************************************************************
740  * VideoWindow::_BlankBitmap
741  *****************************************************************************/
742 void
743 VideoWindow::_BlankBitmap(BBitmap* bitmap) const
744 {
745         // no error checking (we do that earlier on and since it's a private function...
746
747         // YCbCr: 
748         // Loss/Saturation points are Y 16-235 (absoulte); Cb/Cr 16-240 (center 128)
749
750         // YUV: 
751         // Extrema points are Y 0 - 207 (absolute) U -91 - 91 (offset 128) V -127 - 127 (offset 128)
752
753         // we only handle weird colorspaces with special care
754         switch (bitmap->ColorSpace()) {
755                 case B_YCbCr422: {
756                         // Y0[7:0]  Cb0[7:0]  Y1[7:0]  Cr0[7:0]  Y2[7:0]  Cb2[7:0]  Y3[7:0]  Cr2[7:0]
757                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
758                         uint8* bits = (uint8*)bitmap->Bits();
759                         int32 bpr = bitmap->BytesPerRow();
760                         for (int32 y = 0; y < height; y++) {
761                                 // handle 2 bytes at a time
762                                 for (int32 i = 0; i < bpr; i += 2) {
763                                         // offset into line
764                                         bits[i] = 16;
765                                         bits[i + 1] = 128;
766                                 }
767                                 // next line
768                                 bits += bpr;
769                         }
770                         break;
771                 }
772                 case B_YCbCr420: {
773 // TODO: untested!!
774                         // Non-interlaced only, Cb0  Y0  Y1  Cb2 Y2  Y3  on even scan lines ...
775                         // Cr0  Y0  Y1  Cr2 Y2  Y3  on odd scan lines
776                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
777                         uint8* bits = (uint8*)bitmap->Bits();
778                         int32 bpr = bitmap->BytesPerRow();
779                         for (int32 y = 0; y < height; y += 1) {
780                                 // handle 3 bytes at a time
781                                 for (int32 i = 0; i < bpr; i += 3) {
782                                         // offset into line
783                                         bits[i] = 128;
784                                         bits[i + 1] = 16;
785                                         bits[i + 2] = 16;
786                                 }
787                                 // next line
788                                 bits += bpr;
789                         }
790                         break;
791                 }
792                 case B_YUV422: {
793 // TODO: untested!!
794                         // U0[7:0]  Y0[7:0]   V0[7:0]  Y1[7:0]  U2[7:0]  Y2[7:0]   V2[7:0]  Y3[7:0]
795                         int32 height = bitmap->Bounds().IntegerHeight() + 1;
796                         uint8* bits = (uint8*)bitmap->Bits();
797                         int32 bpr = bitmap->BytesPerRow();
798                         for (int32 y = 0; y < height; y += 1) {
799                                 // handle 2 bytes at a time
800                                 for (int32 i = 0; i < bpr; i += 2) {
801                                         // offset into line
802                                         bits[i] = 128;
803                                         bits[i + 1] = 0;
804                                 }
805                                 // next line
806                                 bits += bpr;
807                         }
808                         break;
809                 }
810                 default:
811                         memset(bitmap->Bits(), 0, bitmap->BitsLength());
812                         break;
813         }
814 }
815
816 /*****************************************************************************
817  * VideoWindow::_SetVideoSize
818  *****************************************************************************/
819 void
820 VideoWindow::_SetVideoSize(uint32 mode)
821 {
822         // let size depend on aspect correction
823         int32 width = CorrectAspectRatio() ? i_width : fTrueWidth;
824         int32 height = CorrectAspectRatio() ? i_height : fTrueHeight;
825         switch (mode)
826         {
827                 case RESIZE_50:
828                         width /= 2;
829                         height /= 2;
830                         break;
831                 case RESIZE_200:
832                         width *= 2;
833                         height *= 2;
834                         break;
835                 case RESIZE_100:
836                 default:
837                 break;
838         }
839         fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
840         ResizeTo(width, height);
841 }
842
843 /*****************************************************************************
844  * VideoWindow::_SetToSettings
845  *****************************************************************************/
846 void
847 VideoWindow::_SetToSettings()
848 {
849         // adjust dimensions
850         uint32 mode = RESIZE_100;
851         switch (fSettings->VideoSize())
852         {
853                 case VideoSettings::SIZE_50:
854                         mode = RESIZE_50;
855                         break;
856                 case VideoSettings::SIZE_200:
857                         mode = RESIZE_200;
858                         break;
859                 case VideoSettings::SIZE_100:
860                 case VideoSettings::SIZE_OTHER:
861                 default:
862                         break;
863         }
864         bool fullscreen = IsFullScreen();       // remember settings
865         _SetVideoSize(mode);                            // because this will reset settings
866         // the fullscreen status is reflected in the settings,
867         // but not yet in the windows state
868         if (fullscreen)
869                 SetFullScreen(true);
870         if (fSettings->HasFlags(VideoSettings::FLAG_ON_TOP_ALL))
871                 fCachedFeel = B_FLOATING_ALL_WINDOW_FEEL;
872         else
873                 fCachedFeel = B_NORMAL_WINDOW_FEEL;
874         SetFeel(fCachedFeel);
875 }
876
877 /*****************************************************************************
878  * VideoWindow::_SaveScreenShot
879  *****************************************************************************/
880 void
881 VideoWindow::_SaveScreenShot( BBitmap* bitmap, char* path,
882                                                   uint32 translatorID ) const
883 {
884         // make the info object from the parameters
885         screen_shot_info* info = new screen_shot_info;
886         info->bitmap = bitmap;
887         info->path = path;
888         info->translatorID = translatorID;
889         info->width = CorrectAspectRatio() ? i_width : fTrueWidth;
890         info->height = CorrectAspectRatio() ? i_height : fTrueHeight;
891         // spawn a new thread to take care of the actual saving to disk
892         thread_id thread = spawn_thread( _save_screen_shot,
893                                                                          "screen shot saver",
894                                                                          B_LOW_PRIORITY, (void*)info );
895         // start thread or do the job ourself if something went wrong
896         if ( thread < B_OK || resume_thread( thread ) < B_OK )
897                 _save_screen_shot( (void*)info );
898 }
899
900 /*****************************************************************************
901  * VideoWindow::_save_screen_shot
902  *****************************************************************************/
903 int32
904 VideoWindow::_save_screen_shot( void* cookie )
905 {
906         screen_shot_info* info = (screen_shot_info*)cookie;
907         if ( info && info->bitmap && info->bitmap->IsValid() && info->path )
908         {
909                 // try to be as quick as possible creating the file (the user might have
910                 // taken the next screen shot already!)
911                 // make sure we have a unique name for the screen shot
912                 BString path( info->path );
913                 // create the folder if it doesn't exist
914                 BString folder( info->path );
915                 create_directory( folder.String(), 0777 );
916                 path << "/vlc screenshot";
917                 BEntry entry( path.String() );
918                 int32 appendedNumber = 0;
919                 if ( entry.Exists() && !entry.IsSymLink() )
920                 {
921                         // we would clobber an existing entry
922                         bool foundUniqueName = false;
923                         appendedNumber = 1;
924                         while ( !foundUniqueName ) {
925                                 BString newName( path.String() );
926                                 newName << " " << appendedNumber;
927                                 BEntry possiblyClobberedEntry( newName.String() );
928                                 if ( possiblyClobberedEntry.Exists()
929                                         && !possiblyClobberedEntry.IsSymLink() )
930                                         appendedNumber++;
931                                 else
932                                         foundUniqueName = true;
933                         }
934                 }
935                 if ( appendedNumber > 0 )
936                         path << " " << appendedNumber;
937                 // there is still a slight chance to clobber an existing
938                 // file (if it was created in the "meantime"), but we take it...
939                 BFile outFile( path.String(),
940                                            B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE );
941
942                 // make colorspace converted copy of bitmap
943                 BBitmap* converted = new BBitmap( BRect( 0.0, 0.0, info->width, info->height ),
944                                                                                   B_RGB32 );
945                 status_t status = convert_bitmap( info->bitmap, converted );
946                 if ( status == B_OK )
947                 {
948                         BTranslatorRoster* roster = BTranslatorRoster::Default();
949                         uint32 imageFormat = 0;
950                         translator_id translator = 0;
951                         bool found = false;
952
953                         // find suitable translator
954                         translator_id* ids = NULL;
955                         int32 count = 0;
956                 
957                         status = roster->GetAllTranslators( &ids, &count );
958                         if ( status >= B_OK )
959                         {
960                                 for ( int tix = 0; tix < count; tix++ )
961                                 { 
962                                         const translation_format *formats = NULL; 
963                                         int32 num_formats = 0; 
964                                         bool ok = false; 
965                                         status = roster->GetInputFormats( ids[tix],
966                                                                                                           &formats, &num_formats );
967                                         if (status >= B_OK)
968                                         {
969                                                 for ( int iix = 0; iix < num_formats; iix++ )
970                                                 { 
971                                                         if ( formats[iix].type == B_TRANSLATOR_BITMAP )
972                                                         { 
973                                                                 ok = true; 
974                                                                 break; 
975                                                         }
976                                                 }
977                                         }
978                                         if ( !ok )
979                                                 continue; 
980                                         status = roster->GetOutputFormats( ids[tix],
981                                                                                                            &formats, &num_formats); 
982                                         if ( status >= B_OK )
983                                         {
984                                                 for ( int32 oix = 0; oix < num_formats; oix++ )
985                                                 {
986                                                         if ( formats[oix].type != B_TRANSLATOR_BITMAP )
987                                                         {
988                                                                 if ( formats[oix].type == info->translatorID )
989                                                                 {
990                                                                         found = true;
991                                                                         imageFormat = formats[oix].type;
992                                                                         translator = ids[tix];
993                                                                         break;
994                                                                 }
995                                                         }
996                                                 }
997                                         }
998                                 }
999                         }
1000                         delete[] ids;
1001                         if ( found )
1002                         {
1003                                 // make bitmap stream
1004                                 BBitmapStream outStream( converted );
1005
1006                                 status = outFile.InitCheck();
1007                                 if (status == B_OK) {
1008                                         status = roster->Translate( &outStream, NULL, NULL,
1009                                                                                                 &outFile, imageFormat );
1010                                         if ( status == B_OK )
1011                                         {
1012                                                 BNodeInfo nodeInfo( &outFile );
1013                                                 if ( nodeInfo.InitCheck() == B_OK )
1014                                                 {
1015                                                         translation_format* formats; 
1016                                                         int32 count;
1017                                                         status = roster->GetOutputFormats( translator,
1018                                                                                                                            (const translation_format **) &formats,
1019                                                                                                                            &count);
1020                                                         if ( status >= B_OK )
1021                                                         {
1022                                                                 const char * mime = NULL; 
1023                                                                 for ( int ix = 0; ix < count; ix++ ) {
1024                                                                         if ( formats[ix].type == imageFormat ) {
1025                                                                                 mime = formats[ix].MIME;
1026                                                                                 break;
1027                                                                         }
1028                                                                 } 
1029                                                                 if ( mime )
1030                                                                         nodeInfo.SetType( mime );
1031                                                         }
1032                                                 }
1033                                         }
1034                                 }
1035                                 outStream.DetachBitmap( &converted );
1036                                 outFile.Unset();
1037                         }
1038                 }
1039                 delete converted;
1040         }
1041         if ( info )
1042         {
1043                 delete info->bitmap;
1044                 free( info->path );
1045         }
1046         delete info;
1047         return B_OK;
1048 }
1049
1050
1051 /*****************************************************************************
1052  * VLCView::VLCView
1053  *****************************************************************************/
1054 VLCView::VLCView(BRect bounds, vout_thread_t *p_vout_instance )
1055         : BView(bounds, "video view", B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED),
1056           fLastMouseMovedTime(system_time()),
1057           fCursorHidden(false),
1058           fCursorInside(false),
1059           fIgnoreDoubleClick(false)
1060 {
1061     p_vout = p_vout_instance;
1062     SetViewColor(B_TRANSPARENT_32_BIT);
1063 }
1064
1065 /*****************************************************************************
1066  * VLCView::~VLCView
1067  *****************************************************************************/
1068 VLCView::~VLCView()
1069 {
1070 }
1071
1072 /*****************************************************************************
1073  * VLCVIew::AttachedToWindow
1074  *****************************************************************************/
1075 void
1076 VLCView::AttachedToWindow()
1077 {
1078         // in order to get keyboard events
1079         MakeFocus(true);
1080         // periodically check if we want to hide the pointer
1081         Window()->SetPulseRate(1000000);
1082 }
1083
1084 /*****************************************************************************
1085  * VLCVIew::MouseDown
1086  *****************************************************************************/
1087 void
1088 VLCView::MouseDown(BPoint where)
1089 {
1090         VideoWindow* videoWindow = dynamic_cast<VideoWindow*>(Window());
1091         BMessage* msg = Window()->CurrentMessage();
1092         int32 clicks;
1093         uint32 buttons;
1094         msg->FindInt32("clicks", &clicks);
1095         msg->FindInt32("buttons", (int32*)&buttons);
1096
1097         if (videoWindow)
1098         {
1099                 if (buttons & B_PRIMARY_MOUSE_BUTTON)
1100                 {
1101                         if (clicks == 2 && !fIgnoreDoubleClick)
1102                                 Window()->Zoom();
1103                         /* else
1104                                 videoWindow->ToggleInterfaceShowing(); */
1105                         fIgnoreDoubleClick = false;
1106                 }
1107             else
1108             {
1109                         if (buttons & B_SECONDARY_MOUSE_BUTTON) 
1110                         {
1111                                 // clicks will be 2 next time (if interval short enough)
1112                                 // even if the first click and the second
1113                                 // have not been made with the same mouse button
1114                                 fIgnoreDoubleClick = true;
1115                                 // launch popup menu
1116                                 BPopUpMenu *menu = new BPopUpMenu("context menu");
1117                                 menu->SetRadioMode(false);
1118                                 // Resize to 50%
1119                                 BMenuItem *halfItem = new BMenuItem(_("50%"), new BMessage(RESIZE_50));
1120                                 menu->AddItem(halfItem);
1121                                 // Resize to 100%
1122                                 BMenuItem *origItem = new BMenuItem(_("100%"), new BMessage(RESIZE_100));
1123                                 menu->AddItem(origItem);
1124                                 // Resize to 200%
1125                                 BMenuItem *doubleItem = new BMenuItem(_("200%"), new BMessage(RESIZE_200));
1126                                 menu->AddItem(doubleItem);
1127                                 // Toggle FullScreen
1128                                 BMenuItem *zoomItem = new BMenuItem(_("Fullscreen"), new BMessage(TOGGLE_FULL_SCREEN));
1129                                 zoomItem->SetMarked(videoWindow->IsFullScreen());
1130                                 menu->AddItem(zoomItem);
1131         
1132                                 menu->AddSeparatorItem();
1133         
1134                                 // Toggle vSync
1135                                 BMenuItem *vsyncItem = new BMenuItem(_("Vertical Sync"), new BMessage(VERT_SYNC));
1136                                 vsyncItem->SetMarked(videoWindow->IsSyncedToRetrace());
1137                                 menu->AddItem(vsyncItem);
1138                                 // Correct Aspect Ratio
1139                                 BMenuItem *aspectItem = new BMenuItem(_("Correct Aspect Ratio"), new BMessage(ASPECT_CORRECT));
1140                                 aspectItem->SetMarked(videoWindow->CorrectAspectRatio());
1141                                 menu->AddItem(aspectItem);
1142         
1143                                 menu->AddSeparatorItem();
1144         
1145                                 // Windwo Feel Items
1146 /*                              BMessage *winNormFeel = new BMessage(WINDOW_FEEL);
1147                                 winNormFeel->AddInt32("WinFeel", (int32)B_NORMAL_WINDOW_FEEL);
1148                                 BMenuItem *normWindItem = new BMenuItem("Normal Window", winNormFeel);
1149                                 normWindItem->SetMarked(videoWindow->Feel() == B_NORMAL_WINDOW_FEEL);
1150                                 menu->AddItem(normWindItem);
1151                                 
1152                                 BMessage *winFloatFeel = new BMessage(WINDOW_FEEL);
1153                                 winFloatFeel->AddInt32("WinFeel", (int32)B_FLOATING_APP_WINDOW_FEEL);
1154                                 BMenuItem *onTopWindItem = new BMenuItem("App Top", winFloatFeel);
1155                                 onTopWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_APP_WINDOW_FEEL);
1156                                 menu->AddItem(onTopWindItem);
1157                                 
1158                                 BMessage *winAllFeel = new BMessage(WINDOW_FEEL);
1159                                 winAllFeel->AddInt32("WinFeel", (int32)B_FLOATING_ALL_WINDOW_FEEL);
1160                                 BMenuItem *allSpacesWindItem = new BMenuItem("On Top All Workspaces", winAllFeel);
1161                                 allSpacesWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL);
1162                                 menu->AddItem(allSpacesWindItem);*/
1163
1164                                 BMessage *windowFeelMsg = new BMessage( WINDOW_FEEL );
1165                                 bool onTop = videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL;
1166                                 window_feel feel = onTop ? B_NORMAL_WINDOW_FEEL : B_FLOATING_ALL_WINDOW_FEEL;
1167                                 windowFeelMsg->AddInt32( "WinFeel", (int32)feel );
1168                                 BMenuItem *windowFeelItem = new BMenuItem( _("Stay On Top"), windowFeelMsg );
1169                                 windowFeelItem->SetMarked( onTop );
1170                                 menu->AddItem( windowFeelItem );
1171
1172                                 menu->AddSeparatorItem();
1173
1174                                 BMenuItem* screenShotItem = new BMenuItem( _("Take Screen Shot"),
1175                                                                                                                    new BMessage( SCREEN_SHOT ) );
1176                                 menu->AddItem( screenShotItem );
1177
1178                                 menu->SetTargetForItems( this );
1179                                 ConvertToScreen( &where );
1180                                 menu->Go( where, true, false, true );
1181                 }
1182                 }
1183         }
1184         fLastMouseMovedTime = system_time();
1185         fCursorHidden = false;
1186 }
1187
1188 /*****************************************************************************
1189  * VLCVIew::MouseUp
1190  *****************************************************************************/
1191 void
1192 VLCView::MouseUp( BPoint where )
1193 {
1194     vlc_value_t val;
1195     val.b_bool = VLC_TRUE;
1196     var_Set( p_vout, "mouse-clicked", val );
1197 }
1198
1199 /*****************************************************************************
1200  * VLCVIew::MouseMoved
1201  *****************************************************************************/
1202 void
1203 VLCView::MouseMoved(BPoint point, uint32 transit, const BMessage* dragMessage)
1204 {
1205         fLastMouseMovedTime = system_time();
1206         fCursorHidden = false;
1207         fCursorInside = (transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW);
1208         /* DVD navigation */
1209         unsigned int i_width, i_height, i_x, i_y;
1210     vout_PlacePicture( p_vout, (unsigned int)Bounds().Width(),
1211                        (unsigned int)Bounds().Height(),
1212                        &i_x, &i_y, &i_width, &i_height );
1213         vlc_value_t val;
1214         val.i_int = ( (int)point.x - i_x ) * p_vout->render.i_width / i_width;
1215         var_Set( p_vout, "mouse-x", val );
1216         val.i_int = ( (int)point.y - i_y ) * p_vout->render.i_height / i_height;
1217         var_Set( p_vout, "mouse-y", val );
1218         val.b_bool = VLC_TRUE;
1219     var_Set( p_vout, "mouse-moved", val );
1220 }
1221
1222 /*****************************************************************************
1223  * VLCVIew::Pulse
1224  *****************************************************************************/
1225 void 
1226 VLCView::Pulse()
1227 {
1228         // We are getting the pulse messages no matter if the mouse is over
1229         // this view. If we are in full screen mode, we want to hide the cursor
1230         // even if it is not.
1231         VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
1232         if (!fCursorHidden)
1233         {
1234                 if (fCursorInside
1235                         && system_time() - fLastMouseMovedTime > MOUSE_IDLE_TIMEOUT)
1236                 {
1237                         be_app->ObscureCursor();
1238                         fCursorHidden = true;
1239                         
1240                         // hide the interface window as well if full screen
1241                         if (videoWindow && videoWindow->IsFullScreen())
1242                                 videoWindow->SetInterfaceShowing(false);
1243                 }
1244         }
1245
1246     // Workaround to disable the screensaver in full screen:
1247     // we simulate an activity every 29 seconds 
1248         if( videoWindow && videoWindow->IsFullScreen() &&
1249             system_time() - fLastMouseMovedTime > 29000000 )
1250         {
1251             BPoint where;
1252                 uint32 buttons;
1253                 GetMouse(&where, &buttons, false);
1254                 ConvertToScreen(&where);
1255                 set_mouse_position((int32) where.x, (int32) where.y);
1256         }
1257 }
1258
1259 /*****************************************************************************
1260  * VLCVIew::KeyDown
1261  *****************************************************************************/
1262 void
1263 VLCView::KeyDown(const char *bytes, int32 numBytes)
1264 {
1265     VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
1266     BWindow* interfaceWindow = get_interface_window();
1267         if (videoWindow && numBytes > 0) {
1268                 uint32 mods = modifiers();
1269                 switch (*bytes) {
1270                         case B_TAB:
1271                         case 'f':
1272                         case 'F':
1273                                 // toggle window and full screen mode
1274                                 // not passing on the tab key to the default KeyDown()
1275                                 // implementation also avoids loosing the keyboard focus
1276                                 videoWindow->PostMessage(TOGGLE_FULL_SCREEN);
1277                                 break;
1278                         case B_ESCAPE:
1279                                 // go back to window mode
1280                                 if (videoWindow->IsFullScreen())
1281                                         videoWindow->PostMessage(TOGGLE_FULL_SCREEN);
1282                                 break;
1283                         case B_SPACE:
1284                                 // toggle playback
1285                                 if (interfaceWindow)
1286                                         interfaceWindow->PostMessage(PAUSE_PLAYBACK);
1287                                 break;
1288                         case B_RIGHT_ARROW:
1289                                 if (interfaceWindow)
1290                                 {
1291                                         if (mods & B_SHIFT_KEY)
1292                                                 // next title
1293                                                 interfaceWindow->PostMessage(NEXT_TITLE);
1294                                         else
1295                                                 // next chapter
1296                                                 interfaceWindow->PostMessage(NEXT_CHAPTER);
1297                                 }
1298                                 break;
1299                         case B_LEFT_ARROW:
1300                                 if (interfaceWindow)
1301                                 {
1302                                         if (mods & B_SHIFT_KEY)
1303                                                 // previous title
1304                                                 interfaceWindow->PostMessage(PREV_TITLE);
1305                                         else
1306                                                 // previous chapter
1307                                                 interfaceWindow->PostMessage(PREV_CHAPTER);
1308                                 }
1309                                 break;
1310                         case B_UP_ARROW:
1311                                 // previous file in playlist
1312                                 interfaceWindow->PostMessage(PREV_FILE);
1313                                 break;
1314                         case B_DOWN_ARROW:
1315                                 // next file in playlist
1316                                 interfaceWindow->PostMessage(NEXT_FILE);
1317                                 break;
1318                         case B_PRINT_KEY:
1319                         case 's':
1320                         case 'S':
1321                                 videoWindow->PostMessage( SCREEN_SHOT );
1322                                 break;
1323                         default:
1324                                 BView::KeyDown(bytes, numBytes);
1325                                 break;
1326                 }
1327         }
1328 }
1329
1330 /*****************************************************************************
1331  * VLCVIew::Draw
1332  *****************************************************************************/
1333 void
1334 VLCView::Draw(BRect updateRect) 
1335 {
1336         VideoWindow* window = dynamic_cast<VideoWindow*>( Window() );
1337         if ( window && window->mode == BITMAP )
1338                 FillRect( updateRect );
1339 }
1340
1341 /*****************************************************************************
1342  * Local prototypes
1343  *****************************************************************************/
1344 static int  Init       ( vout_thread_t * );
1345 static void End        ( vout_thread_t * );
1346 // static int  Manage     ( vout_thread_t * );
1347 static void Display    ( vout_thread_t *, picture_t * );
1348
1349 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
1350 static void BeosCloseDisplay( vout_thread_t *p_vout );
1351
1352 /*****************************************************************************
1353  * OpenVideo: allocates BeOS video thread output method
1354  *****************************************************************************
1355  * This function allocates and initializes a BeOS vout method.
1356  *****************************************************************************/
1357 int E_(OpenVideo) ( vlc_object_t *p_this )
1358 {
1359     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1360
1361     /* Allocate structure */
1362     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
1363     if( p_vout->p_sys == NULL )
1364     {
1365         msg_Err( p_vout, "out of memory" );
1366         return( 1 );
1367     }
1368     p_vout->p_sys->i_width = p_vout->render.i_width;
1369     p_vout->p_sys->i_height = p_vout->render.i_height;
1370     p_vout->p_sys->source_chroma = p_vout->render.i_chroma;
1371
1372     p_vout->pf_init = Init;
1373     p_vout->pf_end = End;
1374     p_vout->pf_manage = NULL;
1375     p_vout->pf_render = NULL;
1376     p_vout->pf_display = Display;
1377
1378     return( 0 );
1379 }
1380
1381 /*****************************************************************************
1382  * Init: initialize BeOS video thread output method
1383  *****************************************************************************/
1384 int Init( vout_thread_t *p_vout )
1385 {
1386     int i_index;
1387     picture_t *p_pic;
1388
1389     I_OUTPUTPICTURES = 0;
1390
1391     /* Open and initialize device */
1392     if( BeosOpenDisplay( p_vout ) )
1393     {
1394         msg_Err(p_vout, "vout error: can't open display");
1395         return 0;
1396     }
1397     p_vout->output.i_width  = p_vout->render.i_width;
1398     p_vout->output.i_height = p_vout->render.i_height;
1399
1400     /* Assume we have square pixels */
1401     p_vout->output.i_aspect = p_vout->p_sys->i_width
1402                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
1403     p_vout->output.i_chroma = colspace[p_vout->p_sys->p_window->colspace_index].chroma;
1404     p_vout->p_sys->i_index = 0;
1405
1406     p_vout->b_direct = 1;
1407
1408     p_vout->output.i_rmask  = 0x00ff0000;
1409     p_vout->output.i_gmask  = 0x0000ff00;
1410     p_vout->output.i_bmask  = 0x000000ff;
1411
1412     for (int buffer_index = 0 ; buffer_index < 3; buffer_index++)
1413     {
1414        p_pic = NULL;
1415        /* Find an empty picture slot */
1416        for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
1417        {
1418            p_pic = NULL;
1419            if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
1420            {
1421                p_pic = p_vout->p_picture + i_index;
1422                break;
1423            }
1424        }
1425
1426        if( p_pic == NULL )
1427        {
1428            return 0;
1429        }
1430        p_pic->p->p_pixels = (u8*)p_vout->p_sys->p_window->bitmap[buffer_index]->Bits();
1431        p_pic->p->i_lines = p_vout->p_sys->i_height;
1432
1433        p_pic->p->i_pixel_pitch = colspace[p_vout->p_sys->p_window->colspace_index].pixel_bytes;
1434        p_pic->i_planes = colspace[p_vout->p_sys->p_window->colspace_index].planes;
1435        p_pic->p->i_pitch = p_vout->p_sys->p_window->bitmap[buffer_index]->BytesPerRow(); 
1436        p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch * ( p_vout->p_sys->p_window->bitmap[buffer_index]->Bounds().IntegerWidth() + 1 );
1437
1438        p_pic->i_status = DESTROYED_PICTURE;
1439        p_pic->i_type   = DIRECT_PICTURE;
1440  
1441        PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
1442
1443        I_OUTPUTPICTURES++;
1444     }
1445
1446     return( 0 );
1447 }
1448
1449 /*****************************************************************************
1450  * End: terminate BeOS video thread output method
1451  *****************************************************************************/
1452 void End( vout_thread_t *p_vout )
1453 {
1454     BeosCloseDisplay( p_vout );
1455 }
1456
1457 /*****************************************************************************
1458  * CloseVideo: destroy BeOS video thread output method
1459  *****************************************************************************
1460  * Terminate an output method created by DummyCreateOutputMethod
1461  *****************************************************************************/
1462 void E_(CloseVideo) ( vlc_object_t *p_this )
1463 {
1464     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1465
1466     free( p_vout->p_sys );
1467 }
1468
1469 /*****************************************************************************
1470  * Display: displays previously rendered output
1471  *****************************************************************************
1472  * This function send the currently rendered image to BeOS image, waits until
1473  * it is displayed and switch the two rendering buffers, preparing next frame.
1474  *****************************************************************************/
1475 void Display( vout_thread_t *p_vout, picture_t *p_pic )
1476 {
1477     VideoWindow * p_win = p_vout->p_sys->p_window;
1478
1479     /* draw buffer if required */    
1480     if (!p_win->teardownwindow)
1481     { 
1482        p_win->drawBuffer(p_vout->p_sys->i_index);
1483     }
1484     /* change buffer */
1485     p_vout->p_sys->i_index = ++p_vout->p_sys->i_index % 3;
1486     p_pic->p->p_pixels = (u8*)p_vout->p_sys->p_window->bitmap[p_vout->p_sys->i_index]->Bits();
1487 }
1488
1489 /* following functions are local */
1490
1491 /*****************************************************************************
1492  * BeosOpenDisplay: open and initialize BeOS device
1493  *****************************************************************************/
1494 static int BeosOpenDisplay( vout_thread_t *p_vout )
1495
1496
1497     p_vout->p_sys->p_window = new VideoWindow( p_vout->p_sys->i_width - 1,
1498                                                p_vout->p_sys->i_height - 1,
1499                                                BRect( 20, 50,
1500                                                       20 + p_vout->i_window_width - 1, 
1501                                                       50 + p_vout->i_window_height - 1 ),
1502                                                p_vout );
1503     if( p_vout->p_sys->p_window == NULL )
1504     {
1505         msg_Err( p_vout, "cannot allocate VideoWindow" );
1506         return( 1 );
1507     }
1508     else
1509     {
1510         p_vout->p_sys->p_window->Show();
1511     }
1512     
1513     return( 0 );
1514 }
1515
1516 /*****************************************************************************
1517  * BeosDisplay: close and reset BeOS device
1518  *****************************************************************************
1519  * Returns all resources allocated by BeosOpenDisplay and restore the original
1520  * state of the device.
1521  *****************************************************************************/
1522 static void BeosCloseDisplay( vout_thread_t *p_vout )
1523 {    
1524     VideoWindow * p_win = p_vout->p_sys->p_window;
1525     /* Destroy the video window */
1526     if( p_win != NULL && !p_win->teardownwindow)
1527     {
1528         p_win->Lock();
1529         p_win->teardownwindow = true;
1530         p_win->Hide();
1531         p_win->Quit();
1532     }
1533     p_win = NULL;
1534 }