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