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