VideoOutput.cpp 46.4 KB
Newer Older
1
/*****************************************************************************
2
 * vout_beos.cpp: beos video output display method
3
4
 *****************************************************************************
 * Copyright (C) 2000, 2001 VideoLAN
5
 * $Id: VideoOutput.cpp,v 1.14 2003/04/18 16:10:28 titer Exp $
6
7
8
9
10
 *
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Tony Castley <tcastley@mail.powerup.com.au>
 *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11
 *          Stephan Aßmus <stippi@yellowbites.com>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>
#include <string.h>                                            /* strerror() */
35

36
#include <Application.h>
37
#include <BitmapStream.h>
38
#include <Bitmap.h>
Stephan Assmus's avatar
   
Stephan Assmus committed
39
#include <Directory.h>
40
41
42
43
44
45
#include <DirectWindow.h>
#include <File.h>
#include <InterfaceKit.h>
#include <NodeInfo.h>
#include <String.h>
#include <TranslatorRoster.h>
46

47
/* VLC headers */
48
49
50
51
#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <vlc/vout.h>

52
#include "InterfaceWindow.h"	// for load/save_settings()
53
54
55
#include "DrawingTidbits.h"
#include "MsgVals.h"

56
57
#include "VideoWindow.h"

58
59
60
61
62
63
64
65
66
67
68
69
70
/*****************************************************************************
 * vout_sys_t: BeOS video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the BeOS specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    VideoWindow *  p_window;

    s32 i_width;
    s32 i_height;

71
//    u8 *pp_buffer[3];
72
73
    u32 source_chroma;
    int i_index;
74

75
76
};

77
78
79
80
81
#define MOUSE_IDLE_TIMEOUT 2000000	// two seconds
#define MIN_AUTO_VSYNC_REFRESH 61	// Hz
#define DEFAULT_SCREEN_SHOT_FORMAT 'PNG '
#define DEFAULT_SCREEN_SHOT_PATH "/boot/home/vlc screenshot"

82
83
84
/*****************************************************************************
 * beos_GetAppWindow : retrieve a BWindow pointer from the window name
 *****************************************************************************/
85
86
BWindow*
beos_GetAppWindow(char *name)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{
    int32       index;
    BWindow     *window;
    
    for (index = 0 ; ; index++)
    {
        window = be_app->WindowAt(index);
        if (window == NULL)
            break;
        if (window->LockWithTimeout(20000) == B_OK)
        {
            if (strcmp(window->Name(), name) == 0)
            {
                window->Unlock();
                break;
            }
            window->Unlock();
        }
    }
    return window; 
}

109
110
111
112
113
114
/*****************************************************************************
 * get_interface_window
 *****************************************************************************/
BWindow*
get_interface_window()
{
115
	return beos_GetAppWindow( "VLC " PACKAGE_VERSION );
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
}

class BackgroundView : public BView
{
 public:
							BackgroundView(BRect frame, VLCView* view)
							: BView(frame, "background",
									B_FOLLOW_ALL, B_FULL_UPDATE_ON_RESIZE),
							  fVideoView(view)
							{
								SetViewColor(kBlack);
							}
	virtual					~BackgroundView() {}

	virtual	void			MouseDown(BPoint where)
							{
								// convert coordinates
								where = fVideoView->ConvertFromParent(where);
								// let him handle it
								fVideoView->MouseDown(where);
							}
	virtual	void			MouseMoved(BPoint where, uint32 transit,
									   const BMessage* dragMessage)
							{
								// convert coordinates
								where = fVideoView->ConvertFromParent(where);
								// let him handle it
								fVideoView->MouseMoved(where, transit, dragMessage);
								// notice: It might look like transit should be
								// B_OUTSIDE_VIEW regardless, but leave it like this,
								// otherwise, unwanted things will happen!
							}

 private:
	VLCView*				fVideoView;
};

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

/*****************************************************************************
 * VideoSettings constructor and destructor
 *****************************************************************************/
VideoSettings::VideoSettings()
	: fVideoSize( SIZE_100 ),
	  fFlags( FLAG_CORRECT_RATIO ),
	  fSettings( new BMessage( 'sett' ) )
{
	// read settings from disk
	status_t ret = load_settings( fSettings, "video_settings", "VideoLAN Client" );
	if ( ret == B_OK )
	{
		uint32 flags;
		if ( fSettings->FindInt32( "flags", (int32*)&flags ) == B_OK )
			SetFlags( flags );
		uint32 size;
		if ( fSettings->FindInt32( "video size", (int32*)&size ) == B_OK )
			SetVideoSize( size );
	}
	else
	{
		fprintf( stderr, "error loading video settings: %s\n", strerror( ret ) );
		
		// figure out if we should use vertical sync by default
		BScreen screen(B_MAIN_SCREEN_ID);
		if (screen.IsValid())
		{
			display_mode mode; 
			screen.GetMode(&mode); 
			float refresh = (mode.timing.pixel_clock * 1000)
							/ ((mode.timing.h_total)* (mode.timing.v_total)); 
			if (refresh < MIN_AUTO_VSYNC_REFRESH)
				AddFlags(FLAG_SYNC_RETRACE);
		}
	}
}

VideoSettings::VideoSettings( const VideoSettings& clone )
	: fVideoSize( clone.VideoSize() ),
	  fFlags( clone.Flags() ),
	  fSettings( NULL )
{
}


VideoSettings::~VideoSettings()
{
	if ( fSettings )
	{
		// we are the default settings
		// and write our settings to disk
		if (fSettings->ReplaceInt32( "video size", VideoSize() ) != B_OK)
			fSettings->AddInt32( "video size", VideoSize() );
		if (fSettings->ReplaceInt32( "flags", Flags() ) != B_OK)
			fSettings->AddInt32( "flags", Flags() );

		status_t ret = save_settings( fSettings, "video_settings", "VideoLAN Client" );
		if ( ret != B_OK )
			fprintf( stderr, "error saving video settings: %s\n", strerror( ret ) );
		delete fSettings;
	}
	else
	{
		// we are just a clone of the default settings
		fDefaultSettings.SetVideoSize( VideoSize() );
		fDefaultSettings.SetFlags( Flags() );
	}
}

/*****************************************************************************
 * VideoSettings::DefaultSettings
 *****************************************************************************/
VideoSettings*
VideoSettings::DefaultSettings()
{
	return &fDefaultSettings;
}

/*****************************************************************************
 * VideoSettings::SetVideoSize
 *****************************************************************************/
void
VideoSettings::SetVideoSize( uint32 mode )
{
	fVideoSize = mode;
}

// static variable initialization
VideoSettings
VideoSettings::fDefaultSettings;


246
247
248
/*****************************************************************************
 * VideoWindow constructor and destructor
 *****************************************************************************/
249
250
VideoWindow::VideoWindow(int v_width, int v_height, BRect frame,
                         vout_thread_t *p_videoout)
251
252
253
	: BWindow(frame, NULL, B_TITLED_WINDOW, B_NOT_CLOSABLE | B_NOT_MINIMIZABLE),
	  i_width(frame.IntegerWidth()),
	  i_height(frame.IntegerHeight()),
254
	  winSize(frame),
255
256
257
258
259
260
	  i_buffer(0),
	  teardownwindow(false),
	  fTrueWidth(v_width),
	  fTrueHeight(v_height),
	  fCachedFeel(B_NORMAL_WINDOW_FEEL),
	  fInterfaceShowing(false),
261
262
	  fInitStatus(B_ERROR),
	  fSettings(new VideoSettings(*VideoSettings::DefaultSettings()))
263
{
264
265
    p_vout = p_videoout;
    
266
    // create the view to do the display
267
    view = new VLCView( Bounds(), p_vout );
268

269
270
271
272
	// create background view
    BView *mainView =  new BackgroundView( Bounds(), view );
    AddChild(mainView);
    mainView->AddChild(view);
273

274
275
276
277
	// allocate bitmap buffers
	for (int32 i = 0; i < 3; i++)
		bitmap[i] = NULL;
	fInitStatus = _AllocateBuffers(v_width, v_height, &mode);
278

279
280
	// make sure we layout the view correctly
    FrameResized(i_width, i_height);
281

282
    if (fInitStatus >= B_OK && mode == OVERLAY)
283
284
285
286
    {
       overlay_restrictions r;

       bitmap[1]->GetOverlayRestrictions(&r);
287
288
       SetSizeLimits((i_width * r.min_width_scale), i_width * r.max_width_scale,
                     (i_height * r.min_height_scale), i_height * r.max_height_scale);
289
    }
290
    
291
292
293
294
295
	// vlc settings override settings from disk
	if (config_GetInt(p_vout, "fullscreen"))
		fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);

    _SetToSettings();
296
297
298
299
}

VideoWindow::~VideoWindow()
{
300
301
    int32 result;

302
    teardownwindow = true;
303
304
    wait_for_thread(fDrawThreadID, &result);
    _FreeBuffers();
305
	delete fSettings;
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
}

/*****************************************************************************
 * VideoWindow::MessageReceived
 *****************************************************************************/
void
VideoWindow::MessageReceived( BMessage *p_message )
{
	switch( p_message->what )
	{
		case TOGGLE_FULL_SCREEN:
			BWindow::Zoom();
			break;
		case RESIZE_50:
		case RESIZE_100:
		case RESIZE_200:
322
			if (IsFullScreen())
323
324
325
326
				BWindow::Zoom();
			_SetVideoSize(p_message->what);
			break;
		case VERT_SYNC:
327
			SetSyncToRetrace(!IsSyncedToRetrace());
328
329
330
331
332
333
334
335
			break;
		case WINDOW_FEEL:
			{
				window_feel winFeel;
				if (p_message->FindInt32("WinFeel", (int32*)&winFeel) == B_OK)
				{
					SetFeel(winFeel);
					fCachedFeel = winFeel;
336
337
338
339
					if (winFeel == B_FLOATING_ALL_WINDOW_FEEL)
						fSettings->AddFlags(VideoSettings::FLAG_ON_TOP_ALL);
					else
						fSettings->ClearFlags(VideoSettings::FLAG_ON_TOP_ALL);
340
341
342
343
				}
			}
			break;
		case ASPECT_CORRECT:
344
			SetCorrectAspectRatio(!CorrectAspectRatio());
345
346
347
348
349
350
351
352
353
354
355
356
357
			break;
		case SCREEN_SHOT:
			// save a screen shot
			if ( BBitmap* current = bitmap[i_buffer] )
			{
// the following line might be tempting, but does not work for some overlay bitmaps!!!
//				BBitmap* temp = new BBitmap( current );
// so we clone the bitmap ourselves
// however, we need to take care of potentially different padding!
// memcpy() is slow when reading from grafix memory, but what the heck...
				BBitmap* temp = new BBitmap( current->Bounds(), current->ColorSpace() );
				if ( temp && temp->IsValid() )
				{
Eric Petit's avatar
Eric Petit committed
358
					int32 height = (int32)current->Bounds().Height();
359
360
361
362
363
364
365
366
367
368
369
					uint8* dst = (uint8*)temp->Bits();
					uint8* src = (uint8*)current->Bits();
					int32 dstBpr = temp->BytesPerRow();
					int32 srcBpr = current->BytesPerRow();
					int32 validBytes = dstBpr > srcBpr ? srcBpr : dstBpr;
					for ( int32 y = 0; y < height; y++ )
					{
						memcpy( dst, src, validBytes );
						dst += dstBpr;
						src += srcBpr;
					}
Stephan Assmus's avatar
   
Stephan Assmus committed
370
371
372
373
374
					char* path = config_GetPsz( p_vout, "beos-screenshot-path" );
					if ( !path )
						path = strdup( DEFAULT_SCREEN_SHOT_PATH );
					int32 format = config_GetInt( p_vout, "beos-screenshot-format" );
					_SaveScreenShot( temp, path, format );
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
				}
				else
				{
					delete temp;
				}
			}
			break;
		default:
			BWindow::MessageReceived( p_message );
			break;
	}
}

/*****************************************************************************
 * VideoWindow::Zoom
 *****************************************************************************/
void
VideoWindow::Zoom(BPoint origin, float width, float height )
{
394
	ToggleFullScreen();
395
396
}

397
398
399
400
401
402
/*****************************************************************************
 * VideoWindow::FrameMoved
 *****************************************************************************/
void
VideoWindow::FrameMoved(BPoint origin) 
{
403
404
405
	if (IsFullScreen())
		return ;
	winSize = Frame();
406
407
408
409
410
411
412
}

/*****************************************************************************
 * VideoWindow::FrameResized
 *****************************************************************************/
void
VideoWindow::FrameResized( float width, float height )
413
{
414
415
	int32 useWidth = CorrectAspectRatio() ? i_width : fTrueWidth;
	int32 useHeight = CorrectAspectRatio() ? i_height : fTrueHeight;
416
417
418
419
420
421
    float out_width, out_height;
    float out_left, out_top;
    float width_scale = width / useWidth;
    float height_scale = height / useHeight;

    if (width_scale <= height_scale)
422
    {
423
424
425
426
        out_width = (useWidth * width_scale);
        out_height = (useHeight * width_scale);
        out_left = 0; 
        out_top = (height - out_height) / 2;
427
    }
428
429
430
431
432
433
434
435
436
437
    else   /* if the height is proportionally smaller */
    {
        out_width = (useWidth * height_scale);
        out_height = (useHeight * height_scale);
        out_top = 0;
        out_left = (width - out_width) / 2;
    }
    view->MoveTo(out_left,out_top);
    view->ResizeTo(out_width, out_height);

438
	if (!IsFullScreen())
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        winSize = Frame();
}

/*****************************************************************************
 * VideoWindow::ScreenChanged
 *****************************************************************************/
void
VideoWindow::ScreenChanged(BRect frame, color_space format)
{
	BScreen screen(this);
	display_mode mode; 
	screen.GetMode(&mode); 
	float refresh = (mode.timing.pixel_clock * 1000)
					/ ((mode.timing.h_total) * (mode.timing.v_total)); 
453
	SetSyncToRetrace(refresh < MIN_AUTO_VSYNC_REFRESH);
454
455
}

456
457
458
459
460
461
462
463
464
465
466
467
468
/*****************************************************************************
 * VideoWindow::Activate
 *****************************************************************************/
void
VideoWindow::WindowActivated(bool active)
{
}

/*****************************************************************************
 * VideoWindow::drawBuffer
 *****************************************************************************/
void
VideoWindow::drawBuffer(int bufferIndex)
469
470
471
472
{
    i_buffer = bufferIndex;

    // sync to the screen if required
473
    if (IsSyncedToRetrace())
474
    {
475
476
        BScreen screen(this);
        screen.WaitForRetrace(22000);
477
    }
478
    if (fInitStatus >= B_OK && LockLooper())
479
480
481
482
483
484
485
486
487
    {
       // switch the overlay bitmap
       if (mode == OVERLAY)
       {
          rgb_color key;
          view->SetViewOverlay(bitmap[i_buffer], 
                            bitmap[i_buffer]->Bounds() ,
                            view->Bounds(),
                            &key, B_FOLLOW_ALL,
488
							B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|
489
490
491
492
493
494
		                    B_OVERLAY_TRANSFER_CHANNEL);
		   view->SetViewColor(key);
	   }
       else
       {
         // switch the bitmap
495
         view->DrawBitmap(bitmap[i_buffer], view->Bounds() );
496
497
498
499
500
       }
       UnlockLooper();
    }
}

501
502
503
504
505
/*****************************************************************************
 * VideoWindow::SetInterfaceShowing
 *****************************************************************************/
void
VideoWindow::ToggleInterfaceShowing()
506
{
507
	SetInterfaceShowing(!fInterfaceShowing);
508
509
}

510
511
512
513
514
/*****************************************************************************
 * VideoWindow::SetInterfaceShowing
 *****************************************************************************/
void
VideoWindow::SetInterfaceShowing(bool showIt)
515
{
516
517
	BWindow* window = get_interface_window();
	if (window)
518
	{
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
		if (showIt)
		{
			if (fCachedFeel != B_NORMAL_WINDOW_FEEL)
				SetFeel(B_NORMAL_WINDOW_FEEL);
			window->Activate(true);
			SendBehind(window);
		}
		else
		{
			SetFeel(fCachedFeel);
			Activate(true);
			window->SendBehind(this);
		}
		fInterfaceShowing = showIt;
	}
534
535
}

536
537
538
539
540
/*****************************************************************************
 * VideoWindow::SetCorrectAspectRatio
 *****************************************************************************/
void
VideoWindow::SetCorrectAspectRatio(bool doIt)
541
{
542
	if (CorrectAspectRatio() != doIt)
543
	{
544
545
546
547
		if (doIt)
			fSettings->AddFlags(VideoSettings::FLAG_CORRECT_RATIO);
		else
			fSettings->ClearFlags(VideoSettings::FLAG_CORRECT_RATIO);
548
549
		FrameResized(Bounds().Width(), Bounds().Height());
	}
550
551
}

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*****************************************************************************
 * VideoWindow::CorrectAspectRatio
 *****************************************************************************/
bool
VideoWindow::CorrectAspectRatio() const
{
	return fSettings->HasFlags(VideoSettings::FLAG_CORRECT_RATIO);
}

/*****************************************************************************
 * VideoWindow::ToggleFullScreen
 *****************************************************************************/
void
VideoWindow::ToggleFullScreen()
{
	SetFullScreen(!IsFullScreen());
}

/*****************************************************************************
 * VideoWindow::SetFullScreen
 *****************************************************************************/
void
VideoWindow::SetFullScreen(bool doIt)
{
	if (doIt)
	{
		BScreen screen(this);
		BRect rect = screen.Frame();
		Activate();
		MoveTo(0.0, 0.0);
		ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
		be_app->ObscureCursor();
		fInterfaceShowing = false;
		fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
	}
	else
	{
		MoveTo(winSize.left, winSize.top);
		ResizeTo(winSize.IntegerWidth(), winSize.IntegerHeight());
		be_app->ShowCursor();
		fInterfaceShowing = true;
		fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
	}
}

/*****************************************************************************
 * VideoWindow::IsFullScreen
 *****************************************************************************/
bool
VideoWindow::IsFullScreen() const
{
	return fSettings->HasFlags(VideoSettings::FLAG_FULL_SCREEN);
}

/*****************************************************************************
 * VideoWindow::SetSyncToRetrace
 *****************************************************************************/
void
VideoWindow::SetSyncToRetrace(bool doIt)
{
	if (doIt)
		fSettings->AddFlags(VideoSettings::FLAG_SYNC_RETRACE);
	else
		fSettings->ClearFlags(VideoSettings::FLAG_SYNC_RETRACE);
}

/*****************************************************************************
 * VideoWindow::IsSyncedToRetrace
 *****************************************************************************/
bool
VideoWindow::IsSyncedToRetrace() const
{
	return fSettings->HasFlags(VideoSettings::FLAG_SYNC_RETRACE);
}


628
629
630
631
632
/*****************************************************************************
 * VideoWindow::_AllocateBuffers
 *****************************************************************************/
status_t
VideoWindow::_AllocateBuffers(int width, int height, int* mode)
633
{
634
635
636
637
638
639
640
	// clear any old buffers
	_FreeBuffers();
	// set default mode
	*mode = BITMAP;

	BRect bitmapFrame( 0, 0, width, height );
	// read from config, if we are supposed to use overlay at all
641
    int noOverlay = !config_GetInt( p_vout, "overlay" );
642
	// test for overlay capability
643
644
645
    for (int i = 0; i < COLOR_COUNT; i++)
    {
        if (noOverlay) break;
646
        bitmap[0] = new BBitmap ( bitmapFrame, 
647
648
                                  B_BITMAP_WILL_OVERLAY |
                                  B_BITMAP_RESERVE_OVERLAY_CHANNEL,
649
650
651
652
653
654
                                  colspace[i].colspace);

        if(bitmap[0] && bitmap[0]->InitCheck() == B_OK) 
        {
            colspace_index = i;

655
            bitmap[1] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
656
                                     colspace[colspace_index].colspace);
657
            bitmap[2] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
658
659
660
                                     colspace[colspace_index].colspace);
            if ( (bitmap[2] && bitmap[2]->InitCheck() == B_OK) )
            {
661
               *mode = OVERLAY;
662
663
664
665
666
667
668
               rgb_color key;
               view->SetViewOverlay(bitmap[0], 
                                    bitmap[0]->Bounds() ,
                                    view->Bounds(),
                                    &key, B_FOLLOW_ALL,
		                            B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
		       view->SetViewColor(key);
669
               SetTitle("VLC " PACKAGE_VERSION " (Overlay)");
670
671
672
673
               break;
            }
            else
            {
674
675
               _FreeBuffers();
               *mode = BITMAP; // might want to try again with normal bitmaps
676
677
678
679
680
681
            }
        }
        else
            delete bitmap[0];
	}

682
    if (*mode == BITMAP)
683
	{
684
        // fallback to RGB
685
        colspace_index = DEFAULT_COL;	// B_RGB32
686
        SetTitle( "VLC " PACKAGE_VERSION " (Bitmap)" );
687
688
689
690
691
692
693
694
695
696
697
698
        bitmap[0] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
        bitmap[1] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
        bitmap[2] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
    }
    // see if everything went well
    status_t status = B_ERROR;
    for (int32 i = 0; i < 3; i++)
    {
    	if (bitmap[i])
    		status = bitmap[i]->InitCheck();
		if (status < B_OK)
			break;
699
    }
700
701
702
703
704
705
706
    if (status >= B_OK)
    {
	    // clear bitmaps to black
	    for (int32 i = 0; i < 3; i++)
	    	_BlankBitmap(bitmap[i]);
    }
    return status;
707
708
709
}

/*****************************************************************************
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
 * VideoWindow::_FreeBuffers
 *****************************************************************************/
void
VideoWindow::_FreeBuffers()
{
	delete bitmap[0];
	bitmap[0] = NULL;
	delete bitmap[1];
	bitmap[1] = NULL;
	delete bitmap[2];
	bitmap[2] = NULL;
	fInitStatus = B_ERROR;
}

/*****************************************************************************
 * VideoWindow::_BlankBitmap
 *****************************************************************************/
void
VideoWindow::_BlankBitmap(BBitmap* bitmap) const
{
	// no error checking (we do that earlier on and since it's a private function...

	// YCbCr: 
	// Loss/Saturation points are Y 16-235 (absoulte); Cb/Cr 16-240 (center 128)

	// YUV: 
	// Extrema points are Y 0 - 207 (absolute) U -91 - 91 (offset 128) V -127 - 127 (offset 128)

	// we only handle weird colorspaces with special care
	switch (bitmap->ColorSpace()) {
		case B_YCbCr422: {
			// Y0[7:0]  Cb0[7:0]  Y1[7:0]  Cr0[7:0]  Y2[7:0]  Cb2[7:0]  Y3[7:0]  Cr2[7:0]
			int32 height = bitmap->Bounds().IntegerHeight() + 1;
			uint8* bits = (uint8*)bitmap->Bits();
			int32 bpr = bitmap->BytesPerRow();
			for (int32 y = 0; y < height; y++) {
				// handle 2 bytes at a time
				for (int32 i = 0; i < bpr; i += 2) {
					// offset into line
					bits[i] = 16;
					bits[i + 1] = 128;
				}
				// next line
				bits += bpr;
			}
			break;
		}
		case B_YCbCr420: {
// TODO: untested!!
			// Non-interlaced only, Cb0  Y0  Y1  Cb2 Y2  Y3  on even scan lines ...
			// Cr0  Y0  Y1  Cr2 Y2  Y3  on odd scan lines
			int32 height = bitmap->Bounds().IntegerHeight() + 1;
			uint8* bits = (uint8*)bitmap->Bits();
			int32 bpr = bitmap->BytesPerRow();
			for (int32 y = 0; y < height; y += 1) {
				// handle 3 bytes at a time
				for (int32 i = 0; i < bpr; i += 3) {
					// offset into line
					bits[i] = 128;
					bits[i + 1] = 16;
					bits[i + 2] = 16;
				}
				// next line
				bits += bpr;
			}
			break;
		}
		case B_YUV422: {
// TODO: untested!!
			// U0[7:0]  Y0[7:0]   V0[7:0]  Y1[7:0]  U2[7:0]  Y2[7:0]   V2[7:0]  Y3[7:0]
			int32 height = bitmap->Bounds().IntegerHeight() + 1;
			uint8* bits = (uint8*)bitmap->Bits();
			int32 bpr = bitmap->BytesPerRow();
			for (int32 y = 0; y < height; y += 1) {
				// handle 2 bytes at a time
				for (int32 i = 0; i < bpr; i += 2) {
					// offset into line
					bits[i] = 128;
					bits[i + 1] = 0;
				}
				// next line
				bits += bpr;
			}
			break;
		}
		default:
			memset(bitmap->Bits(), 0, bitmap->BitsLength());
			break;
	}
}

/*****************************************************************************
 * VideoWindow::_SetVideoSize
 *****************************************************************************/
void
VideoWindow::_SetVideoSize(uint32 mode)
{
	// let size depend on aspect correction
808
809
	int32 width = CorrectAspectRatio() ? i_width : fTrueWidth;
	int32 height = CorrectAspectRatio() ? i_height : fTrueHeight;
810
811
812
813
814
815
816
817
818
819
820
821
822
823
	switch (mode)
	{
		case RESIZE_50:
			width /= 2;
			height /= 2;
			break;
		case RESIZE_200:
			width *= 2;
			height *= 2;
			break;
		case RESIZE_100:
		default:
	        break;
	}
824
	fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
825
	ResizeTo(width, height);
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
}

/*****************************************************************************
 * VideoWindow::_SetToSettings
 *****************************************************************************/
void
VideoWindow::_SetToSettings()
{
	// adjust dimensions
	uint32 mode = RESIZE_100;
	switch (fSettings->VideoSize())
	{
		case VideoSettings::SIZE_50:
			mode = RESIZE_50;
			break;
		case VideoSettings::SIZE_200:
			mode = RESIZE_200;
			break;
		case VideoSettings::SIZE_100:
		case VideoSettings::SIZE_OTHER:
		default:
			break;
	}
	bool fullscreen = IsFullScreen();	// remember settings
	_SetVideoSize(mode);				// because this will reset settings
	// the fullscreen status is reflected in the settings,
	// but not yet in the windows state
	if (fullscreen)
		SetFullScreen(true);
	if (fSettings->HasFlags(VideoSettings::FLAG_ON_TOP_ALL))
		fCachedFeel = B_FLOATING_ALL_WINDOW_FEEL;
	else
		fCachedFeel = B_NORMAL_WINDOW_FEEL;
	SetFeel(fCachedFeel);
860
861
862
863
864
865
866
867
868
869
870
871
872
873
}

/*****************************************************************************
 * VideoWindow::_SaveScreenShot
 *****************************************************************************/
void
VideoWindow::_SaveScreenShot( BBitmap* bitmap, char* path,
						  uint32 translatorID ) const
{
	// make the info object from the parameters
	screen_shot_info* info = new screen_shot_info;
	info->bitmap = bitmap;
	info->path = path;
	info->translatorID = translatorID;
874
875
	info->width = CorrectAspectRatio() ? i_width : fTrueWidth;
	info->height = CorrectAspectRatio() ? i_height : fTrueHeight;
876
877
878
879
880
881
882
883
884
885
886
	// spawn a new thread to take care of the actual saving to disk
	thread_id thread = spawn_thread( _save_screen_shot,
									 "screen shot saver",
									 B_LOW_PRIORITY, (void*)info );
	// start thread or do the job ourself if something went wrong
	if ( thread < B_OK || resume_thread( thread ) < B_OK )
		_save_screen_shot( (void*)info );
}

/*****************************************************************************
 * VideoWindow::_save_screen_shot
887
 *****************************************************************************/
888
889
890
891
892
893
894
895
896
897
int32
VideoWindow::_save_screen_shot( void* cookie )
{
	screen_shot_info* info = (screen_shot_info*)cookie;
	if ( info && info->bitmap && info->bitmap->IsValid() && info->path )
	{
		// try to be as quick as possible creating the file (the user might have
		// taken the next screen shot already!)
		// make sure we have a unique name for the screen shot
		BString path( info->path );
Stephan Assmus's avatar
   
Stephan Assmus committed
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
		// create the folder if it doesn't exist
		BString folder( info->path );
		int32 pos = folder.FindLast("/");
		if ( pos > 0 )
		{
			pos++; // leave the last '/' in the string
			if ( pos == path.Length() )
				path << "vlc screenshot";
			else
			{
				int32 removeChars = folder.Length() - pos;	
				folder.Remove( pos, removeChars );
			}
			create_directory( folder.String(), 0777 );
		}
913
914
915
916
917
918
919
920
		BEntry entry( path.String() );
		int32 appendedNumber = 0;
		if ( entry.Exists() && !entry.IsSymLink() )
		{
			// we would clobber an existing entry
			bool foundUniqueName = false;
			appendedNumber = 1;
			while ( !foundUniqueName ) {
Stephan Assmus's avatar
   
Stephan Assmus committed
921
				BString newName( path.String() );
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
				newName << " " << appendedNumber;
				BEntry possiblyClobberedEntry( newName.String() );
				if ( possiblyClobberedEntry.Exists()
					&& !possiblyClobberedEntry.IsSymLink() )
					appendedNumber++;
				else
					foundUniqueName = true;
			}
		}
		if ( appendedNumber > 0 )
			path << " " << appendedNumber;
		// there is still a slight chance to clobber an existing
		// file (if it was created in the "meantime"), but we take it...
		BFile outFile( path.String(),
					   B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE );

		// make colorspace converted copy of bitmap
		BBitmap* converted = new BBitmap( BRect( 0.0, 0.0, info->width, info->height ),
										  B_RGB32 );
		status_t status = convert_bitmap( info->bitmap, converted );
		if ( status == B_OK )
		{
			BTranslatorRoster* roster = BTranslatorRoster::Default();
			uint32 imageFormat = 0;
			translator_id translator = 0;
			bool found = false;

			// find suitable translator
			translator_id* ids = NULL;
			int32 count = 0;
		
			status = roster->GetAllTranslators( &ids, &count );
			if ( status >= B_OK )
			{
				for ( int tix = 0; tix < count; tix++ )
				{ 
					const translation_format *formats = NULL; 
					int32 num_formats = 0; 
					bool ok = false; 
					status = roster->GetInputFormats( ids[tix],
													  &formats, &num_formats );
					if (status >= B_OK)
					{
						for ( int iix = 0; iix < num_formats; iix++ )
						{ 
							if ( formats[iix].type == B_TRANSLATOR_BITMAP )
							{ 
								ok = true; 
								break; 
							}
						}
					}
					if ( !ok )
						continue; 
					status = roster->GetOutputFormats( ids[tix],
													   &formats, &num_formats); 
					if ( status >= B_OK )
					{
						for ( int32 oix = 0; oix < num_formats; oix++ )
						{
				 			if ( formats[oix].type != B_TRANSLATOR_BITMAP )
				 			{
			 					if ( formats[oix].type == info->translatorID )
			 					{
			 						found = true;
				 					imageFormat = formats[oix].type;
				 					translator = ids[tix];
					 				break;
				 				}
				 			}
						}
					}
				}
			}
			delete[] ids;
			if ( found )
			{
				// make bitmap stream
				BBitmapStream outStream( converted );

				status = outFile.InitCheck();
				if (status == B_OK) {
					status = roster->Translate( &outStream, NULL, NULL,
												&outFile, imageFormat );
					if ( status == B_OK )
					{
						BNodeInfo nodeInfo( &outFile );
						if ( nodeInfo.InitCheck() == B_OK )
						{
							translation_format* formats; 
							int32 count;
							status = roster->GetOutputFormats( translator,
															   (const translation_format **) &formats,
															   &count);
							if ( status >= B_OK )
							{
								const char * mime = NULL; 
								for ( int ix = 0; ix < count; ix++ ) {
									if ( formats[ix].type == imageFormat ) {
										mime = formats[ix].MIME;
										break;
									}
								} 
								if ( mime )
									nodeInfo.SetType( mime );
							}
						}
					}
				}
				outStream.DetachBitmap( &converted );
				outFile.Unset();
			}
		}
		delete converted;
	}
	if ( info )
	{
		delete info->bitmap;
Stephan Assmus's avatar
   
Stephan Assmus committed
1040
		free( info->path );
1041
1042
1043
1044
	}
	delete info;
	return B_OK;
}
1045

1046
1047
1048
1049

/*****************************************************************************
 * VLCView::VLCView
 *****************************************************************************/
1050
VLCView::VLCView(BRect bounds, vout_thread_t *p_vout_instance )
1051
1052
1053
1054
1055
	: BView(bounds, "video view", B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED),
	  fLastMouseMovedTime(system_time()),
	  fCursorHidden(false),
	  fCursorInside(false),
	  fIgnoreDoubleClick(false)
1056
{
1057
    p_vout = p_vout_instance;
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
    SetViewColor(B_TRANSPARENT_32_BIT);
}

/*****************************************************************************
 * VLCView::~VLCView
 *****************************************************************************/
VLCView::~VLCView()
{
}

1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
/*****************************************************************************
 * VLCVIew::AttachedToWindow
 *****************************************************************************/
void
VLCView::AttachedToWindow()
{
	// in order to get keyboard events
	MakeFocus(true);
	// periodically check if we want to hide the pointer
	Window()->SetPulseRate(1000000);
}

1080
1081
1082
/*****************************************************************************
 * VLCVIew::MouseDown
 *****************************************************************************/
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
void
VLCView::MouseDown(BPoint where)
{
	VideoWindow* videoWindow = dynamic_cast<VideoWindow*>(Window());
	BMessage* msg = Window()->CurrentMessage();
	int32 clicks;
	uint32 buttons;
	msg->FindInt32("clicks", &clicks);
	msg->FindInt32("buttons", (int32*)&buttons);

	if (videoWindow)
	{
		if (buttons & B_PRIMARY_MOUSE_BUTTON)
		{
			if (clicks == 2 && !fIgnoreDoubleClick)
				Window()->Zoom();
1099
1100
			/* else
				videoWindow->ToggleInterfaceShowing(); */
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
			fIgnoreDoubleClick = false;
		}
	    else
	    {
			if (buttons & B_SECONDARY_MOUSE_BUTTON) 
			{
				// clicks will be 2 next time (if interval short enough)
				// even if the first click and the second
				// have not been made with the same mouse button
				fIgnoreDoubleClick = true;
				// launch popup menu
				BPopUpMenu *menu = new BPopUpMenu("context menu");
				menu->SetRadioMode(false);
				// Resize to 50%
				BMenuItem *halfItem = new BMenuItem("50%", new BMessage(RESIZE_50));
				menu->AddItem(halfItem);
				// Resize to 100%
				BMenuItem *origItem = new BMenuItem("100%", new BMessage(RESIZE_100));
				menu->AddItem(origItem);
				// Resize to 200%
				BMenuItem *doubleItem = new BMenuItem("200%", new BMessage(RESIZE_200));
				menu->AddItem(doubleItem);
				// Toggle FullScreen
				BMenuItem *zoomItem = new BMenuItem("Fullscreen", new BMessage(TOGGLE_FULL_SCREEN));
1125
				zoomItem->SetMarked(videoWindow->IsFullScreen());
1126
1127
1128
1129
1130
1131
				menu->AddItem(zoomItem);
	
				menu->AddSeparatorItem();
	
				// Toggle vSync
				BMenuItem *vsyncItem = new BMenuItem("Vertical Sync", new BMessage(VERT_SYNC));
1132
				vsyncItem->SetMarked(videoWindow->IsSyncedToRetrace());
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
				menu->AddItem(vsyncItem);
				// Correct Aspect Ratio
				BMenuItem *aspectItem = new BMenuItem("Correct Aspect Ratio", new BMessage(ASPECT_CORRECT));
				aspectItem->SetMarked(videoWindow->CorrectAspectRatio());
				menu->AddItem(aspectItem);
	
				menu->AddSeparatorItem();
	
				// Windwo Feel Items
/*				BMessage *winNormFeel = new BMessage(WINDOW_FEEL);
				winNormFeel->AddInt32("WinFeel", (int32)B_NORMAL_WINDOW_FEEL);
				BMenuItem *normWindItem = new BMenuItem("Normal Window", winNormFeel);
				normWindItem->SetMarked(videoWindow->Feel() == B_NORMAL_WINDOW_FEEL);
				menu->AddItem(normWindItem);
				
				BMessage *winFloatFeel = new BMessage(WINDOW_FEEL);
				winFloatFeel->AddInt32("WinFeel", (int32)B_FLOATING_APP_WINDOW_FEEL);
				BMenuItem *onTopWindItem = new BMenuItem("App Top", winFloatFeel);
				onTopWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_APP_WINDOW_FEEL);
				menu->AddItem(onTopWindItem);
				
				BMessage *winAllFeel = new BMessage(WINDOW_FEEL);
				winAllFeel->AddInt32("WinFeel", (int32)B_FLOATING_ALL_WINDOW_FEEL);
				BMenuItem *allSpacesWindItem = new BMenuItem("On Top All Workspaces", winAllFeel);
				allSpacesWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL);
				menu->AddItem(allSpacesWindItem);*/

				BMessage *windowFeelMsg = new BMessage( WINDOW_FEEL );
				bool onTop = videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL;
				window_feel feel = onTop ? B_NORMAL_WINDOW_FEEL : B_FLOATING_ALL_WINDOW_FEEL;
				windowFeelMsg->AddInt32( "WinFeel", (int32)feel );
				BMenuItem *windowFeelItem = new BMenuItem( "Stay On Top", windowFeelMsg );
				windowFeelItem->SetMarked( onTop );
				menu->AddItem( windowFeelItem );

				menu->AddSeparatorItem();

				BMenuItem* screenShotItem = new BMenuItem( "Take Screen Shot",
														   new BMessage( SCREEN_SHOT ) );
				menu->AddItem( screenShotItem );

				menu->SetTargetForItems( this );
				ConvertToScreen( &where );
				menu->Go( where, true, false, true );
	        }
		}
	}
	fLastMouseMovedTime = system_time();
	fCursorHidden = false;
}

1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
/*****************************************************************************
 * VLCVIew::MouseUp
 *****************************************************************************/
void
VLCView::MouseUp( BPoint where )
{
    vlc_value_t val;
    val.b_bool = VLC_TRUE;
    var_Set( p_vout, "mouse-clicked", val );
}

1195
1196
1197
1198
1199
/*****************************************************************************
 * VLCVIew::MouseMoved
 *****************************************************************************/
void
VLCView::MouseMoved(BPoint point, uint32 transit, const BMessage* dragMessage)
1200
{
1201
1202
1203
	fLastMouseMovedTime = system_time();
	fCursorHidden = false;
	fCursorInside = (transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW);
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
	/* DVD navigation */
	unsigned int i_width, i_height, i_x, i_y;
    vout_PlacePicture( p_vout, (unsigned int)Bounds().Width(),
                       (unsigned int)Bounds().Height(),
                       &i_x, &i_y, &i_width, &i_height );
	vlc_value_t val;
	val.i_int = ( (int)point.x - i_x ) * p_vout->render.i_width / i_width;
	var_Set( p_vout, "mouse-x", val );
	val.i_int = ( (int)point.y - i_y ) * p_vout->render.i_height / i_height;
	var_Set( p_vout, "mouse-y", val );
	val.b_bool = VLC_TRUE;
    var_Set( p_vout, "mouse-moved", val );
1216
}
1217

1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
/*****************************************************************************
 * VLCVIew::Pulse
 *****************************************************************************/
void 
VLCView::Pulse()
{
	// We are getting the pulse messages no matter if the mouse is over
	// this view. If we are in full screen mode, we want to hide the cursor
	// even if it is not.
	if (!fCursorHidden)
	{
		if (fCursorInside
			&& system_time() - fLastMouseMovedTime > MOUSE_IDLE_TIMEOUT)
		{
			be_app->ObscureCursor();
			fCursorHidden = true;
			VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
			// hide the interface window as well if full screen
1236
			if (videoWindow && videoWindow->IsFullScreen())
1237
1238
1239
1240
				videoWindow->SetInterfaceShowing(false);
		}
	}
}
1241

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
/*****************************************************************************
 * VLCVIew::KeyDown
 *****************************************************************************/
void
VLCView::KeyDown(const char *bytes, int32 numBytes)
{
    VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
    BWindow* interfaceWindow = get_interface_window();
	if (videoWindow && numBytes > 0) {
		uint32 mods = modifiers();
		switch (*bytes) {
			case B_TAB:
1254
1255
			case 'f':
			case 'F':
1256
1257
1258
1259
1260
1261
1262
				// toggle window and full screen mode
				// not passing on the tab key to the default KeyDown()
				// implementation also avoids loosing the keyboard focus
				videoWindow->PostMessage(TOGGLE_FULL_SCREEN);
				break;
			case B_ESCAPE:
				// go back to window mode
1263
				if (videoWindow->IsFullScreen())
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
					videoWindow->PostMessage(TOGGLE_FULL_SCREEN);
				break;
			case B_SPACE:
				// toggle playback
				if (interfaceWindow)
					interfaceWindow->PostMessage(PAUSE_PLAYBACK);
				break;
			case B_RIGHT_ARROW:
				if (interfaceWindow)
				{
					if (mods & B_SHIFT_KEY)
						// next title
						interfaceWindow->PostMessage(NEXT_TITLE);
					else
						// next chapter
						interfaceWindow->PostMessage(NEXT_CHAPTER);
				}
				break;
			case B_LEFT_ARROW:
				if (interfaceWindow)
				{
					if (mods & B_SHIFT_KEY)
						// previous title
						interfaceWindow->PostMessage(PREV_TITLE);
					else
						// previous chapter
						interfaceWindow->PostMessage(PREV_CHAPTER);
				}
				break;
			case B_UP_ARROW:
				// previous file in playlist
				interfaceWindow->PostMessage(PREV_FILE);
				break;
			case B_DOWN_ARROW:
				// next file in playlist
				interfaceWindow->PostMessage(NEXT_FILE);
				break;
			case B_PRINT_KEY:
			case 's':
			case 'S':
				videoWindow->PostMessage( SCREEN_SHOT );
				break;
			default:
				BView::KeyDown(bytes, numBytes);
				break;
		}
	}
1311
1312
1313
1314
1315
}

/*****************************************************************************
 * VLCVIew::Draw
 *****************************************************************************/
1316
1317
void
VLCView::Draw(BRect updateRect) 
1318
{
1319
1320
1321
	VideoWindow* window = dynamic_cast<VideoWindow*>( Window() );
	if ( window && window->mode == BITMAP )
		FillRect( updateRect );
1322
1323
1324
1325
1326
1327
1328
}

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Init       ( vout_thread_t * );
static void End        ( vout_thread_t * );
Eric Petit's avatar
Eric Petit committed
1329
// static int  Manage     ( vout_thread_t * );
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
static void Display    ( vout_thread_t *, picture_t * );

static int  BeosOpenDisplay ( vout_thread_t *p_vout );
static void BeosCloseDisplay( vout_thread_t *p_vout );

/*****************************************************************************
 * OpenVideo: allocates BeOS video thread output method
 *****************************************************************************
 * This function allocates and initializes a BeOS vout method.
 *****************************************************************************/
int E_(OpenVideo) ( vlc_object_t *p_this )
{
    vout_thread_t * p_vout = (vout_thread_t *)p_this;

    /* Allocate structure */
    p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
        return( 1 );
    }
    p_vout->p_sys->i_width = p_vout->render.i_width;
    p_vout->p_sys->i_height = p_vout->render.i_height;
    p_vout->p_sys->source_chroma = p_vout->render.i_chroma;

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = NULL;
    p_vout->pf_render = NULL;
    p_vout->pf_display = Display;

    return( 0 );
}

/*****************************************************************************
 * Init: initialize BeOS video thread output method
 *****************************************************************************/
int Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic;

    I_OUTPUTPICTURES = 0;

    /* Open and initialize device */
    if( BeosOpenDisplay( p_vout ) )
    {
        msg_Err(p_vout, "vout error: can't open display");
        return 0;
    }
    p_vout->output.i_width  = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;

    /* Assume we have square pixels */
    p_vout->output.i_aspect = p_vout->p_sys->i_width
                               * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
    p_vout->output.i_chroma = colspace[p_vout->p_sys->p_window->colspace_index].chroma;
    p_vout->p_sys->i_index = 0;

    p_vout->b_direct = 1;

    p_vout->output.i_rmask  = 0x00ff0000;
    p_vout->output.i_gmask  = 0x0000ff00;
    p_vout->output.i_bmask  = 0x000000ff;

    for (int buffer_index = 0 ; buffer_index < 3; buffer_index++)
    {
       p_pic = NULL;
       /* Find an empty picture slot */
       for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
       {
           p_pic = NULL;
           if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
           {
               p_pic = p_vout->p_picture + i_index;
               break;
           }
       }

       if( p_pic == NULL )
       {
           return 0;
       }
       p_pic->p->p_pixels = (u8*)p_vout->p_sys->p_window->bitmap[buffer_index]->Bits();
       p_pic->p->i_lines = p_vout->p_sys->i_height;

       p_pic->p->i_pixel_pitch = colspace[p_vout->p_sys->p_window->colspace_index].pixel_bytes;
       p_pic->i_planes = colspace[p_vout->p_sys->p_window->colspace_index].planes;
       p_pic->p->i_pitch = p_vout->p_sys->p_window->bitmap[buffer_index]->BytesPerRow(); 
       p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch * ( p_vout->p_sys->p_window->bitmap[buffer_index]->Bounds().IntegerWidth() + 1 );

       p_pic->i_status = DESTROYED_PICTURE;
       p_pic->i_type   = DIRECT_PICTURE;
 
       PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;

       I_OUTPUTPICTURES++;
    }

    return( 0 );
}

/*****************************************************************************
 * End: terminate BeOS video thread output method
 *****************************************************************************/
void End( vout_thread_t *p_vout )
{
    BeosCloseDisplay( p_vout );
}

/*****************************************************************************
 * CloseVideo: destroy BeOS video thread output method
 *****************************************************************************
 * Terminate an output method created by DummyCreateOutputMethod
 *****************************************************************************/
void E_(CloseVideo) ( vlc_object_t *p_this )
{
    vout_thread_t * p_vout = (vout_thread_t *)p_this;

    free( p_vout->p_sys );
}

/*****************************************************************************
 * Display: displays previously rendered output
 *****************************************************************************
 * This function send the currently rendered image to BeOS image, waits until
 * it is displayed and switch the two rendering buffers, preparing next frame.
 *****************************************************************************/
void Display( vout_thread_t *p_vout, picture_t *p_pic )
{
    VideoWindow * p_win = p_vout->p_sys->p_window;

    /* draw buffer if required */    
    if (!p_win->teardownwindow)
    { 
       p_win->drawBuffer(p_vout->p_sys->i_index);
    }
    /* change buffer */
    p_vout->p_sys->i_index = ++p_vout->p_sys->i_index % 3;
    p_pic->p->p_pixels = (u8*)p_vout->p_sys->p_window->bitmap[p_vout->p_sys->i_index]->Bits();
}

/* following functions are local */

/*****************************************************************************
 * BeosOpenDisplay: open and initialize BeOS device
 *****************************************************************************/
static int BeosOpenDisplay( vout_thread_t *p_vout )
{ 

    p_vout->p_sys->p_window = new VideoWindow( p_vout->p_sys->i_width - 1,
                                               p_vout->p_sys->i_height - 1,
                                               BRect( 20, 50,
                                                      20 + p_vout->i_window_width - 1, 
1484
1485
                                                      50 + p_vout->i_window_height - 1 ),
                                               p_vout );
1486
1487
1488
1489
    if( p_vout->p_sys->p_window == NULL )
    {
        msg_Err( p_vout, "cannot allocate VideoWindow" );
        return( 1 );
1490
1491
1492
1493
1494
    }
    else
    {
        p_vout->p_sys->p_window->Show();
    }
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
    
    return( 0 );
}

/*****************************************************************************
 * BeosDisplay: close and reset BeOS device
 *****************************************************************************
 * Returns all resources allocated by BeosOpenDisplay and restore the original
 * state of the device.
 *****************************************************************************/
static void BeosCloseDisplay( vout_thread_t *p_vout )
{    
    VideoWindow * p_win = p_vout->p_sys->p_window;
    /* Destroy the video window */
    if( p_win != NULL && !p_win->teardownwindow)
    {
        p_win->Lock();
        p_win->teardownwindow = true;
        p_win->Hide();
        p_win->Quit();
    }
    p_win = NULL;
}