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