vout_beos.cpp 18.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * vout_beos.cpp: beos video output display method
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
4
 * Copyright (C) 2000, 2001 VideoLAN
5
 * $Id: vout_beos.cpp,v 1.34 2001/12/06 10:29:40 tcastley Exp $
6
 *
Sam Hocevar's avatar
   
Sam Hocevar committed
7
8
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
Richard Shepherd's avatar
Richard Shepherd committed
9
 *          Tony Castley <tcastley@mail.powerup.com.au>
Sam Hocevar's avatar
   
Sam Hocevar committed
10
 *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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.
 *****************************************************************************/

Sam Hocevar's avatar
   
Sam Hocevar committed
27
28
29
#define MODULE_NAME beos
#include "modules_inner.h"

30
31
32
33
34
35
36
37
38
/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>
#include <string.h>                                            /* strerror() */
39
40
#include <InterfaceKit.h>
#include <DirectWindow.h>
41
#include <Application.h>
42
#include <Bitmap.h>
43
44
45
46
47

extern "C"
{
#include "config.h"
#include "common.h"
48
#include "intf_msg.h"
49
50
#include "threads.h"
#include "mtime.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
51
#include "tests.h"
52
53
54
55

#include "video.h"
#include "video_output.h"

Sam Hocevar's avatar
   
Sam Hocevar committed
56
#include "interface.h"
57
58

#include "main.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
59
60
61

#include "modules.h"
#include "modules_export.h"
62
63
}

Sam Hocevar's avatar
   
Sam Hocevar committed
64
#include "VideoWindow.h"
65

66
67
#define BITS_PER_PLANE  32
#define BYTES_PER_PIXEL 4
68
69

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
70
 * vout_sys_t: BeOS video output method descriptor
71
72
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
Sam Hocevar's avatar
   
Sam Hocevar committed
73
 * It describes the BeOS specific properties of an output thread.
74
75
76
 *****************************************************************************/
typedef struct vout_sys_s
{
77
    VideoWindow *      p_window;
Sam Hocevar's avatar
   
Sam Hocevar committed
78

Sam Hocevar's avatar
   
Sam Hocevar committed
79
    byte_t *              pp_buffer[2];
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    s32                   i_width;
    s32                   i_height;
} vout_sys_t;


/*****************************************************************************
 * beos_GetAppWindow : retrieve a BWindow pointer from the window name
 *****************************************************************************/
BWindow *beos_GetAppWindow(char *name)
{
    int32       index;
    BWindow     *window;
    
    for (index = 0 ; ; index++)
    {
        window = be_app->WindowAt(index);
        if (window == NULL)
            break;
98
        if (window->LockWithTimeout(20000) == B_OK)
99
100
101
102
103
104
105
106
107
108
109
110
        {
            if (strcmp(window->Name(), name) == 0)
            {
                window->Unlock();
                break;
            }
            window->Unlock();
        }
    }
    return window; 
}

111
112
113
114
115
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
153
154
155
156
157
/**************************************************************************** 
* 
 * DrawingThread : thread that really does the drawing 
 **************************************************************************** 
*/ 
int32 Draw(void *data) 
{ 
    //rudolf: sync init: 
    BScreen *screen; 
    display_mode disp_mode; 
    static uint32 refresh, oldrefresh = 0; 

    screen = new BScreen(); 
    screen-> GetMode(&disp_mode); 
    refresh = 
         (disp_mode.timing.pixel_clock * 1000)/((disp_mode.timing.h_total)* 
         (disp_mode.timing.v_total)); 
    if (!(refresh == oldrefresh)) 
    { 
        printf("\nNew refreshrate is %d:Hz\n",refresh); 
        oldrefresh = refresh; 
        if (refresh  < 61) 
        { 
            printf("Enabling retrace sync.\n"); 
        } 
        else 
        { 
            printf("Disabling retrace sync.\n"); 
        } 
    } 

    VideoWindow* p_win; 
    p_win = (VideoWindow *) data; 
    if ( p_win-> voutWindow-> LockLooper() ) 
    { 
        //rudolf: sync: 
        if (refresh  < 61) 
        { 
            screen-> WaitForRetrace(22000);//set timeout for  < 45 Hz... 
        } 

        p_win-> view-> DrawBitmap( p_win-> bitmap[p_win-> i_buffer], 
                                 p_win-> bitmap[p_win-> i_buffer]-> Bounds(), 
                                 p_win-> voutWindow-> Bounds() );  
        p_win-> voutWindow-> UnlockLooper(); 
    } 
    return B_OK; 
Sam Hocevar's avatar
   
Sam Hocevar committed
158
159
}

160
/*****************************************************************************
161
 * bitmapWindow : This is the bitmap window output
162
 *****************************************************************************/
163
164
165
bitmapWindow::bitmapWindow(BRect frame, VideoWindow *theOwner)
        : BWindow( frame, NULL, B_TITLED_WINDOW, 
                   B_OUTLINE_RESIZE | B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
166
{
Sam Hocevar's avatar
   
Sam Hocevar committed
167
    is_zoomed = false;
168
169
170
171
    origRect = frame;
    owner = theOwner;
    SetTitle(VOUT_TITLE " (BBitmap output)");
}
172

173
174
175
bitmapWindow::~bitmapWindow()
{
}
Richard Shepherd's avatar
Richard Shepherd committed
176

177
178
179
180
181
182
183
184
void bitmapWindow::FrameResized( float width, float height )
{
	if (is_zoomed)
	{
	    return;
	}
	float width_scale;
	float height_scale;
185

186
187
188
189
190
191
192
193
194
195
196
197
198
	width_scale = width / origRect.Width();
	height_scale = height / origRect.Height();
	
    /* if the width is proportionally smaller */
    if (width_scale <= height_scale)
    {
        ResizeTo(width, origRect.Height() * width_scale);
    }
    else /* if the height is proportionally smaller */
    {
        ResizeTo(origRect.Width() * height_scale, height);
    }
}
199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
void bitmapWindow::Zoom(BPoint origin, float width, float height )
{
	if(is_zoomed)
	{
		MoveTo(origRect.left, origRect.top);
		ResizeTo(origRect.IntegerWidth(), origRect.IntegerHeight());
		be_app->ShowCursor();
	}
	else
	{
		BScreen *screen;
		screen = new BScreen(this);
		BRect rect = screen->Frame();
		delete screen;
		MoveTo(0,0);
		ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
		be_app->HideCursor();
	}
	is_zoomed = !is_zoomed;
219
220
}

221
222
223
224
225
226
/*****************************************************************************
 * directWindow : This is the bitmap window output
 *****************************************************************************/
directWindow::directWindow(BRect frame, VideoWindow *theOwner)
        : BDirectWindow( frame, NULL, B_TITLED_WINDOW, 
                   B_OUTLINE_RESIZE | B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
227
{
228
229
230
231
232
    is_zoomed = false;
    origRect = frame;
    owner = theOwner;
    SetTitle(VOUT_TITLE " (DirectWindow output)");
}
Sam Hocevar's avatar
   
Sam Hocevar committed
233

234
235
236
directWindow::~directWindow()
{
}
Sam Hocevar's avatar
   
Sam Hocevar committed
237

238
239
240
void directWindow::DirectConnected(direct_buffer_info *info)
{
}
241

242
void directWindow::FrameResized( float width, float height )
Sam Hocevar's avatar
   
Sam Hocevar committed
243
{
244
245
246
247
248
249
250
	if (is_zoomed)
	{
	    return;
	}
	float width_scale;
	float height_scale;

251
252
	width_scale = width / origRect.Width();
	height_scale = height / origRect.Height();
253
254
255
256
	
    /* if the width is proportionally smaller */
    if (width_scale <= height_scale)
    {
257
        ResizeTo(width, origRect.Height() * width_scale);
258
259
260
    }
    else /* if the height is proportionally smaller */
    {
261
        ResizeTo(origRect.Width() * height_scale, height);
262
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
263
264
}

265
void directWindow::Zoom(BPoint origin, float width, float height )
Sam Hocevar's avatar
   
Sam Hocevar committed
266
{
267
	if(is_zoomed)
Sam Hocevar's avatar
   
Sam Hocevar committed
268
	{
269
270
271
	    SetFullScreen(false);
		MoveTo(origRect.left, origRect.top);
		ResizeTo(origRect.IntegerWidth(), origRect.IntegerHeight());
272
		be_app->ShowCursor();
Sam Hocevar's avatar
   
Sam Hocevar committed
273
	}
274
	else
Sam Hocevar's avatar
   
Sam Hocevar committed
275
	{
276
		SetFullScreen(true);
277
278
279
280
281
282
283
		BScreen *screen;
		screen = new BScreen(this);
		BRect rect = screen->Frame();
		delete screen;
		MoveTo(0,0);
		ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
		be_app->HideCursor();
Sam Hocevar's avatar
   
Sam Hocevar committed
284
	}
285
	is_zoomed = !is_zoomed;
Sam Hocevar's avatar
   
Sam Hocevar committed
286
287
}

288
/*****************************************************************************
289
 * VideoWindow constructor and destructor
290
 *****************************************************************************/
291
292
VideoWindow::VideoWindow( int width, int height, 
                          vout_thread_t *p_video_output )
293
{
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
    if ( BDirectWindow::SupportsWindowMode() )
    { 
        voutWindow = new directWindow( BRect( 80, 50, 
	                                      80 + width, 50 + height ), this );
    }
    else
    {
	    voutWindow = new bitmapWindow( BRect( 80, 50, 
	                                      80 + width, 50 + height ), this );
	}

	/* set the VideoWindow variables */
    teardownwindow = false;
	
	/* create the view to do the display */
    view = new VLCView( voutWindow->Bounds() );
    voutWindow->AddChild(view);
311
    
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    /* Bitmap mode overlay not available */
	bitmap[0] = new BBitmap( voutWindow->Bounds(), B_RGB32);
	bitmap[1] = new BBitmap( voutWindow->Bounds(), B_RGB32);
	memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
 	memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());

	i_width = bitmap[0]->Bounds().IntegerWidth();
	i_height = bitmap[0]->Bounds().IntegerHeight();

    voutWindow->Show();
}

VideoWindow::~VideoWindow()
{
    int32 result;

    voutWindow->Hide();
    voutWindow->Sync();
    voutWindow->Lock();
    voutWindow->Quit();
    teardownwindow = true;
    wait_for_thread(fDrawThreadID, &result);
   	delete bitmap[0];
   	delete bitmap[1];
}

void VideoWindow::resizeIfRequired( int newWidth, int newHeight )
{
    if (( newWidth != i_width + 1) &&
        ( newHeight != i_height + 1) &&
        ( newWidth != 0 ))
343
    {
344
        if ( voutWindow->Lock() )
345
        {
346
347
348
349
350
            view->ClearViewBitmap();
            i_width = newWidth - 1;
            i_height = newHeight -1;
            voutWindow->ResizeTo((float) i_width, (float) i_height); 
            voutWindow->Unlock();
351
352
353
354
        }
    }
}

355
void VideoWindow::drawBuffer(int bufferIndex)
356
{
357
358
359
360
361
362
363
	status_t status;
	
	i_buffer = bufferIndex; 
	
    fDrawThreadID = spawn_thread(Draw, "drawing_thread",
                    B_DISPLAY_PRIORITY, (void*) this);
    wait_for_thread(fDrawThreadID, &status);
364
365
}

Sam Hocevar's avatar
   
Sam Hocevar committed
366
367
368
369
370
/*****************************************************************************
 * VLCView::VLCView
 *****************************************************************************/
VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
{
371
	SetViewColor(B_TRANSPARENT_32_BIT);
Sam Hocevar's avatar
   
Sam Hocevar committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
}

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

/*****************************************************************************
 * VLCVIew::~VLCView
 *****************************************************************************/
void VLCView::MouseDown(BPoint point)
{
386
387
	BWindow *win = Window();
	win->Zoom();
Sam Hocevar's avatar
   
Sam Hocevar committed
388
389
}

390
391
392
393
394
395
extern "C"
{

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
static int  vout_Probe      ( probedata_t *p_data );
static int  vout_Create     ( struct vout_thread_s * );
static int  vout_Init       ( struct vout_thread_s * );
static void vout_End        ( struct vout_thread_s * );
static void vout_Destroy    ( struct vout_thread_s * );
static int  vout_Manage     ( struct vout_thread_s * );
static void vout_Display    ( struct vout_thread_s * );

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

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
411
void _M( vout_getfunctions )( function_list_t * p_function_list )
Sam Hocevar's avatar
   
Sam Hocevar committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{
    p_function_list->pf_probe = vout_Probe;
    p_function_list->functions.vout.pf_create     = vout_Create;
    p_function_list->functions.vout.pf_init       = vout_Init;
    p_function_list->functions.vout.pf_end        = vout_End;
    p_function_list->functions.vout.pf_destroy    = vout_Destroy;
    p_function_list->functions.vout.pf_manage     = vout_Manage;
    p_function_list->functions.vout.pf_display    = vout_Display;
    p_function_list->functions.vout.pf_setpalette = NULL;
}

/*****************************************************************************
 * vout_Probe: probe the video driver and return a score
 *****************************************************************************
 * This function tries to initialize SDL and returns a score to the
 * plugin manager so that it can select the best plugin.
 *****************************************************************************/
static int vout_Probe( probedata_t *p_data )
{
    if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
    {
        return( 999 );
    }
    return( 100 );
}
437
438

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
439
 * vout_Create: allocates BeOS video thread output method
440
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
441
 * This function allocates and initializes a BeOS vout method.
442
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
443
int vout_Create( vout_thread_t *p_vout )
444
445
446
447
448
{
    /* Allocate structure */
    p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
449
        intf_ErrMsg( "error: %s", strerror(ENOMEM) );
450
451
452
        return( 1 );
    }
    
453
    /* force scaling off */
454
	p_vout->b_scale = false;
455
456
457
458

    /* Open and initialize device */
    if( BeosOpenDisplay( p_vout ) )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
459
        intf_ErrMsg("vout error: can't open display");
460
461
462
463
464
465
466
467
        free( p_vout->p_sys );
        return( 1 );
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
468
 * vout_Init: initialize BeOS video thread output method
469
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
470
int vout_Init( vout_thread_t *p_vout )
471
472
{
    VideoWindow * p_win = p_vout->p_sys->p_window;
473

474
475
476
477
478
479
    if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
   	{
    	p_vout->pf_setbuffers( p_vout,
               (byte_t *)p_win->bitmap[0]->Bits(),
               (byte_t *)p_win->bitmap[1]->Bits());
    }
480
481
482
483
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
484
 * vout_End: terminate BeOS video thread output method
485
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
486
void vout_End( vout_thread_t *p_vout )
487
{
488
	/* place code here to end the video */
489
490
491
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
492
 * vout_Destroy: destroy BeOS video thread output method
493
494
495
 *****************************************************************************
 * Terminate an output method created by DummyCreateOutputMethod
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
496
void vout_Destroy( vout_thread_t *p_vout )
497
498
499
500
501
502
{
    BeosCloseDisplay( p_vout );
    free( p_vout->p_sys );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
503
 * vout_Manage: handle BeOS events
504
505
506
507
 *****************************************************************************
 * This function should be called regularly by video output thread. It manages
 * console events. It returns a non null value on error.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
508
int vout_Manage( vout_thread_t *p_vout )
509
{
510
511
    VideoWindow * p_win = p_vout->p_sys->p_window;
    
512
513
514
    p_win->resizeIfRequired(p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width,
                            p_vout->p_buffer[p_vout->i_buffer_index].i_pic_height);
                            
515
	return( 0 );
516
517
518
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
519
 * vout_Display: displays previously rendered output
520
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
521
 * This function send the currently rendered image to BeOS image, waits until
522
523
 * it is displayed and switch the two rendering buffers, preparing next frame.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
524
void vout_Display( vout_thread_t *p_vout )
525
{
526

527
    VideoWindow * p_win = p_vout->p_sys->p_window;
528
    /* draw buffer if required */    
529
530
	if (!p_win->teardownwindow)
	{
531
       p_win->drawBuffer(p_vout->i_buffer_index);
532
    }
533
534
    /* change buffer */
	p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
535
536
537
538
539
}

/* following functions are local */

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
540
 * BeosOpenDisplay: open and initialize BeOS device
541
542
543
 *****************************************************************************/
static int BeosOpenDisplay( vout_thread_t *p_vout )
{ 
544
    
545
546
547
548
549
    VideoWindow * p_win = new VideoWindow( p_vout->i_width - 1, 
                                           p_vout->i_height - 1, 
                                           p_vout );

    if( p_win == 0 )
550
551
    {
        free( p_vout->p_sys );
Sam Hocevar's avatar
   
Sam Hocevar committed
552
        intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
553
554
555
        return( 1 );
    }   
    
556
557
558
559
560
561
562
563
564
    p_vout->p_sys->p_window = p_win;
    /* set the system to 32bits always
       let BeOS do all the work */
	p_vout->b_YCbr            = false;
    p_vout->i_screen_depth    = BITS_PER_PLANE;
    p_vout->i_bytes_per_pixel = BYTES_PER_PIXEL;
    p_vout->i_width           = p_win->i_width + 1;
    p_vout->i_height          = p_win->i_height + 1;
    p_vout->i_bytes_per_line  = p_vout->i_width * BYTES_PER_PIXEL;
565

566
567
568
    p_vout->i_red_mask =        0xff0000;
    p_vout->i_green_mask =      0x00ff00;
    p_vout->i_blue_mask =       0x0000ff;
569

570
571
572
573
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
574
 * BeosDisplay: close and reset BeOS device
575
576
577
578
579
580
581
 *****************************************************************************
 * Returns all resources allocated by BeosOpenDisplay and restore the original
 * state of the device.
 *****************************************************************************/
static void BeosCloseDisplay( vout_thread_t *p_vout )
{    
    /* Destroy the video window */
582
    delete p_vout->p_sys->p_window;
583
584
585
}

} /* extern "C" */