vout_beos.cpp 17.1 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.31 2001/10/21 06:06:20 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
39
40
41
/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>
#include <string.h>                                            /* strerror() */
#include <kernel/OS.h>
#include <View.h>
#include <Application.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
42
43
44
#include <Window.h>
#include <Locker.h>
#include <Screen.h>
45
46
47
48
49
50
51
52
53
#include <malloc.h>
#include <string.h>

extern "C"
{
#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
54
#include "tests.h"
55
56
57
58

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

Sam Hocevar's avatar
   
Sam Hocevar committed
59
#include "interface.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
60
#include "intf_msg.h"
61
62

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

#include "modules.h"
#include "modules_export.h"
66
67
}

Sam Hocevar's avatar
   
Sam Hocevar committed
68
69
#include "VideoWindow.h"
#include <Screen.h>
70
71
72
73
74
75
76

#define WIDTH 128
#define HEIGHT 64
#define BITS_PER_PLANE 16
#define BYTES_PER_PIXEL 2

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
77
 * vout_sys_t: BeOS video output method descriptor
78
79
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
Sam Hocevar's avatar
   
Sam Hocevar committed
80
 * It describes the BeOS specific properties of an output thread.
81
82
83
84
85
 *****************************************************************************/
 
typedef struct vout_sys_s
{
    VideoWindow *         p_window;
Sam Hocevar's avatar
   
Sam Hocevar committed
86

Sam Hocevar's avatar
   
Sam Hocevar committed
87
    byte_t *              pp_buffer[2];
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    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;
107
        if (window->LockWithTimeout(20000) == B_OK)
108
109
110
111
112
113
114
115
116
117
118
119
        {
            if (strcmp(window->Name(), name) == 0)
            {
                window->Unlock();
                break;
            }
            window->Unlock();
        }
    }
    return window; 
}

Sam Hocevar's avatar
   
Sam Hocevar committed
120
121
122
123
/*****************************************************************************
 * DrawingThread : thread that really does the drawing
 *****************************************************************************/

124
int32 Draw(void *data)
Sam Hocevar's avatar
   
Sam Hocevar committed
125
{
126
127
128
  VideoWindow *p_win;
  p_win = (VideoWindow*) data;
	  if (p_win->LockLooper())
Sam Hocevar's avatar
   
Sam Hocevar committed
129
      {
130
131
132
133
        p_win->view->DrawBitmap( p_win->bitmap[p_win->i_buffer_index],
                              p_win->bitmap[p_win->i_buffer_index]->Bounds(),
                              p_win->Bounds());  
        p_win->UnlockLooper();
134
      }
Sam Hocevar's avatar
   
Sam Hocevar committed
135
  return B_OK;
Sam Hocevar's avatar
   
Sam Hocevar committed
136
137
}

138
139
140
141
142
/*****************************************************************************
 * VideoWindow constructor and destructor
 *****************************************************************************/

VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
143
        : BWindow(frame, name, B_TITLED_WINDOW, B_OUTLINE_RESIZE)
144
145
{

146
	/* set the VideoWindow variables */
Sam Hocevar's avatar
   
Sam Hocevar committed
147
148
    teardownwindow = false;
    is_zoomed = false;
149
	p_video_output->b_YCbr = false;
Richard Shepherd's avatar
Richard Shepherd committed
150
	
151
	/* create the view to do the display */
Sam Hocevar's avatar
   
Sam Hocevar committed
152
153
    view = new VLCView(Bounds());
    AddChild(view);
154
155
156
157
    
    /* Bitmap mode overlay not available */
	bitmap[0] = new BBitmap(Bounds(), B_RGB32);
	bitmap[1] = new BBitmap(Bounds(), B_RGB32);
158

159
160
	i_screen_depth = 32;
 	SetTitle(VOUT_TITLE " (BBitmap output)");
Richard Shepherd's avatar
Richard Shepherd committed
161

162
163
	memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
 	memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
164
165
166
167

	i_width = bitmap[0]->Bounds().IntegerWidth();
	i_height = bitmap[0]->Bounds().IntegerHeight();
	i_bytes_per_pixel = bitmap[0]->BytesPerRow()/i_width;
Richard Shepherd's avatar
Richard Shepherd committed
168
    fRowBytes = bitmap[0]->BytesPerRow();
169
170
	i_screen_depth = 8 * i_bytes_per_pixel;

171
172
173
174
175
    Show();
}

VideoWindow::~VideoWindow()
{
Sam Hocevar's avatar
   
Sam Hocevar committed
176
177
178
179
    int32 result;

    Hide();
    Sync();
180
181
182
183
    teardownwindow = true;
    wait_for_thread(fDrawThreadID, &result);
   	delete bitmap[0];
   	delete bitmap[1];
Sam Hocevar's avatar
   
Sam Hocevar committed
184
185
 }

186

Sam Hocevar's avatar
   
Sam Hocevar committed
187
188
189
190
191
/*****************************************************************************
 * VideoWindow::FrameResized
 *****************************************************************************/
void VideoWindow::FrameResized( float width, float height )
{
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
	if (is_zoomed)
	{
	    return;
	}
	float width_scale;
	float height_scale;
	float orig_width = bitmap[0]->Bounds().Width();
	float orig_height = bitmap[0]->Bounds().Height();

	width_scale = width / orig_width;
	height_scale = height / orig_height;
	
    /* if the width is proportionally smaller */
    if (width_scale <= height_scale)
    {
        ResizeTo(width, orig_height * width_scale);
    }
    else /* if the height is proportionally smaller */
    {
        ResizeTo(orig_width * height_scale, height);
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
213
214
215
216
217
218
219
220
}

/*****************************************************************************
 * VideoWindow::Zoom
 *****************************************************************************/

void VideoWindow::Zoom(BPoint origin, float width, float height )
{
221
	if(is_zoomed)
Sam Hocevar's avatar
   
Sam Hocevar committed
222
	{
223
224
225
		MoveTo(rect.left, rect.top);
		ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
		be_app->ShowCursor();
Sam Hocevar's avatar
   
Sam Hocevar committed
226
	}
227
	else
Sam Hocevar's avatar
   
Sam Hocevar committed
228
	{
229
230
231
232
233
234
235
236
		rect = Frame();
		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
237
	}
238
	is_zoomed = !is_zoomed;
Sam Hocevar's avatar
   
Sam Hocevar committed
239
240
}

241
242
243
244
245
246
247
248
249
250
251
/*****************************************************************************
 * VideoWindow::MessageReceived
 *****************************************************************************/

void VideoWindow::MessageReceived( BMessage * p_message )
{
    BWindow * p_win;
    
    switch( p_message->what )
    {
    case B_KEY_DOWN:
Sam Hocevar's avatar
   
Sam Hocevar committed
252
    case B_SIMPLE_DATA:
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        // post the message to the interface window which will handle it
        p_win = beos_GetAppWindow( "interface" );
        if( p_win != NULL )
        {
            p_win->PostMessage( p_message );
        }
        break;
    
    default:
        BWindow::MessageReceived( p_message );
        break;
    }
}

/*****************************************************************************
 * VideoWindow::QuitRequested
 *****************************************************************************/

bool VideoWindow::QuitRequested()
{
Sam Hocevar's avatar
   
Sam Hocevar committed
273
274
    /* FIXME: send a message ! */
    p_main->p_intf->b_die = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
275
    teardownwindow = true;
Sam Hocevar's avatar
   
Sam Hocevar committed
276
    return( false );
277
278
}

Sam Hocevar's avatar
   
Sam Hocevar committed
279
280
281
282
283
/*****************************************************************************
 * VLCView::VLCView
 *****************************************************************************/
VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
{
284
	SetViewColor(B_TRANSPARENT_32_BIT);
Sam Hocevar's avatar
   
Sam Hocevar committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
}

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

}

/*****************************************************************************
 * VLCVIew::~VLCView
 *****************************************************************************/
void VLCView::MouseDown(BPoint point)
{
300
301
	VideoWindow *w = (VideoWindow *) Window();
	if(w->is_zoomed)
Sam Hocevar's avatar
   
Sam Hocevar committed
302
	{
303
304
		BWindow *win = Window();
		win->Zoom();
Sam Hocevar's avatar
   
Sam Hocevar committed
305
306
307
	}
}

308
309
310
311
312
313
extern "C"
{

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
329
void _M( vout_getfunctions )( function_list_t * p_function_list )
Sam Hocevar's avatar
   
Sam Hocevar committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
    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 );
}
355
356

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
357
 * vout_Create: allocates BeOS video thread output method
358
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
359
 * This function allocates and initializes a BeOS vout method.
360
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
361
int vout_Create( vout_thread_t *p_vout )
362
363
364
365
366
{
    /* 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
367
        intf_ErrMsg( "error: %s", strerror(ENOMEM) );
368
369
370
        return( 1 );
    }
    
371
	p_vout->b_scale = false;
372
373
374
375

    /* Open and initialize device */
    if( BeosOpenDisplay( p_vout ) )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
376
        intf_ErrMsg("vout error: can't open display");
377
378
379
380
381
382
383
384
        free( p_vout->p_sys );
        return( 1 );
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
385
 * vout_Init: initialize BeOS video thread output method
386
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
387
int vout_Init( vout_thread_t *p_vout )
388
389
{
    VideoWindow * p_win = p_vout->p_sys->p_window;
Sam Hocevar's avatar
   
Sam Hocevar committed
390
391
    u32 i_page_size;
    i_page_size =   p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
392
393
394
395
396
397
    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());
    }
398
399
400
401
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
402
 * vout_End: terminate BeOS video thread output method
403
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
404
void vout_End( vout_thread_t *p_vout )
405
406
407
408
{
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
409
 * vout_Destroy: destroy BeOS video thread output method
410
411
412
 *****************************************************************************
 * Terminate an output method created by DummyCreateOutputMethod
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
413
void vout_Destroy( vout_thread_t *p_vout )
414
415
416
417
418
419
{
    BeosCloseDisplay( p_vout );
    free( p_vout->p_sys );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
420
 * vout_Manage: handle BeOS events
421
422
423
424
 *****************************************************************************
 * 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
425
int vout_Manage( vout_thread_t *p_vout )
426
{
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    VideoWindow * p_win = p_vout->p_sys->p_window;
    
    if ((p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width != p_win->i_width + 1) &&
        (p_vout->p_buffer[p_vout->i_buffer_index].i_pic_height != p_win->i_height + 1) &&
        (p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width != 0 ))
    {
        if (p_win->Lock())
        {
            p_win->view->ClearViewBitmap();
            intf_Msg("Starting Change");
            intf_Msg("New width: %d Height: %d", 
                p_vout->p_buffer[p_vout->i_buffer_index ].i_pic_width,
                p_vout->p_buffer[p_vout->i_buffer_index ].i_pic_height);

            p_win->i_width = p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width - 1;
            p_win->i_height = p_vout->p_buffer[p_vout->i_buffer_index].i_pic_height -1;
            p_win->ResizeTo((float) p_win->i_width, (float) p_win->i_height); 
            p_win->Unlock();
        }
    }
447
	return( 0 );
448
449
450
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
451
 * vout_Display: displays previously rendered output
452
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
453
 * This function send the currently rendered image to BeOS image, waits until
454
455
 * it is displayed and switch the two rendering buffers, preparing next frame.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
456
void vout_Display( vout_thread_t *p_vout )
457
{
458
459
	status_t status;

460
    VideoWindow * p_win = p_vout->p_sys->p_window;
461
    
Sam Hocevar's avatar
   
Sam Hocevar committed
462
   	p_win->i_buffer_index = p_vout->i_buffer_index;
463
	p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
464
465
466
	if (!p_win->teardownwindow)
	{
   	   p_win->fDrawThreadID = spawn_thread(Draw, "drawing_thread",
467
              B_DISPLAY_PRIORITY, (void*) p_win);
468
469
       wait_for_thread(p_win->fDrawThreadID, &status);
    }
470
471
472
473
474
}

/* following functions are local */

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
475
 * BeosOpenDisplay: open and initialize BeOS device
Sam Hocevar's avatar
   
Sam Hocevar committed
476
477
478
479
480
 *****************************************************************************
 * XXX?? The framebuffer mode is only provided as a fast and efficient way to
 * display video, providing the card is configured and the mode ok. It is
 * not portable, and is not supposed to work with many cards. Use at your
 * own risk !
481
482
483
484
 *****************************************************************************/

static int BeosOpenDisplay( vout_thread_t *p_vout )
{ 
485
    
486
    p_vout->p_sys->p_window =
Richard Shepherd's avatar
Richard Shepherd committed
487
        new VideoWindow(  BRect( 80, 50, 80+p_vout->i_width-1, 50+p_vout->i_height-1 ), NULL, p_vout );
488
489
490
    if( p_vout->p_sys->p_window == 0 )
    {
        free( p_vout->p_sys );
Sam Hocevar's avatar
   
Sam Hocevar committed
491
        intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
492
493
        return( 1 );
    }   
Sam Hocevar's avatar
   
Sam Hocevar committed
494
    VideoWindow * p_win = p_vout->p_sys->p_window;
495
    
Sam Hocevar's avatar
   
Sam Hocevar committed
496
497
    p_vout->i_screen_depth =         p_win->i_screen_depth;
    p_vout->i_bytes_per_pixel =      p_win->i_bytes_per_pixel;
498
499
    p_vout->i_width =				 p_win->i_width + 1;
    p_vout->i_height = 				 p_win->i_height + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
500
    p_vout->i_bytes_per_line =       p_vout->i_width*p_win->i_bytes_per_pixel;
501

502
503
504
    p_vout->i_red_mask =        0xff0000;
    p_vout->i_green_mask =      0x00ff00;
    p_vout->i_blue_mask =       0x0000ff;
505
506
507
508
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
509
 * BeosDisplay: close and reset BeOS device
510
511
512
513
514
515
516
517
518
519
520
521
 *****************************************************************************
 * 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 */
    p_vout->p_sys->p_window->Lock();
    p_vout->p_sys->p_window->Quit();
}

} /* extern "C" */